StorePinkServices.php 39 KB

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