Active.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\shop\model\plus\assemble;
  3. use app\common\enum\order\OrderSourceEnum;
  4. use app\common\model\plus\assemble\Active as ActiveModel;
  5. use app\shop\model\order\Order as OrderModel;
  6. use app\shop\model\plus\assemble\Product as AssembleProductModel;
  7. use app\shop\model\plus\assemble\AssembleSku as AssembleSkuModel;
  8. use app\common\model\plus\assemble\Bill as BillModel;
  9. /**
  10. * 拼团活动模型
  11. */
  12. class Active extends ActiveModel
  13. {
  14. /**
  15. * 参与记录列表
  16. */
  17. public function getList($param)
  18. {
  19. $model = $this;
  20. if (isset($param['status']) && $param['status'] > -1) {
  21. switch ($param['status']) {
  22. case 0:
  23. $model = $model->where('start_time', '>', time());
  24. break;
  25. case 1;
  26. $model = $model->where('start_time', '<', time())->where('end_time', '>', time());
  27. break;
  28. case 2;
  29. $model = $model->where('end_time', '<', time());
  30. break;
  31. }
  32. }
  33. if (isset($param['title']) && !empty($param['title'])) {
  34. $model = $model->where('title', 'like', '%' . trim($param['title']) . '%');
  35. }
  36. $list = $model->with(['file'])
  37. ->where('is_delete', '=', 0)
  38. ->order('create_time', 'desc')
  39. ->paginate($param);
  40. foreach ($list as $active) {
  41. //商品数
  42. $product_model = new AssembleProductModel();
  43. $active['product_num'] = $product_model->where('assemble_activity_id', '=', $active['assemble_activity_id'])->count();
  44. //订单数
  45. $active['total_sales'] = $product_model->where('assemble_activity_id', '=', $active['assemble_activity_id'])->sum('total_sales');
  46. }
  47. return $list;
  48. }
  49. /**
  50. *获取为开始的数据列表
  51. */
  52. public function getDatas()
  53. {
  54. return $this->where('end_time', '<', time())->select();
  55. }
  56. /**
  57. * 新增
  58. */
  59. public function add($data)
  60. {
  61. $this->startTrans();
  62. try {
  63. $arr = $this->setData($data);
  64. $this->save($arr);
  65. //添加商品
  66. $product_model = new AssembleProductModel();
  67. $product_model->add($this['assemble_activity_id'], $data['product_list']);
  68. // 事务提交
  69. $this->commit();
  70. return true;
  71. } catch (\Exception $e) {
  72. $this->error = $e->getMessage();
  73. $this->rollback();
  74. return false;
  75. }
  76. }
  77. /**
  78. * 修改
  79. */
  80. public function edit($data)
  81. {
  82. $this->startTrans();
  83. try {
  84. $arr = $this->setData($data);
  85. $this->save($arr);
  86. //添加商品
  87. $product_model = new AssembleProductModel();
  88. $product_model->edit($this['assemble_activity_id'], $data['product_list']);
  89. //删除商品
  90. if(isset($data['product_del_ids']) && count($data['product_del_ids']) > 0){
  91. $product_model->where('assemble_product_id', 'in', $data['product_del_ids'])->delete();
  92. (new AssembleSkuModel)->where('assemble_product_id', 'in', $data['product_del_ids'])->delete();
  93. }
  94. // 事务提交
  95. $this->commit();
  96. return true;
  97. } catch (\Exception $e) {
  98. $this->error = $e->getMessage();
  99. $this->rollback();
  100. return false;
  101. }
  102. }
  103. public function del()
  104. {
  105. //如果有正在拼团的商品
  106. $count = (new BillModel())->where('status', '=', 10)
  107. ->where('assemble_activity_id', '=', $this['assemble_activity_id'])
  108. ->count();
  109. if($count > 0){
  110. $this->error = '该活动下有正在拼团的订单';
  111. return false;
  112. }
  113. // 如果有未付款订单不能删除
  114. $count = (new OrderModel())->where('pay_status', '=', 10)
  115. ->where('order_source', '=', OrderSourceEnum::ASSEMBLE)
  116. ->where('activity_id', '=', $this['assemble_activity_id'])
  117. ->where('is_delete', '=', 0)
  118. ->count();
  119. if($count > 0){
  120. $this->error = '该活动下有未付款的订单';
  121. return false;
  122. }
  123. return $this->save([
  124. 'is_delete' => 1
  125. ]);
  126. }
  127. /**
  128. * 验证并组装数据
  129. */
  130. private function setData($data)
  131. {
  132. return [
  133. 'image_id' => $data['image_id'],
  134. 'title' => $data['title'],
  135. 'start_time' => strtotime($data['start_time']),
  136. 'end_time' => strtotime($data['end_time']),
  137. 'app_id' => self::$app_id,
  138. 'together_time' => $data['together_time'],
  139. 'sort' => $data['sort'],
  140. 'status' => $data['status'],
  141. 'fail_type' => $data['fail_type'],
  142. 'is_single' => $data['is_single'],
  143. ];
  144. }
  145. /**
  146. * 获取拼团商品列表
  147. */
  148. public function activityList()
  149. {
  150. $res = $this->where('start_time', '<=', time())
  151. ->where('end_time', '>=', time())->select();
  152. return !empty($res) ? array_column($res->toArray(), 'assemble_activity_id') : [];
  153. }
  154. }