Product.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\shop\model\plus\assemble;
  3. use app\common\model\plus\assemble\Product as ProductModel;
  4. /**
  5. * 拼团商品模型
  6. * @package app\shop\model\plus\invitationgift
  7. */
  8. class Product extends ProductModel
  9. {
  10. /**
  11. * 获取秒杀商品列表
  12. * @param $param
  13. */
  14. public static function getList($assemble_activity_id)
  15. {
  16. return (new static())->with(['product', 'assembleSku'])
  17. ->where('assemble_activity_id', '=', $assemble_activity_id)
  18. ->order(['sort' => 'asc', 'create_time' => 'asc'])
  19. ->select();
  20. }
  21. /**
  22. * 新增
  23. * @param $data
  24. */
  25. public function add($assemble_activity_id, $product_list)
  26. {
  27. //添加活动
  28. foreach ($product_list as $product){
  29. $this->addProduct($assemble_activity_id, $product);
  30. }
  31. }
  32. public function addProduct($assemble_activity_id, $product, $isUpdate = false)
  33. {
  34. //添加商品
  35. $stock = array_sum(array_column($product['spec_list'], 'assemble_stock'));
  36. $arr = [
  37. 'product_id' => $product['product_id'],
  38. 'limit_num' => $product['limit_num'],
  39. 'stock' => $stock,
  40. 'assemble_activity_id' => $assemble_activity_id,
  41. 'assemble_num' => $product['assemble_num'],
  42. 'sort' => $product['sort'],
  43. 'sales_initial' => $product['sales_initial'],
  44. 'is_delete' => $product['is_delete'],
  45. 'app_id' => self::$app_id,
  46. ];
  47. if($isUpdate){
  48. $model = static::detail($product['assemble_product_id'])?:new self();
  49. }else{
  50. $model = new self();
  51. }
  52. $model->save($arr);
  53. //商品规格
  54. $sku_model = new AssembleSku();
  55. $save_data = [];
  56. $not_in_sku_id = [];
  57. foreach ($product['spec_list'] as $sku) {
  58. $sku_data = [
  59. 'assemble_product_id' => $model['assemble_product_id'],
  60. 'product_id' => $product['product_id'],
  61. 'product_sku_id' => $sku['product_sku_id'],
  62. 'assemble_price' => $sku['assemble_price'],
  63. 'product_price' => $sku['product_price'],
  64. 'assemble_stock' => $sku['assemble_stock'],
  65. 'product_attr' => isset($sku['product_attr'])?$sku['product_attr']:'',
  66. 'assemble_activity_id' => $assemble_activity_id,
  67. 'app_id' => self::$app_id,
  68. ];
  69. if($sku['assemble_product_sku_id'] > 0){
  70. $detail = $sku_model->find($sku['assemble_product_sku_id']);
  71. if($detail){
  72. $detail->save($sku_data);
  73. array_push($not_in_sku_id, $sku['assemble_product_sku_id']);
  74. }
  75. }else{
  76. $save_data[] = $sku_data;
  77. }
  78. }
  79. //删除规格
  80. count($not_in_sku_id) > 0 && $sku_model->where('assemble_product_id', '=', $model['assemble_product_id'])
  81. ->whereNotIn('assemble_product_sku_id', $not_in_sku_id)
  82. ->delete();
  83. //新增规格
  84. count($save_data) > 0 && $sku_model->saveAll($save_data);
  85. }
  86. /**
  87. * 修改
  88. */
  89. public function edit($assemble_activity_id, $product_list)
  90. {
  91. //添加活动
  92. foreach ($product_list as $product){
  93. $this->addProduct($assemble_activity_id, $product, true);
  94. }
  95. }
  96. public function del($assemble_product_id)
  97. {
  98. $this->startTrans();
  99. try {
  100. self::destroy($assemble_product_id);
  101. $model = new AssembleSku();
  102. $model->delAll($assemble_product_id);
  103. // 事务提交
  104. $this->commit();
  105. return true;
  106. } catch (\Exception $e) {
  107. $this->error = $e->getMessage();
  108. $this->rollback();
  109. return false;
  110. }
  111. }
  112. /**
  113. *获取指定活动商品
  114. */
  115. public function getProductList($assemble_activity_id, $param = [])
  116. {
  117. $model = $this;
  118. $res = $model->with(['product.image.file', 'assembleSku', 'active.file'])
  119. ->where('assemble_activity_id', '=', $assemble_activity_id)
  120. ->paginate($param);
  121. if (!empty($res)) {
  122. $res = $res->toArray();
  123. foreach ($res['data'] as $key => $val) {
  124. $arr = array_column($res['data'][$key]['assembleSku'], 'assemble_price');
  125. if (count($arr) == 1) {
  126. $res['data'][$key]['assemble_price'] = '¥' . current($arr);
  127. } else {
  128. sort($arr);
  129. $res['data'][$key]['assemble_price'] = '¥' . current($arr) . ' - ¥' . end($arr);
  130. }
  131. }
  132. }
  133. return $res;
  134. }
  135. public function getAssembleDetail($assemble_product_id)
  136. {
  137. $res = $this->with(['product.image.file', 'assembleSku.productSku.image'])
  138. ->where('assemble_product_id', '=', $assemble_product_id)->find();
  139. if (!empty($res)) {
  140. $arr = array_column($res->toArray()['assembleSku'], 'assemble_price');
  141. foreach ($res['assembleSku'] as $key => $val) {
  142. $res['assembleSku'][$key]['price'] = $res['assembleSku'][$key]['productSku']['product_price'];
  143. }
  144. $arr1 = array_column($res->toArray()['assembleSku'], 'price');
  145. sort($arr);
  146. sort($arr1);
  147. $res['assemble_price'] = '¥' . current($arr);
  148. $res['line_price'] = '¥' . current($arr1);
  149. if (count($arr) > 1) {
  150. $res['assemble_price'] = '¥' . current($arr) . ' - ¥' . end($arr);
  151. $res['line_price'] = '¥' . current($arr1) . ' - ¥' . end($arr1);
  152. }
  153. }
  154. return $res;
  155. }
  156. /**
  157. * 商品ID是否存在,并且活动未结束,或未删除
  158. */
  159. public static function isExistProductId($productId)
  160. {
  161. return !!(new static)->alias('product')
  162. ->join('assemble_activity activity', 'activity.assemble_activity_id = product.assemble_activity_id','left')
  163. ->where('product.product_id', '=', $productId)
  164. ->where('product.is_delete', '=', 0)
  165. ->where('activity.end_time', '>', time())
  166. ->where('activity.is_delete', '=', 0)
  167. ->value('product.assemble_product_id');
  168. }
  169. }