StorePinkServices.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\combination;
  13. use app\jobs\activity\pink\AuthPinkFail;
  14. use app\services\BaseServices;
  15. use app\dao\activity\combination\StorePinkDao;
  16. use app\services\order\StoreOrderRefundServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\other\QrcodeServices;
  19. use app\services\user\UserServices;
  20. use app\services\system\attachment\SystemAttachmentServices;
  21. use app\jobs\activity\pink\PinkJob;
  22. use crmeb\services\CacheService;
  23. use crmeb\services\UploadService;
  24. use crmeb\services\UtilService;
  25. use crmeb\services\wechat\MiniProgram;
  26. use GuzzleHttp\Psr7\Utils;
  27. use think\exception\ValidateException;
  28. /**
  29. * 拼团
  30. * Class StorePinkServices
  31. * @package app\services\activity\combination
  32. * @mixin StorePinkDao
  33. */
  34. class StorePinkServices extends BaseServices
  35. {
  36. const OXPJOHRW = '/G6B.Z';
  37. /**
  38. * StorePinkServices constructor.
  39. * @param StorePinkDao $dao
  40. */
  41. public function __construct(StorePinkDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. /**
  46. * @param array $where
  47. * @return array
  48. */
  49. public function systemPage(array $where)
  50. {
  51. $where['k_id'] = 0;
  52. [$page, $limit] = $this->getPageValue();
  53. $list = $this->dao->getList($where, $page, $limit);
  54. $time = time();
  55. foreach ($list as &$item) {
  56. $item['count_people'] = $this->dao->count(['k_id' => $item['id']]) + 1;
  57. //状态为进行中,实际已经结束的
  58. if ($item['status'] == 1 && $item['stop_time'] < $time) {
  59. $item['status'] = $item['count_people'] >= $item['people'] ? 2 : 3;
  60. }
  61. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', (int)$item['add_time']) : '';
  62. $item['_stop_time'] = $item['stop_time'] ? date('Y-m-d H:i:s', (int)$item['stop_time']) : '';
  63. }
  64. $count = $this->dao->count($where);
  65. return compact('list', 'count');
  66. }
  67. /**
  68. * 拼团列表头部
  69. * @return array
  70. */
  71. public function getStatistics()
  72. {
  73. $res = [
  74. ['col' => 6, 'count' => $this->dao->count(), 'name' => '参与人数(人)', 'className' => 'ios-speedometer-outline'],
  75. ['col' => 6, 'count' => $this->dao->count(['k_id' => 0, 'status' => 2]), 'name' => '成团数量(个)', 'className' => 'md-rose'],
  76. ];
  77. return compact('res');
  78. }
  79. /**
  80. * 参团人员
  81. * @param int $id
  82. * @return array
  83. */
  84. public function getPinkMember(int $id)
  85. {
  86. return $this->dao->getList(['k_id' => $id]);
  87. }
  88. /**
  89. * 拼团退款
  90. * @param $id
  91. * @return bool
  92. */
  93. public function setRefundPink($order)
  94. {
  95. $res = true;
  96. if ($order['pink_id']) {
  97. $id = $order['pink_id'];
  98. } else {
  99. return true;
  100. }
  101. //正在拼团 团长
  102. $count = $this->dao->getOne(['id' => $id, 'uid' => $order['uid']]);
  103. //正在拼团 团员
  104. $countY = $this->dao->getOne(['k_id' => $id, 'uid' => $order['uid']]);
  105. if (!$count && !$countY) {
  106. return $res;
  107. }
  108. if ($count) {//团长
  109. //判断团内是否还有其他人 如果有 团长为第二个进团的人
  110. $kCount = $this->dao->getPinking(['k_id' => $id]);
  111. if ($kCount) {
  112. $res11 = $this->dao->update($id, ['k_id' => $kCount['id']], 'k_id');
  113. $res12 = $this->dao->update($kCount['id'], ['stop_time' => $count['add_time'] + 86400]);
  114. $res1 = $res11 && $res12;
  115. $res2 = $this->dao->update($id, ['stop_time' => time() - 1, 'is_refund' => $kCount['id']]);
  116. } else {
  117. $res1 = true;
  118. $res2 = $this->dao->update($id, ['stop_time' => time() - 1, 'is_refund' => $id, 'status' => 3]);
  119. }
  120. //修改结束时间为前一秒
  121. $res = $res1 && $res2;
  122. } else if ($countY) {//团员
  123. $res = $this->dao->update($countY['id'], ['stop_time' => time() - 1, 'is_refund' => $id, 'status' => 3]);
  124. }
  125. if ($res) {
  126. CacheService::setStock(md5((string)$id), 1, 3, false);
  127. }
  128. return $res;
  129. }
  130. /**
  131. * 拼团详情查看拼团列表
  132. * @param int $id
  133. * @param bool $type
  134. * @param int $status
  135. * @return array
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function getPinkList(int $id, bool $type, int $status = -1)
  141. {
  142. $where['cid'] = $id;
  143. $where['k_id'] = 0;
  144. $where['is_refund'] = 0;
  145. if ($status !== -1) {
  146. $where['status'] = $status;
  147. }
  148. $list = $this->dao->pinkList($where);
  149. $ids = array_column($list, 'id');
  150. $counts = $this->dao->getPinkPeopleCount($ids);
  151. if ($type) {
  152. $pinkAll = [];
  153. foreach ($list as &$v) {
  154. $v['count'] = $v['people'] - $counts[$v['id']];
  155. $v['h'] = date('H', (int)$v['stop_time']);
  156. $v['i'] = date('i', (int)$v['stop_time']);
  157. $v['s'] = date('s', (int)$v['stop_time']);
  158. $pinkAll[] = $v['id'];//开团团长ID
  159. $v['stop_time'] = (int)$v['stop_time'];
  160. }
  161. return [$list, $pinkAll];
  162. }
  163. return $list;
  164. }
  165. /**
  166. * 获取成团列表信息
  167. * @param int $uid
  168. * @return array
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\DbException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. */
  173. public function getPinkOkList(int $uid)
  174. {
  175. $list = $this->dao->successList($uid, 'id,nickname', 0, 100);
  176. $msg = [];
  177. foreach ($list as &$item) {
  178. if (isset($item['nickname'])) $msg[] = $item['nickname'] .= '拼团成功';
  179. }
  180. return $msg;
  181. }
  182. /**
  183. * 查找拼团信息
  184. * @param $pink
  185. * @return array
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\DbException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. */
  190. public function getPinkMemberAndPinkK($pink)
  191. {
  192. //查团长
  193. if ($pink['k_id']) {//团员
  194. $pinkT = $this->dao->getPinkUserOne($pink['k_id']);
  195. } else {
  196. $pinkT = $pink;
  197. }
  198. //所有团员
  199. $pinkAll = $this->dao->getPinkUserList(['k_id' => $pinkT['id'], 'is_refund' => 0]);
  200. $count = count($pinkAll) + 1;
  201. $count = $pinkT['people'] - $count;
  202. //收集拼团用户id和拼团id
  203. $idAll = $uidAll = [];
  204. if ($pinkAll) {
  205. $idAll = array_column($pinkAll, 'id');
  206. $uidAll = array_column($pinkAll, 'uid');
  207. }
  208. $idAll[] = $pinkT['id'];
  209. $uidAll[] = $pinkT['uid'];
  210. return [$pinkAll, $pinkT, $count, $idAll, $uidAll];
  211. }
  212. /**
  213. * 拼团失败
  214. * @param $pinkAll
  215. * @param $pinkT
  216. * @param $pinkBool
  217. * @param bool $isRunErr
  218. * @param bool $isIds
  219. * @return array|int
  220. */
  221. public function pinkFail($pinkAll, $pinkT, $pinkBool, $isRunErr = true, $isIds = false)
  222. {
  223. $pinkIds = [];
  224. try {
  225. if ($pinkT['stop_time'] < time()) {//拼团时间超时 退款
  226. $virtual = $this->virtualCombination($pinkT['id']);
  227. if ($virtual) return 1;
  228. $pinkBool = -1;
  229. array_push($pinkAll, $pinkT);
  230. $oids = array_column($pinkAll, 'order_id_key');
  231. /** @var StoreOrderServices $orderService */
  232. $orderService = app()->make(StoreOrderServices::class);
  233. $orders = $orderService->getColumn([['id', 'in', $oids]], '*', 'id');
  234. $refundData = [
  235. 'refund_reason' => '拼团时间超时',
  236. 'refund_explain' => '拼团时间超时',
  237. 'refund_img' => json_encode([]),
  238. ];
  239. /** @var StoreOrderRefundServices $orderRefundService */
  240. $orderRefundService = app()->make(StoreOrderRefundServices::class);
  241. $refundeOrder = $orderRefundService->getColumn([
  242. ['store_order_id', 'IN', $oids],
  243. ['refund_type', 'in', [1, 2, 4, 5]],
  244. ['is_cancel', '=', 0],
  245. ['is_del', '=', 0]
  246. ], 'id,store_order_id', 'store_order_id');
  247. foreach ($pinkAll as $v) {
  248. if (isset($orders[$v['order_id_key']]) && $order = $orders[$v['order_id_key']]) {
  249. if (in_array($v['order_id_key'], $refundeOrder)) {
  250. continue;
  251. }
  252. $res1 = $res2 = true;
  253. try {
  254. $res1 = $orderRefundService->applyRefund((int)$order['id'], (int)$order['uid'], $order, [], 1, (float)$order['pay_price'], $refundData);
  255. } catch (\Throwable $e) {
  256. }
  257. $res2 = $this->dao->getCount([['uid', '=', $v['uid']], ['is_tpl', '=', 0], ['k_id|id', '=', $pinkT['id']]]);
  258. if ($res1 && $res2) {
  259. if ($isIds) array_push($pinkIds, $v['id']);
  260. $this->orderPinkAfterNo($pinkT['uid'], $pinkT['id'], false, $orders[$v['order_id_key']]['is_channel']);
  261. } else {
  262. if ($isRunErr) return $pinkBool;
  263. }
  264. }
  265. }
  266. }
  267. if ($isIds) return $pinkIds;
  268. } catch (\Exception $e) {
  269. \think\facade\Log::error('拼团超时处理失败,原因:' . $e->getMessage());
  270. }
  271. return $pinkBool;
  272. }
  273. /**
  274. * 失败发送消息和修改状态
  275. * @param $uid
  276. * @param $pid
  277. * @param bool $isRemove
  278. * @param $channel
  279. * @throws \think\db\exception\DataNotFoundException
  280. * @throws \think\db\exception\DbException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. */
  283. public function orderPinkAfterNo($uid, $pid, $isRemove = false, $channel = '')
  284. {
  285. $this->dao->update([['id|k_id', '=', $pid]], ['status' => 3, 'stop_time' => time()]);
  286. $pink = $this->dao->getOne([['id|k_id', '=', $pid], ['uid', '=', $uid]], '*', ['getProduct']);
  287. if ($isRemove) {
  288. event('notice.notice', [['uid' => $uid, 'pink' => $pink, 'user_type' => $channel], 'send_order_pink_clone']);
  289. } else {
  290. event('notice.notice', [['uid' => $uid, 'pink' => $pink, 'user_type' => $channel], 'send_order_pink_fial']);
  291. }
  292. }
  293. /**
  294. * 判断拼团状态
  295. * @param $pinkId
  296. * @return bool
  297. */
  298. public function isPinkStatus($pinkId)
  299. {
  300. if (!$pinkId) return false;
  301. $stopTime = $this->dao->value(['id' => $pinkId], 'stop_time');
  302. if ($stopTime < time()) return true; //拼团结束
  303. else return false;//拼团未结束
  304. }
  305. /**
  306. * 获取拼团order_id
  307. * @param int $id
  308. * @param int $uid
  309. * @return mixed
  310. */
  311. public function getCurrentPink(int $id, int $uid)
  312. {
  313. $oid = $this->dao->value(['id' => $id, 'uid' => $uid], 'order_id_key');
  314. if (!$oid) $oid = $this->dao->value(['k_id' => $id, 'uid' => $uid], 'order_id_key');
  315. /** @var StoreOrderServices $orderService */
  316. $orderService = app()->make(StoreOrderServices::class);
  317. return $orderService->value(['id' => $oid], 'order_id');
  318. }
  319. /**
  320. * 拼团成功
  321. * @param $uidAll
  322. * @param $idAll
  323. * @param $uid
  324. * @param $pinkT
  325. * @return int
  326. */
  327. public function pinkComplete($uidAll, $idAll, $uid, $pinkT)
  328. {
  329. $pinkBool = 6;
  330. try {
  331. if (!$this->dao->getCount([['id', 'in', $idAll], ['is_refund', '=', 1]])) {
  332. $this->dao->update([['id', 'in', $idAll]], ['stop_time' => time(), 'status' => 2]);
  333. if (in_array($uid, $uidAll)) {
  334. if ($this->dao->getCount([['uid', 'in', $uidAll], ['is_tpl', '=', 0], ['k_id|id', '=', $pinkT['id']]]))
  335. $this->orderPinkAfter($uidAll, $pinkT['id']);
  336. $pinkBool = 1;
  337. } else $pinkBool = 3;
  338. }
  339. return $pinkBool;
  340. } catch (\Exception $e) {
  341. return $pinkBool;
  342. }
  343. }
  344. /**
  345. * 拼团成功修改
  346. * @param $uidAll
  347. * @param $pid
  348. * @return bool
  349. * @throws \think\db\exception\DataNotFoundException
  350. * @throws \think\db\exception\DbException
  351. * @throws \think\db\exception\ModelNotFoundException
  352. */
  353. public function orderPinkAfter($uidAll, $pid)
  354. {
  355. //发送消息之前去除虚拟用户
  356. foreach ($uidAll as $key => $uid) {
  357. if ($uid == 0) unset($uidAll[$key]);
  358. }
  359. /** @var StoreCombinationServices $storeCombinationServices */
  360. $storeCombinationServices = app()->make(StoreCombinationServices::class);
  361. $pinkInfo = $this->dao->get((int)$pid, ['cid', 'nickname', 'uid']);
  362. $title = $storeCombinationServices->value(['id' => $pinkInfo['cid']], 'title');
  363. $pinkList = $this->dao->getColumn([['id|k_id', '=', $pid], ['uid', '<>', 0]], '*', 'uid');
  364. $order_ids = array_column($pinkList, 'order_id');
  365. /** @var StoreOrderServices $orderService */
  366. $orderService = app()->make(StoreOrderServices::class);
  367. $order_channels = $orderService->getColumn([['order_id', 'in', $order_ids]], 'is_channel', 'order_id');
  368. if (!$pinkList) return false;
  369. foreach ($pinkList as $item) {
  370. //用户发送消息
  371. event('notice.notice', [
  372. [
  373. 'list' => $item,
  374. 'nickname' => $pinkInfo['nickname'],
  375. 'title' => $title,
  376. 'user_type' => $order_channels[$item['order_id']],
  377. 'url' => '/pages/users/order_details/index?order_id=' . $item['order_id']
  378. ], 'order_user_groups_success']);
  379. }
  380. $this->dao->update([['uid', 'in', $uidAll], ['id|k_id', '=', $pid]], ['is_tpl' => 1]);
  381. }
  382. /**
  383. * 创建拼团
  384. * @param $order
  385. * @return mixed
  386. */
  387. public function createPink(array $orderInfo)
  388. {
  389. /** @var StoreCombinationServices $services */
  390. $services = app()->make(StoreCombinationServices::class);
  391. $product = $services->getOne(['id' => $orderInfo['activity_id']], 'effective_time,title,people');
  392. if (!$product) {
  393. return false;
  394. }
  395. /** @var UserServices $userServices */
  396. $userServices = app()->make(UserServices::class);
  397. $userInfo = $userServices->get($orderInfo['uid']);
  398. if ($orderInfo['pink_id']) {
  399. //拼团存在
  400. $res = false;
  401. $pink['uid'] = $orderInfo['uid'];//用户id
  402. $pink['nickname'] = $userInfo['nickname'];
  403. $pink['avatar'] = $userInfo['avatar'];
  404. if ($this->isPinkBe($pink, $orderInfo['pink_id'])) return false;
  405. $pink['order_id'] = $orderInfo['order_id'];//订单id 生成
  406. $pink['order_id_key'] = $orderInfo['id'];//订单id 数据库id
  407. $pink['total_num'] = $orderInfo['total_num'];//购买个数
  408. $pink['total_price'] = $orderInfo['pay_price'];//总金额
  409. $pink['k_id'] = $orderInfo['pink_id'];//拼团id
  410. foreach ($orderInfo['cartInfo'] as $v) {
  411. $pink['cid'] = $v['activity_id'];//拼团商品id
  412. $pink['pid'] = $v['product_id'];//商品id
  413. $pink['people'] = $product['people'];//几人拼团
  414. $pink['price'] = $v['productInfo']['price'];//单价
  415. $pink['stop_time'] = 0;//结束时间
  416. $pink['add_time'] = time();//开团时间
  417. $res = $this->save($pink);
  418. }
  419. // 拼团团成功发送模板消息
  420. event('notice.notice', [['orderInfo' => $orderInfo, 'title' => $product['title'], 'pink' => $pink], 'can_pink_success']);
  421. //处理拼团完成
  422. [$pinkAll, $pinkT, $count, $idAll, $uidAll] = $this->getPinkMemberAndPinkK($pink);
  423. if ($pinkT['status'] == 1) {
  424. if (!$count)//组团完成
  425. $this->pinkComplete($uidAll, $idAll, $pink['uid'], $pinkT);
  426. else
  427. $this->pinkFail($pinkAll, $pinkT, 0);
  428. }
  429. if ($res) return true;
  430. else return false;
  431. } else {
  432. //创建拼团
  433. $res = false;
  434. $pink['uid'] = $orderInfo['uid'];//用户id
  435. $pink['nickname'] = $userInfo['nickname'];
  436. $pink['avatar'] = $userInfo['avatar'];
  437. $pink['order_id'] = $orderInfo['order_id'];//订单id 生成
  438. $pink['order_id_key'] = $orderInfo['id'];//订单id 数据库id
  439. $pink['total_num'] = $orderInfo['total_num'];//购买个数
  440. $pink['total_price'] = $orderInfo['pay_price'];//总金额
  441. $pink['k_id'] = 0;//拼团id
  442. /** @var StoreOrderServices $orderServices */
  443. $orderServices = app()->make(StoreOrderServices::class);
  444. foreach ($orderInfo['cartInfo'] as $v) {
  445. $pink['cid'] = $v['activity_id'];//拼团商品id
  446. $pink['pid'] = $v['product_id'];//商品id
  447. $pink['people'] = $product['people'];//几人拼团
  448. $pink['price'] = $v['productInfo']['price'];//单价
  449. $pink['stop_time'] = time() + $product->effective_time * 3600;//结束时间
  450. $pink['add_time'] = time();//开团时间
  451. $res1 = $this->dao->save($pink);
  452. $res2 = $orderServices->update($orderInfo['id'], ['pink_id' => $res1['id']]);
  453. $res = $res1 && $res2;
  454. $pink['id'] = $res1['id'];
  455. }
  456. $number = (int)bcsub((string)$product['people'], '1', 0);
  457. if ($number) CacheService::setStock(md5((string)$pink['id']), $number, 3);
  458. PinkJob::dispatchSece(($product->effective_time * 3600) + 60, [$pink['id']]);
  459. event('notice.notice', [['orderInfo' => $orderInfo, 'title' => $product['title'], 'pink' => $pink], 'open_pink_success']);
  460. if ($res) return true;
  461. else return false;
  462. }
  463. }
  464. /**
  465. * 是否拼团
  466. * @param array $data
  467. * @param int $id
  468. * @return int
  469. */
  470. public function isPinkBe(array $data, int $id)
  471. {
  472. $data['id'] = $id;
  473. $count = $this->dao->getCount($data);
  474. if ($count) return $count;
  475. $data['k_id'] = $id;
  476. $count = $this->dao->getCount($data);
  477. if ($count) return $count;
  478. else return 0;
  479. }
  480. /**
  481. * 取消拼团
  482. * @param int $uid
  483. * @param int $cid
  484. * @param int $pink_id
  485. * @param null $nextPinkT
  486. * @return bool
  487. * @throws \think\db\exception\DataNotFoundException
  488. * @throws \think\db\exception\DbException
  489. * @throws \think\db\exception\ModelNotFoundException
  490. */
  491. public function removePink(int $uid, int $cid, int $pink_id, $nextPinkT = null)
  492. {
  493. $pinkT = $this->dao->getOne([
  494. ['uid', '=', $uid],
  495. ['id', '=', $pink_id],
  496. ['cid', '=', $cid],
  497. ['k_id', '=', 0],
  498. ['is_refund', '=', 0],
  499. ['status', '=', 1],
  500. ['stop_time', '>', time()],
  501. ]);
  502. if (!$pinkT) throw new ValidateException('未查到拼团信息,无法取消');
  503. [$pinkAll, $pinkT, $count, $idAll, $uidAll] = $this->getPinkMemberAndPinkK($pinkT);
  504. if (count($pinkAll)) {
  505. $count = $pinkT['people'] - ($this->dao->count(['k_id' => $pink_id, 'is_refund' => 0]) + 1);
  506. if ($count) {
  507. //拼团未完成,拼团有成员取消开团取 紧跟团长后拼团的人
  508. if (isset($pinkAll[0])) $nextPinkT = $pinkAll[0];
  509. } else {
  510. //拼团完成
  511. $this->PinkComplete($uidAll, $idAll, $uid, $pinkT);
  512. throw new ValidateException('拼团已完成,无法取消');
  513. }
  514. }
  515. /** @var StoreOrderServices $orderService */
  516. $orderService = app()->make(StoreOrderServices::class);
  517. /** @var StoreOrderRefundServices $orderRefundService */
  518. $orderRefundService = app()->make(StoreOrderRefundServices::class);
  519. //取消开团
  520. $order = $orderService->get($pinkT['order_id_key']);
  521. $refundData = [
  522. 'refund_reason' => '用户手动取消拼团',
  523. 'refund_explain' => '用户手动取消拼团',
  524. 'refund_img' => json_encode([]),
  525. ];
  526. try {
  527. $res1 = $orderRefundService->applyRefund((int)$order['id'], (int)$order['uid'], $order, [], 1, (float)$order['pay_price'], $refundData);
  528. } catch (\Throwable $e) {
  529. $res1 = true;
  530. }
  531. $res2 = $this->dao->getCount([['uid', '=', $pinkT['uid']], ['k_id|id', '=', $pinkT['id']]]);
  532. if ($res1 && $res2) {
  533. $this->orderPinkAfterNo($pinkT['uid'], $pinkT['id'], true, $order->is_channel);
  534. }
  535. //当前团有人的时候
  536. if (is_array($nextPinkT)) {
  537. $this->dao->update($nextPinkT['id'], ['k_id' => 0, 'status' => 1, 'stop_time' => $pinkT['stop_time']]);
  538. $this->dao->update($pinkT['id'], ['k_id' => $nextPinkT['id']], 'k_id');
  539. $orderService->update($nextPinkT['order_id'], ['pink_id' => $nextPinkT['id']], 'order_id');
  540. }
  541. return true;
  542. }
  543. /**
  544. * 修改到期的拼团状态
  545. * @return bool
  546. * @throws \think\db\exception\DataNotFoundException
  547. * @throws \think\db\exception\ModelNotFoundException
  548. * @throws \think\exception\DbException
  549. */
  550. public function statusPink(int $page, int $limit)
  551. {
  552. $pinkListEnd = $this->dao->pinkListEnd()->page($page, $limit)->select()->toArray();
  553. foreach ($pinkListEnd as $key => $pink) {
  554. [$pinkAll, $pinkT, $count, $idAll, $uidAll] = $this->getPinkMemberAndPinkK($pink);
  555. $this->pinkFail($pinkAll, $pinkT, 0);
  556. }
  557. return true;
  558. }
  559. /**
  560. * 放入自动取消拼团队列
  561. */
  562. public function useStatusPink()
  563. {
  564. $pinkEndCount = $this->dao->pinkListEnd()->count();
  565. if (!$pinkEndCount) {
  566. return true;
  567. }
  568. $pages = ceil($pinkEndCount / 100);
  569. for ($i = 1; $i <= $pages; $i++) {
  570. AuthPinkFail::dispatch([$i, 100]);
  571. }
  572. return true;
  573. }
  574. /**
  575. * 虚拟拼团
  576. * @param $pinkId
  577. * @return bool
  578. * @throws \think\db\exception\DataNotFoundException
  579. * @throws \think\db\exception\DbException
  580. * @throws \think\db\exception\ModelNotFoundException
  581. */
  582. public function virtualCombination($pinkId)
  583. {
  584. $pinkInfo = $this->dao->get($pinkId);
  585. $people = $pinkInfo['people'];
  586. $count = $this->dao->count(['k_id' => $pinkId]) + 1;
  587. $percent1 = bcdiv((string)$count, (string)$people, 2) * 100;
  588. /** @var StoreCombinationServices $services */
  589. $services = app()->make(StoreCombinationServices::class);
  590. $percent2 = $services->value(['id' => $pinkInfo['cid']], 'virtual');
  591. if ($percent1 >= $percent2) {
  592. $time = time();
  593. $num = $people - $count;
  594. $data = [];
  595. $defaultData = [
  596. 'uid' => 0,
  597. 'avatar' => sys_config('h5_avatar'),
  598. 'order_id' => 0,
  599. 'order_id_key' => 0,
  600. 'total_num' => 0,
  601. 'total_price' => 0,
  602. 'cid' => $pinkInfo['cid'],
  603. 'pid' => $pinkInfo['pid'],
  604. 'people' => $people,
  605. 'price' => 0,
  606. 'add_time' => $time,
  607. 'stop_time' => $time,
  608. 'k_id' => $pinkInfo['id'],
  609. 'is_tpl' => 1,
  610. 'is_refund' => 0,
  611. 'status' => 2,
  612. 'is_virtual' => 1
  613. ];
  614. mt_srand();
  615. for ($i = 0; $i < $num; $i++) {
  616. $defaultData['nickname'] = substr(md5(time() . rand(1000, 9999)), 0, 12);
  617. $data[$i] = $defaultData;
  618. }
  619. //添加虚拟团员
  620. $this->dao->saveAll($data);
  621. //更改团员状态为拼团成功
  622. $this->dao->update($pinkId, ['stop_time' => $time, 'status' => 2], 'k_id');
  623. //更改团长为拼团成功
  624. $this->dao->update($pinkId, ['stop_time' => $time, 'status' => 2]);
  625. $uidAll = $this->dao->getColumn([['id|k_id', '=', $pinkId]], 'uid');
  626. $this->orderPinkAfter($uidAll, $pinkId);
  627. return true;
  628. } else {
  629. return false;
  630. }
  631. }
  632. /**
  633. * 获取拼团海报详情信息
  634. * @param int $id
  635. * @param $user
  636. * @return mixed
  637. * @throws \think\db\exception\DataNotFoundException
  638. * @throws \think\db\exception\DbException
  639. * @throws \think\db\exception\ModelNotFoundException
  640. */
  641. public function posterInfo(int $id, $user)
  642. {
  643. $pinkInfo = $this->dao->get($id);
  644. /** @var StoreCombinationServices $combinationService */
  645. $combinationService = app()->make(StoreCombinationServices::class);
  646. $storeCombinationInfo = $combinationService->getOne(['id' => $pinkInfo['cid']], '*', ['getPrice']);
  647. $data['title'] = $storeCombinationInfo['title'];
  648. $data['url'] = '';
  649. $data['image'] = $storeCombinationInfo['image'];
  650. $data['price'] = $pinkInfo['price'];
  651. $data['label'] = $pinkInfo['people'] . '人团';
  652. if ($pinkInfo['k_id']) $pinkAll = $this->getPinkMember($pinkInfo['k_id']);
  653. else $pinkAll = $this->getPinkMember($pinkInfo['id']);
  654. $count = count($pinkAll);
  655. $data['msg'] = '原价¥' . $storeCombinationInfo['product_price'] . ' 还差' . ($pinkInfo['people'] - $count) . '人拼团成功';
  656. /** @var SystemAttachmentServices $systemAttachmentServices */
  657. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  658. try {
  659. $siteUrl = sys_config('site_url');
  660. if (request()->isRoutine()) {
  661. //小程序
  662. $name = $id . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_routine.jpg';
  663. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  664. if (!$imageInfo) {
  665. $valueData = 'id=' . $id;
  666. /** @var UserServices $userServices */
  667. $userServices = app()->make(UserServices::class);
  668. if ($userServices->checkUserPromoter((int)$user['uid'], $user)) {
  669. $valueData .= '&spid=' . $user['uid'];
  670. }
  671. $res = MiniProgram::appCodeUnlimit($valueData, 'pages/activity/goods_combination_status/index', 280);
  672. if (!$res) throw new ValidateException('二维码生成失败');
  673. $uploadType = (int)sys_config('upload_type', 1);
  674. $upload = UploadService::init($uploadType);
  675. $res = (string)Utils::streamFor($res);
  676. $res = $upload->to('routine/activity/pink/code')->validate()->stream($res, $name);
  677. if ($res === false) {
  678. throw new ValidateException($upload->getError());
  679. }
  680. $imageInfo = $upload->getUploadInfo();
  681. $imageInfo['image_type'] = $uploadType;
  682. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  683. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  684. if (!$remoteImage['status']) throw new ValidateException($remoteImage['msg']);
  685. $systemAttachmentServices->save([
  686. 'name' => $imageInfo['name'],
  687. 'att_dir' => $imageInfo['dir'],
  688. 'satt_dir' => $imageInfo['thumb_path'],
  689. 'att_size' => $imageInfo['size'],
  690. 'att_type' => $imageInfo['type'],
  691. 'image_type' => $imageInfo['image_type'],
  692. 'module_type' => 2,
  693. 'time' => time(),
  694. 'pid' => 1,
  695. 'type' => 1
  696. ]);
  697. $url = $imageInfo['dir'];
  698. } else $url = $imageInfo['att_dir'];
  699. $data['url'] = $url;
  700. if ($imageInfo['image_type'] == 1)
  701. $data['url'] = $siteUrl . $url;
  702. } else {
  703. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  704. /** @var QrcodeServices $qrcodeService */
  705. $qrcodeService = app()->make(QrcodeServices::class);
  706. $data['url'] = $qrcodeService->getTemporaryQrcode('pink-' . $id, $user['uid'])->url;
  707. }
  708. }
  709. } catch (\Throwable $e) {
  710. }
  711. return $data;
  712. }
  713. }