AuctionOrder.php 14 KB

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