ProductModel.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\common\model;
  3. use traits\model\SoftDelete;
  4. /**
  5. * 产品信息表
  6. * Class ProductModel
  7. * @package app\common\model
  8. */
  9. class ProductModel extends BaseModel
  10. {
  11. use SoftDelete;
  12. protected $table = 'product';
  13. protected $deleteTime = 'delete_time';
  14. /**
  15. * 获取元单位的金额
  16. * @param $value
  17. * @return string
  18. */
  19. public function getMemberPrice($value)
  20. {
  21. $member_price = $value/100;
  22. return sprintf("%.2f", $member_price);
  23. }
  24. /**
  25. * 获取数组图片
  26. * @param $image
  27. * @return array
  28. */
  29. public function getImages($image)
  30. {
  31. $arr = json_decode($image,true);
  32. $images = [];
  33. foreach ($arr as $item){
  34. $images[] = config('admin_host').$item;
  35. }
  36. return $images;
  37. }
  38. /**
  39. * 获取单张图片
  40. * @param $image
  41. * @return mixed
  42. */
  43. public function getImage($image)
  44. {
  45. $arr = json_decode($image,true);
  46. return config('admin_host').$arr[0];
  47. }
  48. /**
  49. * 检查商品的库存
  50. * @param $product_arr
  51. * @return array|bool
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @throws \think\exception\DbException
  55. */
  56. public function checkProductNumber($product_arr)
  57. {
  58. $product_info = $product_arr['product_info'];
  59. $error='';
  60. foreach ($product_info as $item){
  61. if($item['number'] > $item['stock']){
  62. $error.=$item['product_name'].'剩余'.$item['stock'].';';
  63. }
  64. }
  65. if(!empty($error)){
  66. return [
  67. 'title'=>'库存不足',
  68. 'msg'=>$error
  69. ];
  70. }else{
  71. return false;
  72. }
  73. }
  74. public function getProductPage($page,$pageSize,$condition=[],$is_count = false,$order=''){
  75. $join=[
  76. ['product_type type','type.type_id=product.type_id','LEFT'],
  77. ];
  78. $this->alias('product')
  79. ->field('product.*,type.type_name')
  80. ->join($join);
  81. if(!empty($condition)){
  82. $this->where($condition);
  83. }
  84. if(!empty($order)){
  85. $this->order($order);
  86. }
  87. if($is_count){
  88. return $this->count();
  89. }
  90. return $this->page($page,$pageSize)->select();
  91. }
  92. /**
  93. * 获取商品和商品促销信息给首页用
  94. * @param array $condition
  95. * @param int $doctor_no
  96. * @return false|\PDOStatement|string|\think\Collection
  97. */
  98. public function getProductAndPromoList($condition=[],$doctor_no=0){
  99. //开始获取产品和促销信息
  100. $list = $this->alias('product')
  101. ->field('product.*,product_promo.price as promo_price,product_config.config_id')
  102. ->join('product_promo product_promo','product.product_no=product_promo.product_no and product_promo.status=1','left')
  103. ->join('product_config product_config','product.product_no=product_config.product_no and product_config.status=1','left')
  104. ->join('doctor_guide doctor_guide',"doctor_guide.product_no=product.product_no and doctor_guide.doctor_no=$doctor_no",'left')
  105. ->where($condition)
  106. ->select();
  107. foreach($list as &$product){
  108. $member_price = !empty($product['promo_price'])?$product['promo_price']:$product['member_price'];
  109. $intPrice = intval($member_price/100);
  110. $dotPrice = intval($member_price%100);
  111. $dotPrice = str_pad($dotPrice,2,0);
  112. $product['sales_price'] = $intPrice.".".$dotPrice;
  113. }
  114. return $list;
  115. }
  116. public function getProductAndPromoOne($condition=[],$doctor_no=0){
  117. //开始获取产品和促销信息
  118. $product = $this->alias('product')
  119. ->field('product.*,product_promo.price as promo_price,product_config.config_id')
  120. ->join('product_promo product_promo','product.product_id=product_promo.product_id and product_promo.status=1','left')
  121. ->join('product_config product_config','product.product_id=product_config.product_id and product_config.status=1','left')
  122. ->join('doctor_guide doctor_guide',"doctor_guide.product_no=product.product_no and doctor_guide.doctor_no=$doctor_no",'left')
  123. ->where($condition)
  124. ->find();
  125. $member_price = !empty($product['promo_price'])?$product['promo_price']:$product['member_price'];
  126. $intPrice = intval($member_price/100);
  127. $dotPrice = intval($member_price%100);
  128. $dotPrice = str_pad($dotPrice,2,0);
  129. $product['sales_price'] = $intPrice.".".$dotPrice;
  130. return $product;
  131. }
  132. }