StorePink.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 $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. if ($order['pink_id']) {//拼团存在
  377. $res = false;
  378. $pink['uid'] = $order['uid'];//用户id
  379. if (self::isPinkBe($pink, $order['pink_id'])) return false;
  380. $pink['order_id'] = $order['order_id'];//订单id 生成
  381. $pink['order_id_key'] = $order['id'];//订单id 数据库id
  382. $pink['total_num'] = $order['total_num'];//购买个数
  383. $pink['total_price'] = $order['pay_price'];//总金额
  384. $pink['k_id'] = $order['pink_id'];//拼团id
  385. foreach ($order['cartInfo'] as $v) {
  386. $pink['cid'] = $v['combination_id'];//拼团产品id
  387. $pink['pid'] = $v['product_id'];//产品id
  388. $pink['people'] = StoreCombination::where('id', $v['combination_id'])->value('people');//几人拼团
  389. $pink['price'] = $v['productInfo']['price'];//单价
  390. $pink['stop_time'] = 0;//结束时间
  391. $pink['add_time'] = time();//开团时间
  392. $res = self::create($pink)->toArray();
  393. }
  394. if ($openid) { //公众号模板消息
  395. $urlWeChat = Route::buildUrl('order/detail/' . $order['order_id'])->suffix('')->domain(true)->build();
  396. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
  397. 'first' => '亲,您已成功参与拼团',
  398. 'keyword1' => $order['order_id'],
  399. 'keyword2' => $product->title,
  400. 'remark' => '点击查看订单详情'
  401. ], $urlWeChat);
  402. } else if ($routineOpenid) {
  403. $nickname = User::where('uid', self::where('id', $pink['k_id'])->value('uid'))->value('nickname');
  404. RoutineTemplate::sendPinkSuccess(
  405. $order['uid'], $product->title,
  406. $nickname,
  407. $pink['add_time'],
  408. $pink['people'],
  409. '/pages/order_details/index?order_id=' . $pink['order_id']
  410. );
  411. }
  412. //处理拼团完成
  413. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = self::getPinkMemberAndPinkK($pink);
  414. if ($pinkT['status'] == 1) {
  415. if (!$count)//组团完成
  416. self::PinkComplete($uidAll, $idAll, $pink['uid'], $pinkT);
  417. else
  418. self::PinkFail($pinkAll, $pinkT, 0);
  419. }
  420. if ($res) return true;
  421. else return false;
  422. } else {
  423. $res = false;
  424. $pink['uid'] = $order['uid'];//用户id
  425. $pink['order_id'] = $order['order_id'];//订单id 生成
  426. $pink['order_id_key'] = $order['id'];//订单id 数据库id
  427. $pink['total_num'] = $order['total_num'];//购买个数
  428. $pink['total_price'] = $order['pay_price'];//总金额
  429. $pink['k_id'] = 0;//拼团id
  430. foreach ($order['cartInfo'] as $v) {
  431. $pink['cid'] = $v['combination_id'];//拼团产品id
  432. $pink['pid'] = $v['product_id'];//产品id
  433. $pink['people'] = StoreCombination::where('id', $v['combination_id'])->value('people');//几人拼团
  434. $pink['price'] = $v['productInfo']['price'];//单价
  435. $pink['stop_time'] = bcadd(time(), bcmul($product->effective_time, 3600, 0), 0);//结束时间
  436. $pink['add_time'] = time();//开团时间
  437. $res1 = self::create($pink)->toArray();
  438. $res2 = StoreOrder::where('id', $order['id'])->update(['pink_id' => $res1['id']]);
  439. $res = $res1 && $res2;
  440. }
  441. // 开团成功发送模板消息
  442. if ($openid && $order['is_channel'] != 1) { //公众号模板消息
  443. $urlWeChat = Route::buildUrl('/order/detail/' . $pink['order_id'])->suffix('')->domain(true)->build();
  444. WechatTemplateService::sendTemplate($openid, WechatTemplateService::OPEN_PINK_SUCCESS, [
  445. 'first' => '您好,您已成功开团!赶紧与小伙伴们分享吧!!!',
  446. 'keyword1' => $product->title,
  447. 'keyword2' => $pink['total_price'],
  448. 'keyword3' => $pink['people'],
  449. 'remark' => '点击查看订单详情'
  450. ], $urlWeChat);
  451. } else if ($routineOpenid && $order['is_channel'] == 1) {
  452. $nickname = User::where('uid', $order['uid'])->value('nickname');
  453. RoutineTemplate::sendPinkSuccess(
  454. $order['uid'], $product->title,
  455. $nickname,
  456. $pink['add_time'],
  457. $pink['people'],
  458. '/pages/order_details/index?order_id=' . $pink['order_id']
  459. );
  460. }
  461. if ($res) return true;
  462. else return false;
  463. }
  464. } else {
  465. Log::error('拼团支付成功读取产品数据失败订单号:' . $order['order_id']);
  466. }
  467. }
  468. /*
  469. * 获取一条今天正在拼团的人的头像和名称
  470. * */
  471. public static function getPinkSecondOne()
  472. {
  473. $addTime = mt_rand(time() - 30000, time());
  474. 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();
  475. }
  476. /**
  477. * 拼团成功后给团长返佣金
  478. * @param int $id
  479. * @return bool
  480. */
  481. // public static function setRakeBackColonel($id = 0){
  482. // if(!$id) return false;
  483. // $pinkRakeBack = self::where('id',$id)->field('people,price,uid,id')->find()->toArray();
  484. // $countPrice = bcmul($pinkRakeBack['people'],$pinkRakeBack['price'],2);
  485. // if(bcsub((float)$countPrice,0,2) <= 0) return true;
  486. // $rakeBack = (sys_config('rake_back_colonel') ?: 0)/100;
  487. // if($rakeBack <= 0) return true;
  488. // $rakeBackPrice = bcmul($countPrice,$rakeBack,2);
  489. // if($rakeBackPrice <= 0) return true;
  490. // $mark = '拼团成功,奖励佣金'.floatval($rakeBackPrice);
  491. // self::beginTrans();
  492. // $res1 = UserBill::income('获得拼团佣金',$pinkRakeBack['uid'],'now_money','colonel',$rakeBackPrice,$id,0,$mark);
  493. // $res2 = User::bcInc($pinkRakeBack['uid'],'now_money',$rakeBackPrice,'uid');
  494. // $res = $res1 && $res2;
  495. // self::checkTrans($res);
  496. // return $res;
  497. // }
  498. /*
  499. * 拼团完成更改数据写入内容
  500. * @param array $uidAll 当前拼团uid
  501. * @param array $idAll 当前拼团pink_id
  502. * @param array $pinkT 团长信息
  503. * @return int
  504. * */
  505. public static function PinkComplete($uidAll, $idAll, $uid, $pinkT)
  506. {
  507. $pinkBool = 6;
  508. try {
  509. if (self::setPinkStatus($idAll)) {
  510. self::setPinkStopTime($idAll);
  511. if (in_array($uid, $uidAll)) {
  512. if (self::isTpl($uidAll, $pinkT['id'])) self::orderPinkAfter($uidAll, $pinkT['id']);
  513. $pinkBool = 1;
  514. } else $pinkBool = 3;
  515. }
  516. return $pinkBool;
  517. } catch (\Exception $e) {
  518. self::setErrorInfo($e->getMessage());
  519. return $pinkBool;
  520. }
  521. }
  522. /*
  523. * 拼团失败 退款
  524. * @param array $pinkAll 拼团数据,不包括团长
  525. * @param array $pinkT 团长数据
  526. * @param int $pinkBool
  527. * @param boolen $isRunErr 是否返回错误信息
  528. * @param boolen $isIds 是否返回记录所有拼团id
  529. * @return int| boolen
  530. * */
  531. public static function PinkFail($pinkAll, $pinkT, $pinkBool, $isRunErr = true, $isIds = false)
  532. {
  533. self::startTrans();
  534. $pinkIds = [];
  535. try {
  536. if ($pinkT['stop_time'] < time()) {//拼团时间超时 退款
  537. $pinkBool = -1;
  538. array_push($pinkAll, $pinkT);
  539. foreach ($pinkAll as $v) {
  540. if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($v['order_id_key']), $v['uid'], '拼团时间超时') && self::isTpl($v['uid'], $pinkT['id'])) {
  541. if ($isIds) array_push($pinkIds, $v['id']);
  542. self::orderPinkAfterNo($pinkT['uid'], $pinkT['id']);
  543. } else {
  544. if ($isRunErr) return $pinkBool;
  545. }
  546. }
  547. }
  548. self::commit();
  549. if ($isIds) return $pinkIds;
  550. return $pinkBool;
  551. } catch (\Exception $e) {
  552. self::rollback();
  553. return $pinkBool;
  554. }
  555. }
  556. /*
  557. * 获取参团人和团长和拼团总人数
  558. * @param array $pink
  559. * @return array
  560. * */
  561. public static function getPinkMemberAndPinkK($pink)
  562. {
  563. //查找拼团团员和团长
  564. if ($pink['k_id']) {
  565. $pinkAll = self::getPinkMember($pink['k_id']);
  566. $pinkT = self::getPinkUserOne($pink['k_id']);
  567. } else {
  568. $pinkAll = self::getPinkMember($pink['id']);
  569. $pinkT = $pink;
  570. }
  571. $pinkT = $pinkT->hidden(['order_id', 'total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
  572. $pinkAll = $pinkAll->hidden(['total_price', 'cid', 'pid', 'add_time', 'k_id', 'is_tpl', 'is_refund'])->toArray();
  573. $count = (int)bcadd(count($pinkAll), 1, 0);
  574. $count = (int)bcsub($pinkT['people'], $count, 0);
  575. $idAll = [];
  576. $uidAll = [];
  577. //收集拼团用户id和拼团id
  578. foreach ($pinkAll as $k => $v) {
  579. $idAll[$k] = $v['id'];
  580. $uidAll[$k] = $v['uid'];
  581. }
  582. $idAll[] = $pinkT['id'];
  583. $uidAll[] = $pinkT['uid'];
  584. return [$pinkAll, $pinkT, $count, $idAll, $uidAll];
  585. }
  586. /*
  587. * 取消开团
  588. * @param int $uid 用户id
  589. * @param int $pink_id 团长id
  590. * @return boolean
  591. * */
  592. public static function removePink($uid, $cid, $pink_id, $nextPinkT = null)
  593. {
  594. $pinkT = self::where('uid', $uid)
  595. ->where('id', $pink_id)
  596. ->where('cid', $cid)
  597. ->where('k_id', 0)
  598. ->where('is_refund', 0)
  599. ->where('status', 1)
  600. ->where('stop_time', '>', time())
  601. ->find();
  602. if (!$pinkT) return self::setErrorInfo('未查到拼团信息,无法取消');
  603. self::startTrans();
  604. try {
  605. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = self::getPinkMemberAndPinkK($pinkT);
  606. if (count($pinkAll)) {
  607. if (self::getPinkPeople($pink_id, $pinkT['people'])) {
  608. //拼团未完成,拼团有成员取消开团取 紧跟团长后拼团的人
  609. if (isset($pinkAll[0])) $nextPinkT = $pinkAll[0];
  610. } else {
  611. //拼团完成
  612. self::PinkComplete($uidAll, $idAll, $uid, $pinkT);
  613. return self::setErrorInfo(['status' => 200, 'msg' => '拼团已完成,无法取消']);
  614. }
  615. }
  616. //取消开团
  617. if (StoreOrder::orderApplyRefund(StoreOrder::getPinkOrderId($pinkT['order_id_key']), $pinkT['uid'], '拼团取消开团') && self::isTpl($pinkT['uid'], $pinkT['id'])) {
  618. $formId = RoutineFormId::getFormIdOne($uid);
  619. if ($formId) RoutineFormId::delFormIdOne($formId);
  620. self::orderPinkAfterNo($pinkT['uid'], $pinkT['id'], $formId, '拼团取消开团', true);
  621. } else
  622. return self::setErrorInfo(['status' => 200, 'msg' => StoreOrder::getErrorInfo()], true);
  623. //当前团有人的时候
  624. if (is_array($nextPinkT)) {
  625. self::where('id', $nextPinkT['id'])->update(['k_id' => 0, 'status' => 1, 'stop_time' => $pinkT['stop_time']]);
  626. self::where('k_id', $pinkT['id'])->update(['k_id' => $nextPinkT['id']]);
  627. StoreOrder::where('order_id', $nextPinkT['order_id'])->update(['pink_id' => $nextPinkT['id']]);
  628. }
  629. self::commitTrans();
  630. return true;
  631. } catch (\Exception $e) {
  632. return self::setErrorInfo($e->getLine() . ':' . $e->getMessage() . ':' . $e->getFile(), true);
  633. }
  634. }
  635. /**
  636. * 获取用户拼团到结束时间后还是拼团中的拼团
  637. * @return mixed
  638. */
  639. public static function pinkListEnd()
  640. {
  641. $model = new self;
  642. $model = $model->field('id,people');//开团编号
  643. $model = $model->where('stop_time', '<=', time());//小于当前时间
  644. $model = $model->where('status', 1);//进行中的拼团
  645. $model = $model->where('k_id', 0);//团长
  646. $model = $model->where('is_refund', 0);//未退款
  647. return $model->select();
  648. }
  649. /**
  650. * 拼团成功
  651. * @param array $pinkRegimental 成功的团长编号
  652. * @return bool
  653. * @throws \Exception
  654. */
  655. public static function successPinkEdit(array $pinkRegimental)
  656. {
  657. if (!count($pinkRegimental)) return true;
  658. foreach ($pinkRegimental as $key => &$item) {
  659. $pinkList = self::where('k_id', $item)->column('id', 'id');
  660. $pinkList[] = $item;
  661. $pinkList = implode(',', $pinkList);
  662. self::setPinkStatus($pinkList);//修改完成状态
  663. self::setPinkStopTime($pinkList);//修改结束时间
  664. $pinkUidList = self::isTplPink($pinkList);//获取未发送模板消息的用户
  665. if (count($pinkUidList)) self::sendPinkTemplateMessageSuccess($pinkUidList, $item);//发送模板消息
  666. }
  667. return true;
  668. }
  669. /**
  670. * 拼团失败
  671. * @param array $pinkRegimental 失败的团长编号
  672. * @return bool
  673. * @throws \think\db\exception\DataNotFoundException
  674. * @throws \think\db\exception\ModelNotFoundException
  675. * @throws \think\exception\DbException
  676. */
  677. public static function failPinkEdit(array $pinkRegimental)
  678. {
  679. if (!count($pinkRegimental)) return true;
  680. foreach ($pinkRegimental as $key => &$item) {
  681. $pinkList = self::where('k_id', $item)->column('id', 'id');
  682. $pinkList[] = $item;
  683. $pinkList = implode(',', $pinkList);
  684. self::refundPink($pinkList);//申请退款
  685. self::pinkStopStatus($pinkList);//修改状态
  686. $pinkUidList = self::isTplPink($pinkList);//获取未发送模板消息的用户
  687. if (count($pinkUidList)) self::sendPinkTemplateMessageError($pinkUidList, $item);//发送模板消息
  688. }
  689. return true;
  690. }
  691. /**
  692. * 发送模板消息 失败
  693. * @param array $pinkUidList 拼团用户编号
  694. * @param $pink 团长编号
  695. * @throws \think\db\exception\DataNotFoundException
  696. * @throws \think\db\exception\ModelNotFoundException
  697. * @throws \think\exception\DbException
  698. */
  699. public static function sendPinkTemplateMessageError(array $pinkUidList, $pink)
  700. {
  701. foreach ($pinkUidList as $key => &$item) {
  702. $openid = WechatUser::uidToOpenid($item, 'openid');
  703. $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
  704. $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();
  705. $pink = self::where('id|k_id', $pink)->where('uid', $item)->find();
  706. if ($openid) {
  707. //公众号模板消息
  708. $urlWeChat = Route::buildUrl('order/detail/' . $pink->order_id)->suffix('')->domain(true)->build();
  709. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_LOSE, [
  710. 'first' => '亲,您的拼团失败',
  711. 'keyword1' => $store->title,
  712. 'keyword2' => $pink->price,
  713. 'keyword3' => $pink->price,
  714. 'remark' => '点击查看订单详情'
  715. ], $urlWeChat);
  716. } else if ($routineOpenid) {
  717. //小程序模板消息
  718. RoutineTemplate::sendPinkFail(
  719. $item,
  720. $store->title,
  721. $pink->people,
  722. '亲,您拼团失败,自动为您申请退款,退款金额为:' . $pink->price,
  723. '/pages/order_details/index?order_id=' . $pink->order_id
  724. );
  725. }
  726. }
  727. self::where('uid', 'IN', implode(',', $pinkUidList))->where('id|k_id', $pink)->update(['is_tpl' => 1]);
  728. }
  729. /**
  730. * 拼团失败 申请退款
  731. * @param $pinkList
  732. * @return bool
  733. */
  734. public static function refundPink($pinkList)
  735. {
  736. $refundPinkList = self::where('id', 'IN', $pinkList)->column('order_id,uid', 'id');
  737. if (!count($refundPinkList)) return true;
  738. foreach ($refundPinkList as $key => &$item) {
  739. StoreOrder::orderApplyRefund($item['order_id'], $item['uid'], '拼团时间超时');//申请退款
  740. }
  741. }
  742. /**
  743. * 拼团结束修改状态
  744. * @param $pinkList
  745. * @return StorePink
  746. */
  747. public static function pinkStopStatus($pinkList)
  748. {
  749. return self::where('id', 'IN', $pinkList)->update(['status' => 3]);
  750. }
  751. /**
  752. * 获取未发送模板消息的用户
  753. * @param $pinkList 拼团编号
  754. * @return array
  755. */
  756. public static function isTplPink($pinkList)
  757. {
  758. return self::where('id', 'IN', $pinkList)->where('is_tpl', 0)->column('uid', 'uid');
  759. }
  760. /**
  761. * 发送模板消息 成功
  762. * @param array $pinkUidList 拼团用户编号
  763. * @param $pink 团长编号
  764. * @throws \Exception
  765. */
  766. public static function sendPinkTemplateMessageSuccess(array $pinkUidList, $pink)
  767. {
  768. foreach ($pinkUidList as $key => &$item) {
  769. $openid = WechatUser::uidToOpenid($item, 'openid');
  770. $routineOpenid = WechatUser::uidToOpenid($item, 'routine_openid');
  771. $nickname = WechatUser::uidToOpenid(self::where('id', $pink)->value('uid'), 'nickname');
  772. if ($openid) {
  773. //公众号模板消息
  774. $firstWeChat = '亲,您的拼团已经完成了';
  775. $keyword1WeChat = self::where('id|k_id', $pink)->where('uid', $item)->value('order_id');
  776. $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');
  777. $remarkWeChat = '点击查看订单详情';
  778. $urlWeChat = Route::buildUrl('order/detail/' . $keyword1WeChat)->suffix('')->domain(true)->build();
  779. WechatTemplateService::sendTemplate($openid, WechatTemplateService::ORDER_USER_GROUPS_SUCCESS, [
  780. 'first' => $firstWeChat,
  781. 'keyword1' => $keyword1WeChat,
  782. 'keyword2' => $keyword2WeChat,
  783. 'remark' => $remarkWeChat
  784. ], $urlWeChat);
  785. } else if ($routineOpenid) {
  786. //小程序模板消息
  787. $pinkInfo = self::where('k.id|k.k_id', $pink)->alias('k')->where('k.uid', $item)
  788. ->field(['k.order_id', 'k.people', 'k.add_time', 'c.title'])
  789. ->join('store_combination c', 'c.id = k.cid')->find();
  790. RoutineTemplate::sendPinkSuccess(
  791. $item, $pinkInfo['title'] ?? '',
  792. $nickname,
  793. $pinkInfo['add_time'] ?? 0,
  794. $pinkInfo['people'] ?? 0,
  795. '/pages/order_details/index?order_id=' . $pinkInfo['order_id'] ?? ''
  796. );
  797. }
  798. }
  799. self::where('uid', 'IN', implode(',', $pinkUidList))->where('id|k_id', $pink)->update(['is_tpl' => 1]);
  800. }
  801. /**
  802. * 修改到期的拼团状态
  803. * @return bool
  804. * @throws \think\db\exception\DataNotFoundException
  805. * @throws \think\db\exception\ModelNotFoundException
  806. * @throws \think\exception\DbException
  807. */
  808. public static function statusPink()
  809. {
  810. $pinkListEnd = self::pinkListEnd();
  811. if (!$pinkListEnd) return true;
  812. $pinkListEnd = $pinkListEnd->toArray();
  813. $failPinkList = [];//拼团失败
  814. $successPinkList = [];//拼团失败
  815. foreach ($pinkListEnd as $key => &$value) {
  816. $countPeople = (int)bcadd(self::where('k_id', $value['id'])->count(), 1, 0);
  817. if ($countPeople == $value['people'])
  818. $successPinkList[] = $value['id'];
  819. else
  820. $failPinkList[] = $value['id'];
  821. }
  822. $success = self::successPinkEdit($successPinkList);
  823. $error = self::failPinkEdit($failPinkList);
  824. $res = $success && $error;
  825. if (!$res)
  826. throw new \Exception('拼团订单取消失败!');
  827. }
  828. }