StorePink.php 14 KB

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