AuctionOrder.php 9.7 KB

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