StorePinkServices.php 38 KB

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