AuctionOrder.php 12 KB

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