StorePink.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\routine\model\store;
  8. use app\routine\model\store\StoreCombination;
  9. use app\routine\model\user\User;
  10. use app\routine\model\user\WechatUser;
  11. use basic\ModelBasic;
  12. use service\WechatTemplateService;
  13. use think\Url;
  14. use traits\ModelTrait;
  15. /**
  16. * 拼团Model
  17. * Class StorePink
  18. * @package app\wap\model\store
  19. */
  20. class StorePink extends ModelBasic
  21. {
  22. use ModelTrait;
  23. /**
  24. * 获取一条拼团数据
  25. * @param $id
  26. * @return mixed
  27. */
  28. public static function getPinkUserOne($id){
  29. $model = new self();
  30. $model = $model->alias('p');
  31. $model = $model->field('p.*,u.nickname,u.avatar');
  32. $model = $model->where('id',$id);
  33. $model = $model->join('__USER__ u','u.uid = p.uid');
  34. $list = $model->find();
  35. if($list) return $list->toArray();
  36. else return [];
  37. }
  38. /**
  39. * 获取拼团的团员
  40. * @param $id
  41. * @return mixed
  42. */
  43. public static function getPinkMember($id){
  44. $model = new self();
  45. $model = $model->alias('p');
  46. $model = $model->field('p.*,u.nickname,u.avatar');
  47. $model = $model->where('k_id',$id);
  48. $model = $model->where('is_refund',0);
  49. $model = $model->join('__USER__ u','u.uid = p.uid');
  50. $model = $model->order('id asc');
  51. $list = $model->select();
  52. if($list) return $list->toArray();
  53. else return [];
  54. }
  55. /**
  56. * 设置结束时间
  57. * @param $idAll
  58. * @return $this
  59. */
  60. public static function setPinkStopTime($idAll){
  61. $model = new self();
  62. $model = $model->where('id','IN',$idAll);
  63. return $model->update(['stop_time'=>time(),'status'=>2]);
  64. }
  65. /**
  66. * 获取正在拼团的数据 团长
  67. * @return mixed
  68. */
  69. public static function getPinkAll($cid){
  70. $model = new self();
  71. $model = $model->alias('p');
  72. $model = $model->field('p.*,u.nickname,u.avatar');
  73. $model = $model->where('stop_time','GT',time());
  74. $model = $model->where('cid',$cid);
  75. $model = $model->where('k_id',0);
  76. $model = $model->where('is_refund',0);
  77. $model = $model->order('add_time desc');
  78. $model = $model->join('__USER__ u','u.uid = p.uid');
  79. $list = $model->select();
  80. if($list) return $list->toArray();
  81. else return [];
  82. }
  83. /**
  84. * 获取还差几人
  85. */
  86. public static function getPinkPeople($kid,$people){
  87. $model = new self();
  88. $model = $model->where('k_id',$kid)->where('is_refund',0);
  89. $count = bcadd($model->count(),1,0);
  90. return bcsub($people,$count,0);
  91. }
  92. /**
  93. * 判断订单是否在当前的拼团中
  94. * @param $orderId
  95. * @param $kid
  96. * @return bool
  97. */
  98. public static function getOrderIdAndPink($orderId,$kid){
  99. $model = new self();
  100. $pink = $model->where('k_id',$kid)->whereOr('id',$kid)->column('order_id');
  101. if(in_array($orderId,$pink))return true;
  102. else return false;
  103. }
  104. /**
  105. * 判断用户是否在团内
  106. * @param $id
  107. * @return int|string
  108. */
  109. public static function getIsPinkUid($id = 0,$uid = 0){
  110. $pinkT = self::where('id',$id)->where('uid',$uid)->where('is_refund',0)->count();
  111. $pink = self::whereOr('k_id',$id)->where('uid',$uid)->where('is_refund',0)->count();
  112. if($pinkT) return true;
  113. if($pink) return true;
  114. else return false;
  115. }
  116. /**
  117. * 判断是否发送模板消息 0 未发送 1已发送
  118. * @param $uidAll
  119. * @return int|string
  120. */
  121. public static function isTpl($uidAll,$pid){
  122. if(is_array($uidAll)){
  123. $countK = self::where('uid','IN',implode(',',$uidAll))->where('is_tpl',0)->where('id',$pid)->count();
  124. $count = self::where('uid','IN',implode(',',$uidAll))->where('is_tpl',0)->where('k_id',$pid)->count();
  125. }
  126. else {
  127. $countK = self::where('uid',$uidAll)->where('is_tpl',0)->where('id',$pid)->count();
  128. $count = self::where('uid',$uidAll)->where('is_tpl',0)->where('k_id',$pid)->count();
  129. }
  130. return bcadd($countK,$count,0);
  131. }
  132. /**
  133. * 拼团成功提示模板消息
  134. * @param $uidAll
  135. * @param $pid
  136. */
  137. public static function orderPinkAfter($uidAll,$pid){
  138. foreach ($uidAll as $v){
  139. $openid = WechatUser::uidToOpenid($v);
  140. WechatTemplateService::sendTemplate($openid,WechatTemplateService::ORDER_USER_GROUPS_SUCCESS,[
  141. 'first'=>'亲,您的拼团已经完成了',
  142. 'keyword1'=> self::where('id',$pid)->whereOr('k_id',$pid)->where('uid',$v)->value('order_id'),
  143. 'keyword2'=> self::alias('p')->where('p.id',$pid)->whereOr('p.k_id',$pid)->where('p.uid',$v)->join('__STORE_COMBINATION__ c','c.id=p.cid')->value('c.title'),
  144. 'remark'=>'点击查看订单详情'
  145. ],Url::build('My/order_pink_after',['id'=>$pid],true,true));
  146. }
  147. self::where('uid','IN',implode(',',$uidAll))->where('id',$pid)->whereOr('k_id',$pid)->update(['is_tpl'=>1]);
  148. }
  149. /**
  150. * 拼团失败发送的模板消息
  151. * @param $uid
  152. * @param $pid
  153. */
  154. public static function orderPinkAfterNo($uid,$pid){
  155. $openid = WechatUser::uidToOpenid($uid);
  156. WechatTemplateService::sendTemplate($openid,WechatTemplateService::ORDER_USER_GROUPS_LOSE,[
  157. 'first'=>'亲,您的拼团失败',
  158. 'keyword1'=> self::alias('p')->where('p.id',$pid)->whereOr('p.k_id',$pid)->where('p.uid',$uid)->join('__STORE_COMBINATION__ c','c.id=p.cid')->value('c.title'),
  159. 'keyword2'=> self::where('id',$pid)->whereOr('k_id',$pid)->where('uid',$uid)->value('price'),
  160. 'keyword3'=> self::alias('p')->where('p.id',$pid)->whereOr('p.k_id',$pid)->where('p.uid',$uid)->join('__STORE_ORDER__ c','c.order_id=p.order_id')->value('c.pay_price'),
  161. 'remark'=>'点击查看订单详情'
  162. ],Url::build('My/order_pink_after',['id'=>$pid],true,true));
  163. self::where('id',$pid)->update(['status'=>3]);
  164. self::where('k_id',$pid)->update(['status'=>3]);
  165. }
  166. /**
  167. * 获取当前拼团数据返回订单编号
  168. * @param $id
  169. * @return array|false|\PDOStatement|string|\think\Model
  170. */
  171. public static function getCurrentPink($id){
  172. $uid = User::getActiveUid();//获取当前登录人的uid
  173. $pink = self::where('id',$id)->where('uid',$uid)->find();
  174. if(!$pink) $pink = self::where('k_id',$id)->where('uid',$uid)->find();
  175. return StoreOrder::where('id',$pink['order_id_key'])->value('order_id');
  176. }
  177. public static function systemPage($where){
  178. $model = new self;
  179. $model = $model->alias('p');
  180. $model = $model->field('p.*,c.title');
  181. if($where['data'] !== ''){
  182. list($startTime,$endTime) = explode(' - ',$where['data']);
  183. $model = $model->where('p.add_time','>',strtotime($startTime));
  184. $model = $model->where('p.add_time','<',strtotime($endTime));
  185. }
  186. if($where['status']) $model = $model->where('p.status',$where['status']);
  187. $model = $model->where('p.k_id',0);
  188. $model = $model->order('p.id desc');
  189. $model = $model->join('StoreCombination c','c.id=p.cid');
  190. return self::page($model,function($item)use($where){
  191. $item['count_people'] = bcadd(self::where('k_id',$item['id'])->count(),1,0);
  192. },$where);
  193. }
  194. public static function isPinkBe($data,$id){
  195. $data['id'] = $id;
  196. $count = self::where($data)->count();
  197. if($count) return $count;
  198. $data['k_id'] = $id;
  199. $count = self::where($data)->count();
  200. if($count) return $count;
  201. else return 0;
  202. }
  203. public static function isPinkStatus($pinkId){
  204. if(!$pinkId) return false;
  205. $stopTime = self::where('id',$pinkId)->value('stop_time');
  206. if($stopTime < time()) return true; //拼团结束
  207. else return false;//拼团未结束
  208. }
  209. /**
  210. * 判断拼团结束 后的状态
  211. * @param $pinkId
  212. * @return bool
  213. */
  214. public static function isSetPinkOver($pinkId){
  215. $people = self::where('id',$pinkId)->value('people');
  216. $stopTime = self::where('id',$pinkId)->value('stop_time');
  217. if($stopTime < time()){
  218. $countNum = self::getPinkPeople($pinkId,$people);
  219. if($countNum) return false;//拼团失败
  220. else return true;//拼团成功
  221. }else return true;
  222. }
  223. /**
  224. * 拼团退款
  225. * @param $id
  226. * @return bool
  227. */
  228. public static function setRefundPink($oid){
  229. $res = true;
  230. $order = StoreOrder::where('id',$oid)->find();
  231. if($order['pink_id']) $id = $order['pink_id'];
  232. else return $res;
  233. $count = self::where('id',$id)->where('uid',$order['uid'])->find();//正在拼团 团长
  234. $countY = self::where('k_id',$id)->where('uid',$order['uid'])->find();//正在拼团 团员
  235. if(!$count && !$countY) return $res;
  236. if($count){//团长
  237. //判断团内是否还有其他人 如果有 团长为第二个进团的人
  238. $kCount = self::where('k_id',$id)->order('add_time asc')->find();
  239. if($kCount){
  240. $res11 = self::where('k_id',$id)->update(['k_id'=>$kCount['id']]);
  241. $res12 = self::where('id',$kCount['id'])->update(['stop_time'=>$count['add_time']+86400,'k_id'=>0]);
  242. $res1 = $res11 && $res12;
  243. $res2 = self::where('id',$id)->update(['stop_time'=>time()-1,'k_id'=>0,'is_refund'=>$kCount['id'],'status'=>3]);
  244. }else{
  245. $res1 = true;
  246. $res2 = self::where('id',$id)->update(['stop_time'=>time()-1,'k_id'=>0,'is_refund'=>$id,'status'=>3]);
  247. }
  248. //修改结束时间为前一秒 团长ID为0
  249. $res = $res1 && $res2;
  250. }else if($countY){//团员
  251. $res = self::where('id',$countY['id'])->update(['stop_time'=>time()-1,'k_id'=>0,'is_refund'=>$id,'status'=>3]);
  252. }
  253. return $res;
  254. }
  255. /**
  256. * 拼团人数完成时,判断全部人都是未退款状态
  257. * @param $pinkIds
  258. * @return bool
  259. */
  260. public static function setPinkStatus($pinkIds){
  261. $orderPink = self::where('id','IN',$pinkIds)->where('is_refund',1)->count();
  262. if(!$orderPink) return true;
  263. else return false;
  264. }
  265. /**
  266. * 创建拼团
  267. * @param $order
  268. * @return mixed
  269. */
  270. public static function createPink($order){
  271. $order = StoreOrder::tidyOrder($order,true)->toArray();
  272. if($order['pink_id']){//拼团存在
  273. $res = false;
  274. $pink['uid'] = $order['uid'];//用户id
  275. if(self::isPinkBe($pink,$order['pink_id'])) return false;
  276. $pink['order_id'] = $order['order_id'];//订单id 生成
  277. $pink['order_id_key'] = $order['id'];//订单id 数据库id
  278. $pink['total_num'] = $order['total_num'];//购买个数
  279. $pink['total_price'] = $order['pay_price'];//总金额
  280. $pink['k_id'] = $order['pink_id'];//拼团id
  281. foreach ($order['cartInfo'] as $v){
  282. $pink['cid'] = $v['combination_id'];//拼团产品id
  283. $pink['pid'] = $v['product_id'];//产品id
  284. $pink['people'] = StoreCombination::where('id',$v['combination_id'])->value('people');//几人拼团
  285. $pink['price'] = $v['productInfo']['price'];//单价
  286. $pink['stop_time'] = 0;//结束时间
  287. $pink['add_time'] = time();//开团时间
  288. $res = StorePink::set($pink)->toArray();
  289. }
  290. if($res) return true;
  291. else return false;
  292. }else{
  293. $res = false;
  294. $pink['uid'] = $order['uid'];//用户id
  295. $pink['order_id'] = $order['order_id'];//订单id 生成
  296. $pink['order_id_key'] = $order['id'];//订单id 数据库id
  297. $pink['total_num'] = $order['total_num'];//购买个数
  298. $pink['total_price'] = $order['pay_price'];//总金额
  299. $pink['k_id'] = 0;//拼团id
  300. foreach ($order['cartInfo'] as $v){
  301. $pink['cid'] = $v['combination_id'];//拼团产品id
  302. $pink['pid'] = $v['product_id'];//产品id
  303. $pink['people'] = StoreCombination::where('id',$v['combination_id'])->value('people');//几人拼团
  304. $pink['price'] = $v['productInfo']['price'];//单价
  305. // $stopTime = StoreCombination::where('id',$v['combination_id'])->value('stop_time');//获取拼团产品结束的时间
  306. // if($stopTime < time()+86400) $pink['stop_time'] = $stopTime;//结束时间
  307. $pink['stop_time'] = time()+86400;//结束时间
  308. $pink['add_time'] = time();//开团时间
  309. $res1 = self::set($pink)->toArray();
  310. $res2 = StoreOrder::where('id',$order['id'])->update(['pink_id'=>$res1['id']]);
  311. $res = $res1 && $res2;
  312. }
  313. if($res) return true;
  314. else return false;
  315. }
  316. }
  317. }