StorePinkServices.php 39 KB

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