123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace app\models\store;
- use app\models\user\User;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- class StoreBargainUserHelp extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'store_bargain_user_help';
- use ModelTrait;
-
- public static function getList($bargainUserTableId,$page = 1,$limit = 15){
- if(!$bargainUserTableId) return [];
- if($page) $list = self::where('bargain_user_id',$bargainUserTableId)->order('add_time desc')->page($page,$limit)->column("uid,price,from_unixtime(add_time,'%Y-%m-%d %H:%i:%s') as add_time",'id');
- else $list = self::where('bargain_user_id',$bargainUserTableId)->order('add_time desc')->column("uid,price,from_unixtime(add_time,'%Y-%m-%d %H:%i:%s') as add_time",'id');
- if($list){
- foreach ($list as $key=>&$value){
- $userInfo = User::getUserInfo($value['uid'],'nickname,avatar');
- if($userInfo){
- $value['nickname'] = $userInfo['nickname'];
- $value['avatar'] = $userInfo['avatar'];
- }else{
- $value['nickname'] = '此用户已失效';
- $value['avatar'] = '';
- }
- unset($value['uid']);
- unset($value['id']);
- }
- }
- return array_values($list);
- }
-
- public static function setBargainUserHelp($bargainId = 0,$bargainUserUid = 0,$uid = 0){
- if(!$bargainId || !$bargainUserUid || !$uid || !StoreBargain::validBargain($bargainId) || !StoreBargainUser::be(['bargain_id'=>$bargainId,'uid'=>$bargainUserUid,'status'=>1,'is_del'=>0])) return false;
- $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);
- $priceSection = StoreBargain::getBargainMaxMinPrice($bargainId);
- $coverPrice = StoreBargainUser::getBargainUserDiffPriceFloat($bargainUserTableId);
- $alreadyPrice= StoreBargainUser::getBargainUserPrice($bargainUserTableId);
- $surplusPrice = (float)bcsub($coverPrice,$alreadyPrice,2);
- if(0.00 === (float)bcsub($surplusPrice,0,2)) return false;
- $data['uid'] = $uid;
- $data['bargain_id'] = $bargainId;
- $data['bargain_user_id'] = $bargainUserTableId;
- $data['price'] = self::randomFloat($priceSection['bargain_min_price'],$priceSection['bargain_max_price']);
- $data['add_time'] = time();
- if(0.00 === (float)$data['price']) $data['price'] = self::randomFloat($priceSection['bargain_min_price'],$priceSection['bargain_max_price']);
- if($data['price'] > $surplusPrice) $data['price'] = $surplusPrice;
- $price = bcadd($alreadyPrice,$data['price'],2);
- $bargainUserData['price'] = $price;
- self::beginTrans();
- $res1 = StoreBargainUser::setBargainUserPrice($bargainUserTableId,$bargainUserData);
- $res2 = self::create($data);
- $res = $res1 && $res2;
- self::checkTrans($res);
- return $res;
- }
-
- public static function randomFloat($min = 0,$max = 1){
- $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
- return sprintf("%.2f",$num);
- }
-
- public static function isBargainUserHelpCount($bargainId = 0,$bargainUserUid = 0,$bargainUserHelpUid = 0){
- $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);
- $bargainNum = StoreBargain::getBargainNum($bargainId);
- $count = self::where('bargain_id',$bargainId)->where('bargain_user_id',$bargainUserTableId)->where('uid',$bargainUserHelpUid)->count();
- if($count < $bargainNum) return true;
- else return false;
- }
-
- public static function getBargainUserHelpPeopleCount($bargainId = 0,$bargainUserUid = 0){
- if(!$bargainId || !$bargainUserUid) return 0;
- $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);
- if($bargainUserTableId) return self::where('bargain_user_id',$bargainUserTableId)->where('bargain_id',$bargainId)->count();
- else return 0;
- }
-
- public static function getSurplusPrice($bargainId = 0,$bargainUserUid = 0){
- $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);
- $coverPrice = StoreBargainUser::getBargainUserDiffPriceFloat($bargainUserTableId);
- $alreadyPrice= StoreBargainUser::getBargainUserPrice($bargainUserTableId);
- $surplusPrice = (float)bcsub($coverPrice,$alreadyPrice,2);
- return $surplusPrice;
- }
-
- public static function getSurplusPricePercent($bargainId = 0,$bargainUserUid = 0){
- $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);
- $coverPrice = StoreBargainUser::getBargainUserDiffPriceFloat($bargainUserTableId);
- $alreadyPrice = StoreBargainUser::getBargainUserPrice($bargainUserTableId);
- if($alreadyPrice) return (int)bcmul(bcdiv($alreadyPrice,$coverPrice,2),100,0);
- else return 0;
- }
-
- public static function getBargainUserBargainPrice($bargainId = 0,$bargainUserTableId = 0,$uid = 0,$field = 'price'){
- if(!$bargainId || !$bargainUserTableId || !$uid) return false;
- return self::where('uid',$uid)->where('bargain_id',$bargainId)->where('bargain_user_id',$bargainUserTableId)->value($field);
- }
-
- public static function getBargainUserHelpUserInfo($uid = 0){
- if(!$uid) return [];
- $userInfo = User::where('uid',$uid)->column('nickname,avatar','uid');
- return $userInfo;
- }
- }
|