AuctionOrder.php 14 KB

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