AuctionOrder.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $date = date("Y-m-d");
  149. $firstday = date('Y-m-01 00:00:00', strtotime($date)); //本月第一天
  150. $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day")); //本月最后一天
  151. $userBok = AuctionBooking::where('uid', '=',$value['uid'])->where('th', '=', 1)->whereBetweenTime('create_time', $firstday, $lastday)->count(); //预约未购买退回的次数
  152. if ($userBok >= 3){
  153. $userBok++;
  154. if ($userBok == 4) $bf = 0.1;
  155. if ($userBok == 5) $bf = 0.2;
  156. if ($userBok >= 6) $bf = 0.5;
  157. }else{
  158. $bf = 0;
  159. }
  160. $find = AuctionBooking::find($value['id']);
  161. $find['status'] = 0;
  162. $find['th'] = 1;
  163. $find['deduct_advert'] = $value['advert']*$bf; // 扣除广告书
  164. $user = User::where('uid', $value['uid'])->find();
  165. $user['aid_val'] += ($value['advert']-$find['deduct_advert']); // 增加用户广告值
  166. $user->save();
  167. $find->save();
  168. UserBill::income('未购买解冻但扣除'. $find['deduct_advert'].'广告值',$user['uid'], 'aid_val', 'add_aid_val',$value['advert'] - ($value['advert'] * $bf), 0, $user['aid_val'], '未购买解冻但扣除'. $find['deduct_advert'].'广告值');
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * 支付订单
  178. * @param $order
  179. * @param $uid
  180. * @return void
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\DbException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. */
  185. public static function pay($order, $uid)
  186. {
  187. $user = User::where('uid', $uid)->find();
  188. $user['now_money'] -= $order['actual_price']; // 扣除用户余额
  189. AuctionProduct::where('id', $order['product_id'])->update(['uid' => $uid, 'is_show' => 0]); // 修改商品拥有人,商品下架等待挂售
  190. $user->save();
  191. if ($order['collection_id'] > 0){
  192. self::m_user($order['collection_id'], $order['actual_price']);
  193. }
  194. AuctionOrder::where('id', $order['id'])->update(['status' => 2]); // 修改订单状态
  195. UserBill::expend('购买商品支付', $uid, 'now_money', 'sub_money', $order['actual_price'], $user['spread_uid'], $user['now_money'], '购买商品支付订单');
  196. }
  197. /**
  198. * 卖家增加余额
  199. * @param $uid
  200. * @param $price
  201. * @return void
  202. * @throws \think\db\exception\DataNotFoundException
  203. * @throws \think\db\exception\DbException
  204. * @throws \think\db\exception\ModelNotFoundException
  205. */
  206. public static function m_user($uid, $price)
  207. {
  208. $user = User::where('uid', $uid)->find();
  209. $user['now_money'] += $price; // 卖家增加余额
  210. $user->save();
  211. UserBill::income('卖出商品', $uid, 'now_money', 'add_money', $price, $user['spread_uid'], $user['now_money'], '商品卖出');
  212. }
  213. }