StoreBargainUser.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 StoreBargainUser
  9. * @package app\models\store
  10. */
  11. class StoreBargainUser 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';
  23. use ModelTrait;
  24. /**
  25. * TODO 根据砍价产品获取正在参与的用户头像
  26. * @param array $bargain
  27. * @param int $limit
  28. * @return array
  29. */
  30. public static function getUserList($bargain = array(),$limit = 10){
  31. if(count($bargain) < 1) return [];
  32. foreach ($bargain as $k=>$v){
  33. if(is_array($v)){
  34. $uid = self::getUserIdList($v['id']);
  35. if(count($uid) > 0) {
  36. $userInfo = User::where('uid','IN',implode(',',$uid))->limit($limit)->column('avatar','uid');
  37. $bargain[$k]['userInfo'] = $userInfo;
  38. $bargain[$k]['userInfoCount'] = count($userInfo);
  39. }
  40. else {
  41. $bargain[$k]['userInfo'] = [];
  42. $bargain[$k]['userInfoCount'] = 0;
  43. }
  44. }else{
  45. $uid = self::getUserIdList($bargain['id']);
  46. if(count($uid) > 0) $bargain['userInfo'] = User::where('uid','IN',implode(',',$uid))->column('avatar','uid');
  47. else $bargain['userInfo'] = [];
  48. }
  49. }
  50. return $bargain;
  51. }
  52. /**
  53. * TODO 根据砍价产品编号获取正在参与人的编号
  54. * @param int $bargainId $bargainId 砍价产品ID
  55. * @param int $status $status 状态 1 进行中 2 结束失败 3结束成功
  56. * @return array
  57. */
  58. public static function getUserIdList($bargainId = 0,$status = 1){
  59. if(!$bargainId) return [];
  60. return self::where('bargain_id',$bargainId)->where('status',$status)->column('uid','id');
  61. }
  62. /**
  63. * TODO 添加一条砍价记录
  64. * @param int $bargainId $bargainId 砍价产品编号
  65. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  66. * @return bool|object
  67. */
  68. public static function setBargain($bargainId = 0,$bargainUserUid = 0){
  69. if(!$bargainId || !$bargainUserUid || !StoreBargain::validBargain($bargainId) || self::be(['bargain_id'=>$bargainId,'uid'=>$bargainUserUid,'status'=>1,'is_del'=>0])) return false;
  70. $data['bargain_id'] = $bargainId;
  71. $data['uid'] = $bargainUserUid;
  72. $data['bargain_price_min'] = StoreBargain::where('id',$bargainId)->value('min_price');
  73. $data['bargain_price'] = StoreBargain::where('id',$bargainId)->value('price');
  74. $data['price'] = 0;
  75. $data['status'] = 1;
  76. $data['is_del'] = 0;
  77. $data['add_time'] = time();
  78. return self::create($data);
  79. }
  80. /**
  81. * TODO 判断当前人是否已经参与砍价
  82. * @param int $bargainId $bargainId 砍价产品编号
  83. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  84. * @return bool|int|string
  85. * @throws \think\Exception
  86. */
  87. public static function isBargainUser($bargainId = 0,$bargainUserUid = 0){
  88. if(!$bargainId || !$bargainUserUid || !StoreBargain::validBargain($bargainId)) return false;
  89. return self::where('bargain_id',$bargainId)->where('uid',$bargainUserUid)->where('is_del',0)->count();
  90. }
  91. /**
  92. * TODO 获取用户砍掉的价格
  93. * @param int $id $id 用户参与砍价表编号
  94. * @return float
  95. */
  96. public static function getBargainUserPrice($id = 0){
  97. return (float)self::where('id',$id)->value('price');
  98. }
  99. /**
  100. * 获取砍掉用户当前状态
  101. * @param int $id $id 用户参与砍价表编号
  102. * @return int
  103. */
  104. public static function getBargainUserStatusEnd($id = 0){
  105. return (int)self::where('id',$id)->value('status');
  106. }
  107. /**
  108. * TODO 获取用户可以砍掉的价格
  109. * @param $id $id 用户参与砍价表编号
  110. * @return float
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public static function getBargainUserDiffPriceFloat($id){
  116. $price = self::where('id',$id)->field('bargain_price,bargain_price_min')->find();
  117. return (float)bcsub($price['bargain_price'],$price['bargain_price_min'],2);
  118. }
  119. /**
  120. * TODO 获取砍价表ID
  121. * @param int $bargainId $bargainId 砍价产品
  122. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  123. * @param int $status $status 砍价状态 1参与中 2 活动结束参与失败 3活动结束参与成功
  124. * @return mixed
  125. */
  126. public static function getBargainUserTableId($bargainId = 0,$bargainUserUid = 0){
  127. return self::where('bargain_id',$bargainId)->where('uid',$bargainUserUid)->where('is_del',0)->value('id');
  128. }
  129. /**
  130. * TODO 修改砍价价格
  131. * @param $id $id 用户参与砍价表编号
  132. * @param array $price 砍掉的价格
  133. * @return bool
  134. */
  135. public static function setBargainUserPrice($id, $price = array()){
  136. if(!$id) return false;
  137. return self::edit($price,$id,'id');
  138. }
  139. /**
  140. * TODO 获取用户的砍价产品
  141. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  142. * @param int $page
  143. * @param int $limit
  144. * @return array
  145. */
  146. public static function getBargainUserAll($bargainUserUid = 0,$page = 0,$limit = 20){
  147. if(!$bargainUserUid) return [];
  148. $model = new self;
  149. $model = $model->alias('u');
  150. $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');
  151. $model = $model->join('StoreBargain b','b.id=u.bargain_id');
  152. $model = $model->where('u.uid',$bargainUserUid);
  153. $model = $model->where('u.is_del',0);
  154. $model = $model->order('u.id desc');
  155. if($page) $model = $model->page($page,$limit);
  156. $list = $model->select();
  157. if($list) return $list->toArray();
  158. else return [];
  159. }
  160. /**
  161. * TODO 修改用户砍价状态 支付订单
  162. * @param int $bargainId $bargainId 砍价产品
  163. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  164. * @return StoreBargainUser|bool
  165. * @throws \think\Exception
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. * @throws \think\exception\DbException
  169. */
  170. public static function setBargainUserStatus($bargainId = 0, $bargainUserUid = 0){
  171. if(!$bargainId || !$bargainUserUid) return false;
  172. $bargainUserTableId = self::getBargainUserTableId($bargainId,$bargainUserUid);//TODO 获取用户参与砍价表编号 下订单
  173. if(!$bargainUserTableId) return false;
  174. $count = self::where('id',$bargainUserTableId)->where('status',1)->count();
  175. if(!$count) return false;
  176. $userPrice = (float)self::where('id',$bargainUserTableId)->where('status',1)->value('price');
  177. $price = self::getBargainUserDiffPriceFloat($bargainUserTableId);//TODO 用户可以砍掉的金额 下订单
  178. if(bcsub($price,$userPrice,2) > 0) return false;
  179. return self::where('id',$bargainUserTableId)->where('status',1)->update(['status'=>3]);
  180. }
  181. /**
  182. * 批量修改砍价状态为 砍价失败
  183. * @return StoreBargainUser|bool
  184. */
  185. public static function startBargainUserStatus()
  186. {
  187. $currentBargain = self::getBargainUserCurrent(0); //TODO 获取当前用户正在砍价的产品
  188. $bargainProduct = StoreBargain::validBargainNumber(); //TODO 获取正在开启的砍价产品编号
  189. $closeBargain = [];
  190. foreach ($currentBargain as $key=>&$item) { if(!in_array($item,$bargainProduct)) { $closeBargain[] = $item; } }// TODO 获取已经结束的砍价产品
  191. if(count($closeBargain)) return self::where('status',1)->where('bargain_id','IN',implode(',',$closeBargain))->update(['status'=>2]);
  192. return true;
  193. }
  194. /**
  195. * TODO 修改砍价状态为 砍价失败
  196. * @param $uid $uid 当前用户编号
  197. * @return StoreBargainUser|bool
  198. */
  199. public static function editBargainUserStatus($uid){
  200. $currentBargain = self::getBargainUserCurrent($uid); //TODO 获取当前用户正在砍价的产品
  201. $bargainProduct = StoreBargain::validBargainNumber(); //TODO 获取正在开启的砍价产品编号
  202. $closeBargain = [];
  203. foreach ($currentBargain as $key=>&$item) { if(!in_array($item,$bargainProduct)) { $closeBargain[] = $item; } }// TODO 获取已经结束的砍价产品
  204. if(count($closeBargain)) return self::where('uid',$uid)->where('status',1)->where('bargain_id','IN',implode(',',$closeBargain))->update(['status'=>2]);
  205. return true;
  206. }
  207. /**
  208. * TODO 获取当前用户正在砍价的产品
  209. * @param $uid $uid 当前用户编号
  210. * @return array
  211. */
  212. public static function getBargainUserCurrent($uid){
  213. if($uid) return self::where('uid',$uid)->where('is_del',0)->where('status',1)->column('bargain_id');
  214. else return self::where('is_del',0)->where('status',1)->column('bargain_id');
  215. }
  216. /**
  217. * TODO 获取砍价成功的用户信息
  218. * @return array|false|\PDOStatement|string|\think\Collection
  219. * @throws \think\db\exception\DataNotFoundException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. * @throws \think\exception\DbException
  222. */
  223. public static function getBargainUserStatusSuccess(){
  224. $bargainUser = self::where('status',3)->order('id desc')->field('uid,bargain_price_min,bargain_id')->select();
  225. if($bargainUser) {
  226. $bargainUser = $bargainUser->toArray();
  227. foreach ($bargainUser as $k=>$v){
  228. $bargainUser[$k]['info'] = User::where('uid',$v['uid'])->value('nickname').'砍价成功了'.$v['bargain_price_min'].'砍到了'.StoreBargain::where('id',$v['bargain_id'])->value('title');
  229. }
  230. }
  231. else{
  232. $bargainUser[]['info'] = '砍价上线了,快邀请您的好友来砍价';
  233. }
  234. return $bargainUser;
  235. }
  236. /**
  237. * TODO 获取用户砍价产品状态
  238. * @param int $bargainId $bargainId 砍价产品
  239. * @param int $bargainUserUid $bargainUserUid 开启砍价用户编号
  240. * @return bool|mixed
  241. */
  242. public static function getBargainUserStatus($bargainId,$bargainUserUid){
  243. if(!$bargainId || !$bargainUserUid) return false;
  244. //TODO status 砍价状态 1参与中 2 活动结束参与失败 3活动结束参与成功
  245. return self::where('bargain_id',$bargainId)->where('uid',$bargainUserUid)->order('add_time DESC')->value('status');
  246. }
  247. /**
  248. * 获取参与的ID
  249. * @param int $bargainId
  250. * @param int $uid
  251. * @param int $status
  252. * @return array|mixed
  253. */
  254. public static function setUserBargain($bargainId = 0,$uid = 0,$status = 1){
  255. if(!$bargainId || !$uid) return [];
  256. $bargainIdUserTableId = self::where('bargain_id',$bargainId)->where('uid',$uid)->where('status',$status)->value('id');
  257. return $bargainIdUserTableId;
  258. }
  259. }