PtActivityForm.php 29 KB

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