AuctionOrder.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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::alias('a')
  47. ->field('a.*, au.name as au_name')
  48. ->leftJoin('auction au', 'au.id = a.auction_id')
  49. ->where('a.uid', $uid)
  50. ->where('a.is_show', 0)
  51. ->select();
  52. }else{
  53. $list = self::where([['uid', '=', $uid], ['status', '<', 1]])->page($data['page'], $data['limit'])->select(); //过期订单
  54. }
  55. }
  56. $list = !empty($list)? $list->toArray(): [];
  57. return $list;
  58. }
  59. /**
  60. * 卖家订单
  61. * @param $data
  62. * @param $uid
  63. * @return \think\Collection
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public static function seller_list($data,$uid)
  69. {
  70. if ($data['order_id']){
  71. $list = self::alias('a')
  72. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  73. ->leftJoin('user u', 'a.uid = u.uid')
  74. ->where('a.order_id', $data['order_id'])->find(); //详细订单
  75. }else{
  76. if ($data['type'] == 3){
  77. $list = AuctionProduct::alias('a')
  78. ->field('a.*, au.name as au_name')
  79. ->leftJoin('auction au', 'au.id = a.auction_id')
  80. ->where('a.uid', $uid)
  81. ->where('a.is_show', 1)
  82. ->select();
  83. }else{
  84. if ($data['type'] == 1){
  85. $status = 1;
  86. }elseif ($data['type'] == 2){
  87. $status = 2;
  88. }else{
  89. $status = 0;
  90. }
  91. $list = self::alias('a')
  92. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  93. ->leftJoin('user u', 'a.uid = u.uid')
  94. ->where([['a.collection_id', '=', $uid], ['a.status', '=', $status]])
  95. ->page($data['page'], $data['limit'])
  96. ->select(); //待支付订单
  97. }
  98. }
  99. $list = !empty($list)? $list->toArray(): [];
  100. return $list;
  101. }
  102. /**
  103. * 订单过期
  104. * @return void
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. public static function deduction()
  110. {
  111. $hour = strtotime(date('Y-m-d H:i:s', strtotime('-30 minute'))); // 30分钟以前
  112. $order = AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->select(); // 查询不在当前30分钟前的订单
  113. if ($order){
  114. foreach ($order as $K => $v){
  115. $product = AuctionProduct::where('id', $v['product_id'])->find();
  116. $auction = Auction::where('id', $product['auction_id'])->find();
  117. $booking = AuctionBooking::where([['uid', '=', $v['uid']], ['status', '=', 1], ['auction_id', '=', $auction['id']]])->where('status', '=', 1)->find(); // 找到预约订单
  118. if ($booking){
  119. AuctionBooking::where('id', $booking['id'])->update(['status' => 2]); // 修改预约订单状态 为扣除
  120. }
  121. AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
  122. }
  123. }
  124. }
  125. /**
  126. * 退回广告值
  127. * @return void
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. */
  132. public static function th()
  133. {
  134. $auction = Auction::where('rend_time', '<', time())->select();
  135. if ($auction){
  136. foreach ($auction as $k => $v){
  137. $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1]])->select();
  138. if ($booking){
  139. foreach ($booking as $key => $value){
  140. $product = AuctionProduct::where('auction_id', $value['auction_id'])->column('id');
  141. $order = AuctionOrder::where([['product_id', 'in', $product], ['frequency', '=', $value['frequency']], ['uid', '=', $value['uid']]])->find();
  142. if (!$order){
  143. $userBok = AuctionBooking::where('uid', '=',$value['uid'])->where('deduct_advert', '>', 0)->count(); //预约未购买退回的次数
  144. $bf = (0.1*$userBok >= 1)? 1: 0.1*$userBok == 0? 0.1 : 0.1*$userBok; // 0.1 * 退回次数
  145. $find = AuctionBooking::find($value['id']);
  146. $find['status'] = 0;
  147. $find['deduct_advert'] = $value['advert']*$bf; // 扣除广告书
  148. $user = User::where('uid', $value['uid'])->find();
  149. $user['aid_val'] = $user['aid_val'] + ($value['advert'] - ($value['advert'] * $bf)); // 增加用户广告值
  150. $user->save();
  151. $find->save();
  152. UserBill::income('未购买解冻但扣除'. $find['deduct_advert'].'广告值',$user['uid'], 'aid_val', 'add_aid_val',$value['advert'] - ($value['advert'] * $bf), 0, $user['aid_val'], '未购买解冻但扣除'. $find['deduct_advert'].'广告值');
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * 支付订单
  161. * @param $order
  162. * @param $uid
  163. * @return void
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public static function pay($order, $uid)
  169. {
  170. $user = User::where('uid', $uid)->find();
  171. $bk = AuctionBooking::where([['uid', '=', $uid], ['auction_id', '=', $order['auction_id']], ['frequency', '=', $order['frequency']]])->find();
  172. $bk['status'] = 0; //解冻广告值
  173. $user['now_money'] -= $order['actual_price']; // 扣除用户余额
  174. $user['aid_val'] += $bk['advert']; // 退回用户广告值
  175. AuctionProduct::where('id', $order['product_id'])->update(['uid' => $uid, 'is_show' => 0]); // 修改商品拥有人,商品下架等待挂售
  176. $user->save();
  177. $bk->save();
  178. if ($order['collection_id'] > 0){
  179. self::m_user($order['collection_id'], $order['actual_price']);
  180. }
  181. if ($order['collection_id'] > 0){
  182. // 如果是用户挂售商品
  183. $collection = \app\models\user\User::where('uid', $order['uid'])->find();
  184. $collection['now_money'] += $order['actual_price'];// 增加买家余额
  185. $collection->save();
  186. UserBill::income('挂售商品卖出', $collection['uid'], 'now_money', 'add_now_money', $order['actual_price'], $collection['spread_uid'], $collection['now_money']);
  187. }
  188. AuctionOrder::where('id', $order['id'])->update(['status' => 2]); // 修改订单状态
  189. UserBill::expend('购买商品支付', $uid, 'now_money', 'sub_money', $order['actual_price'], $user['spread_uid'], $user['now_money'], '购买商品支付订单');
  190. UserBill::income('广告值退回', $uid, 'aid_val', 'add_aid_val', $bk['advert'], $user['spread_uid'], $user['aid_val'], '购买商品成功退回广告值');
  191. }
  192. /**
  193. * 卖家增加余额
  194. * @param $uid
  195. * @param $price
  196. * @return void
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public static function m_user($uid, $price)
  202. {
  203. $user = User::where('uid', $uid)->find();
  204. $user['now_money'] += $price; // 卖家增加余额
  205. $user->save();
  206. UserBill::income('卖出商品', $uid, 'now_money', 'add_money', $price, $user['spread_uid'], $user['now_money'], '商品卖出');
  207. }
  208. }