KjActivity.php 9.2 KB

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