AuctionOrder.php 12 KB

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