StorePinkServices.php 40 KB

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