StorePink.php 37 KB

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