AuctionOrder.php 14 KB

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