StorePinkServices.php 38 KB

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