Product.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\supplier\model\plus\point;
  3. use app\common\model\plus\point\Product as PointProductModel;
  4. use app\common\model\plus\point\ProductSku as PointSkuModel;
  5. /**
  6. * 参加记录模型
  7. */
  8. class Product extends PointProductModel
  9. {
  10. /*
  11. * 获取列表
  12. */
  13. public function getList($shop_supplier_id,$query = [])
  14. {
  15. $model = $this;
  16. if($query['status'] == 'has_audit'){
  17. $model = $model->where('status' ,'=', 10);
  18. }
  19. if($query['status'] == 'wait_audit'){
  20. $model = $model->where('status' ,'=', 0);
  21. }
  22. // 获取列表数据
  23. return $model->with(['product.image.file','sku'])
  24. ->where('shop_supplier_id', '=', $shop_supplier_id)
  25. ->where('is_delete','=', 0)
  26. ->order(['sort' => 'asc'])
  27. ->paginate($query);
  28. }
  29. /**
  30. * 添加商品
  31. * @param $data
  32. * @return bool
  33. */
  34. public function saveProduct($shop_supplier_id, $data)
  35. {
  36. $product = $data['product'];
  37. $this->startTrans();
  38. try {
  39. $stock = array_sum(array_column($product['spec_list'], 'point_stock'));
  40. //添加商品表
  41. $this->save([
  42. 'product_id' => $data['product_id'],
  43. 'limit_num' => $data['limit_num'],
  44. 'stock' => $stock,
  45. 'shop_supplier_id' => $shop_supplier_id,
  46. 'app_id' => self::$app_id,
  47. ]);
  48. //商品规格
  49. $sku_model = new PointSkuModel();
  50. $sku_data = [];
  51. foreach ($product['spec_list'] as $sku) {
  52. $sku_data[] = [
  53. 'point_product_id' => $this['point_product_id'],
  54. 'product_id' => $data['product_id'],
  55. 'product_sku_id' => $sku['product_sku_id'],
  56. 'point_stock' => $sku['point_stock'],
  57. 'point_num' => $sku['point_num'],
  58. 'product_attr' => $sku['product_attr'],
  59. 'product_price' => $sku['product_price'],
  60. 'point_money' => $sku['point_money'],
  61. 'app_id' => self::$app_id,
  62. ];
  63. }
  64. //新增规格
  65. $sku_model->saveAll($sku_data);
  66. $this->commit();
  67. } catch (\Exception $e) {
  68. $this->error = $e->getMessage();
  69. $this->rollback();
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * 修改
  76. */
  77. public function edit($data){
  78. $this->startTrans();
  79. try {
  80. //添加商品
  81. $stock = array_sum(array_column($data['product']['spec_list'], 'point_stock'));
  82. $this->save([
  83. 'status' => 0,
  84. 'remark' => '',
  85. 'stock' => $stock,
  86. 'limit_num' => $data['limit_num']
  87. ]);
  88. //商品规格
  89. $sku_model = new PointSkuModel();
  90. //先删除所有规格
  91. $sku_model->where('point_product_id', '=', $this['point_product_id'])->delete();
  92. $sku_data = [];
  93. foreach ($data['product']['spec_list'] as $sku) {
  94. $sku_data[] = [
  95. 'point_product_id' => $this['point_product_id'],
  96. 'product_id' => $data['product_id'],
  97. 'product_sku_id' => $sku['product_sku_id'],
  98. 'point_stock' => $sku['point_stock'],
  99. 'point_num' => $sku['point_num'],
  100. 'product_attr' => $sku['product_attr'],
  101. 'product_price' => $sku['product_price'],
  102. 'point_money' => $sku['point_money'],
  103. 'app_id' => self::$app_id,
  104. ];
  105. }
  106. $sku_model->saveAll($sku_data);
  107. // 事务提交
  108. $this->commit();
  109. return true;
  110. } catch (\Exception $e) {
  111. $this->error = $e->getMessage();
  112. $this->rollback();
  113. return false;
  114. }
  115. }
  116. /**
  117. * 检查商品是否存在
  118. */
  119. public function checkProduct($product_id)
  120. {
  121. return $this->where('product_id', '=', $product_id)->where('is_delete', '=', 0)->find();
  122. }
  123. /**
  124. * 获取排除商品id,报名了活动,待审核,或者通过了,并且活动未结束
  125. */
  126. public function getExcludeIds($shop_supplier_id){
  127. $list = $this->distinct(true)->field('product.*')
  128. ->where('is_delete', '=', 0)
  129. ->where('shop_supplier_id', '=', $shop_supplier_id)
  130. ->where('status', 'in', [0,10])
  131. ->column('product_id');
  132. $exclude_ids = [];
  133. foreach ($list as $key => $item){
  134. array_push($exclude_ids, $item);
  135. }
  136. return $exclude_ids;
  137. }
  138. /**
  139. * 删除
  140. */
  141. public function remove($point_product_id, $shop_supplier_id){
  142. // 如果不是审核通过,真删
  143. if($this['status'] != 10){
  144. return $this->where('point_product_id', '=', $point_product_id)
  145. ->where('shop_supplier_id', '=', $shop_supplier_id)
  146. ->delete();
  147. }else{
  148. // 逻辑删
  149. return $this->where('point_product_id', '=', $point_product_id)
  150. ->where('shop_supplier_id', '=', $shop_supplier_id)
  151. ->update([
  152. 'is_delete' => 1
  153. ]);
  154. }
  155. }
  156. }