AuctionOrder.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\models\auction;
  8. use app\admin\model\user\User;
  9. use app\admin\model\user\UserBill;
  10. use crmeb\services\product\Product;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. /**
  14. * 预约 Model
  15. * Class WechatNews
  16. * @package app\admin\model\wechat
  17. */
  18. class AuctionOrder extends BaseModel
  19. {
  20. use ModelTrait;
  21. protected $pk = 'id';
  22. protected $name = 'auction_order';
  23. protected $autoWriteTimestamp = true;
  24. /**
  25. * 用户订单
  26. * @param $data
  27. * @param $uid
  28. * @return \think\Collection
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public static function userOrder($data,$uid)
  34. {
  35. if ($data['order_id']){
  36. $list = self::alias('a')
  37. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  38. ->leftJoin('user u', 'a.collection_id = u.uid')
  39. ->where('a.order_id', $data['order_id'])->find(); //详细订单
  40. }else{
  41. if ($data['type'] == 1){
  42. $list = self::where([['uid', '=', $uid], ['status', '=', 1]])->page($data['page'], $data['limit'])->select(); //待支付
  43. }else if($data['type'] == 2){
  44. $list = self::where([['uid', '=', $uid],['status', '=', 2]])->page($data['page'], $data['limit'])->select(); //完成订单
  45. }else if($data['type'] == 3){
  46. $list = AuctionProduct::where([['uid', '=', $uid], ['is_show', '=', 0]])->page($data['page'], $data['limit'])->select(); //未上架订单
  47. }else{
  48. $list = self::where([['uid', '=', $uid], ['status', '<', 1]])->page($data['page'], $data['limit'])->select(); //过期订单
  49. }
  50. }
  51. $list = !empty($list)? $list->toArray(): [];
  52. return $list;
  53. }
  54. /**
  55. * 卖家订单
  56. * @param $data
  57. * @param $uid
  58. * @return \think\Collection
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public static function seller_list($data,$uid)
  64. {
  65. if ($data['order_id']){
  66. $list = self::alias('a')
  67. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  68. ->leftJoin('user u', 'a.uid = u.uid')
  69. ->where('a.order_id', $data['order_id'])->find(); //详细订单
  70. }else{
  71. if ($data['type'] == 3){
  72. $list = AuctionProduct::where('uid', $uid)
  73. ->where('is_show', 1)->select();
  74. }else{
  75. if ($data['type'] == 1){
  76. $status = 1;
  77. }elseif ($data['type'] == 2){
  78. $status = 2;
  79. }else{
  80. $status = 0;
  81. }
  82. $list = self::alias('a')
  83. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  84. ->leftJoin('user u', 'a.uid = u.uid')
  85. ->where([['a.collection_id', '=', $uid], ['a.status', '=', $status]])
  86. ->page($data['page'], $data['limit'])
  87. ->select(); //待支付订单
  88. }
  89. }
  90. $list = !empty($list)? $list->toArray(): [];
  91. return $list;
  92. }
  93. /**
  94. * 订单过期
  95. * @return void
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. public static function deduction()
  101. {
  102. $hour = strtotime(date('Y-m-d H:i:s', strtotime('-30 minute'))); // 30分钟以前
  103. $order = AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->select(); // 查询不在当前30分钟前的订单
  104. if ($order){
  105. foreach ($order as $K => $v){
  106. $product = AuctionProduct::where('id', $v['product_id'])->find();
  107. $auction = Auction::where('id', $product['auction_id'])->find();
  108. $booking = AuctionBooking::where([['uid', '=', $v['uid']], ['status', '=', 1], ['auction_id', '=', $auction['id']]])->where('status', '=', 1)->find(); // 找到预约订单
  109. if ($booking){
  110. AuctionBooking::where('id', $booking['id'])->update(['status' => 2]); // 修改预约订单状态 为扣除
  111. }
  112. AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
  113. }
  114. }
  115. }
  116. /**
  117. * 退回广告值
  118. * @return void
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public static function th()
  124. {
  125. $auction = Auction::where('rend_time', '<', time())->select();
  126. if ($auction){
  127. foreach ($auction as $k => $v){
  128. $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1]])->select();
  129. if ($booking){
  130. foreach ($booking as $key => $value){
  131. $userBok = AuctionBooking::where('uid', '=',$value['uid'])->where('deduct_advert', '>', 0)->count(); //预约未购买退回的次数
  132. $bf = (0.1*$userBok >= 1)? 1: 0.1*$userBok; // 0.1 * 退回次数
  133. $find = AuctionBooking::find($value['id']);
  134. $find['status'] = 0;
  135. $find['deduct_advert'] = $value['advert']*$bf; // 扣除广告书
  136. $user = User::where('uid', $value['uid'])->find();
  137. $user['aid_val'] = $user['aid_val'] + ($value['advert'] - ($value['advert'] * $bf)); // 增加用户广告值
  138. $user->save();
  139. $find->save();
  140. UserBill::income('未购买解冻但扣除'. $find['deduct_advert'].'广告值',$user['uid'], 'aid_val', 'add_aid_val',$value['advert'] - ($value['advert'] * $bf), 0, $user['aid_val'], '未购买解冻但扣除'. $find['deduct_advert'].'广告值');
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * 支付订单
  148. * @param $order
  149. * @param $uid
  150. * @return void
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public static function pay($order, $uid)
  156. {
  157. $user = User::where('uid', $uid)->find();
  158. $bk = AuctionBooking::where([['uid', '=', $uid], ['auction_id', '=', $order['auction_id']], ['frequency', '=', $order['frequency']]])->find();
  159. $bk['status'] = 0; //解冻广告值
  160. $user['now_money'] -= $order['actual_price']; // 扣除用户余额
  161. $user['aid_val'] += $bk['advert']; // 退回用户广告值
  162. AuctionProduct::where('id', $order['product_id'])->update(['uid' => $uid, 'is_show' => 0]); // 修改商品拥有人,商品下架等待挂售
  163. $user->save();
  164. $bk->save();
  165. if ($order['collection_id'] > 0){
  166. self::m_user($order['collection_id'], $order['actual_price']);
  167. }
  168. AuctionOrder::where('id', $order['id'])->update(['status' => 2]); // 修改订单状态
  169. UserBill::expend('购买商品支付', $uid, 'now_money', 'sub_money', $order['actual_price'], $user['spread_uid'], $user['now_money'], '购买商品支付订单');
  170. UserBill::income('广告值退回', $uid, 'aid_val', 'add_aid_val', $bk['advert'], $user['spread_uid'], $user['aid_val'], '购买商品成功退回广告值');
  171. }
  172. /**
  173. * 卖家增加余额
  174. * @param $uid
  175. * @param $price
  176. * @return void
  177. * @throws \think\db\exception\DataNotFoundException
  178. * @throws \think\db\exception\DbException
  179. * @throws \think\db\exception\ModelNotFoundException
  180. */
  181. public static function m_user($uid, $price)
  182. {
  183. $user = User::where('uid', $uid)->find();
  184. $user['now_money'] += $price; // 卖家增加余额
  185. $user->save();
  186. UserBill::income('卖出商品', $uid, 'now_money', 'add_money', $price, $user['spread_uid'], $user['now_money'], '商品卖出');
  187. }
  188. }