123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace app\models\store;
- use app\models\user\User;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- class StoreBargainUser extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'store_bargain_user';
- use ModelTrait;
-
- public static function getUserList($bargain = array(),$limit = 10){
- if(count($bargain) < 1) return [];
- foreach ($bargain as $k=>$v){
- if(is_array($v)){
- $uid = self::getUserIdList($v['id']);
- if(count($uid) > 0) {
- $userInfo = User::where('uid','IN',implode(',',$uid))->limit($limit)->column('avatar','uid');
- $bargain[$k]['userInfo'] = $userInfo;
- $bargain[$k]['userInfoCount'] = count($userInfo);
- }
- else {
- $bargain[$k]['userInfo'] = [];
- $bargain[$k]['userInfoCount'] = 0;
- }
- }else{
- $uid = self::getUserIdList($bargain['id']);
- if(count($uid) > 0) $bargain['userInfo'] = User::where('uid','IN',implode(',',$uid))->column('avatar','uid');
- else $bargain['userInfo'] = [];
- }
- }
- return $bargain;
- }
-
- public static function getUserIdList($bargainId = 0,$status = 1){
- if(!$bargainId) return [];
- return self::where('bargain_id',$bargainId)->where('status',$status)->column('uid','id');
- }
-
- public static function setBargain($bargainId = 0,$bargainUserUid = 0){
- if(!$bargainId || !$bargainUserUid || !StoreBargain::validBargain($bargainId) || self::be(['bargain_id'=>$bargainId,'uid'=>$bargainUserUid,'status'=>1,'is_del'=>0])) return false;
- $data['bargain_id'] = $bargainId;
- $data['uid'] = $bargainUserUid;
- $data['bargain_price_min'] = StoreBargain::where('id',$bargainId)->value('min_price');
- $data['bargain_price'] = StoreBargain::where('id',$bargainId)->value('price');
- $data['price'] = 0;
- $data['status'] = 1;
- $data['is_del'] = 0;
- $data['add_time'] = time();
- return self::create($data);
- }
-
- public static function isBargainUser($bargainId = 0,$bargainUserUid = 0){
- if(!$bargainId || !$bargainUserUid || !StoreBargain::validBargain($bargainId)) return false;
- return self::where('bargain_id',$bargainId)->where('uid',$bargainUserUid)->where('is_del',0)->count();
- }
-
- public static function getBargainUserPrice($id = 0){
- return (float)self::where('id',$id)->value('price');
- }
-
- public static function getBargainUserStatusEnd($id = 0){
- return (int)self::where('id',$id)->value('status');
- }
-
- public static function getBargainUserDiffPriceFloat($id){
- $price = self::where('id',$id)->field('bargain_price,bargain_price_min')->find();
- return (float)bcsub($price['bargain_price'],$price['bargain_price_min'],2);
- }
-
- public static function getBargainUserTableId($bargainId = 0,$bargainUserUid = 0){
- return self::where('bargain_id',$bargainId)->where('uid',$bargainUserUid)->where('is_del',0)->value('id');
- }
-
- public static function setBargainUserPrice($id, $price = array()){
- if(!$id) return false;
- return self::edit($price,$id,'id');
- }
-
- public static function getBargainUserAll($bargainUserUid = 0,$page = 0,$limit = 20){
- if(!$bargainUserUid) return [];
- $model = new self;
- $model = $model->alias('u');
- $model = $model->field('u.uid,u.is_del,u.bargain_price - u.price as residue_price,u.id,u.bargain_id,u.bargain_price,u.bargain_price_min,u.price,u.status,b.title,b.image,b.stop_time as datatime');
- $model = $model->join('StoreBargain b','b.id=u.bargain_id');
- $model = $model->where('u.uid',$bargainUserUid);
- $model = $model->where('u.is_del',0);
- $model = $model->order('u.id desc');
- if($page) $model = $model->page($page,$limit);
- $list = $model->select();
- if($list) return $list->toArray();
- else return [];
- }
-
- public static function setBargainUserStatus($bargainId = 0, $bargainUserUid = 0){
- if(!$bargainId || !$bargainUserUid) return false;
- $bargainUserTableId = self::getBargainUserTableId($bargainId,$bargainUserUid);
- if(!$bargainUserTableId) return false;
- $count = self::where('id',$bargainUserTableId)->where('status',1)->count();
- if(!$count) return false;
- $userPrice = (float)self::where('id',$bargainUserTableId)->where('status',1)->value('price');
- $price = self::getBargainUserDiffPriceFloat($bargainUserTableId);
- if(bcsub($price,$userPrice,2) > 0) return false;
- return self::where('id',$bargainUserTableId)->where('status',1)->update(['status'=>3]);
- }
-
- public static function startBargainUserStatus()
- {
- $currentBargain = self::getBargainUserCurrent(0);
- $bargainProduct = StoreBargain::validBargainNumber();
- $closeBargain = [];
- foreach ($currentBargain as $key=>&$item) { if(!in_array($item,$bargainProduct)) { $closeBargain[] = $item; } }
- if(count($closeBargain)) return self::where('status',1)->where('bargain_id','IN',implode(',',$closeBargain))->update(['status'=>2]);
- return true;
- }
-
- public static function editBargainUserStatus($uid){
- $currentBargain = self::getBargainUserCurrent($uid);
- $bargainProduct = StoreBargain::validBargainNumber();
- $closeBargain = [];
- foreach ($currentBargain as $key=>&$item) { if(!in_array($item,$bargainProduct)) { $closeBargain[] = $item; } }
- if(count($closeBargain)) return self::where('uid',$uid)->where('status',1)->where('bargain_id','IN',implode(',',$closeBargain))->update(['status'=>2]);
- return true;
- }
-
- public static function getBargainUserCurrent($uid){
- if($uid) return self::where('uid',$uid)->where('is_del',0)->where('status',1)->column('bargain_id');
- else return self::where('is_del',0)->where('status',1)->column('bargain_id');
- }
-
- public static function getBargainUserStatusSuccess(){
- $bargainUser = self::where('status',3)->order('id desc')->field('uid,bargain_price_min,bargain_id')->select();
- if($bargainUser) {
- $bargainUser = $bargainUser->toArray();
- foreach ($bargainUser as $k=>$v){
- $bargainUser[$k]['info'] = User::where('uid',$v['uid'])->value('nickname').'砍价成功了'.$v['bargain_price_min'].'砍到了'.StoreBargain::where('id',$v['bargain_id'])->value('title');
- }
- }
- else{
- $bargainUser[]['info'] = '砍价上线了,快邀请您的好友来砍价';
- }
- return $bargainUser;
- }
-
- public static function getBargainUserStatus($bargainId,$bargainUserUid){
- if(!$bargainId || !$bargainUserUid) return false;
-
- return self::where('bargain_id',$bargainId)->where('uid',$bargainUserUid)->order('add_time DESC')->value('status');
- }
-
- public static function setUserBargain($bargainId = 0,$uid = 0,$status = 1){
- if(!$bargainId || !$uid) return [];
- $bargainIdUserTableId = self::where('bargain_id',$bargainId)->where('uid',$uid)->where('status',$status)->value('id');
- return $bargainIdUserTableId;
- }
- }
|