ProductService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\index\service;
  3. use app\index\model\BuyCarModel;
  4. use app\index\model\ProductAttrModel;
  5. use app\index\model\ProductAttrValueModel;
  6. use app\index\model\ProductImgModel;
  7. use app\index\model\ProductModel;
  8. use app\index\model\ProductCategoryModel;
  9. use app\index\model\ProductOrderDetailModel;
  10. use app\index\model\RelProductAttrModel;
  11. use think\Db;
  12. class ProductService extends BaseService
  13. {
  14. /**
  15. * @param $data
  16. * @return mixed
  17. * 修改产品
  18. */
  19. public function upProductInfo($data)
  20. {
  21. $where['product_id'] = $data['product_id'];
  22. $ProductModel = new ProductModel();
  23. $ProductModel->upInfo($where,$data);
  24. return $this->return_code[200];
  25. }
  26. /**
  27. * @param $data
  28. * @return mixed
  29. * 添加产品
  30. */
  31. public function addProduct($data){
  32. $product_value_id = $data['product_value_id'];
  33. unset($data['product_value_id']);
  34. $db_logistic = Db::connect('db_mall');
  35. $db_logistic->startTrans();
  36. $ProductModel = new ProductModel();
  37. $id = $ProductModel->addInfo($data);
  38. if(!$id){
  39. $db_logistic->rollback();
  40. return $this->return_code[40001];
  41. }
  42. $arr = [];
  43. foreach($product_value_id as $k=>$v){
  44. $arr[$k]['product_id'] = $id;
  45. $arr[$k]['product_value_id'] = $v;
  46. }
  47. $RelProductAttrModel = new RelProductAttrModel();
  48. $rows = $RelProductAttrModel->addAllInfo($arr);
  49. if(!$rows){
  50. $db_logistic->rollback();
  51. return $this->return_code[40002];
  52. }
  53. $db_logistic->commit();
  54. return $this->return_code[200];
  55. }
  56. public function addProductCategory($data){
  57. $ProductCategoryModel = new ProductCategoryModel();
  58. $id = $ProductCategoryModel->addInfo($data);
  59. return $id;
  60. }
  61. public function upProductCategory($data){
  62. $where['category_id'] = $data['category_id'];
  63. $ProductCategoryModel = new ProductCategoryModel();
  64. $res = $ProductCategoryModel->upInfo($where,$data);
  65. return $res;
  66. }
  67. public function addProductAttr($data){
  68. $ProductAttrModel = new ProductAttrModel();
  69. $ProductAttrModel->addInfo($data);
  70. return $this->return_code[200];
  71. }
  72. public function upProductAttr($data){
  73. $where['attr_id'] = $data['attr_id'];
  74. $ProductTypeModel = new ProductAttrModel();
  75. $ProductTypeModel->upInfo($where,$data);
  76. return $this->return_code[200];
  77. }
  78. //添加产品属性值
  79. public function addProductAttrValue($data){
  80. $arr = [];
  81. foreach($data['value_name'] as $k=>$v){
  82. $arr[$k]['attr_id'] = $data['attr_id'];
  83. $arr[$k]['value_name'] = $v;
  84. }
  85. $ProductAttrValueModel = new ProductAttrValueModel();
  86. $ProductAttrValueModel->addAllInfo($arr);
  87. return $this->return_code[200];
  88. }
  89. //修改产品属性值
  90. public function upProductAttrValue($data){
  91. $where['product_value_id'] = $data['product_value_id'];
  92. $ProductTypeValueModel = new ProductAttrValueModel();
  93. $ProductTypeValueModel->upInfo($where,$data);
  94. return $this->return_code[200];
  95. }
  96. //添加图片
  97. public function addProductImg($data){
  98. $arr = [];
  99. foreach($data['img'] as $k=>$v){
  100. $arr[$k]['product_id'] = $data['product_id'];
  101. $arr[$k]['img'] = $v;
  102. }
  103. $ProductImgModel = new ProductImgModel();
  104. $ProductImgModel->addAllInfo($arr);
  105. return $this->return_code[200];
  106. }
  107. //修改图片
  108. public function upProductImg($data){
  109. $where['id'] = $data['id'];
  110. $ProductImgModel = new ProductImgModel();
  111. $ProductImgModel->upInfo($where,$data);
  112. return $this->return_code[200];
  113. }
  114. /**
  115. * 获取分类通过条件
  116. * @param $condition
  117. * @return array|false|\PDOStatement|string|\think\Model
  118. */
  119. public function getProductCategoryByCondition($condition){
  120. $productCategory = new ProductCategoryModel();
  121. return $productCategory->where($condition)->find();
  122. }
  123. /**
  124. * 获取购物车里面的产品数量和价格
  125. * @param $order_no
  126. * @param $productIds
  127. * @param $user_id
  128. * @return int
  129. */
  130. public function getOrderPriceByProductIds($order_no,$productIds,$user_id){
  131. $buyCarModel = new BuyCarModel();
  132. $orderDetailModel = new ProductOrderDetailModel();
  133. $buyCar = $buyCarModel->getCarAndProductInfo(['buy_car.product_id'=>['in',$productIds],'user_id'=>$user_id]);
  134. $totalPrice = 0;
  135. foreach($buyCar as $buyItem){
  136. $totalPrice += $buyItem['num']*$buyItem['sales_price'];
  137. $orderDetailModel->insertGetId([
  138. 'order_no'=>$order_no,
  139. 'product_id'=>$buyItem['product_id'],
  140. 'price'=>$buyItem['sales_price'],
  141. 'num'=>$buyItem['num'],
  142. ]);
  143. $buyCarModel->where(['id'=>$buyItem['buy_car_id']])->delete();
  144. }
  145. return $totalPrice;
  146. }
  147. /**
  148. * 减少商品的库存
  149. * @param $product_id
  150. * @param $num
  151. * @return bool
  152. */
  153. public function reduceStock($product_id,$num){
  154. $productModel = new ProductModel();
  155. $oneProduct = $productModel->getOne(['product_id'=>$product_id]);
  156. if(empty($oneProduct) || $oneProduct['remain_num'] < $num){
  157. HelperService::returnJson(['code'=>'400','msg'=>"product($product_id) remain num is less $num"]);
  158. }
  159. return $productModel->upInfo([
  160. 'product_id'=>$product_id,
  161. 'remain_num'=>$oneProduct['remain_num']
  162. ],['remain_num'=>$oneProduct['remain_num'] - $num]);
  163. }
  164. }