Product.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\shop\model\plus\point;
  3. use app\common\model\plus\point\Product as ProductModel;
  4. use app\common\model\plus\point\ProductSku as ProductSkuModel;
  5. /**
  6. * Class Exchange
  7. * 积分兑换模型
  8. * @package app\shop\model\plus\exchange
  9. */
  10. class Product extends ProductModel
  11. {
  12. /*
  13. * 获取列表
  14. */
  15. public function getList($query = [])
  16. {
  17. // 获取列表数据
  18. return $this->with(['product.image.file','sku'])
  19. ->where('is_delete','=', 0)
  20. ->order(['sort' => 'asc', 'create_time' => 'asc'])
  21. ->paginate($query);
  22. }
  23. /*
  24. * 获取排除id
  25. */
  26. public function getExcludeIds()
  27. {
  28. // 获取列表数据
  29. return $this->field(['product_id'])->where('is_delete','=', 0)
  30. ->select()->toArray();
  31. }
  32. /**
  33. * 添加商品
  34. * @param $data
  35. * @return bool
  36. */
  37. public function saveProduct($data, $isUpdate = false)
  38. {
  39. $product = $data['product'];
  40. $this->startTrans();
  41. try {
  42. $stock = array_sum(array_column($product['spec_list'], 'point_stock'));
  43. //添加商品表
  44. $this->save([
  45. 'product_id' => $data['product_id'],
  46. 'limit_num' => $data['limit_num'],
  47. 'sort' => $data['sort'],
  48. 'status' => $data['status'],
  49. 'stock' => $stock,
  50. 'app_id' => self::$app_id
  51. ]);
  52. //商品规格
  53. $sku_model = new ProductSkuModel();
  54. $save_data = [];
  55. $not_in_sku_id = [];
  56. foreach ($product['spec_list'] as $sku) {
  57. $sku_data = [
  58. 'point_product_id' => $this['point_product_id'],
  59. 'product_id' => $data['product_id'],
  60. 'product_sku_id' => $sku['product_sku_id'],
  61. 'point_stock' => $sku['point_stock'],
  62. 'point_num' => $sku['point_num'],
  63. 'product_attr' => $sku['product_attr'],
  64. 'product_price' => $sku['product_price'],
  65. 'point_money' => $sku['point_money'],
  66. 'app_id' => self::$app_id,
  67. ];
  68. if($sku['point_product_sku_id'] > 0){
  69. $detail = $sku_model->find($sku['point_product_sku_id']);
  70. if($detail){
  71. $detail->save($sku_data);
  72. array_push($not_in_sku_id, $sku['point_product_sku_id']);
  73. }
  74. }else{
  75. $save_data[] = $sku_data;
  76. }
  77. }
  78. //删除规格
  79. count($not_in_sku_id) > 0 && $sku_model->where('point_product_id', '=', $data['point_product_id'])
  80. ->whereNotIn('point_product_sku_id', $not_in_sku_id)
  81. ->delete();
  82. //新增规格
  83. count($save_data) > 0 && $sku_model->saveAll($save_data);
  84. $this->commit();
  85. } catch (\Exception $e) {
  86. $this->error = $e->getMessage();
  87. $this->rollback();
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * 检查商品是否存在
  94. * @param int $product_id
  95. */
  96. public function checkProduct($product_id)
  97. {
  98. return $this->where('product_id', '=', $product_id)->where('is_delete', '=', 0)->find();
  99. }
  100. /**
  101. * 获取商品信息
  102. * @param $id
  103. */
  104. public function getPointData($arr)
  105. {
  106. return $this->where('point_product_id', '=', $arr['point_product_id'])->with(['product.image.file','sku'])->find();
  107. }
  108. /**
  109. * @param $data
  110. * 修改商品信息
  111. */
  112. public function edit($data)
  113. {
  114. $this->startTrans();
  115. try {
  116. //添加商品表
  117. $this->save([
  118. 'limit_num' => $data['limit_num'],
  119. 'sort' => $data['sort'],
  120. 'stock' => isset($data['stock'])?$data['stock']: 0,
  121. 'status' => $data['status'],
  122. ]);
  123. if($data['spec_type'] == 20) {
  124. $stock = 0;//总库存
  125. foreach ($data['specData']['spec_list'] as $item) {
  126. $sku = $this->getSkuModel($item['spec_form']['point_product_sku_id']);
  127. $sku->save([
  128. 'stock' => $item['spec_form']['stock'],
  129. 'point_num' => $item['spec_form']['point_num'],
  130. 'point_money' => $item['spec_form']['point_money'],
  131. ]);
  132. $stock += $item['spec_form']['stock'];
  133. }
  134. $this->save([
  135. 'stock' => $stock,
  136. ]);
  137. }else{
  138. $sku_model = $this['sku'][0];
  139. //单规格
  140. $sku_model->save([
  141. 'stock' => $data['skuData']['stock'],
  142. 'point_num' => $data['skuData']['point_num'],
  143. 'point_money' => $data['skuData']['point_money'],
  144. ]);
  145. }
  146. $this->commit();
  147. } catch (\Exception $e) {
  148. $this->error = $e->getMessage();
  149. $this->rollback();
  150. return false;
  151. }
  152. return true;
  153. }
  154. private function getSkuModel($point_product_sku_id){
  155. foreach ($this['sku'] as $sku){
  156. if($sku['point_product_sku_id'] == $point_product_sku_id){
  157. return $sku;
  158. }
  159. }
  160. return false;
  161. }
  162. /**
  163. * 删除
  164. */
  165. public function del($id)
  166. {
  167. return $this->where('point_product_id', '=', $id)->update([
  168. 'is_delete' => 1
  169. ]);
  170. }
  171. /**
  172. * 商品ID是否存在
  173. */
  174. public static function isExistProductId($productId)
  175. {
  176. return !!(new static)->where('product_id', '=', $productId)
  177. ->where('is_delete', '=', 0)
  178. ->value('point_product_id');
  179. }
  180. }