StorePinkServices.php 38 KB

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