StorePinkServices.php 36 KB

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