StorePink.php 35 KB

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