AuctionOrder.php 12 KB

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