StoreBargainUserHelp.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\models\store;
  3. use app\models\user\User;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. /**
  7. * TODO 砍价帮砍Model
  8. * Class StoreBargainUserHelp
  9. * @package app\models\store
  10. */
  11. class StoreBargainUserHelp extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'store_bargain_user_help';
  23. use ModelTrait;
  24. /**
  25. * TODO 获取砍价帮
  26. * @param $bargainUserTableId
  27. * @param int $page
  28. * @param int $limit
  29. * @return array
  30. * @throws \think\Exception
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. */
  35. public static function getList($bargainUserTableId,$page = 1,$limit = 15){
  36. if(!$bargainUserTableId) return [];
  37. 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');
  38. 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');
  39. if($list){
  40. foreach ($list as $key=>&$value){
  41. $userInfo = User::getUserInfo($value['uid'],'nickname,avatar');
  42. if($userInfo){
  43. $value['nickname'] = $userInfo['nickname'];
  44. $value['avatar'] = $userInfo['avatar'];
  45. }else{
  46. $value['nickname'] = '此用户已失效';
  47. $value['avatar'] = '';
  48. }
  49. unset($value['uid']);
  50. unset($value['id']);
  51. }
  52. }
  53. return array_values($list);
  54. }
  55. /**
  56. * TODO 帮忙砍价
  57. * @param int $bargainId
  58. * @param int $bargainUserUid
  59. * @param int $uid
  60. * @return bool
  61. * @throws \think\Exception
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. */
  66. public static function setBargainUserHelp($bargainId = 0,$bargainUserUid = 0,$uid = 0){
  67. if(!$bargainId || !$bargainUserUid || !$uid || !StoreBargain::validBargain($bargainId) || !StoreBargainUser::be(['bargain_id'=>$bargainId,'uid'=>$bargainUserUid,'status'=>1,'is_del'=>0])) return false;
  68. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);//TODO 获取 用户参与砍价表编号
  69. $priceSection = StoreBargain::getBargainMaxMinPrice($bargainId); //TODO 获取随机砍掉的价格区间
  70. $coverPrice = StoreBargainUser::getBargainUserDiffPriceFloat($bargainUserTableId);//TODO 用户可以砍掉的金额 好友砍价之前获取可以砍价金额
  71. $alreadyPrice= StoreBargainUser::getBargainUserPrice($bargainUserTableId);//TODO 用户已经砍掉的价格
  72. $surplusPrice = (float)bcsub($coverPrice,$alreadyPrice,2);//TODO 用户剩余要砍掉的价格
  73. if(0.00 === (float)bcsub($surplusPrice,0,2)) return false;
  74. $data['uid'] = $uid;
  75. $data['bargain_id'] = $bargainId;
  76. $data['bargain_user_id'] = $bargainUserTableId;
  77. $data['price'] = self::randomFloat($priceSection['bargain_min_price'],$priceSection['bargain_max_price']);
  78. $data['add_time'] = time();
  79. if(0.00 === (float)$data['price']) $data['price'] = self::randomFloat($priceSection['bargain_min_price'],$priceSection['bargain_max_price']);
  80. if($data['price'] > $surplusPrice) $data['price'] = $surplusPrice;
  81. $price = bcadd($alreadyPrice,$data['price'],2);
  82. $bargainUserData['price'] = $price;
  83. self::beginTrans();
  84. $res1 = StoreBargainUser::setBargainUserPrice($bargainUserTableId,$bargainUserData);
  85. $res2 = self::create($data);
  86. $res = $res1 && $res2;
  87. self::checkTrans($res);
  88. return $res;
  89. }
  90. /**
  91. * TODO 获取俩个数之间的随机数
  92. * @param int $min $min 最小数
  93. * @param int $max $max 最大数
  94. * @return string
  95. */
  96. public static function randomFloat($min = 0,$max = 1){
  97. $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
  98. return sprintf("%.2f",$num);
  99. }
  100. /**
  101. * TODO 判断用户是否还可以砍价
  102. * @param int $bargainId $bargainId 砍价产品编号
  103. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  104. * @param int $bargainUserHelpUid $bargainUserUid 当前用户编号
  105. * @return bool
  106. * @throws \think\Exception
  107. */
  108. public static function isBargainUserHelpCount($bargainId = 0,$bargainUserUid = 0,$bargainUserHelpUid = 0){
  109. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);
  110. $bargainNum = StoreBargain::getBargainNum($bargainId);//TODO 获取每个人可以砍价几次
  111. $count = self::where('bargain_id',$bargainId)->where('bargain_user_id',$bargainUserTableId)->where('uid',$bargainUserHelpUid)->count();//TODO 获取当前用户砍价了几次
  112. if($count < $bargainNum) return true;
  113. else return false;
  114. }
  115. /**
  116. * TODO 获取砍价帮总人数
  117. * @param int $bargainId $bargainId 砍价产品编号
  118. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  119. * @return int|string
  120. * @throws \think\Exception
  121. */
  122. public static function getBargainUserHelpPeopleCount($bargainId = 0,$bargainUserUid = 0){
  123. if(!$bargainId || !$bargainUserUid) return 0;
  124. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);//TODO 获取用户参与砍价表编号
  125. if($bargainUserTableId) return self::where('bargain_user_id',$bargainUserTableId)->where('bargain_id',$bargainId)->count();
  126. else return 0;
  127. }
  128. /**
  129. * TODO 获取用户还剩余的砍价金额
  130. * @param int $bargainId $bargainId 砍价产品编号
  131. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  132. * @return float
  133. * @throws \think\db\exception\DataNotFoundException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. * @throws \think\exception\DbException
  136. */
  137. public static function getSurplusPrice($bargainId = 0,$bargainUserUid = 0){
  138. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);// TODO 获取用户参与砍价表编号
  139. $coverPrice = StoreBargainUser::getBargainUserDiffPriceFloat($bargainUserTableId);//TODO 获取用户可以砍掉的金额 好友砍价之后获取砍价金额
  140. $alreadyPrice= StoreBargainUser::getBargainUserPrice($bargainUserTableId);//TODO 用户已经砍掉的价格 好友砍价之后获取用户已经砍掉的价格
  141. $surplusPrice = (float)bcsub($coverPrice,$alreadyPrice,2);//TODO 用户剩余要砍掉的价格
  142. return $surplusPrice;
  143. }
  144. /**
  145. * TODO 获取砍价进度条
  146. * @param int $bargainId $bargainId 砍价产品编号
  147. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  148. * @return int|string
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. * @throws \think\exception\DbException
  152. */
  153. public static function getSurplusPricePercent($bargainId = 0,$bargainUserUid = 0){
  154. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid); // TODO 获取用户参与砍价表编号 计算进度条
  155. $coverPrice = StoreBargainUser::getBargainUserDiffPriceFloat($bargainUserTableId);//TODO 用户可以砍掉的金额 计算进度条
  156. $alreadyPrice = StoreBargainUser::getBargainUserPrice($bargainUserTableId);//TODO 用户已经砍掉的价格 计算进度条
  157. if($alreadyPrice) return (int)bcmul(bcdiv($alreadyPrice,$coverPrice,2),100,0);
  158. else return 0;
  159. }
  160. /**
  161. * TODO 获取用户砍掉的金额
  162. * @param int $bargainId $bargainId 砍价产品编号
  163. * @param int $bargainUserTableId $bargainUserTableId 用户参与砍价表编号
  164. * @param int $uid $uid 帮忙砍价人编号
  165. * @param string $field
  166. * @return bool|mixed
  167. */
  168. public static function getBargainUserBargainPrice($bargainId = 0,$bargainUserTableId = 0,$uid = 0,$field = 'price'){
  169. if(!$bargainId || !$bargainUserTableId || !$uid) return false;
  170. return self::where('uid',$uid)->where('bargain_id',$bargainId)->where('bargain_user_id',$bargainUserTableId)->value($field);
  171. }
  172. /**
  173. * TODO 获取用的昵称和头像
  174. * @param int $uid
  175. * @return array
  176. */
  177. public static function getBargainUserHelpUserInfo($uid = 0){
  178. if(!$uid) return [];
  179. $userInfo = User::where('uid',$uid)->column('nickname,avatar','uid');
  180. return $userInfo;
  181. }
  182. }