AuctionOrder.php 12 KB

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