ActivityDiscount.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\model;
  4. use app\common\library\helper;
  5. use app\common\model\ActivityDiscountGoods as ActivityDiscountGoodsModel;
  6. use app\common\model\Goods;
  7. use app\common\model\GoodsSku;
  8. use app\common\model\CommonHis as CommonHisModel;
  9. use app\common\service\store\User as StoreUserService;
  10. /**
  11. * 活动中心 ▸ N件X折
  12. * Class Ad
  13. * @package app\common\model
  14. */
  15. class ActivityDiscount extends BaseModel
  16. {
  17. protected $name = 'activity_discount';
  18. protected $append = ['status'];
  19. /**
  20. * 一对多关联商品表
  21. */
  22. public function goods(){
  23. return $this->hasMany('ActivityDiscountGoods', 'activity_id')->order('id', 'desc');
  24. }
  25. /**
  26. * 获取器:阶梯属性
  27. * @param $value
  28. * @return array
  29. */
  30. public function getDiscountPropsAttr($value)
  31. {
  32. return helper::jsonDecode($value);
  33. }
  34. /**
  35. * 设置器:阶梯属性
  36. * @param $value
  37. * @return string
  38. */
  39. public function setDiscountPropsAttr($value)
  40. {
  41. return $value ? helper::jsonEncode($value) : '';
  42. }
  43. /**
  44. * 获取列表
  45. * @param array $param
  46. * @return \think\Paginator
  47. * @throws \think\db\exception\DbException
  48. */
  49. public function getList($param = [])
  50. {
  51. // 检索查询调价你
  52. $filter = $this->getFilter($param);
  53. // 排序条件
  54. // $sort = $this->setQuerySort($param);
  55. $sort = ['id'=>'desc'];
  56. // 查询列表数据
  57. return $this->where($filter)->with([])->order($sort)->paginate(15);
  58. }
  59. //当前的进行中的活动id
  60. public function getActivityGoodIdArray($activity_id =0){
  61. $filter[] = ['start_time','<=',date('Y-m-d H:i:s')];
  62. $filter[] = ['end_time','>=',date('Y-m-d H:i:s')];
  63. // $filter[] = ['audit_status_zg','=',10];
  64. // $filter[] = ['is_up','=',1];
  65. $id_array = $this->where($filter)->column("id");
  66. if($activity_id>0){
  67. $id_array = array_diff($id_array,[$activity_id]);
  68. }
  69. $goods_id_array = ActivityDiscountGoodsModel::where("activity_id","in",$id_array)->column("goods_id");
  70. return $goods_id_array;
  71. }
  72. /**
  73. * 检索查询条件
  74. * @param array $param
  75. * @return array
  76. */
  77. private function getFilter($param = [])
  78. {
  79. // 默认查询条件
  80. $params = $this->setQueryDefaultValue($param, ['name' => '','audit_status_zg'=>-1,'status'=>-1]);
  81. // 检索查询条件
  82. $filter = [];
  83. !empty($params['name']) && $filter[] = ['name', 'like', "%{$params['name']}%"];
  84. if(isset($params['audit_status_zg']) && $params['audit_status_zg']>-1){
  85. $filter[] = ['audit_status_zg', '=', $params['audit_status_zg']];
  86. }
  87. if (!empty($params['betweenTime'])) {
  88. $times = between_time($params['betweenTime']);
  89. $filter[] = ['start_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  90. $filter[] = ['end_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 86400)];
  91. }
  92. //活动状态
  93. if(isset($params['status'])&&$params['status']>-1){
  94. switch ($params['status']){
  95. case 1://进行中
  96. $filter[] = ['start_time','<=',date('Y-m-d H:i:s')];
  97. $filter[] = ['end_time','>=',date('Y-m-d H:i:s')];
  98. // $filter[] = ['audit_status_zg','=',10];
  99. $filter[] = ['is_up','=',1];
  100. $filter[] = ['audit_status_zg','=',10];
  101. break;
  102. case 2://未开始
  103. $filter[] = ['start_time','>',date('Y-m-d H:i:s')];
  104. $filter[] = ['is_up','=',1];
  105. $filter[] = ['audit_status_zg','=',10];
  106. break;
  107. case 3://已结束
  108. $filter[] = ['end_time','<',date('Y-m-d H:i:s')];
  109. $filter[] = ['is_up','=',0];
  110. $filter[] = ['audit_status_zg','=',10];
  111. break;
  112. }
  113. }
  114. return $filter;
  115. }
  116. //活动状态 即将开始或已结束
  117. public function getStatusAttr() {
  118. if($this->audit_status_zg!=10){
  119. return 0;
  120. }
  121. if($this->is_up==0){
  122. return 3;
  123. }
  124. $now = date("Y-m-d H:i:s",time());
  125. if ($now < $this->start_time) {
  126. return 2;
  127. }
  128. if ($now > $this->end_time) {
  129. return 3;
  130. }
  131. if($this->is_up==1){
  132. return 1;
  133. }else{
  134. return 2;
  135. }
  136. }
  137. /**
  138. * 审核
  139. * @param array $data
  140. * @return bool
  141. */
  142. public function audit(array $data): bool
  143. {
  144. $this->transaction(function () use ($data) {
  145. $id = (int)$this['id'];
  146. $this->save($data);
  147. $user = StoreUserService::getLoginInfo();
  148. $real_name = $user['user']['real_name']??'';
  149. $id = (int)$this['id'];
  150. if($data['audit_status_zg']==10){
  151. (new CommonHisModel())->add(1, $id, $real_name,'同意并发布活动');
  152. }
  153. if($data['audit_status_zg']==20){
  154. $remark = '不同意'." ".$data['refuse_desc_zg'];
  155. (new CommonHisModel())->add(1, $id, $real_name,$remark);
  156. }
  157. });
  158. return true;
  159. }
  160. /**
  161. * 作废
  162. * @param array $data
  163. * @return bool
  164. */
  165. public function down(array $data): bool {
  166. $this->transaction(function () use ($data) {
  167. $id = (int)$this['id'];
  168. $this->save($data);
  169. });
  170. return true;
  171. }
  172. /**
  173. * 详情
  174. * @param int $id
  175. * @return null|static
  176. */
  177. public static function detail(int $id,array $with=[])
  178. {
  179. return self::get($id,$with);
  180. }
  181. /**
  182. * 添加
  183. * @param $data
  184. * @return false|int
  185. */
  186. public function addOne($data)
  187. {
  188. isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  189. isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  190. $this->transaction(function () use ($data) {
  191. $this->save($data);
  192. if(isset($data['goods'])){
  193. $goods = new ActivityDiscountGoodsModel;
  194. $goods->add($this->id,$data['goods']);
  195. }
  196. $user = StoreUserService::getLoginInfo();
  197. $real_name = $user['user']['real_name']??'';
  198. $id = (int)$this['id'];
  199. (new CommonHisModel())->add(1, $id, $real_name,'提交审核');
  200. });
  201. return true;
  202. }
  203. /**
  204. * 编辑
  205. * @param $data
  206. * @return bool|int
  207. */
  208. public function edit($data)
  209. {
  210. isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null;
  211. isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null;
  212. $this->transaction(function () use ($data) {
  213. $this->save($data);
  214. if(isset($data['goods'])){
  215. $goods = new ActivityDiscountGoodsModel;
  216. $goods->add($this->id,$data['goods']);
  217. }
  218. $user = StoreUserService::getLoginInfo();
  219. $real_name = $user['user']['real_name']??'';
  220. $id = (int)$this['id'];
  221. (new CommonHisModel())->add(1, $id, $real_name,'提交审核');
  222. });
  223. return true;
  224. }
  225. /**
  226. * 关联记录
  227. * @return \think\model\relation\BelongsTo
  228. */
  229. public function his()
  230. {
  231. return $this->hasMany('CommonHis','target_id','id')->where('target_type',1)->order('id', 'desc');
  232. }
  233. // /**
  234. // * 删除
  235. // * @param $id
  236. // * @return bool|int
  237. // */
  238. // public function remove()
  239. // {
  240. // return $this->delete();
  241. // }
  242. }