AuctionOrder.php 14 KB

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