StorePinkServices.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\dao\activity\combination\StorePinkDao;
  14. use app\jobs\PinkJob;
  15. use app\services\BaseServices;
  16. use app\services\order\StoreOrderRefundServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\other\PosterServices;
  19. use app\services\other\QrcodeServices;
  20. use app\services\system\attachment\SystemAttachmentServices;
  21. use app\services\user\UserServices;
  22. use crmeb\exceptions\ApiException;
  23. use crmeb\services\CacheService;
  24. use crmeb\services\app\MiniProgramService;
  25. use app\services\other\UploadService;
  26. use Guzzle\Http\EntityBody;
  27. /**
  28. *
  29. * Class StorePinkServices
  30. * @package app\services\activity
  31. * @method getPinkCount(array $where)
  32. * @method int count(array $where = []) 获取指定条件下的条数
  33. * @method getPinkOkSumTotalNum()
  34. * @method isPink(int $id, int $uid) 是否能继续拼团
  35. * @method getPinkUserOne(int $id) 拼团
  36. * @method getCount(array $where) 获取某些条件总数
  37. * @method value(array $where, string $field)
  38. * @method getColumn(array $where, string $field, ?string $key)
  39. * @method update(array $where, array $data)
  40. */
  41. class StorePinkServices extends BaseServices
  42. {
  43. /**
  44. * StorePinkServices constructor.
  45. * @param StorePinkDao $dao
  46. */
  47. public function __construct(StorePinkDao $dao)
  48. {
  49. $this->dao = $dao;
  50. }
  51. /**
  52. * @param array $where
  53. * @return array
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function systemPage(array $where)
  59. {
  60. $where['k_id'] = 0;
  61. [$page, $limit] = $this->getPageValue();
  62. $list = $this->dao->getList($where, $page, $limit);
  63. foreach ($list as &$item) {
  64. $item['count_people'] = $this->dao->count(['k_id' => $item['id']]) + 1;
  65. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', (int)$item['add_time']) : '';
  66. $item['_stop_time'] = $item['stop_time'] ? date('Y-m-d H:i:s', (int)$item['stop_time']) : '';
  67. }
  68. $count = $this->dao->count($where);
  69. return compact('list', 'count');
  70. }
  71. /**
  72. * 拼团列表头部
  73. * @return array
  74. */
  75. public function getStatistics()
  76. {
  77. $res = [
  78. ['col' => 6, 'count' => $this->dao->count(), 'name' => '参与人数(人)', 'className' => 'ios-speedometer-outline'],
  79. ['col' => 6, 'count' => $this->dao->count(['k_id' => 0, 'status' => 2]), 'name' => '成团数量(个)', 'className' => 'md-rose'],
  80. ];
  81. return compact('res');
  82. }
  83. /**
  84. * 参团人员
  85. * @param int $id
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function getPinkMember(int $id)
  92. {
  93. return $this->dao->getList(['k_id' => $id, 'is_refund' => 0]);
  94. }
  95. /**
  96. * 拼团退款
  97. * @param $order
  98. * @return bool
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public function setRefundPink($order)
  104. {
  105. $res = true;
  106. if ($order['pink_id']) {
  107. $id = $order['pink_id'];
  108. } else {
  109. return true;
  110. }
  111. //正在拼团 团长
  112. $count = $this->dao->getOne(['id' => $id, 'uid' => $order['uid']]);
  113. //正在拼团 团员
  114. $countY = $this->dao->getOne(['k_id' => $id, 'uid' => $order['uid']]);
  115. if (!$count && !$countY) {
  116. return $res;
  117. }
  118. if ($count) {//团长
  119. //判断团内是否还有其他人 如果有 团长为第二个进团的人
  120. $kCount = $this->dao->getPinking(['k_id' => $id]);
  121. if ($kCount) {
  122. $res11 = $this->dao->update($id, ['k_id' => $kCount['id']], 'k_id');
  123. $res12 = $this->dao->update($kCount['id'], ['stop_time' => $count['add_time'] + 86400, 'k_id' => 0]);
  124. $res1 = $res11 && $res12;
  125. $res2 = $this->dao->update($id, ['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $kCount['id'], 'status' => 3]);
  126. } else {
  127. $res1 = true;
  128. $res2 = $this->dao->update($id, ['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $id, 'status' => 3]);
  129. }
  130. //修改结束时间为前一秒 团长ID为0
  131. $res = $res1 && $res2;
  132. } else if ($countY) {//团员
  133. $res = $this->dao->update($countY['id'], ['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $id, 'status' => 3]);
  134. }
  135. return $res;
  136. }
  137. /**
  138. * 拼团详情查看拼团列表
  139. * @param int $id
  140. * @param bool $type
  141. * @return array
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. public function getPinkList(int $id, bool $type)
  147. {
  148. $where['cid'] = $id;
  149. $where['k_id'] = 0;
  150. $where['is_refund'] = 0;
  151. $pinkList = $this->dao->pinkList($where);
  152. $ids = array_column($pinkList, 'id');
  153. $orderIdKey = array_column($pinkList, 'order_id_key');
  154. $refunList = [];
  155. if ($orderIdKey) {
  156. $refunList = app()->make(StoreOrderRefundServices::class)->getColumn([['store_order_id', 'in', $orderIdKey]], 'id', 'store_order_id');
  157. }
  158. if ($refunList) {
  159. $list = [];
  160. foreach ($pinkList as $item) {
  161. if (!isset($refunList[$item['order_id_key']])) {
  162. $list[] = $item;
  163. }
  164. }
  165. } else {
  166. $list = $pinkList;
  167. }
  168. $counts = $this->dao->getPinkPeopleCount($ids);
  169. if ($type) {
  170. $pinkAll = [];
  171. foreach ($list as &$v) {
  172. $v['count'] = $v['people'] - $counts[$v['id']];
  173. $v['h'] = date('H', (int)$v['stop_time']);
  174. $v['i'] = date('i', (int)$v['stop_time']);
  175. $v['s'] = date('s', (int)$v['stop_time']);
  176. $pinkAll[] = $v['id'];//开团团长ID
  177. $v['stop_time'] = (int)$v['stop_time'];
  178. $v['avatar'] = set_file_url($v['avatar']);
  179. }
  180. return [$list, $pinkAll];
  181. }
  182. return $list;
  183. }
  184. /**
  185. * 获取成团列表信息
  186. * @param int $uid
  187. * @return array
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function getPinkOkList(int $uid)
  193. {
  194. $list = $this->dao->successList($uid);
  195. $msg = [];
  196. foreach ($list as &$item) {
  197. if (isset($item['nickname'])) $msg[] = $item['nickname'] .= '拼团成功';
  198. }
  199. return $msg;
  200. }
  201. /**
  202. * 查找拼团信息
  203. * @param $pink
  204. * @return array
  205. * @throws \think\db\exception\DataNotFoundException
  206. * @throws \think\db\exception\DbException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. */
  209. public function getPinkMemberAndPinkK($pink)
  210. {
  211. //查找拼团团员和团长
  212. if ($pink['k_id']) {
  213. $pinkAll = $this->dao->getPinkUserList(['k_id' => $pink['k_id'], 'is_refund' => 0]);
  214. $pinkT = $this->dao->getPinkUserOne($pink['k_id']);
  215. } else {
  216. $pinkAll = $this->dao->getPinkUserList(['k_id' => $pink['id'], 'is_refund' => 0]);
  217. $pinkT = $pink;
  218. }
  219. $count = count($pinkAll) + 1;
  220. $count = $pinkT['people'] - $count;
  221. $idAll = [];
  222. $uidAll = [];
  223. //收集拼团用户id和拼团id
  224. foreach ($pinkAll as $k => $v) {
  225. $idAll[$k] = $v['id'];
  226. $uidAll[$k] = $v['uid'];
  227. }
  228. $idAll[] = $pinkT['id'];
  229. $uidAll[] = $pinkT['uid'];
  230. return [$pinkAll, $pinkT, $count, $idAll, $uidAll];
  231. }
  232. /**
  233. * 拼团失败
  234. * @param $pinkAll
  235. * @param $pinkT
  236. * @param $pinkBool
  237. * @param bool $isRunErr
  238. * @param bool $isIds
  239. * @return array|int
  240. */
  241. public function pinkFail($pinkAll, $pinkT, $pinkBool, $isRunErr = true, $isIds = false)
  242. {
  243. /** @var StoreOrderServices $orderService */
  244. $orderService = app()->make(StoreOrderServices::class);
  245. /** @var StoreOrderRefundServices $orderRefundService */
  246. $orderRefundService = app()->make(StoreOrderRefundServices::class);
  247. $pinkIds = [];
  248. try {
  249. if ($pinkT['stop_time'] < time()) {//拼团时间超时 退款
  250. $virtual = $this->virtualCombination($pinkT['id']);
  251. if ($virtual) return 1;
  252. $pinkBool = -1;
  253. array_push($pinkAll, $pinkT);
  254. $oids = array_column($pinkAll, 'order_id_key');
  255. $orders = $orderService->getColumn([['id', 'in', $oids]], '*', 'id');
  256. $refundData = [
  257. 'refund_reason' => '拼团时间超时',
  258. 'refund_explain' => '拼团时间超时',
  259. 'refund_img' => json_encode([]),
  260. ];
  261. foreach ($pinkAll as $v) {
  262. if (isset($orders[$v['order_id_key']]) && $order = $orders[$v['order_id_key']]) {
  263. $res1 = $res2 = true;
  264. if (!in_array($order['refund_status'], [1, 2])) {
  265. $res1 = $orderRefundService->applyRefund((int)$order['id'], (int)$order['uid'], $order, [], 1, (float)$order['pay_price'], $refundData, 1);
  266. }
  267. $res2 = $this->dao->getCount([['uid', '=', $v['uid']], ['is_tpl', '=', 0], ['k_id|id', '=', $pinkT['id']]]);
  268. if ($res1 && $res2) {
  269. if ($isIds) array_push($pinkIds, $v['id']);
  270. $this->orderPinkAfterNo($pinkT['uid'], $pinkT['id'], false, $orders[$v['order_id_key']]['is_channel']);
  271. } else {
  272. if ($isRunErr) return $pinkBool;
  273. }
  274. }
  275. }
  276. }
  277. if ($isIds) return $pinkIds;
  278. return $pinkBool;
  279. } catch (\Exception $e) {
  280. return $pinkBool;
  281. }
  282. }
  283. /**
  284. * 失败发送消息和修改状态
  285. * @param $uid
  286. * @param $pid
  287. * @param bool $isRemove
  288. * @param $channel
  289. * @throws \think\db\exception\DataNotFoundException
  290. * @throws \think\db\exception\DbException
  291. * @throws \think\db\exception\ModelNotFoundException
  292. */
  293. public function orderPinkAfterNo($uid, $pid, $isRemove = false, $channel)
  294. {
  295. $pink = $this->dao->getOne([['id|k_id', '=', $pid], ['uid', '=', $uid]], '*', ['getProduct']);
  296. if ($isRemove) {
  297. event('NoticeListener', [['uid' => $uid, 'pink' => $pink, 'user_type' => $channel], 'send_order_pink_clone']);
  298. } else {
  299. event('NoticeListener', [['uid' => $uid, 'pink' => $pink, 'user_type' => $channel], 'send_order_pink_fial']);
  300. }
  301. $this->dao->update([['id|k_id', '=', $pid]], ['status' => 3, 'stop_time' => time()]);
  302. }
  303. /**
  304. * 判断拼团状态
  305. * @param $pinkId
  306. * @return bool
  307. */
  308. public function isPinkStatus($pinkId)
  309. {
  310. if (!$pinkId) return false;
  311. $stopTime = $this->dao->value(['id' => $pinkId], 'stop_time');
  312. if ($stopTime < time()) return true; //拼团结束
  313. else return false;//拼团未结束
  314. }
  315. /**
  316. * 获取拼团order_id
  317. * @param int $id
  318. * @param int $uid
  319. * @return mixed
  320. */
  321. public function getCurrentPink(int $id, int $uid)
  322. {
  323. $oid = $this->dao->value(['id' => $id, 'uid' => $uid], 'order_id_key');
  324. if (!$oid) $oid = $this->dao->value(['k_id' => $id, 'uid' => $uid], 'order_id_key');
  325. /** @var StoreOrderServices $orderService */
  326. $orderService = app()->make(StoreOrderServices::class);
  327. return $orderService->value(['id' => $oid], 'order_id');
  328. }
  329. /**
  330. * 拼团成功
  331. * @param $uidAll
  332. * @param $idAll
  333. * @param $uid
  334. * @param $pinkT
  335. * @return int
  336. */
  337. public function pinkComplete($uidAll, $idAll, $uid, $pinkT)
  338. {
  339. $pinkBool = 6;
  340. try {
  341. if (!$this->dao->getCount([['id', 'in', $idAll], ['is_refund', '=', 1]])) {
  342. $this->dao->update([['id', 'in', $idAll]], ['stop_time' => time(), 'status' => 2]);
  343. if (in_array($uid, $uidAll)) {
  344. if ($this->dao->getCount([['uid', 'in', $uidAll], ['is_tpl', '=', 0], ['k_id|id', '=', $pinkT['id']]]))
  345. $this->orderPinkAfter($uidAll, $pinkT['id']);
  346. $pinkBool = 1;
  347. } else $pinkBool = 3;
  348. }
  349. return $pinkBool;
  350. } catch (\Exception $e) {
  351. return $pinkBool;
  352. }
  353. }
  354. /**
  355. * 拼团成功修改
  356. * @param $uidAll
  357. * @param $pid
  358. * @return bool
  359. * @throws \think\db\exception\DataNotFoundException
  360. * @throws \think\db\exception\DbException
  361. * @throws \think\db\exception\ModelNotFoundException
  362. */
  363. public function orderPinkAfter($uidAll, $pid)
  364. {
  365. //发送消息之前去除虚拟用户
  366. foreach ($uidAll as $key => $uid) {
  367. if ($uid == 0) unset($uidAll[$key]);
  368. }
  369. /** @var StoreCombinationServices $storeCombinationServices */
  370. $storeCombinationServices = app()->make(StoreCombinationServices::class);
  371. $title = $storeCombinationServices->value(['id' => $this->dao->value(['id' => $pid], 'cid')], 'title');
  372. $pinkList = $this->dao->getColumn([['id|k_id', '=', $pid], ['uid', '<>', 0]], '*', 'uid');
  373. $pinkT_name = $this->dao->value(['id' => $pid], 'nickname');
  374. $order_ids = array_column($pinkList, 'order_id');
  375. /** @var StoreOrderServices $orderService */
  376. $orderService = app()->make(StoreOrderServices::class);
  377. $order_channels = $orderService->getColumn([['order_id', 'in', $order_ids]], 'is_channel', 'order_id');
  378. if (!$pinkList) return false;
  379. foreach ($pinkList as $item) {
  380. $item['nickname'] = $pinkT_name;
  381. //用户发送消息
  382. event('NoticeListener', [
  383. [
  384. 'list' => $item,
  385. 'title' => $title,
  386. 'user_type' => $order_channels[$item['order_id']],
  387. 'url' => '/pages/users/order_details/index?order_id=' . $item['order_id']
  388. ], 'order_user_groups_success']);
  389. }
  390. $this->dao->update([['uid', 'in', $uidAll], ['id|k_id', '=', $pid]], ['is_tpl' => 1]);
  391. }
  392. /**
  393. * 创建拼团
  394. * @param $order
  395. * @return mixed
  396. */
  397. public function createPink(array $orderInfo)
  398. {
  399. /** @var StoreCombinationServices $services */
  400. $services = app()->make(StoreCombinationServices::class);
  401. $product = $services->getOne(['id' => $orderInfo['combination_id']], 'effective_time,title,people');
  402. if (!$product) {
  403. return false;
  404. }
  405. /** @var UserServices $userServices */
  406. $userServices = app()->make(UserServices::class);
  407. $userInfo = $userServices->get($orderInfo['uid']);
  408. if ($orderInfo['pink_id']) {
  409. //拼团存在
  410. $res = false;
  411. $pink['uid'] = $orderInfo['uid'];//用户id
  412. $pink['nickname'] = $userInfo['nickname'];
  413. $pink['avatar'] = $userInfo['avatar'];
  414. if ($this->isPinkBe($pink, $orderInfo['pink_id'])) return false;
  415. $pink['order_id'] = $orderInfo['order_id'];//订单id 生成
  416. $pink['order_id_key'] = $orderInfo['id'];//订单id 数据库id
  417. $pink['total_num'] = $orderInfo['total_num'];//购买个数
  418. $pink['total_price'] = $orderInfo['pay_price'];//总金额
  419. $pink['k_id'] = $orderInfo['pink_id'];//拼团id
  420. foreach ($orderInfo['cartInfo'] as $v) {
  421. $pink['cid'] = $v['combination_id'];//拼团商品id
  422. $pink['pid'] = $v['product_id'];//商品id
  423. $pink['people'] = $product['people'];//几人拼团
  424. $pink['price'] = $v['productInfo']['price'];//单价
  425. $pink['stop_time'] = 0;//结束时间
  426. $pink['add_time'] = time();//开团时间
  427. $res = $this->save($pink);
  428. }
  429. // 拼团团成功发送模板消息
  430. event('NoticeListener', [['orderInfo' => $orderInfo, 'title' => $product['title'], 'pink' => $pink], 'can_pink_success']);
  431. //处理拼团完成
  432. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = $this->getPinkMemberAndPinkK($pink);
  433. if ($pinkT['status'] == 1) {
  434. if (!$count)//组团完成
  435. $this->pinkComplete($uidAll, $idAll, $pink['uid'], $pinkT);
  436. else
  437. $this->pinkFail($pinkAll, $pinkT, 0);
  438. }
  439. if ($res) return true;
  440. else return false;
  441. } else {
  442. //创建拼团
  443. $res = false;
  444. $pink['uid'] = $orderInfo['uid'];//用户id
  445. $pink['nickname'] = $userInfo['nickname'];
  446. $pink['avatar'] = $userInfo['avatar'];
  447. $pink['order_id'] = $orderInfo['order_id'];//订单id 生成
  448. $pink['order_id_key'] = $orderInfo['id'];//订单id 数据库id
  449. $pink['total_num'] = $orderInfo['total_num'];//购买个数
  450. $pink['total_price'] = $orderInfo['pay_price'];//总金额
  451. $pink['k_id'] = 0;//拼团id
  452. /** @var StoreOrderServices $orderServices */
  453. $orderServices = app()->make(StoreOrderServices::class);
  454. foreach ($orderInfo['cartInfo'] as $v) {
  455. $pink['cid'] = $v['combination_id'];//拼团商品id
  456. $pink['pid'] = $v['product_id'];//商品id
  457. $pink['people'] = $product['people'];//几人拼团
  458. $pink['price'] = $v['productInfo']['price'];//单价
  459. $pink['stop_time'] = time() + $product->effective_time * 3600;//结束时间
  460. $pink['add_time'] = time();//开团时间
  461. $res1 = $this->dao->save($pink);
  462. $res2 = $orderServices->update($orderInfo['id'], ['pink_id' => $res1['id']]);
  463. $res = $res1 && $res2;
  464. $pink['id'] = $res1['id'];
  465. }
  466. PinkJob::dispatchSecs((int)(($product->effective_time * 3600) + 60), [$pink['id']]);
  467. // 开团成功发送模板消息
  468. event('NoticeListener', [['orderInfo' => $orderInfo, 'title' => $product['title'], 'pink' => $pink], 'open_pink_success']);
  469. if ($res) return true;
  470. else return false;
  471. }
  472. }
  473. /**
  474. * 是否拼团
  475. * @param array $data
  476. * @param int $id
  477. * @return int
  478. */
  479. public function isPinkBe(array $data, int $id)
  480. {
  481. $data['id'] = $id;
  482. $count = $this->dao->getCount($data);
  483. if ($count) return $count;
  484. $data['k_id'] = $id;
  485. $count = $this->dao->getCount($data);
  486. if ($count) return $count;
  487. else return 0;
  488. }
  489. /**
  490. * 取消拼团
  491. * @param int $uid
  492. * @param int $cid
  493. * @param int $pink_id
  494. * @param null $nextPinkT
  495. * @return bool
  496. * @throws \think\db\exception\DataNotFoundException
  497. * @throws \think\db\exception\DbException
  498. * @throws \think\db\exception\ModelNotFoundException
  499. */
  500. public function removePink(int $uid, int $cid, int $pink_id, $nextPinkT = null)
  501. {
  502. $pinkT = $this->dao->getOne([
  503. ['uid', '=', $uid],
  504. ['id', '=', $pink_id],
  505. ['cid', '=', $cid],
  506. ['k_id', '=', 0],
  507. ['is_refund', '=', 0],
  508. ['status', '=', 1],
  509. ['stop_time', '>', time()],
  510. ]);
  511. if (!$pinkT) throw new ApiException(410314);
  512. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = $this->getPinkMemberAndPinkK($pinkT);
  513. if (count($pinkAll)) {
  514. $count = $pinkT['people'] - ($this->dao->count(['k_id' => $pink_id, 'is_refund' => 0]) + 1);
  515. if ($count) {
  516. //拼团未完成,拼团有成员取消开团取 紧跟团长后拼团的人
  517. if (isset($pinkAll[0])) $nextPinkT = $pinkAll[0];
  518. } else {
  519. //拼团完成
  520. $this->PinkComplete($uidAll, $idAll, $uid, $pinkT);
  521. throw new ApiException(410316);
  522. }
  523. }
  524. /** @var StoreOrderServices $orderService */
  525. $orderService = app()->make(StoreOrderServices::class);
  526. /** @var StoreOrderRefundServices $orderRefundService */
  527. $orderRefundService = app()->make(StoreOrderRefundServices::class);
  528. //取消开团
  529. $order = $orderService->get($pinkT['order_id_key']);
  530. $refundData = [
  531. 'refund_reason' => '用户手动取消拼团',
  532. 'refund_explain' => '用户手动取消拼团',
  533. 'refund_img' => json_encode([]),
  534. ];
  535. $res1 = $orderRefundService->applyRefund((int)$order['id'], (int)$order['uid'], $order, [], 1, (float)$order['pay_price'], $refundData, 1);
  536. $res2 = $this->dao->getCount([['uid', '=', $pinkT['uid']], ['k_id|id', '=', $pinkT['id']]]);
  537. if ($res1 && $res2) {
  538. $this->orderPinkAfterNo($pinkT['uid'], $pinkT['id'], true, $order->is_channel);
  539. }
  540. //当前团有人的时候
  541. if (is_array($nextPinkT)) {
  542. $this->dao->update($nextPinkT['id'], ['k_id' => 0, 'status' => 1, 'stop_time' => $pinkT['stop_time']]);
  543. $this->dao->update($pinkT['id'], ['k_id' => $nextPinkT['id']], 'k_id');
  544. $orderService->update($nextPinkT['order_id'], ['pink_id' => $nextPinkT['id']], 'order_id');
  545. }
  546. return true;
  547. }
  548. /**
  549. * 获取拼团海报
  550. * @param $pinkId
  551. * @param $from
  552. * @param $user
  553. * @return string
  554. */
  555. public function getPinkPoster($pinkId, $from, $user)
  556. {
  557. $pinkInfo = $this->dao->get((int)$pinkId);
  558. /** @var StoreCombinationServices $combinationService */
  559. $combinationService = app()->make(StoreCombinationServices::class);
  560. $storeCombinationInfo = $combinationService->getOne(['id' => $pinkInfo['cid']], '*', ['getPrice']);
  561. $data['title'] = $storeCombinationInfo['title'];
  562. $data['image'] = $storeCombinationInfo['image'];
  563. $data['price'] = $pinkInfo['price'];
  564. $data['label'] = $pinkInfo['people'] . '人团';
  565. if ($pinkInfo['k_id']) $pinkAll = $this->getPinkMember($pinkInfo['k_id']);
  566. else $pinkAll = $this->getPinkMember($pinkInfo['id']);
  567. $count = count($pinkAll);
  568. $data['msg'] = '原价¥' . $storeCombinationInfo['product_price'] . ' 还差' . ($pinkInfo['people'] - $count) . '人拼团成功';
  569. /** @var SystemAttachmentServices $systemAttachmentServices */
  570. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  571. try {
  572. $siteUrl = sys_config('site_url');
  573. if ($from == 'routine') {
  574. //小程序
  575. $name = $pinkId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_routine.jpg';
  576. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  577. if (!$imageInfo) {
  578. $valueData = 'id=' . $pinkId;
  579. /** @var UserServices $userServices */
  580. $userServices = app()->make(UserServices::class);
  581. if ($userServices->checkUserPromoter((int)$user['uid'], $user)) {
  582. $valueData .= '&pid=' . $user['uid'];
  583. }
  584. $res = MiniProgramService::appCodeUnlimitService($valueData, 'pages/activity/goods_combination_status/index', 280);
  585. if (!$res) throw new ApiException(410167);
  586. $uploadType = (int)sys_config('upload_type', 1);
  587. $upload = UploadService::init();
  588. $res = (string)EntityBody::factory($res);
  589. $res = $upload->to('routine/activity/pink/code')->validate()->setAuthThumb(false)->stream($res, $name);
  590. if ($res === false) {
  591. throw new ApiException($upload->getError());
  592. }
  593. $imageInfo = $upload->getUploadInfo();
  594. $imageInfo['image_type'] = $uploadType;
  595. if ($imageInfo['image_type'] == 1) $remoteImage = PosterServices::remoteImage($siteUrl . $imageInfo['dir']);
  596. else $remoteImage = PosterServices::remoteImage($imageInfo['dir']);
  597. if (!$remoteImage['status']) throw new ApiException($remoteImage['msg']);
  598. $systemAttachmentServices->save([
  599. 'name' => $imageInfo['name'],
  600. 'att_dir' => $imageInfo['dir'],
  601. 'satt_dir' => $imageInfo['thumb_path'],
  602. 'att_size' => $imageInfo['size'],
  603. 'att_type' => $imageInfo['type'],
  604. 'image_type' => $imageInfo['image_type'],
  605. 'module_type' => 2,
  606. 'time' => time(),
  607. 'pid' => 1,
  608. 'type' => 1
  609. ]);
  610. $url = $imageInfo['dir'];
  611. } else $url = $imageInfo['att_dir'];
  612. $data['url'] = $url;
  613. if ($imageInfo['image_type'] == 1)
  614. $data['url'] = $siteUrl . $url;
  615. $posterImage = PosterServices::setShareMarketingPoster($data, 'routine/activity/pink/poster');
  616. if (!is_array($posterImage)) throw new ApiException(410172);
  617. $systemAttachmentServices->save([
  618. 'name' => $posterImage['name'],
  619. 'att_dir' => $posterImage['dir'],
  620. 'satt_dir' => $posterImage['thumb_path'],
  621. 'att_size' => $posterImage['size'],
  622. 'att_type' => $posterImage['type'],
  623. 'image_type' => $posterImage['image_type'],
  624. 'module_type' => 2,
  625. 'time' => $posterImage['time'],
  626. 'pid' => 1,
  627. 'type' => 1
  628. ]);
  629. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  630. $routinePosterImage = set_http_type($posterImage['dir'], 0);//小程序推广海报
  631. return $routinePosterImage;
  632. } else if ($from == 'wechat') {
  633. //公众号
  634. $name = $pinkId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_wap.jpg';
  635. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  636. if (!$imageInfo) {
  637. $codeUrl = set_http_type($siteUrl . '/pages/activity/goods_combination_status/index?id=' . $pinkId . '&spread=' . $user['uid'], 1);//二维码链接
  638. $imageInfo = PosterServices::getQRCodePath($codeUrl, $name);
  639. if (is_string($imageInfo)) {
  640. throw new ApiException(410167);
  641. }
  642. $systemAttachmentServices->save([
  643. 'name' => $imageInfo['name'],
  644. 'att_dir' => $imageInfo['dir'],
  645. 'satt_dir' => $imageInfo['thumb_path'],
  646. 'att_size' => $imageInfo['size'],
  647. 'att_type' => $imageInfo['type'],
  648. 'image_type' => $imageInfo['image_type'],
  649. 'module_type' => 2,
  650. 'time' => $imageInfo['time'],
  651. 'pid' => 1,
  652. 'type' => 1
  653. ]);
  654. $url = $imageInfo['dir'];
  655. } else $url = $imageInfo['att_dir'];
  656. $data['url'] = $url;
  657. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  658. $posterImage = PosterServices::setShareMarketingPoster($data, 'wap/activity/pink/poster');
  659. if (!is_array($posterImage)) throw new ApiException(410172);
  660. $systemAttachmentServices->save([
  661. 'name' => $posterImage['name'],
  662. 'att_dir' => $posterImage['dir'],
  663. 'satt_dir' => $posterImage['thumb_path'],
  664. 'att_size' => $posterImage['size'],
  665. 'att_type' => $posterImage['type'],
  666. 'image_type' => $posterImage['image_type'],
  667. 'module_type' => 2,
  668. 'time' => $posterImage['time'],
  669. 'pid' => 1,
  670. 'type' => 1
  671. ]);
  672. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  673. $wapPosterImage = set_http_type($posterImage['dir'], 1);//公众号推广海报
  674. return $wapPosterImage;
  675. }
  676. throw new ApiException(100100);
  677. } catch (\Exception $e) {
  678. throw new ApiException($e->getMessage());
  679. }
  680. }
  681. /**
  682. * 修改到期的拼团状态
  683. * @return bool
  684. * @throws \think\db\exception\DataNotFoundException
  685. * @throws \think\db\exception\ModelNotFoundException
  686. * @throws \think\exception\DbException
  687. */
  688. public function statusPink()
  689. {
  690. $pinkListEnd = $this->dao->pinkListEnd();
  691. foreach ($pinkListEnd as $key => $pink) {
  692. [$pinkAll, $pinkT, $count, $idAll, $uidAll] = $this->getPinkMemberAndPinkK($pink);
  693. $this->pinkFail($pinkAll, $pinkT, 0);
  694. }
  695. return true;
  696. }
  697. /**
  698. * 拼团成功
  699. * @param array $pinkRegimental 成功的团长编号
  700. * @return bool
  701. * @throws \Exception
  702. */
  703. public function successPinkEdit(array $pinkRegimental)
  704. {
  705. if (!count($pinkRegimental)) return true;
  706. foreach ($pinkRegimental as $key => &$item) {
  707. $pinkList = $this->dao->getColumn(['k_id' => $item], 'id', 'id');
  708. $pinkList[] = $item;
  709. $pinkList = implode(',', $pinkList);
  710. $this->dao->update([['id', 'in', $pinkList]], ['stop_time' => time(), 'status' => 2]);
  711. $pinkUidList = $this->dao->getColumn([['id', 'in', $pinkList], ['is_tpl', '=', 0]], 'uid', 'uid');
  712. if (count($pinkUidList)) $this->orderPinkAfter($pinkUidList, $item);//发送模板消息
  713. }
  714. return true;
  715. }
  716. /**
  717. * 拼团失败
  718. * @param array $pinkRegimental 失败的团长编号
  719. * @return bool
  720. * @throws \think\db\exception\DataNotFoundException
  721. * @throws \think\db\exception\ModelNotFoundException
  722. * @throws \think\exception\DbException
  723. */
  724. public function failPinkEdit(array $pinkRegimental)
  725. {
  726. if (!count($pinkRegimental)) return true;
  727. foreach ($pinkRegimental as $key => &$item) {
  728. $pinkList = $this->dao->getColumn(['k_id' => $item], 'id', 'id');
  729. $pinkList[] = $item;
  730. $pinkList = implode(',', $pinkList);
  731. $refundPinkList = $this->dao->getColumn([['id', 'in', $pinkList]], 'order_id,uid', 'id');
  732. if ($refundPinkList) {
  733. /** @var StoreOrderRefundServices $orderRefundService */
  734. $orderRefundService = app()->make(StoreOrderRefundServices::class);
  735. $refundData = [
  736. 'refund_reason' => '拼团时间超时',
  737. 'refund_explain' => '拼团时间超时',
  738. 'refund_img' => json_encode([]),
  739. ];
  740. foreach ($refundPinkList as &$items) {
  741. $orderRefundService->applyRefund((int)$items['id'], (int)$items['uid'], $items, [], 1, (float)$items['pay_price'], $refundData, 1);//申请退款
  742. }
  743. }
  744. $this->dao->update([['id', 'in', $pinkList]], ['status' => 3]);
  745. }
  746. return true;
  747. }
  748. /**
  749. * 虚拟拼团
  750. * @param $pinkId
  751. * @return bool
  752. * @throws \think\db\exception\DataNotFoundException
  753. * @throws \think\db\exception\DbException
  754. * @throws \think\db\exception\ModelNotFoundException
  755. */
  756. public function virtualCombination($pinkId)
  757. {
  758. $pinkInfo = $this->dao->get($pinkId);
  759. $people = $pinkInfo['people'];
  760. $count = $this->dao->count(['k_id' => $pinkId]) + 1;
  761. $percent1 = bcdiv((string)$count, (string)$people, 2) * 100;
  762. /** @var StoreCombinationServices $services */
  763. $services = app()->make(StoreCombinationServices::class);
  764. $percent2 = $services->value(['id' => $pinkInfo['cid']], 'virtual');
  765. if ($percent1 >= $percent2) {
  766. $time = time();
  767. $num = $people - $count;
  768. $data = [];
  769. for ($i = 0; $i < $num; $i++) {
  770. $data[$i]['uid'] = 0;
  771. $data[$i]['nickname'] = substr(md5(time() . rand(1000, 9999)), 0, 12);
  772. $data[$i]['avatar'] = sys_config('h5_avatar');
  773. $data[$i]['order_id'] = 0;
  774. $data[$i]['order_id_key'] = 0;
  775. $data[$i]['total_num'] = 0;
  776. $data[$i]['total_price'] = 0;
  777. $data[$i]['cid'] = $pinkInfo['cid'];
  778. $data[$i]['pid'] = $pinkInfo['pid'];
  779. $data[$i]['people'] = $people;
  780. $data[$i]['price'] = 0;
  781. $data[$i]['add_time'] = $time;
  782. $data[$i]['stop_time'] = $time;
  783. $data[$i]['k_id'] = $pinkInfo['id'];
  784. $data[$i]['is_tpl'] = 1;
  785. $data[$i]['is_refund'] = 0;
  786. $data[$i]['status'] = 2;
  787. $data[$i]['is_virtual'] = 1;
  788. }
  789. //添加虚拟团员
  790. $this->dao->saveAll($data);
  791. //更改团员状态为拼团成功
  792. $this->dao->update($pinkId, ['stop_time' => $time, 'status' => 2], 'k_id');
  793. //更改团长为拼团成功
  794. $this->dao->update($pinkId, ['stop_time' => $time, 'status' => 2]);
  795. $uidAll = $this->dao->getColumn([['id|k_id', '=', $pinkId]], 'uid');
  796. $this->orderPinkAfter($uidAll, $pinkId);
  797. return true;
  798. } else {
  799. return false;
  800. }
  801. }
  802. /**
  803. * 获取拼团海报详情信息
  804. * @param int $id
  805. * @param $user
  806. * @return mixed
  807. * @throws \think\db\exception\DataNotFoundException
  808. * @throws \think\db\exception\DbException
  809. * @throws \think\db\exception\ModelNotFoundException
  810. */
  811. public function posterInfo(int $id, $user)
  812. {
  813. $pinkInfo = $this->dao->get($id);
  814. /** @var StoreCombinationServices $combinationService */
  815. $combinationService = app()->make(StoreCombinationServices::class);
  816. $storeCombinationInfo = $combinationService->getOne(['id' => $pinkInfo['cid']], '*', ['getPrice']);
  817. $data['title'] = $storeCombinationInfo['title'];
  818. $data['url'] = '';
  819. $data['image'] = $storeCombinationInfo['image'];
  820. $data['price'] = $pinkInfo['price'];
  821. $data['label'] = $pinkInfo['people'] . '人团';
  822. if ($pinkInfo['k_id']) $pinkAll = $this->getPinkMember($pinkInfo['k_id']);
  823. else $pinkAll = $this->getPinkMember($pinkInfo['id']);
  824. $count = count($pinkAll);
  825. $data['msg'] = '原价¥' . $storeCombinationInfo['product_price'] . ' 还差' . ($pinkInfo['people'] - $count) . '人拼团成功';
  826. /** @var SystemAttachmentServices $systemAttachmentServices */
  827. $systemAttachmentServices = app()->make(SystemAttachmentServices::class);
  828. try {
  829. $siteUrl = sys_config('site_url');
  830. if (request()->isRoutine()) {
  831. //小程序
  832. $name = $id . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_pink_share_routine.jpg';
  833. $imageInfo = $systemAttachmentServices->getInfo(['name' => $name]);
  834. if (!$imageInfo) {
  835. $valueData = 'id=' . $id;
  836. /** @var UserServices $userServices */
  837. $userServices = app()->make(UserServices::class);
  838. if ($userServices->checkUserPromoter((int)$user['uid'], $user)) {
  839. $valueData .= '&pid=' . $user['uid'];
  840. }
  841. $res = MiniProgramService::appCodeUnlimitService($valueData, 'pages/activity/goods_combination_status/index', 280);
  842. if (!$res) throw new ApiException(410167);
  843. $uploadType = (int)sys_config('upload_type', 1);
  844. $upload = UploadService::init();
  845. $res = $upload->to('routine/activity/pink/code')->validate()->setAuthThumb(false)->stream($res, $name);
  846. if ($res === false) {
  847. throw new ApiException($upload->getError());
  848. }
  849. $imageInfo = $upload->getUploadInfo();
  850. $imageInfo['image_type'] = $uploadType;
  851. if ($imageInfo['image_type'] == 1) $remoteImage = PosterServices::remoteImage($siteUrl . $imageInfo['dir']);
  852. else $remoteImage = PosterServices::remoteImage($imageInfo['dir']);
  853. if (!$remoteImage['status']) throw new ApiException($remoteImage['msg']);
  854. $systemAttachmentServices->save([
  855. 'name' => $imageInfo['name'],
  856. 'att_dir' => $imageInfo['dir'],
  857. 'satt_dir' => $imageInfo['thumb_path'],
  858. 'att_size' => $imageInfo['size'],
  859. 'att_type' => $imageInfo['type'],
  860. 'image_type' => $imageInfo['image_type'],
  861. 'module_type' => 2,
  862. 'time' => time(),
  863. 'pid' => 1,
  864. 'type' => 1
  865. ]);
  866. $url = $imageInfo['dir'];
  867. } else $url = $imageInfo['att_dir'];
  868. $data['url'] = $url;
  869. if ($imageInfo['image_type'] == 1)
  870. $data['url'] = $siteUrl . $url;
  871. } else {
  872. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  873. /** @var QrcodeServices $qrcodeService */
  874. $qrcodeService = app()->make(QrcodeServices::class);
  875. $data['url'] = $qrcodeService->getTemporaryQrcode('pink-' . $id, $user['uid'])->url;
  876. }
  877. }
  878. } catch (\Throwable $e) {
  879. }
  880. return $data;
  881. }
  882. }