Active.php 4.7 KB

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