StorePinkServices.php 39 KB

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