StorePinkServices.php 39 KB

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