MjSendActivity.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace app\store\model\mj;
  3. use app\common\model\mj\MjSendActivity as MjSendActivityModel;
  4. use app\common\model\mj\MjSendActivityAction as MjSendActivityActionModel;
  5. use app\common\model\mj\MjSendGoods as MjSendGoodsModel;
  6. use app\common\model\mj\MjSendGiftGoods as MjSendGiftGoodsModel;
  7. use think\facade\Log;
  8. /**
  9. * 裂变优惠券活动模型
  10. * @package app\store\model\coupon
  11. */
  12. class MjSendActivity extends MjSendActivityModel
  13. {
  14. protected $append = ['activity_status'];
  15. public function getActivityStatusAttr($value,$data){
  16. if($data['start_time']>date('Y-m-d H:i:s')){
  17. return ['code'=>2,'text'=>'未开始'];
  18. }
  19. if($data['start_time']<=date('Y-m-d H:i:s')&&$data['end_time']>=date('Y-m-d H:i:s')){
  20. return ['code'=>1,'text'=>'进行中'];
  21. }
  22. if($data['end_time']<date('Y-m-d H:i:s')){
  23. return ['code'=>3,'text'=>'已结束'];
  24. }
  25. return ['code'=>1,'text'=>'进行中'];
  26. }
  27. /**
  28. * 获取列表
  29. * @param array $param 查询条件
  30. * @param int $listRows 分页数量
  31. * @return mixed
  32. * @throws \think\db\exception\DbException
  33. */
  34. public function getList(array $param = [], int $listRows = 15)
  35. {
  36. // 检索查询条件
  37. $filter = $this->getQueryFilter($param);
  38. // 执行查询
  39. $list = $this->with([])
  40. ->where($filter)
  41. ->order(['audit_status'=>'asc','create_time'=>'desc'])
  42. ->paginate($listRows);
  43. return $list;
  44. }
  45. /**
  46. * 检索查询条件
  47. * @param array $param
  48. * @return \think\db\BaseQuery
  49. */
  50. private function getQueryFilter(array $param)
  51. {
  52. // 商品列表获取条件
  53. $params = $this->setQueryDefaultValue($param, [
  54. // 'is_delete' => 0,
  55. ]);
  56. // 筛选条件
  57. $filter = [];
  58. // 活动起止时间
  59. if (!empty($params['betweenTime'])) {
  60. $times = between_date($params['betweenTime']);
  61. $filter[] = ['start_time', '>=', $times['start_date']];
  62. $filter[] = ['end_time', '<=', $times['end_date']];
  63. }
  64. // 活动名称
  65. !empty($params['name']) && $filter[] = ['name', 'like', "%{$params['name']}%"];
  66. // 审核状态 0待审核 1审核通过 2审核不通过
  67. if(isset($params['auditStatus'])&&$params['auditStatus']>-1){
  68. $filter[] = ['audit_status','=',$params['auditStatus']];
  69. }
  70. //启用状态0禁用 1启用
  71. if(isset($params['status'])&&$params['status']>-1){
  72. $filter[] = ['status','=',$params['status']];
  73. }
  74. //活动状态
  75. if(isset($params['statusText'])&&$params['statusText']>0){
  76. switch ($params['statusText']){
  77. case 1://进行中
  78. $filter[] = ['start_time','<=',date('Y-m-d H:i:s')];
  79. $filter[] = ['end_time','>=',date('Y-m-d H:i:s')];
  80. $filter[] = ['audit_status','=',1];
  81. break;
  82. case 2://未开始
  83. $filter[] = ['start_time','>',date('Y-m-d H:i:s')];
  84. break;
  85. case 3://已结束
  86. $filter[] = ['end_time','<',date('Y-m-d H:i:s')];
  87. break;
  88. }
  89. }
  90. // 实例化新查询对象
  91. return $filter;
  92. }
  93. /**
  94. * 启用 禁用
  95. * @param array $data
  96. * @return bool
  97. */
  98. public function status(array $data): bool
  99. {
  100. $act = MjSendActivityModel::detail($data['activity_id']);
  101. if ( $data['status'] == 1 && $this->checkGoodsValid($act))return false;
  102. $this->transaction(function () use ($data) {
  103. $this->save($data);
  104. });
  105. return true;
  106. }
  107. /**
  108. * 审核
  109. * @param array $data
  110. * @return bool
  111. */
  112. public function audit(array $data): bool
  113. {
  114. $act = MjSendActivityModel::detail($data['activity_id']);
  115. if ($data['audit_status'] == 1 && $this->checkGoodsValid($act))return false;
  116. $this->transaction(function () use ($data) {
  117. $this->save($data);
  118. $this->addAction($data['activity_id'],$data);
  119. });
  120. return true;
  121. }
  122. /**
  123. * 新增记录
  124. * @param array $data
  125. * @return int
  126. */
  127. public function addOne(array $data)
  128. {
  129. //检查是否已经有有效活动时间重叠了
  130. if ($this->checkGoodsValid($data))return 3;
  131. $data['code'] = strval(mt_rand(100000000,999999999));
  132. try {
  133. $this->transaction(function () use ($data) {
  134. $this->save($data);
  135. // 选择的优惠券存入
  136. $couponActivityCouponModel = new MjSendGoodsModel();
  137. $couponActivityCouponModel->add($this->id,$data['goods_ids']??[]);
  138. //赠品写入
  139. $giftGoodsModel = new MjSendGiftGoodsModel();
  140. $giftGoodsModel->add($this->id,$data['gift_ids']);
  141. //写入行为记录
  142. //$data['audit_status'] = 0;
  143. $this->addAction($this->id,$data);
  144. });
  145. }catch (\Exception $e){
  146. Log::error(__METHOD__.'::'.$e->getMessage());
  147. return 2;
  148. }
  149. return 1;
  150. }
  151. //添加行为记录
  152. public function addAction($coupon_activity_id,$data){
  153. $behavior = '';
  154. switch ($data['audit_status']){
  155. case 0:$behavior = '提交审核'; break;
  156. case 1:$behavior = '同意并发布活动'; break;
  157. case 2:$behavior = '不同意'; break;
  158. }
  159. $refuse['mj_send_activity_id'] = $coupon_activity_id;
  160. $refuse['audit_status'] = $data['audit_status'];
  161. $refuse['audit_reason'] = $data['audit_reason']??'';
  162. $refuse['behavior'] = $behavior;
  163. $refuse['audit_time'] = date("Y-m-d H:i:s",time());
  164. $refuse['audit_user'] = $data['audit_user'];
  165. $refuse['audit_admin_id'] = $data['audit_admin_id'];
  166. $refuseModel = new MjSendActivityAction();
  167. $refuseModel->save($refuse);
  168. }
  169. /**
  170. * 更新记录
  171. * @param array $data
  172. * @return bool
  173. */
  174. public function updOne(array $data)
  175. {
  176. if ($this->checkGoodsValid($data))return 3;
  177. try {
  178. $this->transaction(function () use ($data) {
  179. //$data['audit_status'] = 0;
  180. $this->save($data);
  181. //写入行为记录
  182. // 选择的优惠券存入
  183. $couponActivityCouponModel = new MjSendGoodsModel();
  184. $couponActivityCouponModel->add($data['id'],$data['goods_ids']??[]);
  185. //赠品写入
  186. $giftGoodsModel = new MjSendGiftGoodsModel();
  187. $giftGoodsModel->add($data['id'],$data['gift_ids']);
  188. //写入行为记录
  189. $this->addAction($this->id,$data);
  190. });
  191. }catch (\Exception $e){
  192. Log::error(__METHOD__.'::'.$e->getMessage());
  193. return 2;
  194. }
  195. return 1;
  196. }
  197. /**
  198. * 校验同款商品不能和正在有效期的活动时间交叉
  199. * @param $data
  200. * @return false
  201. */
  202. public function checkGoodsValid($data){
  203. $now = date('Y-m-d H:i:s');
  204. $filter[] = ['start_time','<',$now];
  205. $filter[] = ['end_time','>',$now];
  206. $filter[] = ['audit_status','=',1];
  207. $filter[] = ['status','=',1];
  208. $goodsId = $data['goods_ids'][0]['goods_id']??0;
  209. if (!$goodsId)return false;
  210. $m = self::alias('ma')->where('ma.status',1)
  211. ->leftJoin('mj_send_goods ag','ag.mj_send_activity_id = ma.id');
  212. if (!empty($data['id'])){
  213. $m->where('ma.id','<>',$data['id']);
  214. }
  215. return $m->where('ag.goods_id','=',$goodsId)
  216. ->where(function ($s) use ($data){
  217. $s->whereOr(function($query) use ($data){
  218. $query->whereBetweenTime('ma.start_time', $data['start_time'], $data['end_time']);
  219. })->whereOr(function ($query) use ($data){
  220. $query->whereBetweenTime('ma.end_time',$data['start_time'],$data['end_time']);
  221. })->whereOr(function ($query) use ($data){
  222. $query->where('ma.start_time','<',$data['start_time'])->where('end_time','>',$data['end_time']);
  223. });
  224. })->find();
  225. }
  226. }