StorePink.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. <?php
  2. namespace app\models\store;
  3. use app\models\routine\RoutineFormId;
  4. use app\models\routine\RoutineTemplate;
  5. use app\models\user\User;
  6. use app\models\user\WechatUser;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\services\WechatTemplateService;
  9. use crmeb\traits\ModelTrait;
  10. use think\facade\Route;
  11. use think\facade\Cache;
  12. /**
  13. * TODO 拼团Model
  14. * Class StorePink
  15. * @package app\models\store
  16. */
  17. class StorePink extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'store_pink';
  29. use ModelTrait;
  30. /**
  31. * 获取拼团完成的用户
  32. * @param $uid
  33. * @return array
  34. */
  35. public static function getPinkOkList($uid)
  36. {
  37. $list = self::alias('a')->where('a.status', 2)->where('a.is_refund', 0)->where('a.uid', '<>', $uid)->join('User u', 'u.uid=a.uid', 'right')->column('nickname', 'id');
  38. $msg = [];
  39. foreach ($list as &$item) {
  40. $msg[] = $item .= '拼团成功';
  41. }
  42. return $msg;
  43. }
  44. /*
  45. * 获取拼团完成的商品总件数
  46. * */
  47. public static function getPinkOkSumTotalNum($id)
  48. {
  49. return self::where('status', 2)->where('is_refund', 0)->sum('total_num');
  50. }
  51. /**
  52. * 获取一条拼团数据
  53. * @param $id
  54. * @return mixed
  55. */
  56. public static function getPinkUserOne($id)
  57. {
  58. $model = new self();
  59. $model = $model->alias('p');
  60. $model = $model->field('p.*,u.nickname,u.avatar');
  61. $model = $model->where('id', $id);
  62. $model = $model->join('user u', 'u.uid = p.uid');
  63. return $model->find();
  64. }
  65. /**
  66. * 获取拼团的团员
  67. * @param $id
  68. * @return mixed
  69. */
  70. public static function getPinkMember($id)
  71. {
  72. $model = new self();
  73. $model = $model->alias('p');
  74. $model = $model->field('p.*,u.nickname,u.avatar');
  75. $model = $model->where('k_id', $id);
  76. $model = $model->where('is_refund', 0);
  77. $model = $model->join('user u', 'u.uid = p.uid');
  78. $model = $model->order('id asc');
  79. return $model->select();
  80. }
  81. /**
  82. * 设置结束时间
  83. * @param $idAll
  84. * @return $this
  85. */
  86. public static function setPinkStopTime($idAll)
  87. {
  88. $model = new self();
  89. $model = $model->where('id', 'IN', $idAll);
  90. return $model->update(['stop_time' => time(), 'status' => 2]);
  91. }
  92. /**
  93. * 获取正在拼团的数据 团长
  94. * @param int $cid 产品id
  95. * @param int $isAll 是否查找所有拼团
  96. * @return array
  97. */
  98. public static function getPinkAll($cid, $isAll = false)
  99. {
  100. $model = new self();
  101. $model = $model->alias('p');
  102. $model = $model->field('p.id,p.uid,p.people,p.price,p.stop_time,u.nickname,u.avatar');
  103. $model = $model->where('stop_time', '>', time());
  104. $model = $model->where('p.cid', $cid);
  105. $model = $model->where('p.k_id', 0);
  106. $model = $model->where('p.is_refund', 0);
  107. $model = $model->order('p.add_time desc');
  108. $model = $model->join('user u', 'u.uid = p.uid');
  109. $list = $model->select();
  110. $list = count($list) ? $list->toArray() : [];
  111. if ($isAll) {
  112. $pindAll = [];
  113. foreach ($list as &$v) {
  114. $v['count'] = self::getPinkPeople($v['id'], $v['people']);
  115. $v['h'] = date('H', $v['stop_time']);
  116. $v['i'] = date('i', $v['stop_time']);
  117. $v['s'] = date('s', $v['stop_time']);
  118. $pindAll[] = $v['id']; //开团团长ID
  119. $v['stop_time'] = (int)$v['stop_time'];
  120. }
  121. return [$list, $pindAll];
  122. }
  123. return $list;
  124. }
  125. /**
  126. * 获取还差几人
  127. * @param $kid
  128. * @param $people
  129. * @return string
  130. */
  131. public static function getPinkPeople($kid, $people)
  132. {
  133. $model = new self();
  134. $model = $model->where('k_id', $kid)->where('is_refund', 0);
  135. $count = bcadd($model->count(), 1, 0);
  136. return bcsub($people, $count, 0);
  137. }
  138. /**
  139. * 判断订单是否在当前的拼团中
  140. * @param $orderId
  141. * @param $kid
  142. * @return bool
  143. */
  144. public static function getOrderIdAndPink($orderId, $kid)
  145. {
  146. $model = new self();
  147. $pink = $model->where('k_id|id', $kid)->column('order_id');
  148. if (in_array($orderId, $pink)) return true;
  149. else return false;
  150. }
  151. /**
  152. * 判断用户是否在团内
  153. * @param $id
  154. * @return int|string
  155. */
  156. public static function getIsPinkUid($id = 0, $uid = 0)
  157. {
  158. $pink = self::where('k_id|id', $id)->where('uid', $uid)->where('is_refund', 0)->count();
  159. if ($pink) return true;
  160. else return false;
  161. }
  162. /**
  163. * 判断是否发送模板消息 0 未发送 1已发送
  164. * @param $uidAll
  165. * @return int|string
  166. */
  167. public static function isTpl($uidAll, $pid)
  168. {
  169. if (is_array($uidAll))
  170. $count = self::where('uid', 'IN', implode(',', $uidAll))->where('is_tpl', 0)->where('id|k_id', $pid)->count();
  171. else
  172. $count = self::where('uid', $uidAll)->where('is_tpl', 0)->where('k_id|id', $pid)->count();
  173. return $count;
  174. }
  175. /**
  176. * 拼团成功提示模板消息
  177. * @param $uidAll
  178. * @param $pid
  179. */
  180. public static function orderPinkAfter($uidAll, $pid)
  181. {
  182. $pinkInfo = self::where('p.id|p.k_id', $pid)->alias('p')->field(['p.people', 't.title', 'p.add_time', 'p.order_id', 'u.nickname'])->join('user u', 'u.uid = p.uid')->join('store_combination t', 'p.cid = t.id')->find();
  183. if (!$pinkInfo) return false;
  184. foreach ($uidAll as $key => &$item) {
  185. $openid = WechatUser::uidToOpenid($item, 'openid');
  186. $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
  187. if ($openid) { //公众号模板消息
  188. $firstWeChat = '亲,您的拼团已经完成了';
  189. $keyword1WeChat = self::where('id|k_id', $pid)->where('uid', $item)->value('order_id');
  190. $keyword2WeChat = self::alias('p')->where('p.id|p.k_id', $pid)->where('p.uid', $item)->join('store_combination c', 'c.id=p.cid')->value('c.title');
  191. $remarkWeChat = '点击查看订单详情';
  192. $urlWeChat = Route::buildUrl('order/detail/' . $keyword1WeChat)->suffix('')->domain(true)->build();
  193. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
  194. 'first' => $firstWeChat,
  195. 'keyword1' => $keyword1WeChat,
  196. 'keyword2' => $keyword2WeChat,
  197. 'remark' => $remarkWeChat
  198. ], $urlWeChat);
  199. } else if ($routineOpenid) { // 小程序模板消息
  200. RoutineTemplate::sendPinkSuccess($item, $pinkInfo['title'], $pinkInfo['nickname'] ?? '', $pinkInfo['add_time'], $pinkInfo['people'], '/pages/order_details/index?order_id=' . $pinkInfo['order_id']);
  201. }
  202. }
  203. self::beginTrans();
  204. $res1 = self::where('uid', 'IN', implode(',', $uidAll))->where('id|k_id', $pid)->update(['is_tpl' => 1]);
  205. self::checkTrans($res1);
  206. }
  207. /**
  208. * 拼团失败发送的模板消息
  209. * @param $uid
  210. * @param $pid
  211. */
  212. public static function orderPinkAfterNo($uid, $pid, $formId = '', $fillTilt = '', $isRemove = false)
  213. {
  214. $store = self::alias('p')->where('p.id|p.k_id', $pid)->field('c.*')->where('p.uid', $uid)->join('store_combination c', 'c.id=p.cid')->find();
  215. $pink = self::where('id|k_id', $pid)->where('uid', $uid)->find();
  216. $openid = WechatUser::uidToOpenid($uid, 'openid');
  217. $routineOpenid = WechatUser::uidToOpenid($uid, 'routine_openid');
  218. if ($isRemove) {
  219. if ($openid) { //公众号发送模板消息
  220. $urlWeChat = Route::buildUrl('order/detail/' . $pink->order_id)->suffix('')->domain(true)->build();
  221. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
  222. 'first' => '亲,您的拼团取消',
  223. 'keyword1' => $store->title,
  224. 'keyword2' => $pink->price,
  225. 'keyword3' => $pink->price,
  226. 'remark' => '点击查看订单详情'
  227. ], $urlWeChat);
  228. } else if ($routineOpenid) { //小程序发送模板消息
  229. RoutineTemplate::sendPinkFail($uid, $store->title, $pink->people, '亲,您的拼团取消,点击查看订单详情', '/pages/order_details/index?order_id=' . $pink->order_id);
  230. }
  231. } else {
  232. if ($openid) { //公众号发送模板消息
  233. $urlWeChat = Route::buildUrl('order/detail/' . $pink->order_id)->suffix('')->domain(true)->build();
  234. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
  235. 'first' => '亲,您的拼团失败',
  236. 'keyword1' => $store->title,
  237. 'keyword2' => $pink->price,
  238. 'keyword3' => $pink->price,
  239. 'remark' => '点击查看订单详情'
  240. ], $urlWeChat);
  241. } else if ($routineOpenid) { //小程序发送模板消息
  242. RoutineTemplate::sendPinkFail(
  243. $uid,
  244. $store->title,
  245. $pink->people,
  246. '亲,您拼团失败,自动为您申请退款,退款金额为:' . $pink->price,
  247. '/pages/order_details/index?order_id=' . $pink->order_id
  248. );
  249. }
  250. }
  251. self::where('id', $pid)->update(['status' => 3, 'stop_time' => time()]);
  252. self::where('k_id', $pid)->update(['status' => 3, 'stop_time' => time()]);
  253. }
  254. /**
  255. * 获取当前拼团数据返回订单编号
  256. * @param $id
  257. * @return array|false|\PDOStatement|string|\think\Model
  258. */
  259. public static function getCurrentPink($id, $uid)
  260. {
  261. $pink = self::where('id', $id)->where('uid', $uid)->find();
  262. if (!$pink) $pink = self::where('k_id', $id)->where('uid', $uid)->find();
  263. return StoreOrder::where('id', $pink['order_id_key'])->value('order_id');
  264. }
  265. public static function systemPage($where)
  266. {
  267. $model = new self;
  268. $model = $model->alias('p');
  269. $model = $model->field('p.*,c.title');
  270. if ($where['data'] !== '') {
  271. list($startTime, $endTime) = explode(' - ', $where['data']);
  272. $model = $model->where('p.add_time', '>', strtotime($startTime));
  273. $model = $model->where('p.add_time', '<', strtotime($endTime));
  274. }
  275. if ($where['status']) $model = $model->where('p.status', $where['status']);
  276. $model = $model->where('p.k_id', 0);
  277. $model = $model->order('p.id desc');
  278. $model = $model->join('StoreCombination c', 'c.id=p.cid');
  279. return self::page($model, function ($item) use ($where) {
  280. $item['count_people'] = bcadd(self::where('k_id', $item['id'])->count(), 1, 0);
  281. }, $where);
  282. }
  283. public static function isPinkBe($data, $id)
  284. {
  285. $data['id'] = $id;
  286. $count = self::where($data)->count();
  287. if ($count) return $count;
  288. $data['k_id'] = $id;
  289. $count = self::where($data)->count();
  290. if ($count) return $count;
  291. else return 0;
  292. }
  293. public static function isPinkStatus($pinkId)
  294. {
  295. if (!$pinkId) return false;
  296. $stopTime = self::where('id', $pinkId)->value('stop_time');
  297. if ($stopTime < time()) return true; //拼团结束
  298. else return false; //拼团未结束
  299. }
  300. /**
  301. * 判断拼团结束 后的状态
  302. * @param $pinkId
  303. * @return bool
  304. */
  305. public static function isSetPinkOver($pinkId)
  306. {
  307. $people = self::where('id', $pinkId)->value('people');
  308. $stopTime = self::where('id', $pinkId)->value('stop_time');
  309. if ($stopTime < time()) {
  310. $countNum = self::getPinkPeople($pinkId, $people);
  311. if ($countNum) return false; //拼团失败
  312. else return true; //拼团成功
  313. } else return true;
  314. }
  315. /**
  316. * 拼团退款
  317. * @param $id
  318. * @return bool
  319. */
  320. public static function setRefundPink($oid)
  321. {
  322. $res = true;
  323. $order = StoreOrder::where('id', $oid)->find();
  324. if ($order['pink_id']) $id = $order['pink_id'];
  325. else return $res;
  326. $count = self::where('id', $id)->where('uid', $order['uid'])->find(); //正在拼团 团长
  327. $countY = self::where('k_id', $id)->where('uid', $order['uid'])->find(); //正在拼团 团员
  328. if (!$count && !$countY) return $res;
  329. if ($count) { //团长
  330. //判断团内是否还有其他人 如果有 团长为第二个进团的人
  331. $kCount = self::where('k_id', $id)->order('add_time asc')->find();
  332. if ($kCount) {
  333. $res11 = self::where('k_id', $id)->update(['k_id' => $kCount['id']]);
  334. $res12 = self::where('id', $kCount['id'])->update(['stop_time' => $count['add_time'] + 86400, 'k_id' => 0]);
  335. $res1 = $res11 && $res12;
  336. $res2 = self::where('id', $id)->update(['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $kCount['id'], 'status' => 3]);
  337. } else {
  338. $res1 = true;
  339. $res2 = self::where('id', $id)->update(['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $id, 'status' => 3]);
  340. }
  341. //修改结束时间为前一秒 团长ID为0
  342. $res = $res1 && $res2;
  343. } else if ($countY) { //团员
  344. $res = self::where('id', $countY['id'])->update(['stop_time' => time() - 1, 'k_id' => 0, 'is_refund' => $id, 'status' => 3]);
  345. }
  346. return $res;
  347. }
  348. /**
  349. * 拼团人数完成时,判断全部人都是未退款状态
  350. * @param $pinkIds
  351. * @return bool
  352. */
  353. public static function setPinkStatus($pinkIds)
  354. {
  355. $orderPink = self::where('id', 'IN', $pinkIds)->where('is_refund', 1)->count();
  356. if (!$orderPink) return true;
  357. else return false;
  358. }
  359. /**
  360. * 创建拼团
  361. * @param $order
  362. * @return mixed
  363. */
  364. public static function createPink($order)
  365. {
  366. $order = StoreOrder::tidyOrder($order, true)->toArray();
  367. $openid = WechatUser::uidToOpenid($order['uid'], 'openid');
  368. $routineOpenid = WechatUser::uidToOpenid($order['uid'], 'routine_openid');
  369. $product = StoreCombination::where('id', $order['combination_id'])->field('effective_time,title')->find();
  370. if ($product) {
  371. $stop_time = bcmul($product->effective_time, 3600, 0);
  372. if ($order['pink_id']) { //拼团存在
  373. $pink_id = $order['pink_id'];
  374. $pink_one = self::where('id', $order['pink_id'])->find();
  375. if ($pink_one['kid']) {
  376. $pink_id = $pink_one['k_id'];
  377. $pink_one = self::where('id', $pink_id)->find();
  378. }
  379. $res = false;
  380. $pink['uid'] = $order['uid']; //用户id
  381. if (self::isPinkBe($pink, $order['pink_id'])) return false;
  382. $pink['order_id'] = $order['order_id']; //订单id 生成
  383. $pink['order_id_key'] = $order['id']; //订单id 数据库id
  384. $pink['total_num'] = $order['total_num']; //购买个数
  385. $pink['total_price'] = $order['pay_price']; //总金额
  386. $pink['k_id'] = $order['pink_id']; //拼团id
  387. foreach ($order['cartInfo'] as $v) {
  388. $pink['cid'] = $v['combination_id']; //拼团产品id
  389. $pink['pid'] = $v['product_id']; //产品id
  390. $pink['people'] = StoreCombination::where('id', $v['combination_id'])->value('people'); //几人拼团
  391. $pink['price'] = $v['productInfo']['price']; //单价
  392. $pink['stop_time'] = 0; //结束时间
  393. $pink['add_time'] = time(); //开团时间
  394. $res = self::create($pink)->toArray();
  395. }
  396. if ($openid) { //公众号模板消息
  397. $urlWeChat = Route::buildUrl('order/detail/' . $order['order_id'])->suffix('')->domain(true)->build();
  398. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
  399. 'first' => '亲,您已成功参与拼团',
  400. 'keyword1' => $order['order_id'],
  401. 'keyword2' => $product->title,
  402. 'remark' => '点击查看订单详情'
  403. ], $urlWeChat);
  404. } else if ($routineOpenid) {
  405. $nickname = User::where('uid', self::where('id', $pink['k_id'])->value('uid'))->value('nickname');
  406. RoutineTemplate::sendPinkSuccess(
  407. $order['uid'],
  408. $product->title,
  409. $nickname,
  410. $pink['add_time'],
  411. $pink['people'],
  412. '/pages/order_details/index?order_id=' . $pink['order_id']
  413. );
  414. }
  415. //处理拼团完成
  416. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = self::getPinkMemberAndPinkK($pink);
  417. if ($pinkT['status'] == 1) {
  418. if (!$count) //组团完成
  419. self::PinkComplete($uidAll, $idAll, $pink['uid'], $pinkT);
  420. else
  421. self::PinkFail($pinkAll, $pinkT, 0);
  422. }
  423. if ($res) {
  424. $cache_pink = Cache::get(md5('store_pink_' . $pink_id));
  425. $number = 1;
  426. if ($cache_pink) {
  427. $number = bcadd($cache_pink['now_people'], $number, 0);
  428. }
  429. //设置团内人数
  430. Cache::set(md5('store_pink_' . $pink_id), ['people' => $pink_one['people'], 'now_people' => $number], bcsub($pink_one['stop_time'], time(), 0));
  431. return true;
  432. } else return false;
  433. } else {
  434. $res = false;
  435. $pink['uid'] = $order['uid']; //用户id
  436. $pink['order_id'] = $order['order_id']; //订单id 生成
  437. $pink['order_id_key'] = $order['id']; //订单id 数据库id
  438. $pink['total_num'] = $order['total_num']; //购买个数
  439. $pink['total_price'] = $order['pay_price']; //总金额
  440. $pink['k_id'] = 0; //拼团id
  441. foreach ($order['cartInfo'] as $v) {
  442. $pink['cid'] = $v['combination_id']; //拼团产品id
  443. $pink['pid'] = $v['product_id']; //产品id
  444. $pink['people'] = StoreCombination::where('id', $v['combination_id'])->value('people'); //几人拼团
  445. $pink['price'] = $v['productInfo']['price']; //单价
  446. $pink['stop_time'] = bcadd(time(), $stop_time, 0); //结束时间
  447. $pink['add_time'] = time(); //开团时间
  448. $res1 = self::create($pink)->toArray();
  449. $res2 = StoreOrder::where('id', $order['id'])->update(['pink_id' => $res1['id']]);
  450. $res = $res1 && $res2;
  451. }
  452. // 开团成功发送模板消息
  453. if ($openid && $order['is_channel'] != 1) { //公众号模板消息
  454. $urlWeChat = Route::buildUrl('/order/detail/' . $pink['order_id'])->suffix('')->domain(true)->build();
  455. WechatTemplateService::sendTemplate($openid, WechatTemplateService::OPEN_PINK_SUCCESS, [
  456. 'first' => '您好,您已成功开团!赶紧与小伙伴们分享吧!!!',
  457. 'keyword1' => $product->title,
  458. 'keyword2' => $pink['total_price'],
  459. 'keyword3' => $pink['people'],
  460. 'remark' => '点击查看订单详情'
  461. ], $urlWeChat);
  462. } else if ($routineOpenid && $order['is_channel'] == 1) {
  463. $nickname = User::where('uid', $order['uid'])->value('nickname');
  464. RoutineTemplate::sendPinkSuccess(
  465. $order['uid'],
  466. $product->title,
  467. $nickname,
  468. $pink['add_time'],
  469. $pink['people'],
  470. '/pages/order_details/index?order_id=' . $pink['order_id']
  471. );
  472. }
  473. if ($res) {
  474. //存入缓存
  475. Cache::set(md5('store_pink_' . $res1['id']), ['people' => $pink['people'], 'now_people' => 1], $stop_time);
  476. return true;
  477. } else return false;
  478. }
  479. } else {
  480. errlog('拼团支付成功读取产品数据失败订单号:' . $order['order_id']);
  481. }
  482. }
  483. /*
  484. * 获取一条今天正在拼团的人的头像和名称
  485. * */
  486. public static function getPinkSecondOne()
  487. {
  488. $addTime = mt_rand(time() - 30000, time());
  489. return self::where('p.add_time', '>', $addTime)->alias('p')->where('p.status', 1)->join('User u', 'u.uid=p.uid')->field('u.nickname,u.avatar as src')->find();
  490. }
  491. /**
  492. * 拼团成功后给团长返佣金
  493. * @param int $id
  494. * @return bool
  495. */
  496. // public static function setRakeBackColonel($id = 0){
  497. // if(!$id) return false;
  498. // $pinkRakeBack = self::where('id',$id)->field('people,price,uid,id')->find()->toArray();
  499. // $countPrice = bcmul($pinkRakeBack['people'],$pinkRakeBack['price'],2);
  500. // if(bcsub((float)$countPrice,0,2) <= 0) return true;
  501. // $rakeBack = (sys_config('rake_back_colonel') ?: 0)/100;
  502. // if($rakeBack <= 0) return true;
  503. // $rakeBackPrice = bcmul($countPrice,$rakeBack,2);
  504. // if($rakeBackPrice <= 0) return true;
  505. // $mark = '拼团成功,奖励佣金'.floatval($rakeBackPrice);
  506. // self::beginTrans();
  507. // $res1 = UserBill::income('获得拼团佣金',$pinkRakeBack['uid'],'now_money','colonel',$rakeBackPrice,$id,0,$mark);
  508. // $res2 = User::bcInc($pinkRakeBack['uid'],'now_money',$rakeBackPrice,'uid');
  509. // $res = $res1 && $res2;
  510. // self::checkTrans($res);
  511. // return $res;
  512. // }
  513. /*
  514. * 拼团完成更改数据写入内容
  515. * @param array $uidAll 当前拼团uid
  516. * @param array $idAll 当前拼团pink_id
  517. * @param array $pinkT 团长信息
  518. * @return int
  519. * */
  520. public static function PinkComplete($uidAll, $idAll, $uid, $pinkT)
  521. {
  522. $pinkBool = 6;
  523. try {
  524. if (self::setPinkStatus($idAll)) {
  525. self::setPinkStopTime($idAll);
  526. if (in_array($uid, $uidAll)) {
  527. if (self::isTpl($uidAll, $pinkT['id'])) self::orderPinkAfter($uidAll, $pinkT['id']);
  528. $pinkBool = 1;
  529. } else $pinkBool = 3;
  530. }
  531. return $pinkBool;
  532. } catch (\Exception $e) {
  533. self::setErrorInfo($e->getMessage());
  534. return $pinkBool;
  535. }
  536. }
  537. /*
  538. * 拼团失败 退款
  539. * @param array $pinkAll 拼团数据,不包括团长
  540. * @param array $pinkT 团长数据
  541. * @param int $pinkBool
  542. * @param boolen $isRunErr 是否返回错误信息
  543. * @param boolen $isIds 是否返回记录所有拼团id
  544. * @return int| boolen
  545. * */
  546. public static function PinkFail($pinkAll, $pinkT, $pinkBool, $isRunErr = true, $isIds = false)
  547. {
  548. self::startTrans();
  549. $pinkIds = [];
  550. try {
  551. if ($pinkT['stop_time'] < time()) { //拼团时间超时 退款
  552. $pinkBool = -1;
  553. array_push($pinkAll, $pinkT);
  554. foreach ($pinkAll as $v) {
  555. if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($v['order_id_key']), $v['uid'], '拼团时间超时') && self::isTpl($v['uid'], $pinkT['id'])) {
  556. if ($isIds) array_push($pinkIds, $v['id']);
  557. self::orderPinkAfterNo($pinkT['uid'], $pinkT['id']);
  558. } else {
  559. if ($isRunErr) return $pinkBool;
  560. }
  561. }
  562. }
  563. self::commit();
  564. if ($isIds) return $pinkIds;
  565. return $pinkBool;
  566. } catch (\Exception $e) {
  567. self::rollback();
  568. return $pinkBool;
  569. }
  570. }
  571. /*
  572. * 获取参团人和团长和拼团总人数
  573. * @param array $pink
  574. * @return array
  575. * */
  576. public static function getPinkMemberAndPinkK($pink)
  577. {
  578. //查找拼团团员和团长
  579. if ($pink['k_id']) {
  580. $pinkAll = self::getPinkMember($pink['k_id']);
  581. $pinkT = self::getPinkUserOne($pink['k_id']);
  582. } else {
  583. $pinkAll = self::getPinkMember($pink['id']);
  584. $pinkT = $pink;
  585. }
  586. $pinkT = $pinkT->hidden(['order_id', 'total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
  587. $pinkAll = $pinkAll->hidden(['total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
  588. $count = (int)bcadd(count($pinkAll), 1, 0);
  589. $count = (int)bcsub($pinkT['people'], $count, 0);
  590. $idAll = [];
  591. $uidAll = [];
  592. //收集拼团用户id和拼团id
  593. foreach ($pinkAll as $k => $v) {
  594. $idAll[$k] = $v['id'];
  595. $uidAll[$k] = $v['uid'];
  596. }
  597. $idAll[] = $pinkT['id'];
  598. $uidAll[] = $pinkT['uid'];
  599. return [$pinkAll, $pinkT, $count, $idAll, $uidAll];
  600. }
  601. /*
  602. * 取消开团
  603. * @param int $uid 用户id
  604. * @param int $pink_id 团长id
  605. * @return boolean
  606. * */
  607. public static function removePink($uid, $cid, $pink_id, $nextPinkT = null)
  608. {
  609. $pinkT = self::where('uid', $uid)
  610. ->where('id', $pink_id)
  611. ->where('cid', $cid)
  612. ->where('k_id', 0)
  613. ->where('is_refund', 0)
  614. ->where('status', 1)
  615. ->where('stop_time', '>', time())
  616. ->find();
  617. if (!$pinkT) return self::setErrorInfo('未查到拼团信息,无法取消');
  618. self::startTrans();
  619. try {
  620. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = self::getPinkMemberAndPinkK($pinkT);
  621. if (count($pinkAll)) {
  622. if (self::getPinkPeople($pink_id, $pinkT['people'])) {
  623. //拼团未完成,拼团有成员取消开团取 紧跟团长后拼团的人
  624. if (isset($pinkAll[0])) $nextPinkT = $pinkAll[0];
  625. } else {
  626. //拼团完成
  627. self::PinkComplete($uidAll, $idAll, $uid, $pinkT);
  628. return self::setErrorInfo(['status' => 200, 'msg' => '拼团已完成,无法取消']);
  629. }
  630. }
  631. //取消开团
  632. if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($pinkT['order_id_key']), $pinkT['uid'], '拼团取消开团') && self::isTpl($pinkT['uid'], $pinkT['id'])) {
  633. $formId = RoutineFormId::getFormIdOne($uid);
  634. if ($formId) RoutineFormId::delFormIdOne($formId);
  635. self::orderPinkAfterNo($pinkT['uid'], $pinkT['id'], $formId, '拼团取消开团', true);
  636. $cache_pink = Cache::get(md5('store_pink_' . $pinkT['id']));
  637. if ($cache_pink) {
  638. $number = bcsub($cache_pink['now_people'], 1, 0);
  639. $cache_pink['now_people'] = $number;
  640. Cache::set(md5('store_pink_' . $pinkT['id']), $cache_pink, bcsub($pinkT['stop_time'], time(), 0));
  641. }
  642. } else
  643. return self::setErrorInfo(['status' => 200, 'msg' => StoreOrder::getErrorInfo()], true);
  644. //当前团有人的时候
  645. if (is_array($nextPinkT)) {
  646. self::where('id', $nextPinkT['id'])->update(['k_id' => 0, 'status' => 1, 'stop_time' => $pinkT['stop_time']]);
  647. self::where('k_id', $pinkT['id'])->update(['k_id' => $nextPinkT['id']]);
  648. StoreOrder::where('order_id', $nextPinkT['order_id'])->update(['pink_id' => $nextPinkT['id']]);
  649. }
  650. self::commitTrans();
  651. return true;
  652. } catch (\Exception $e) {
  653. return self::setErrorInfo($e->getLine() . ':' . $e->getMessage() . ':' . $e->getFile(), true);
  654. }
  655. }
  656. /**
  657. * 获取用户拼团到结束时间后还是拼团中的拼团
  658. * @return mixed
  659. */
  660. public static function pinkListEnd()
  661. {
  662. $model = new self;
  663. $model = $model->field('id,people'); //开团编号
  664. $model = $model->where('stop_time', '<=', time()); //小于当前时间
  665. $model = $model->where('status', 1); //进行中的拼团
  666. $model = $model->where('k_id', 0); //团长
  667. $model = $model->where('is_refund', 0); //未退款
  668. return $model->select();
  669. }
  670. /**
  671. * 拼团成功
  672. * @param array $pinkRegimental 成功的团长编号
  673. * @return bool
  674. * @throws \Exception
  675. */
  676. public static function successPinkEdit(array $pinkRegimental)
  677. {
  678. if (!count($pinkRegimental)) return true;
  679. foreach ($pinkRegimental as $key => &$item) {
  680. $pinkList = self::where('k_id', $item)->column('id', 'id');
  681. $pinkList[] = $item;
  682. $pinkList = implode(',', $pinkList);
  683. self::setPinkStatus($pinkList); //修改完成状态
  684. self::setPinkStopTime($pinkList); //修改结束时间
  685. $pinkUidList = self::isTplPink($pinkList); //获取未发送模板消息的用户
  686. if (count($pinkUidList)) self::sendPinkTemplateMessageSuccess($pinkUidList, $item); //发送模板消息
  687. }
  688. return true;
  689. }
  690. /**
  691. * 拼团失败
  692. * @param array $pinkRegimental 失败的团长编号
  693. * @return bool
  694. * @throws \think\db\exception\DataNotFoundException
  695. * @throws \think\db\exception\ModelNotFoundException
  696. * @throws \think\exception\DbException
  697. */
  698. public static function failPinkEdit(array $pinkRegimental)
  699. {
  700. if (!count($pinkRegimental)) return true;
  701. foreach ($pinkRegimental as $key => &$item) {
  702. $pinkList = self::where('k_id', $item)->column('id', 'id');
  703. $pinkList[] = $item;
  704. $pinkList = implode(',', $pinkList);
  705. self::refundPink($pinkList); //申请退款
  706. self::pinkStopStatus($pinkList); //修改状态
  707. $pinkUidList = self::isTplPink($pinkList); //获取未发送模板消息的用户
  708. if (count($pinkUidList)) self::sendPinkTemplateMessageError($pinkUidList, $item); //发送模板消息
  709. }
  710. return true;
  711. }
  712. /**
  713. * 发送模板消息 失败
  714. * @param array $pinkUidList 拼团用户编号
  715. * @param $pink 团长编号
  716. * @throws \think\db\exception\DataNotFoundException
  717. * @throws \think\db\exception\ModelNotFoundException
  718. * @throws \think\exception\DbException
  719. */
  720. public static function sendPinkTemplateMessageError(array $pinkUidList, $pink)
  721. {
  722. foreach ($pinkUidList as $key => &$item) {
  723. $openid = WechatUser::uidToOpenid($item, 'openid');
  724. $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
  725. $store = self::alias('p')->where('p.id|p.k_id', $pink)->field('c.*')->where('p.uid', $item)->join('store_combination c', 'c.id = p.cid')->find();
  726. $pink = self::where('id|k_id', $pink)->where('uid', $item)->find();
  727. if ($openid) {
  728. //公众号模板消息
  729. $urlWeChat = Route::buildUrl('order/detail/' . $pink->order_id)->suffix('')->domain(true)->build();
  730. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
  731. 'first' => '亲,您的拼团失败',
  732. 'keyword1' => $store->title,
  733. 'keyword2' => $pink->price,
  734. 'keyword3' => $pink->price,
  735. 'remark' => '点击查看订单详情'
  736. ], $urlWeChat);
  737. } else if ($routineOpenid) {
  738. //小程序模板消息
  739. RoutineTemplate::sendPinkFail(
  740. $item,
  741. $store->title,
  742. $pink->people,
  743. '亲,您拼团失败,自动为您申请退款,退款金额为:' . $pink->price,
  744. '/pages/order_details/index?order_id=' . $pink->order_id
  745. );
  746. }
  747. }
  748. self::where('uid', 'IN', implode(',', $pinkUidList))->where('id|k_id', $pink)->update(['is_tpl' => 1]);
  749. }
  750. /**
  751. * 拼团失败 申请退款
  752. * @param $pinkList
  753. * @return bool
  754. */
  755. public static function refundPink($pinkList)
  756. {
  757. $refundPinkList = self::where('id', 'IN', $pinkList)->column('order_id,uid', 'id');
  758. if (!count($refundPinkList)) return true;
  759. foreach ($refundPinkList as $key => &$item) {
  760. StoreOrder::orderApplyRefund($item['order_id'], $item['uid'], '拼团时间超时'); //申请退款
  761. }
  762. }
  763. /**
  764. * 拼团结束修改状态
  765. * @param $pinkList
  766. * @return StorePink
  767. */
  768. public static function pinkStopStatus($pinkList)
  769. {
  770. return self::where('id', 'IN', $pinkList)->update(['status' => 3]);
  771. }
  772. /**
  773. * 获取未发送模板消息的用户
  774. * @param $pinkList 拼团编号
  775. * @return array
  776. */
  777. public static function isTplPink($pinkList)
  778. {
  779. return self::where('id', 'IN', $pinkList)->where('is_tpl', 0)->column('uid', 'uid');
  780. }
  781. /**
  782. * 发送模板消息 成功
  783. * @param array $pinkUidList 拼团用户编号
  784. * @param $pink 团长编号
  785. * @throws \Exception
  786. */
  787. public static function sendPinkTemplateMessageSuccess(array $pinkUidList, $pink)
  788. {
  789. foreach ($pinkUidList as $key => &$item) {
  790. $openid = WechatUser::uidToOpenid($item, 'openid');
  791. $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
  792. $nickname = WechatUser::uidToOpenid(self::where('id', $pink)->value('uid'), 'nickname');
  793. if ($openid) {
  794. //公众号模板消息
  795. $firstWeChat = '亲,您的拼团已经完成了';
  796. $keyword1WeChat = self::where('id|k_id', $pink)->where('uid', $item)->value('order_id');
  797. $keyword2WeChat = self::alias('p')->where('p.id|p.k_id', $pink)->where('p.uid', $item)->join('store_combination c', 'c.id=p.cid')->value('c.title');
  798. $remarkWeChat = '点击查看订单详情';
  799. $urlWeChat = Route::buildUrl('order/detail/' . $keyword1WeChat)->suffix('')->domain(true)->build();
  800. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
  801. 'first' => $firstWeChat,
  802. 'keyword1' => $keyword1WeChat,
  803. 'keyword2' => $keyword2WeChat,
  804. 'remark' => $remarkWeChat
  805. ], $urlWeChat);
  806. } else if ($routineOpenid) {
  807. //小程序模板消息
  808. $pinkInfo = self::where('k.id|k.k_id', $pink)->alias('k')->where('k.uid', $item)
  809. ->field(['k.order_id', 'k.people', 'k.add_time', 'c.title'])
  810. ->join('store_combination c', 'c.id = k.cid')->find();
  811. RoutineTemplate::sendPinkSuccess(
  812. $item,
  813. $pinkInfo['title'] ?? '',
  814. $nickname,
  815. $pinkInfo['add_time'] ?? 0,
  816. $pinkInfo['people'] ?? 0,
  817. '/pages/order_details/index?order_id=' . $pinkInfo['order_id'] ?? ''
  818. );
  819. }
  820. }
  821. self::where('uid', 'IN', implode(',', $pinkUidList))->where('id|k_id', $pink)->update(['is_tpl' => 1]);
  822. }
  823. /**
  824. * 修改到期的拼团状态
  825. * @return bool
  826. * @throws \think\db\exception\DataNotFoundException
  827. * @throws \think\db\exception\ModelNotFoundException
  828. * @throws \think\exception\DbException
  829. */
  830. public static function statusPink()
  831. {
  832. $pinkListEnd = self::pinkListEnd();
  833. if (!$pinkListEnd) return true;
  834. $pinkListEnd = $pinkListEnd->toArray();
  835. $failPinkList = []; //拼团失败
  836. $successPinkList = []; //拼团失败
  837. foreach ($pinkListEnd as $key => &$value) {
  838. $countPeople = (int)bcadd(self::where('k_id', $value['id'])->count(), 1, 0);
  839. if ($countPeople < $value['people'])
  840. $failPinkList[] = $value['id'];
  841. else
  842. $successPinkList[] = $value['id'];
  843. }
  844. $success = self::successPinkEdit($successPinkList);
  845. $error = self::failPinkEdit($failPinkList);
  846. $res = $success && $error;
  847. if (!$res)
  848. throw new \Exception('拼团订单取消失败!');
  849. }
  850. }