AuctionOrder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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,c.nickname as c_nickname,c.phone')
  38. ->leftJoin('user u', 'a.uid = u.uid')
  39. ->leftJoin('user c', 'a.collection_id = c.uid')
  40. ->where('a.order_id', $data['order_id'])->find(); //详细订单
  41. $pay = AuctionPay::where('uid', $list['collection_id'])->select();
  42. $list['wx'] = [];
  43. $list['zfb'] = [];
  44. $list['bank'] = [];
  45. $list['time'] = strtotime($list['create_time']);
  46. if ($pay){
  47. foreach ($pay as $k => $v){
  48. if ($v['type'] == 1){
  49. $list['wx'] = $v;
  50. }elseif ($v['type'] == 2){
  51. $list['zfb'] = $v;
  52. }elseif ($v['type'] == 3){
  53. $list['bank'] = $v;
  54. }
  55. }
  56. }
  57. }else{
  58. if ($data['type'] == 1){
  59. $list = self::where([['uid', '=', $uid], ['status', '=', 1]])->page($data['page'], $data['limit'])->select(); //待上传订单
  60. }else if($data['type'] == 2){
  61. $list = self::where([['uid', '=', $uid], ['status', '=', 2]])->page($data['page'], $data['limit'])->select(); //待审核订单
  62. }else if($data['type'] == 3) {
  63. $list = self::where([['uid', '=', $uid], ['status', '=', 3], ['is_gs', '=', 1]])->page($data['page'], $data['limit'])->select(); //完成订单
  64. }else if($data['type'] == 4) {
  65. $list = self::where([['uid', '=', $uid], ['status', '=', 3],['is_gs', '=', 0]])->page($data['page'], $data['limit'])->select(); //待挂售订单
  66. }else{
  67. $list = self::where([['uid', '=', $uid], ['status', '<', 1]])->page($data['page'], $data['limit'])->select(); //过期订单
  68. }
  69. }
  70. $list = !empty($list)? $list->toArray(): [];
  71. return $list;
  72. }
  73. /**
  74. * 卖家订单
  75. * @param $data
  76. * @param $uid
  77. * @return \think\Collection
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. public static function seller_list($data,$uid)
  83. {
  84. if ($data['order_id']){
  85. $list = self::alias('a')
  86. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  87. ->leftJoin('user u', 'a.uid = u.uid')
  88. ->where('a.order_id', $data['order_id'])->find(); //详细订单
  89. }else{
  90. if ($data['type'] == 1){
  91. $status = 1;
  92. }else if($data['type'] == 2){
  93. $status = 2;
  94. }else if($data['type'] == 3) {
  95. $status = 3;
  96. }else{
  97. $status = 0;
  98. }
  99. if ($data['type'] == 4){
  100. $product = AuctionTime::alias('a')
  101. ->field('FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as gs_time,au.nickname,b.*')
  102. ->order('a.id DESC')
  103. ->where([['a.uid', '=', $uid]])
  104. ->leftJoin('auction_product b', 'a.product_id = b.id')
  105. ->leftJoin('auction au', 'au.id = a.auction_id')
  106. ->select();
  107. }else{
  108. $list = self::alias('a')
  109. ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
  110. ->leftJoin('user u', 'a.uid = u.uid')
  111. ->order('id DESC')
  112. ->where([['a.collection_id', '=', $uid], ['a.status', '=', $status]])
  113. ->page($data['page'], $data['limit'])
  114. ->select(); //待上传订单
  115. }
  116. }
  117. if ($data['type'] != 4){
  118. $list = !empty($list)? $list->toArray(): [];
  119. foreach ($list as $k => $v) {
  120. $pay = AuctionPay::where('uid', $v['uid'])->find();
  121. $list[$k]['phone'] = $pay['phone'];
  122. }
  123. return $list;
  124. }else{
  125. $product = empty($product) ? []: $product->toArray();
  126. return $product;
  127. }
  128. }
  129. /**
  130. * 卖家操作
  131. * @param $id //商品所属人
  132. * @param $price //卖出价格
  133. * @param $product //商品详情
  134. * @return void
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. public static function earn($id, $price,$product)
  140. {
  141. $userModel = new \app\models\user\User();
  142. $productModel = new AuctionProduct();
  143. $user = $userModel->find($id);
  144. if ($user['spread_uid'] > 0){
  145. $s_price = round(($price - $product['price']) * 0.1, 2); // 卖出价格减去购买价格的百分之十 为上级直推奖励
  146. $spread = $userModel->find($user['spread_uid']);
  147. $spread['integral'] = $spread['integral'] + $s_price; //积分增加
  148. $spread->save();
  149. \app\models\user\UserBill::income('直推奖励', $spread['uid'], 'anticipate', 'add_anticipate', $s_price, 0, $spread['integral'], '奖励趣豆');
  150. }
  151. // $user['anticipate'] = $user['anticipate']-$price*($product['deduct']/100); // 扣除当前卖出价格百分比的预约卷
  152. // $user->save();
  153. // UserBill::expend('预约卷扣除', $user['uid'], 'anticipate','reduce_anticipate', $price*($product['deduct']/100), 0, $user['anticipate'] ,'卖出扣除预约卷');
  154. $productModel->where('id', $product['id'])->save(['is_show' => 0]); //下架等待挂售
  155. }
  156. /**
  157. * 购买成功退预约卷
  158. * @param $id
  159. * @return void
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public static function return($id)
  165. {
  166. $data = self::find($id);
  167. $userModel = new User();
  168. $productModel = new AuctionProduct();
  169. $auctionModel = new Auction();
  170. $bookingModel = new AuctionBooking();
  171. $user = $userModel->find($data['uid']);
  172. if ($user['is_new'] == 1){
  173. if ($user['spread_uid']) {
  174. $spread = $userModel->where('uid', $user['spread_uid'])->find();
  175. $spread['green_time'] = strtotime(date('Y-m-d', strtotime('+1 day'))); // 开启明天的绿色通道
  176. }
  177. $orderCount = AuctionOrder::where([['uid', '=', $user['uid']], ['status','=', 3]])->count();
  178. if ($orderCount >= 5){
  179. $user['is_new'] = 0;
  180. }
  181. }
  182. $product = $productModel->where('id', $data['product_id'])->find();
  183. $auction = $auctionModel->where('id', $product['auction_id'])->find();
  184. $time = strtotime(date('Y-m-d' ,strtotime($data['create_time']))); // 订单当天时间段
  185. $totime = strtotime(date('Y-m-d' ,strtotime($data['create_time']))) + 86400; // 订单凌晨时间段
  186. $booking = $bookingModel->where('auction_id', $auction['id'])->where('uid', $user['uid'])->whereBetweenTime('create_time', $time, $totime)->find();
  187. if ($booking['status'] > 0){
  188. $booking['status'] = 0;
  189. $booking->save();
  190. $anticipate = $booking['anticipate'] - $booking['deduction'];
  191. $user['anticipate'] = $user['anticipate'] + $anticipate;// 退还预约卷
  192. $user->save();
  193. \app\models\user\UserBill::create([
  194. 'uid' => $user['uid'],
  195. 'pm' => 1,
  196. 'title' => '预约卷退还',
  197. 'category' => 'anticipate',
  198. 'type' => 'add_anticipate',
  199. 'mark' => '退还'.$anticipate.'预约卷扣除'.$booking['deduction'],
  200. 'add_time' => time(),
  201. 'number' => $anticipate,
  202. 'balance' => $user['anticipate']
  203. ]);
  204. }
  205. }
  206. /**
  207. * 订单过期
  208. * @return void
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\DbException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. */
  213. public static function deduction()
  214. {
  215. $hour = strtotime(date('Y-m-d H:i:s', strtotime('-1 hour'))); // 一小时以前
  216. $time = $hour - 1800; // 一个半小时
  217. $order = AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->select(); // 查询不在当前一个半小时内的所有订单
  218. if ($order){
  219. foreach ($order as $K => $v){
  220. $product = AuctionProduct::where('id', $v['product_id'])->find();
  221. $auction = Auction::where('id', $product['auction_id'])->find();
  222. $booking = AuctionBooking::where([['uid', '=', $v['uid']], ['status', '=', 1], ['auction_id', '=', $auction['id']]])->find(); // 找到预约订单
  223. if (strtotime($v['create_time']) <= $hour and strtotime($v['create_time']) > $time){
  224. // 订单在一个小时到一个半小时内
  225. if ($booking){
  226. if ($booking['deduction'] == 0){
  227. $user = \app\models\user\User::where('uid', $v['collection_id'])->find();
  228. $user['anticipate'] = $user['anticipate'] + $auction['anticipate']/2; // 卖家增加预约卷
  229. $booking['deduction'] = $auction['anticipate']/2;
  230. UserBill::income('增加预约卷', $v['collection_id'], 'anticipate', 'add_anticipate', $auction['anticipate']/2, $v['uid'], $user['anticipate'], '卖出订单未上传支付凭证,增加'.($auction['anticipate']/2).'预约卷');
  231. $booking->save();
  232. $user->save();
  233. }
  234. }
  235. }elseif (strtotime($v['create_time']) <= $time){
  236. if ($booking) {
  237. $user = \app\models\user\User::where('uid', $v['collection_id'])->find();
  238. $user['anticipate'] = $user['anticipate'] + ($auction['anticipate'] - $auction['deduction']); // 卖家增加预约卷
  239. $booking['deduction'] = $auction['anticipate'];
  240. UserBill::income('增加预约卷', $v['collection_id'], 'anticipate', 'add_anticipate', $auction['anticipate'] - $auction['deduction'], $v['uid'], $user['anticipate'], '卖出订单未上传支付凭证,增加' . ($auction['anticipate'] - $auction['deduction']) . '预约卷');
  241. $booking->save();
  242. $user->save();
  243. AuctionBooking::where('id', $booking['id'])->update(['status' => 2]); // 修改预约订单状态 为扣除
  244. AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
  245. }
  246. }
  247. }
  248. }
  249. }
  250. /**
  251. * 退回预约卷
  252. * @return void
  253. * @throws \think\db\exception\DataNotFoundException
  254. * @throws \think\db\exception\DbException
  255. * @throws \think\db\exception\ModelNotFoundException
  256. */
  257. public static function th()
  258. {
  259. $auction = Auction::where('rend_time', '<', date('H:i:s', time()))->select();
  260. if ($auction){
  261. foreach ($auction as $k => $v){
  262. $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1]])->select();
  263. if ($booking){
  264. foreach ($booking as $key => $value){
  265. $product = AuctionProduct::where('auction_id', $value['auction_id'])->column('id');
  266. $order = AuctionOrder::where([['product_id', 'in', $product], ['frequency', '=', $value['frequency']], ['uid', '=', $value['uid']]])->find();
  267. if (!$order){
  268. $find = AuctionBooking::find($value['id']);
  269. $find['status'] = 0;
  270. $user = User::where('uid', $value['uid'])->find();
  271. $user['anticipate'] = $user['anticipate'] + $value['anticipate'];
  272. $user->save();
  273. $find->save();
  274. UserBill::income('退回预约券',$user['uid'], 'anticipate', 'add_anticipate', $value['anticipate'], 0, $user['anticipate'], '预约卷'.$value['anticipate'].'退回');
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }