StoreBargainUser.php 12 KB

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