StorePink.php 35 KB

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