AuctionOrder.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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::select();
  138. if ($auction){
  139. foreach ($auction as $k => $v){
  140. if (strtotime($v['rend_time'])+1800 < time()){
  141. // 结束后半个小时退回广告值
  142. $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1], ['frequency', '=', $v['frequency']]])->select();
  143. if ($booking){
  144. foreach ($booking as $key => $value){
  145. $product = AuctionProduct::where('auction_id', $value['auction_id'])->column('id');
  146. $order = AuctionOrder::where([['product_id', 'in', $product], ['frequency', '=', $value['frequency']], ['uid', '=', $value['uid']]])->find();
  147. if (!$order){
  148. $userBok = AuctionBooking::where('uid', '=',$value['uid'])->where('deduct_advert', '>', 0)->count(); //预约未购买退回的次数
  149. $bf = (0.1*$userBok >= 1)? 1: 0.1*$userBok == 0? 0.1 : 0.1*$userBok; // 0.1 * 退回次数
  150. $find = AuctionBooking::find($value['id']);
  151. $find['status'] = 0;
  152. $find['deduct_advert'] = $value['advert']*$bf; // 扣除广告书
  153. $user = User::where('uid', $value['uid'])->find();
  154. $user['aid_val'] = $user['aid_val'] + ($value['advert'] - ($value['advert'] * $bf)); // 增加用户广告值
  155. $user->save();
  156. $find->save();
  157. UserBill::income('未购买解冻但扣除'. $find['deduct_advert'].'广告值',$user['uid'], 'aid_val', 'add_aid_val',$value['advert'] - ($value['advert'] * $bf), 0, $user['aid_val'], '未购买解冻但扣除'. $find['deduct_advert'].'广告值');
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. /**
  166. * 支付订单
  167. * @param $order
  168. * @param $uid
  169. * @return void
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public static function pay($order, $uid)
  175. {
  176. $user = User::where('uid', $uid)->find();
  177. $user['now_money'] -= $order['actual_price']; // 扣除用户余额
  178. AuctionProduct::where('id', $order['product_id'])->update(['uid' => $uid, 'is_show' => 0]); // 修改商品拥有人,商品下架等待挂售
  179. $user->save();
  180. if ($order['collection_id'] > 0){
  181. self::m_user($order['collection_id'], $order['actual_price']);
  182. }
  183. AuctionOrder::where('id', $order['id'])->update(['status' => 2]); // 修改订单状态
  184. UserBill::expend('购买商品支付', $uid, 'now_money', 'sub_money', $order['actual_price'], $user['spread_uid'], $user['now_money'], '购买商品支付订单');
  185. }
  186. /**
  187. * 卖家增加余额
  188. * @param $uid
  189. * @param $price
  190. * @return void
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. */
  195. public static function m_user($uid, $price)
  196. {
  197. $user = User::where('uid', $uid)->find();
  198. $user['now_money'] += $price; // 卖家增加余额
  199. $user->save();
  200. UserBill::income('卖出商品', $uid, 'now_money', 'add_money', $price, $user['spread_uid'], $user['now_money'], '商品卖出');
  201. }
  202. }