TeamLogic.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\marketing;
  20. use app\common\enum\ActivityEnum;
  21. use app\common\enum\OrderEnum;
  22. use app\common\enum\TeamEnum;
  23. use app\common\logic\RefundLogic;
  24. use app\common\model\AfterSale;
  25. use app\common\model\AfterSaleGoods;
  26. use app\common\model\Goods;
  27. use app\common\model\GoodsActivity;
  28. use app\common\model\GoodsItem;
  29. use app\common\model\Order;
  30. use app\common\model\OrderGoods;
  31. use app\common\model\TeamActivity;
  32. use app\common\model\TeamFound;
  33. use app\common\model\TeamGoods;
  34. use app\common\model\TeamGoodsItem;
  35. use app\common\model\TeamJoin;
  36. use \Exception;
  37. use think\facade\Db;
  38. class TeamLogic
  39. {
  40. /**
  41. * @notes 拼团概况
  42. * @author 张无忌
  43. * @date 2021/8/2 19:13
  44. */
  45. public static function survey()
  46. {
  47. $detail['base'] = [
  48. //累计活动场次
  49. 'total_activity_num' => (new TeamActivity())->count(),
  50. //累计浏览量
  51. 'total_browse_volume' => (new TeamGoods())->alias('TG')->join('team_activity TA', 'TA.id = TG.team_id')->sum('TG.browse_volume'),
  52. //累计销售金额
  53. 'total_sales_amount' => (new TeamGoodsItem())->alias('TGI')->join('team_activity TA', 'TA.id = TGI.team_id')->sum('TGI.sales_amount'),
  54. //累计销售量
  55. 'total_sales_volume' => (new TeamGoodsItem())->alias('TGI')->join('team_activity TA', 'TA.id = TGI.team_id')->sum('TGI.sales_volume'),
  56. //累计成交订单
  57. 'total_closing_order' => (new TeamGoodsItem())->alias('TGI')->join('team_activity TA', 'TA.id = TGI.team_id')->sum('TGI.closing_order'),
  58. //累计成团数
  59. 'total_success_team' => (new TeamFound())->alias('TF')->join('team_activity TA', 'TA.id = TF.team_id')->where(['TF.status' => TeamEnum::TEAM_FOUND_SUCCESS])->count()
  60. ];
  61. // $detail['sort_browse_volume'] = (new TeamGoods())->alias('TG')
  62. // ->field(['TA.id,TA.name,TG.browse_volume'])
  63. // ->join('team_activity TA', 'TA.id = TG.team_id')
  64. // ->group('TG.team_id')
  65. // ->order('TG.browse_volume desc, TG.id desc')
  66. // ->limit(10)
  67. // ->select()->toArray();
  68. $detail['sort_browse_volume'] = (new TeamGoods())->alias('TG')
  69. ->field(['TA.id,TA.name,sum(TG.browse_volume) as sum_browse_volume'])
  70. ->join('team_activity TA', 'TA.id = TG.team_id')
  71. ->group('TG.team_id')
  72. ->order('sum_browse_volume desc, TG.id desc')
  73. ->limit(10)
  74. ->select()->toArray();
  75. // $detail['sort_sales_volume'] = (new TeamGoodsItem())->alias('TGI')
  76. // ->field(['TA.id,TA.name,TGI.sales_volume'])
  77. // ->join('team_activity TA', 'TA.id = TGI.team_id')
  78. // ->group('TGI.team_id')
  79. // ->order('TGI.sales_volume desc, TGI.id desc')
  80. // ->limit(10)
  81. // ->select()->toArray();
  82. $detail['sort_sales_amount'] = (new TeamGoodsItem())->alias('TGI')
  83. ->field(['TA.id,TA.name,sum(TGI.sales_amount) as sum_sales_amount'])
  84. ->join('team_activity TA', 'TA.id = TGI.team_id')
  85. ->group('TGI.team_id')
  86. ->order('sum_sales_amount desc, TGI.id desc')
  87. ->limit(10)
  88. ->select()->toArray();
  89. return $detail;
  90. }
  91. /**
  92. * @notes 拼团详细信息
  93. * @param $params
  94. * @return array
  95. * @author 张无忌
  96. * @date 2021/8/2 19:13
  97. * @throws @\think\db\exception\DbException
  98. * @throws @\think\db\exception\ModelNotFoundException
  99. * @throws @\think\db\exception\DataNotFoundException
  100. */
  101. public static function detail($params)
  102. {
  103. $detail = (new TeamActivity())
  104. ->withoutField(['create_time', 'update_time', 'delete_time'])
  105. ->with(['goods'])
  106. ->findOrEmpty($params['id'])
  107. ->toArray();
  108. $goodsItem = (new TeamGoodsItem())
  109. ->alias('TGI')
  110. ->field('TGI.*, GI.stock')
  111. ->join('goods_item GI', 'GI.id = TGI.item_id')
  112. ->where(['TGI.team_id' => $params['id']])
  113. ->select()->toArray();
  114. $detail['start_time'] = date('Y-m-d H:i:s', $detail['start_time']);
  115. $detail['end_time'] = date('Y-m-d H:i:s', $detail['end_time']);
  116. foreach ($detail['goods'] as &$goods) {
  117. $g = (new Goods())->field('total_stock,spec_type,min_price')
  118. ->where(['id'=>$goods['goods_id']])
  119. ->findOrEmpty();
  120. $snap = $goods['goods_snap'];
  121. $goods['id'] = $goods['goods_id'];
  122. $goods['name'] = $snap['name'];
  123. $goods['image'] = $snap['image'];
  124. $goods['total_stock'] = $g['total_stock'];
  125. $goods['spec_type'] = $g['spec_type'];
  126. $goods['sell_price'] = $g['min_price'];
  127. $goods['item'] = [];
  128. foreach ($goodsItem as $item) {
  129. if ($item['goods_id'] == $goods['goods_id']) {
  130. $goods['item'][] = [
  131. 'id' => $item['item_id'],
  132. 'goods_id' => $item['goods_id'],
  133. 'item_id' => $item['item_id'],
  134. 'spec_value_str' => $item['spec_value_str'],
  135. 'sell_price' => $item['sell_price'],
  136. 'team_price' => $item['team_price'],
  137. 'stock' => $item['stock'],
  138. ];
  139. unset($item);
  140. }
  141. }
  142. unset($goods['team_id']);
  143. unset($goods['goods_snap']);
  144. unset($goods['goods_id']);
  145. }
  146. return $detail;
  147. }
  148. /**
  149. * @notes 拼团数据信息
  150. * @param $params
  151. * @return array
  152. * @author 张无忌
  153. * @date 2021/8/2 19:14
  154. * @throws @\think\db\exception\DbException
  155. * @throws @\think\db\exception\ModelNotFoundException
  156. * @throws @\think\db\exception\DataNotFoundException
  157. */
  158. public static function info($params)
  159. {
  160. $detail['base'] = (new TeamActivity())
  161. ->field('id,sn,name,start_time,end_time,status,create_time')
  162. ->with(['goods'])
  163. ->findOrEmpty($params['id'])
  164. ->toArray();
  165. $detail['base']['start_time'] = date('Y-m-d H:i:s', $detail['base']['start_time']);
  166. $detail['base']['end_time'] = date('Y-m-d H:i:s', $detail['base']['end_time']);
  167. $detail['base']['activity_time'] = $detail['base']['start_time'] . ' 至 ' . $detail['base']['end_time'];
  168. $detail['base']['status_text'] = TeamEnum::getActivityStatusDesc($detail['base']['status']);
  169. $detail['data'] = [
  170. 'browse_volume' => (new TeamGoods)->where(['team_id' => $params['id']])->sum('browse_volume'),
  171. 'sales_amount' => (new TeamGoodsItem())->where(['team_id' => $params['id']])->sum('sales_amount'),
  172. 'sales_volume' => (new TeamGoodsItem())->where(['team_id' => $params['id']])->sum('sales_volume'),
  173. 'closing_order' => (new TeamGoodsItem())->where(['team_id' => $params['id']])->sum('closing_order'),
  174. 'team_success' => (new TeamFound())->where(['team_id' => $params['id'], 'status' => TeamEnum::TEAM_FOUND_SUCCESS])->count()
  175. ];
  176. $goodsItem = (new TeamGoodsItem())->where(['team_id' => $params['id']])->select()->toArray();
  177. foreach ($detail['base']['goods'] as &$goods) {
  178. $snap = $goods['goods_snap'];
  179. $goods['name'] = $snap['name'];
  180. $goods['image'] = $snap['image'];
  181. $goods['items'] = [];
  182. foreach ($goodsItem as $item) {
  183. if ($item['goods_id'] == $goods['goods_id']) {
  184. $goods['items'][] = [
  185. 'item_id' => $item['item_id'],
  186. 'spec_value_str' => $item['spec_value_str'],
  187. 'sell_price' => $item['sell_price'],
  188. 'team_price' => $item['team_price'],
  189. 'sales_amount' => $item['sales_amount'],
  190. 'browse_volume' => $goods['browse_volume'],
  191. 'sales_volume' => $item['sales_volume'],
  192. 'closing_order' => $item['closing_order']
  193. ];
  194. unset($item);
  195. }
  196. }
  197. unset($goods['team_id']);
  198. unset($goods['goods_snap']);
  199. }
  200. return $detail;
  201. }
  202. /**
  203. * @notes 新增拼团活动
  204. * @param $params
  205. * @return string
  206. * @author 张无忌
  207. * @date 2021/8/2 14:40
  208. */
  209. public static function add($params)
  210. {
  211. Db::startTrans();
  212. try {
  213. // 验证商品是否正在参与活动
  214. $result = self::checkGoods($params);
  215. if ($result !== false) {
  216. throw new Exception($result . ':正在参与活动中,不可重复添加');
  217. }
  218. // 1、创建拼团活动信息
  219. $team = TeamActivity::create([
  220. 'sn' => create_code(4),
  221. 'name' => $params['name'],
  222. 'people_num' => $params['people_num'],
  223. 'explain' => $params['explain'] ?? '',
  224. // 'min_buy' => $params['min_buy'],
  225. 'max_buy' => $params['max_buy'],
  226. //'is_coupon' => $params['is_coupon'],
  227. //'is_distribution' => $params['is_distribution'],
  228. 'is_automatic' => $params['is_automatic'],
  229. 'effective_time' => $params['effective_time'],
  230. 'start_time' => strtotime($params['start_time']),
  231. 'end_time' => strtotime($params['end_time']),
  232. ]);
  233. # 2、创建活动商品信息
  234. $goodsItemData = [];
  235. $goodsActivityData = [];
  236. foreach ($params['goods'] as $item) {
  237. $goods = Goods::field([
  238. 'id,name,code,image,video,poster,express_type',
  239. 'express_money,express_template_id,spec_type',
  240. 'min_price,max_price,content'
  241. ])->findOrEmpty(intval($item['goods_id']))->toArray();
  242. if (!$goods) {
  243. throw new Exception('商品不存在');
  244. }
  245. $teamGoods = TeamGoods::create([
  246. 'team_id' => $team['id'],
  247. 'goods_id' => $goods['id'],
  248. 'min_team_price' => min(array_column($item['items'], 'team_price')),
  249. 'max_team_price' => max(array_column($item['items'], 'team_price')),
  250. 'goods_snap' => json_encode($goods, JSON_UNESCAPED_UNICODE),
  251. 'virtual_sales_num' => $item['virtual_sales_num'] ?? 0,
  252. 'virtual_click_num' => $item['virtual_click_num'] ?? 0,
  253. ]);
  254. foreach ($item['items'] as $val) {
  255. $goodsItem = GoodsItem::where(['goods_id' => $goods['id'], 'id' => intval($val['item_id'])])->findOrEmpty()->toArray();
  256. if (!$goodsItem) {
  257. throw new Exception('商品规格不存在');
  258. }
  259. $goodsItemData[] = [
  260. 'team_gid' => $teamGoods['id'],
  261. 'team_id' => $team['id'],
  262. 'goods_id' => $goods['id'],
  263. 'item_id' => $goodsItem['id'],
  264. 'spec_value_str' => $goodsItem['spec_value_str'],
  265. 'sell_price' => $goodsItem['sell_price'],
  266. 'team_price' => $val['team_price'],
  267. 'item_snap' => json_encode($goodsItem, JSON_UNESCAPED_UNICODE)
  268. ];
  269. $goodsActivityData[] = [
  270. 'activity_type' => ActivityEnum::TEAM,
  271. 'activity_id' => $team['id'],
  272. 'goods_id' => $goods['id'],
  273. 'item_id' => $goodsItem['id'],
  274. ];
  275. }
  276. }
  277. if (!empty($goodsItemData)) {
  278. (new TeamGoodsItem())->saveAll($goodsItemData);
  279. }
  280. // 商品参与活动的信息
  281. if (!empty($goodsActivityData)) {
  282. (new GoodsActivity())->saveAll($goodsActivityData);
  283. }
  284. Db::commit();
  285. return true;
  286. } catch (Exception $e) {
  287. Db::rollback();
  288. return $e->getMessage();
  289. }
  290. }
  291. /**
  292. * @notes 编辑拼团活动
  293. * @param $params
  294. * @return bool|string
  295. * @author 张无忌
  296. * @date 2021/8/2 16:12
  297. */
  298. public static function edit($params)
  299. {
  300. Db::startTrans();
  301. try {
  302. $teamActivity = (new TeamActivity())->findOrEmpty($params['id'])->toArray();
  303. if (!$teamActivity) {
  304. throw new Exception('拼团活动不存在,请刷新列表');
  305. }
  306. // 活动已开启: 只允许修改部分内容
  307. if ($teamActivity['status'] != TeamEnum::TEAM_STATUS_NOT) {
  308. TeamActivity::update([
  309. 'sn' => '',
  310. 'name' => $params['name'],
  311. 'explain' => $params['explain'] ?? '',
  312. 'end_time' => strtotime($params['end_time']),
  313. ], ['id' => $params['id']]);
  314. }
  315. // 获取未开启: 可以修改任意内容
  316. if ($teamActivity['status'] == TeamEnum::TEAM_STATUS_NOT) {
  317. // 删除旧活动商品
  318. (new TeamGoods())->where(['team_id' => $params['id']])->delete();
  319. (new TeamGoodsItem())->where(['team_id' => $params['id']])->delete();
  320. // 删除旧的商品参与活动的信息
  321. (new GoodsActivity())->where([
  322. 'activity_type' => ActivityEnum::TEAM,
  323. 'activity_id' => $params['id']
  324. ])
  325. ->useSoftDelete('delete_time',time())
  326. ->delete();
  327. // 验证商品是否正在参与活动
  328. $result = self::checkGoods($params);
  329. if ($result !== false) {
  330. throw new Exception($result . ':正在参与活动中,不可重复添加');
  331. }
  332. // 创建拼团活动
  333. TeamActivity::update([
  334. 'name' => $params['name'],
  335. 'people_num' => $params['people_num'],
  336. 'explain' => $params['explain'] ?? '',
  337. // 'min_buy' => $params['min_buy'],
  338. 'max_buy' => $params['max_buy'],
  339. //'is_coupon' => $params['is_coupon'],
  340. //'is_distribution' => $params['is_distribution'],
  341. 'is_automatic' => $params['is_automatic'],
  342. 'effective_time' => $params['effective_time'],
  343. 'start_time' => strtotime($params['start_time']),
  344. 'end_time' => strtotime($params['end_time']),
  345. ], ['id' => $params['id']]);
  346. // 置换新活动商品
  347. $goodsItemData = [];
  348. $goodsActivityData = [];
  349. foreach ($params['goods'] as $item) {
  350. $goods = Goods::field([
  351. 'id,name,code,image,video,poster,express_type',
  352. 'express_money,express_template_id,spec_type',
  353. 'min_price,max_price,content'
  354. ])->findOrEmpty(intval($item['goods_id']))->toArray();
  355. if (!$goods) {
  356. throw new Exception('商品不存在');
  357. }
  358. $teamGoods = TeamGoods::create([
  359. 'team_id' => $params['id'],
  360. 'goods_id' => $goods['id'],
  361. 'min_team_price' => min(array_column($item['items'], 'team_price')),
  362. 'max_team_price' => max(array_column($item['items'], 'team_price')),
  363. 'goods_snap' => json_encode($goods, JSON_UNESCAPED_UNICODE),
  364. 'virtual_sales_num' => $item['virtual_sales_num'] ?? 0,
  365. 'virtual_click_num' => $item['virtual_click_num'] ?? 0,
  366. ]);
  367. foreach ($item['items'] as $val) {
  368. $goodsItem = GoodsItem::where(['goods_id' => $goods['id'], 'id' => intval($val['item_id'])])->findOrEmpty()->toArray();
  369. if (!$goodsItem) {
  370. throw new Exception('商品规格不存在');
  371. }
  372. $goodsItemData[] = [
  373. 'team_gid' => $teamGoods['id'],
  374. 'team_id' => $params['id'],
  375. 'goods_id' => $goods['id'],
  376. 'item_id' => $goodsItem['id'],
  377. 'spec_value_str' => $goodsItem['spec_value_str'],
  378. 'sell_price' => $goodsItem['sell_price'],
  379. 'team_price' => $val['team_price'],
  380. 'item_snap' => json_encode($goodsItem, JSON_UNESCAPED_UNICODE)
  381. ];
  382. $goodsActivityData[] = [
  383. 'activity_type' => ActivityEnum::TEAM,
  384. 'activity_id' => $params['id'],
  385. 'goods_id' => $goods['id'],
  386. 'item_id' => $goodsItem['id'],
  387. ];
  388. }
  389. }
  390. if (!empty($goodsItemData)) {
  391. (new TeamGoodsItem())->saveAll($goodsItemData);
  392. }
  393. if (!empty($goodsActivityData)) {
  394. (new GoodsActivity())->saveAll($goodsActivityData);
  395. }
  396. }
  397. Db::commit();
  398. return true;
  399. } catch (Exception $e) {
  400. Db::rollback();
  401. return $e->getMessage();
  402. }
  403. }
  404. /**
  405. * @notes 删除拼团活动
  406. * @param $params
  407. * @return bool
  408. * @author 张无忌
  409. * @date 2021/8/2 17:00
  410. */
  411. public static function delete($params)
  412. {
  413. Db::startTrans();
  414. try {
  415. //1、拼团活动状态设置成已结束
  416. TeamActivity::update([
  417. 'status' => TeamEnum::TEAM_STATUS_END,
  418. 'update_time' => time()
  419. ], ['id'=>$params['id']]);
  420. //2、删除拼团活动
  421. TeamActivity::destroy($params['id']);
  422. //3、查找所有待成团
  423. $teamFoundIds = (new TeamFound())
  424. ->where([
  425. 'team_id' => $params['id'],
  426. 'status' => TeamEnum::TEAM_FOUND_CONDUCT
  427. ])->column('id');
  428. //4、关闭未支付的订单
  429. (new Order())
  430. ->where([['team_found_id', 'in', $teamFoundIds], ['pay_status', '=', 0]])
  431. ->update(['order_status' => OrderEnum::STATUS_CLOSE]);
  432. //5、退款已支付的订单
  433. $orders = (new Order())
  434. ->where([
  435. ['team_found_id', 'in', $teamFoundIds],
  436. ['pay_status', '=', 1],
  437. ['order_status', '=', 1]
  438. ])
  439. ->select()->toArray();
  440. foreach ($orders as $order) {
  441. $orderGoods = (new OrderGoods())->where(['order_id'=>$order['id']])->findOrEmpty()->toArray();
  442. $afterSale = AfterSale::create([
  443. 'sn' => generate_sn((new AfterSale()), 'sn'),
  444. 'user_id' => $order['user_id'],
  445. 'order_id' => $order['id'],
  446. 'order_goods_id' => $orderGoods['id'],
  447. 'refund_reason' => '拼团失败',
  448. 'refund_remark' => '系统发起整单退款',
  449. 'refund_type' => 1,
  450. 'refund_method' => 1,
  451. 'refund_total_amount' => $order['order_amount'],
  452. 'refund_way' => 1,
  453. 'refund_status' => 3,
  454. 'status' => 1,
  455. 'sub_status' => 12,
  456. ]);
  457. AfterSaleGoods::create([
  458. 'after_sale_id' => $afterSale->id,
  459. 'order_goods_id' => $orderGoods['id'],
  460. 'goods_id' => $orderGoods['goods_id'],
  461. 'item_id' => $orderGoods['item_id'],
  462. 'goods_num' => $orderGoods['goods_num'],
  463. 'goods_price' => $orderGoods['goods_price'],
  464. 'refund_amount' => $order['order_amount'],
  465. ]);
  466. RefundLogic::refund(1, $order, $afterSale->id, $order['order_amount']);
  467. Order::update([
  468. 'id' => $order['id'],
  469. 'order_status' => OrderEnum::STATUS_CLOSE,
  470. 'update_time' => time()
  471. ]);
  472. }
  473. // 删除商品参与活动的信息
  474. $goodsActivityIds = GoodsActivity::where([
  475. 'activity_type' => ActivityEnum::TEAM,
  476. 'activity_id' => $params['id'],
  477. ])->column('id');
  478. if (count($goodsActivityIds)) {
  479. GoodsActivity::destroy($goodsActivityIds);
  480. }
  481. Db::commit();
  482. return true;
  483. } catch (Exception $e) {
  484. Db::rollback();
  485. return $e->getMessage();
  486. }
  487. }
  488. /**
  489. * @notes 启动活动
  490. * @param $params
  491. * @return string
  492. * @throws Exception
  493. * @author 张无忌
  494. * @date 2021/8/2 20:41
  495. */
  496. public static function open($params)
  497. {
  498. $id = $params['id'];
  499. $teamActivity = (new TeamActivity())->findOrEmpty($id)->toArray();
  500. if (!$teamActivity) {
  501. throw new Exception('拼团活动不存在,请刷新列表');
  502. }
  503. if ($teamActivity['status'] == TeamEnum::TEAM_STATUS_END) {
  504. throw new Exception('结束的拼团不允许再次启动');
  505. }
  506. if ($teamActivity['status'] == TeamEnum::TEAM_STATUS_CONDUCT) {
  507. throw new Exception('此拼团活动已是在进行中,无需重复操作');
  508. }
  509. TeamActivity::where(['id' => $id])->update([
  510. 'status' => TeamEnum::TEAM_STATUS_CONDUCT,
  511. 'update_time' => time()
  512. ]);
  513. return true;
  514. }
  515. /**
  516. * @notes 停止活动
  517. * @author 张无忌
  518. * @date 2021/8/2 20:41
  519. */
  520. public static function stop($params)
  521. {
  522. Db::startTrans();
  523. try {
  524. //1、拼团活动状态设置成已结束
  525. TeamActivity::update([
  526. 'status' => TeamEnum::TEAM_STATUS_END,
  527. 'update_time' => time()
  528. ], ['id'=>$params['id']]);
  529. //2、查找所有待成团
  530. $teamFounds = (new TeamFound())
  531. ->where([
  532. 'team_id' => $params['id'],
  533. 'status' => TeamEnum::TEAM_FOUND_CONDUCT
  534. ])->select()->toArray();
  535. foreach ($teamFounds as $item) {
  536. \app\shopapi\logic\TeamLogic::signFailTeam($item['order_id']);
  537. }
  538. // 3、删除商品参与活动的信息
  539. $goodsActivityIds = GoodsActivity::where([
  540. 'activity_type' => ActivityEnum::TEAM,
  541. 'activity_id' => $params['id'],
  542. ])->column('id');
  543. if (count($goodsActivityIds)) {
  544. GoodsActivity::destroy($goodsActivityIds);
  545. }
  546. Db::commit();
  547. return true;
  548. } catch (Exception $e) {
  549. Db::rollback();
  550. return $e->getMessage();
  551. }
  552. }
  553. /**
  554. * @notes 验证新增的商品是否已参与活动
  555. * @param $params
  556. * @return bool
  557. * @author 张无忌
  558. * @date 2021/8/4 19:46
  559. */
  560. private static function checkGoods($params)
  561. {
  562. // 获取正在进行的或者未开始的活动
  563. $teamIds = (new TeamActivity())->whereIn('status', [0, 1])->column('id');
  564. if ($teamIds) {
  565. $goodsId = array_column($params['goods'], 'goods_id');
  566. $teamGoods = (new TeamGoods())
  567. ->whereIn('team_id', $teamIds)
  568. ->whereIn('goods_id', $goodsId)
  569. ->findOrEmpty()->toArray();
  570. if ($teamGoods) {
  571. $goods = (new Goods())->findOrEmpty($teamGoods['goods_id'])->toArray();
  572. return $goods['name'];
  573. }
  574. }
  575. return false;
  576. }
  577. /**
  578. * @notes 结束拼团记录
  579. * @param $params
  580. * @return bool
  581. * @throws @\think\db\exception\DataNotFoundException
  582. * @throws @\think\db\exception\DbException
  583. * @throws @\think\db\exception\ModelNotFoundException
  584. * @author suny
  585. * @date 2021/9/30 11:30 上午
  586. */
  587. public static function cancel($params)
  588. {
  589. $teamFound = (new TeamFound())->where(['id'=>intval($params['id'])])->findOrEmpty()->toArray();
  590. \app\shopapi\logic\TeamLogic::signFailTeam($teamFound['order_id']);
  591. return true;
  592. }
  593. }