AuctionOrder.php 9.6 KB

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