StorePinkServices.php 37 KB

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