StorePink.php 37 KB

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