Product.php 6.2 KB

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