SeckillActivityForm.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\constants\OptionSetting;
  9. use app\jobs\storeSync\DiyCommon;
  10. use app\models\ActivityNewUser;
  11. use app\models\ActivityNewUserGoods;
  12. use app\models\Cat;
  13. use app\models\DeliveryRules;
  14. use app\models\Goods;
  15. use app\models\GoodsCat;
  16. use app\models\MdGroupActivities;
  17. use app\models\MdGroupActivitiesGoods;
  18. use app\models\Option;
  19. use app\models\PtActivity;
  20. use app\models\PtActivityGoods;
  21. use app\models\SeckillActivity;
  22. use app\models\SeckillActivityGoods;
  23. use app\models\SeckillActivityOrderLog;
  24. use app\models\Store;
  25. use app\models\StoreSyncExtLog;
  26. use yii\base\Model;
  27. class SeckillActivityForm extends Model
  28. {
  29. public $id;
  30. public $ids;
  31. public $name;
  32. public $is_use_coupon;
  33. public $start_time;
  34. public $end_time;
  35. public $goods;
  36. public $status;
  37. public $self_limit_num;
  38. public $order_limit_num;
  39. public $cat_id;
  40. public $is_platform = 0;
  41. public $is_platform_audit = 0;
  42. public $store_name;
  43. public $group_activity = 0;
  44. public function rules()
  45. {
  46. return [
  47. [['status', 'is_use_coupon', 'id', 'goods_id', 'self_limit_num', 'order_limit_num', 'cat_id', 'is_platform', 'is_platform_audit','group_activity'], 'integer'],
  48. [['start_time', 'end_time', 'ids', 'name', 'store_name'], 'string'],
  49. [['goods'], 'safe'],
  50. ['group_activity','default','value'=>0]
  51. ];
  52. }
  53. public function search ()
  54. {
  55. try {
  56. $query = SeckillActivity::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
  57. if ((int)$this->status === 1) {//未开始
  58. $query->andWhere(['>' , 'start_time', time()]);
  59. }
  60. if ((int)$this->status === 2) {//进行中
  61. $query->andWhere(['AND', ['<' , 'start_time', time()], ['>' , 'end_time', time()]]);
  62. }
  63. if ((int)$this->status === 3) { //已结束
  64. $query->andWhere(['<' , 'end_time', time()]);
  65. }
  66. if (!empty($this->name)) { //名称
  67. $query->andWhere(['LIKE' , 'name', $this->name]);
  68. }
  69. $query->andWhere(['is_platform' => $this->is_platform]);
  70. if (!empty($this->start_time) && !empty($this->end_time)) { //时间筛选
  71. $query->andWhere(['OR',
  72. ['AND',
  73. ['<=' , 'start_time', strtotime($this->start_time)],
  74. ['>=' , 'end_time',strtotime($this->end_time)]
  75. ],
  76. ['AND',
  77. ['<=' , 'start_time', strtotime($this->start_time)],
  78. ['<=' , 'end_time',strtotime($this->end_time)],
  79. ['>=' , 'end_time',strtotime($this->start_time)]
  80. ],
  81. ['AND',
  82. ['>=' , 'start_time', strtotime($this->start_time)],
  83. ['<=' , 'end_time',strtotime($this->end_time)]
  84. ],
  85. ['AND',
  86. ['>=' , 'start_time', strtotime($this->start_time)],
  87. ['>=' , 'end_time',strtotime($this->end_time)],
  88. ['<=' , 'start_time',strtotime($this->end_time)]
  89. ],
  90. ]);
  91. }
  92. $query->select('id, start_time, end_time, created_at, updated_at, name, is_platform_audit');
  93. $pagination = pagination_make($query);
  94. foreach ($pagination['list'] as &$item) {
  95. $item['is_platform_audit'] = (int)$item['is_platform_audit'];
  96. //获取活动状态
  97. if ($item['start_time'] > time()) {
  98. $item['status'] = 1;
  99. }
  100. if ($item['start_time'] < time() && $item['end_time'] > time()) {
  101. $item['status'] = 2;
  102. }
  103. if ($item['end_time'] < time()) {
  104. $item['status'] = 3;
  105. }
  106. //格式化时间
  107. $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
  108. $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
  109. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  110. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  111. $item['order_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('order_id')->count();
  112. $item['self_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('user_id')->count();;
  113. $item['order_price'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->sum('sale_price') ?: '0.00';
  114. }
  115. return [
  116. 'code' => 0,
  117. 'msg' => 'success',
  118. 'data' => [
  119. 'data' => $pagination['list'],
  120. 'pageNo' => $pagination['pageNo'],
  121. 'totalCount' => $pagination['totalCount'],
  122. ]
  123. ];
  124. } catch (\Exception $e) {
  125. return [
  126. 'code' => 1,
  127. 'msg' => $e->getMessage()
  128. ];
  129. }
  130. }
  131. public function getGoods()
  132. {
  133. try {
  134. // 如果是团购活动请求商品 要排除一下已经添加过的商品
  135. if($this->group_activity == 1){
  136. $activities = MdGroupActivities::find()->where(['store_id'=>get_store_id(),'is_delete'=>0])->andWhere(['>','end_time',time()])->select('id')->column();
  137. $notGoodsId = MdGroupActivitiesGoods::find()->where(['activities_id'=>$activities,'is_delete'=>0])->select('goods_id')->column();
  138. }
  139. $query = Goods::find()->alias('g')->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'status' => 1, 'product_type' => 0]);
  140. if(!empty($notGoodsId)){
  141. $query->andWhere(['not in','g.id',$notGoodsId]);
  142. }
  143. if ($this->name) {
  144. $query->andWhere(['LIKE', 'g.name', $this->name]);
  145. }
  146. if ($this->cat_id) {
  147. $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id = g.id')
  148. ->leftJoin(['c' => Cat::tableName()], 'c.id = gc.cat_id')
  149. ->andWhere(['gc.cat_id' => $this->cat_id]);
  150. }
  151. $query->select('g.id, g.name, g.cover_pic, g.goods_num, g.price, g.attr, g.use_attr');
  152. $pagination = pagination_make($query);
  153. foreach ($pagination['list'] as &$item) {
  154. $item['attr'] = json_decode($item['attr'], true);
  155. if ($item['attr']) {
  156. foreach ($item['attr'] as &$value) {
  157. $value['pt_price'] = "";
  158. }
  159. }
  160. $item['cat'] = GoodsCat::find()->alias('gc')->where(['gc.goods_id' => $item['id'], 'gc.is_delete' => 0])
  161. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id = c.id')->select('name')->asArray()->all();
  162. }
  163. return [
  164. 'code' => 0,
  165. 'msg' => "获取成功",
  166. 'data' => [
  167. 'data' => $pagination['list'],
  168. 'pageNo' => $pagination['pageNo'],
  169. 'totalCount' => $pagination['totalCount'],
  170. ]
  171. ];
  172. } catch (\Exception $e) {
  173. return [
  174. 'code' => 1,
  175. 'msg' => $e->getMessage()
  176. ];
  177. }
  178. }
  179. public function getInfo()
  180. {
  181. try {
  182. $activity = SeckillActivity::find()->where(['id' => $this->id, 'is_delete' => 0])
  183. ->select('id, name, start_time, end_time, self_limit_num ,order_limit_num ,is_use_coupon, is_platform')->one();
  184. if ($activity) {
  185. $activity_goods = SeckillActivityGoods::find()->alias('sag')->leftJoin(['g' => Goods::tableName()], 'g.id = sag.goods_id')
  186. ->where(['sag.activity_id' => $activity->id, 'sag.is_delete' => 0])
  187. ->select('g.id, g.name, g.cover_pic, sag.attr, g.use_attr, g.price, g.goods_num, sag.virtual_sales, sag.virtual_num, sag.seckill_num, sag.sale_num, sag.seckill_price')
  188. ->asArray()->all();
  189. foreach ($activity_goods as &$activity_good) {
  190. $activity_good['attr'] = json_decode($activity_good['attr'], true);
  191. }
  192. $activity['start_time'] = date("Y-m-d H:i:s", $activity['start_time']);
  193. $activity['end_time'] = date("Y-m-d H:i:s", $activity['end_time']);
  194. }
  195. return [
  196. 'code' => 0,
  197. 'msg' => '获取成功',
  198. 'data' => [
  199. 'activity_goods' => $activity_goods ?? [],
  200. 'activity' => $activity ?: []
  201. ]
  202. ];
  203. } catch (\Exception $e) {
  204. return [
  205. 'code' => 1,
  206. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  207. ];
  208. }
  209. }
  210. public function save ()
  211. {
  212. $t = \Yii::$app->db->beginTransaction();
  213. try {
  214. if (!$this->name || !$this->start_time || !$this->end_time || !$this->goods) {
  215. throw new \Exception("请将参数填充完整");
  216. }
  217. $activity = SeckillActivity::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  218. if (empty($activity)) {
  219. $activity = new SeckillActivity();
  220. $activity->store_id = get_store_id();
  221. }
  222. // else {
  223. // SeckillActivityGoods::updateAll(['is_delete' => 1], ['activity_id' => $activity->id]);
  224. // }
  225. $activity->name = $this->name;
  226. $activity->start_time = strtotime($this->start_time);
  227. $activity->end_time = strtotime($this->end_time);
  228. $activity->self_limit_num = (int)$this->self_limit_num;
  229. $activity->order_limit_num = (int)$this->order_limit_num;
  230. $activity->is_use_coupon = $this->is_use_coupon ?? 1;
  231. $activity->is_platform = (int)$this->is_platform;
  232. if (!$activity->save()) {
  233. throw new \Exception(json_encode($activity->errors));
  234. }
  235. $goods = $this->goods;
  236. SeckillActivityGoods::updateAll(['is_delete' => 1], ['activity_id' => $activity->id, 'is_delete' => 0]);
  237. $open = false;
  238. foreach ($goods as $item) {
  239. $Goods = Goods::find()->where(['id' => $item['id'], 'store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])->select('id, use_attr, name, attr')->one();
  240. if ($Goods) {
  241. $old_goods = SeckillActivityGoods::findOne(['activity_id' => $activity->id, 'goods_id' => $item['id']]);
  242. $sale_num = 0;
  243. if ($old_goods) {
  244. $sale_num = $old_goods->sale_num;
  245. } else {
  246. $open = true;
  247. }
  248. //验证是否存在其他时间段(未开始 / 进行中)相同的活动产品
  249. $is_exist_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $item['id'], 'sag.store_id' => get_store_id()])
  250. ->leftJoin(['sg' => SeckillActivity::tableName()], 'sag.activity_id = sg.id')
  251. ->andWhere(['<>', 'sg.id', $activity->id])
  252. ->andWhere(['OR',
  253. ['AND',
  254. ['<=' , 'start_time', strtotime($this->start_time)],
  255. ['>=' , 'end_time',strtotime($this->end_time)]
  256. ],
  257. ['AND',
  258. ['<=' , 'start_time', strtotime($this->start_time)],
  259. ['<=' , 'end_time',strtotime($this->end_time)],
  260. ['>=' , 'end_time',strtotime($this->start_time)]
  261. ],
  262. ['AND',
  263. ['>=' , 'start_time', strtotime($this->start_time)],
  264. ['<=' , 'end_time',strtotime($this->end_time)]
  265. ],
  266. ['AND',
  267. ['>=' , 'start_time', strtotime($this->start_time)],
  268. ['>=' , 'end_time',strtotime($this->end_time)],
  269. ['<=' , 'start_time',strtotime($this->end_time)]
  270. ],
  271. ])->andWhere(['sg.is_delete' => 0, 'sag.is_delete' => 0])->select('sag.id, sag.activity_id')->one();
  272. if ($is_exist_goods) {
  273. throw new \Exception("部分商品已经在其他未开始/进行中的活动中");
  274. }
  275. // //验证相同时间段是否存在其他活动产品(新人专享)
  276. // $is_exist_goods = ActivityNewUserGoods::find()->alias('pag')->where(['pag.goods_id' => $item['id'], 'pag.store_id' => get_store_id()])
  277. // ->leftJoin(['pg' => ActivityNewUser::tableName()], 'pag.activity_id = pg.id')
  278. // ->andWhere(['OR',
  279. // ['AND',
  280. // ['<=' , 'pg.start_time', strtotime($this->start_time)],
  281. // ['>=' , 'pg.end_time',strtotime($this->end_time)]
  282. // ],
  283. // ['AND',
  284. // ['<=' , 'pg.start_time', strtotime($this->start_time)],
  285. // ['<=' , 'pg.end_time',strtotime($this->end_time)],
  286. // ['>=' , 'pg.end_time',strtotime($this->start_time)]
  287. // ],
  288. // ['AND',
  289. // ['>=' , 'pg.start_time', strtotime($this->start_time)],
  290. // ['<=' , 'pg.end_time',strtotime($this->end_time)]
  291. // ],
  292. // ['AND',
  293. // ['>=' , 'pg.start_time', strtotime($this->start_time)],
  294. // ['>=' , 'pg.end_time',strtotime($this->end_time)],
  295. // ['<=' , 'pg.start_time',strtotime($this->end_time)]
  296. // ],
  297. // ])->andWhere(['pg.is_delete' => 0, 'pag.is_delete' => 0])->select('pag.id, pag.activity_id')->one();
  298. // if ($is_exist_goods) {
  299. // throw new \Exception("部分商品已经在其他未开始/进行中的新人专享活动中");
  300. // }
  301. $activity_goods = new SeckillActivityGoods();
  302. $activity_goods->goods_id = $item['id'];
  303. $activity_goods->virtual_sales = (int)$item['virtual_sales'];
  304. $activity_goods->virtual_num = (int)$item['virtual_num'];
  305. $activity_goods->store_id = get_store_id();
  306. $activity_goods->use_attr = (int)$Goods->use_attr;
  307. $activity_goods->sale_num = $sale_num;
  308. if ((int)$Goods->use_attr === 1 && empty($item['attr'])) {
  309. throw new \Exception("保存失败, 商品" . $Goods->name . '没有规格参数');
  310. }
  311. if (is_string($item['attr'])) {
  312. $item['attr'] = json_decode($item['attr'], true);
  313. }
  314. if ((int)$Goods->use_attr === 1 && $item['attr']) {
  315. $num = 0;
  316. foreach ($item['attr'] as $value) {
  317. if (!isset($value['attr_list']) || !isset($value['seckill_num']) || !isset($value['seckill_price'])) {
  318. throw new \Exception("保存失败, 商品" . $Goods->name . '规格参数错误');
  319. }
  320. $num += $value['seckill_num'];
  321. }
  322. $activity_goods->seckill_num = (int)$num;
  323. $activity_goods->attr = json_encode($item['attr']);
  324. } else {
  325. $attr = json_decode($Goods->attr, true);
  326. $attr[0]['seckill_num'] = $activity_goods->seckill_num = (int)$item['seckill_num'];
  327. $attr[0]['seckill_price'] = $activity_goods->seckill_price = (float)$item['seckill_price'];
  328. $activity_goods->attr = json_encode($attr);
  329. }
  330. $activity_goods->activity_id = $activity->id;
  331. if (!$activity_goods->save()) {
  332. throw new \Exception(json_encode($activity_goods->errors));
  333. }
  334. if ($old_goods && ($old_goods->use_attr != $activity_goods->use_attr ||
  335. $old_goods->attr != $activity_goods->attr ||
  336. $old_goods->virtual_sales != $activity_goods->virtual_sales ||
  337. $old_goods->virtual_num != $activity_goods->virtual_num ||
  338. $old_goods->sale_num != $activity_goods->sale_num ||
  339. $old_goods->seckill_num != $activity_goods->seckill_num ||
  340. $old_goods->seckill_price != $activity_goods->seckill_price
  341. )
  342. ) {
  343. $open = true;
  344. }
  345. } else {
  346. throw new \Exception("商品未找到或已下架");
  347. }
  348. }
  349. if ($open) {
  350. $activity->is_platform_audit = 0;
  351. if (!$activity->save()) {
  352. throw new \Exception(json_encode($activity->errors, JSON_UNESCAPED_UNICODE));
  353. }
  354. }
  355. //觉得没啥用,直接保存后删除
  356. SeckillActivityGoods::deleteAll(['activity_id' => $activity->id, 'is_delete' => 1]);
  357. $t->commit();
  358. return [
  359. 'code' => 0,
  360. 'msg' => '操作成功!'
  361. ];
  362. } catch (\Exception $e) {
  363. $t->rollBack();
  364. return [
  365. 'code' => 1,
  366. 'msg' => $e->getMessage()
  367. ];
  368. }
  369. }
  370. public function setStatus ()
  371. {
  372. try {
  373. if ($this->ids) {
  374. $ids = explode(',', $this->ids);
  375. if (in_array($this->status, [0, 1])) {
  376. SeckillActivity::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  377. }
  378. if ((int)$this->status === 2) {
  379. SeckillActivity::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  380. SeckillActivityGoods::updateAll(['is_delete' => 1], ['activity_id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
  381. }
  382. if (count($ids) === 1) {
  383. (new DiyCommon)->JobBehaviors(get_store_id(), StoreSyncExtLog::TYPE_SECKILL, $ids);
  384. }
  385. } else {
  386. $rules = SeckillActivity::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
  387. if (empty($rules)) {
  388. throw new \Exception("规则不存在");
  389. }
  390. if (in_array($this->status, [0, 1])) {
  391. $rules->status = $this->status;
  392. }
  393. if ((int)$this->status === 2) {
  394. $rules->is_delete = 1;
  395. }
  396. if (!$rules->save()) {
  397. throw new \Exception(json_encode($rules->errors));
  398. }
  399. }
  400. return [
  401. 'code' => 0,
  402. 'msg' => '操作成功!'
  403. ];
  404. } catch (\Exception $e) {
  405. return [
  406. 'code' => 1,
  407. 'msg' => $e->getMessage()
  408. ];
  409. }
  410. }
  411. /**
  412. * 审核列表
  413. */
  414. public function auditList() {
  415. try {
  416. $query = SeckillActivity::find()->alias('a')->where(['a.is_delete' => 0, 'a.is_platform' => 1, 's.is_delete' => 0]);
  417. $query->leftJoin(['s' => Store::tableName()], 's.id = a.store_id');
  418. if (intval($this->status) === 1) {//未开始
  419. $query->andWhere(['AND', ['>' , 'a.start_time', time()], ['a.status' => 1]]);
  420. }
  421. if (intval($this->status) === 2) {//进行中
  422. $query->andWhere(['AND', ['<' , 'a.start_time', time()], ['>' , 'a.end_time', time()], ['a.status' => 1]]);
  423. }
  424. if (intval($this->status) === 3) { //已结束
  425. $query->andWhere(['AND', ['<' , 'a.end_time', time()], ['a.status' => 1]]);
  426. }
  427. if (intval($this->status) === 4) { //已终止
  428. $query->andWhere(['a.status' => 0]);
  429. }
  430. if (!empty($this->name)) { //名称
  431. $query->andWhere(['LIKE' , 'a.name', $this->name]);
  432. }
  433. if (!empty($this->store_name)) { //名称
  434. $query->andWhere(['LIKE' , 's.name', $this->store_name]);
  435. }
  436. if ($this->is_platform_audit > -1 && $this->is_platform_audit !== null) {
  437. $query->andWhere(['a.is_platform_audit' => $this->is_platform_audit]);
  438. }
  439. //只有平台运营的商城或者单店铺的无独立小程序才展示商盟的营销活动
  440. if (\Yii::$app->prod_is_dandianpu()) {
  441. $store_id = Option::find()->where(['group' => 'store', 'name' => 'self_mini'])->select('store_id')->column();
  442. $query->andWhere(['NOT IN', 's.id', $store_id]);
  443. } else {
  444. $query->andWhere(['s.business_model' => 2]);
  445. }
  446. if (!empty($this->start_time) && !empty($this->end_time)) { //时间筛选
  447. $query->andWhere(['OR',
  448. ['AND',
  449. ['<=' , 'a.start_time', strtotime($this->start_time)],
  450. ['>=' , 'a.end_time',strtotime($this->end_time)]
  451. ],
  452. ['AND',
  453. ['<=' , 'a.start_time', strtotime($this->start_time)],
  454. ['<=' , 'a.end_time',strtotime($this->end_time)],
  455. ['>=' , 'a.end_time',strtotime($this->start_time)]
  456. ],
  457. ['AND',
  458. ['>=' , 'a.start_time', strtotime($this->start_time)],
  459. ['<=' , 'a.end_time',strtotime($this->end_time)]
  460. ],
  461. ['AND',
  462. ['>=' , 'a.start_time', strtotime($this->start_time)],
  463. ['>=' , 'a.end_time',strtotime($this->end_time)],
  464. ['<=' , 'a.start_time',strtotime($this->end_time)]
  465. ],
  466. ]);
  467. }
  468. $query->select('a.id, a.start_time, a.end_time, a.created_at, a.updated_at, a.name, a.is_platform_audit, s.name store_name, s.logo, s.id store_id, a.status');
  469. $pagination = pagination_make($query);
  470. foreach ($pagination['list'] as &$item) {
  471. $item['logo'] = $item['logo'] ?: Option::get(OptionSetting::STORE_LOGO, $item['store_id'], 'store', '')['value'];
  472. //获取活动状态
  473. if (intval($item['status']) === 1) {
  474. if ($item['start_time'] > time()) {
  475. $item['status'] = 1;
  476. }
  477. if ($item['start_time'] < time() && $item['end_time'] > time()) {
  478. $item['status'] = 2;
  479. }
  480. if ($item['end_time'] < time()) {
  481. $item['status'] = 3;
  482. }
  483. } else {
  484. $item['status'] = 4;
  485. }
  486. $item['is_platform_audit'] = (int)$item['is_platform_audit'];
  487. //格式化时间
  488. $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']);
  489. $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']);
  490. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  491. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  492. $item['order_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('order_id')->count();
  493. $item['self_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('user_id')->count();;
  494. $item['order_price'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->sum('sale_price') ?: '0.00';
  495. }
  496. return [
  497. 'code' => 0,
  498. 'msg' => 'success',
  499. 'data' => [
  500. 'data' => $pagination['list'],
  501. 'pageNo' => $pagination['pageNo'],
  502. 'totalCount' => $pagination['totalCount'],
  503. ]
  504. ];
  505. } catch (\Exception $e) {
  506. return [
  507. 'code' => 1,
  508. 'msg' => $e->getMessage()
  509. ];
  510. }
  511. }
  512. /**
  513. * 处理审核
  514. */
  515. public function auditHandle() {
  516. try {
  517. $id = $this->id;
  518. $status = $this->status;
  519. $cut_activity = SeckillActivity::findOne(['id' => $id, 'is_platform' => 1, 'is_delete' => 0]);
  520. if (!$cut_activity) {
  521. throw new \Exception('活动不存在');
  522. }
  523. if (in_array($status, [1, 2])) {
  524. if (intval($cut_activity->is_platform_audit) !== 0) {
  525. throw new \Exception('活动已经审核');
  526. }
  527. $cut_activity->is_platform_audit = $status;
  528. }
  529. if (intval($status) === 3) {
  530. $cut_activity->is_delete = 1;
  531. }
  532. if (in_array($status, [4, 5])) {
  533. $cut_activity->status = intval($status - 4);
  534. }
  535. if (!$cut_activity->save()) {
  536. throw new \Exception(json_encode($cut_activity->errors, JSON_UNESCAPED_UNICODE));
  537. }
  538. return [
  539. 'code' => 0,
  540. 'msg' => '操作成功'
  541. ];
  542. } catch (\Exception $e) {
  543. return [
  544. 'code' => 1,
  545. 'msg' => $e->getMessage()
  546. ];
  547. }
  548. }
  549. }