StorePinkServices.php 38 KB

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