AuctionOrder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. foreach ($list as $k => $v) {
  119. $pay = AuctionPay::where('uid', $v['uid'])->find();
  120. $list[$k]['phone'] = $pay['phone'];
  121. }
  122. return $list;
  123. }
  124. /**
  125. * 卖家操作
  126. * @param $id //商品所属人
  127. * @param $price //卖出价格
  128. * @param $product //商品详情
  129. * @return void
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\DbException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. */
  134. public static function earn($id, $price,$product)
  135. {
  136. $userModel = new \app\models\user\User();
  137. $productModel = new AuctionProduct();
  138. $user = $userModel->find($id);
  139. if ($user['spread_uid'] > 0){
  140. $s_price = number_format(($price - $product['price']) * 0.1, 2); // 卖出价格减去购买价格的百分之十 为上级直推奖励
  141. $spread = $userModel->find($user['spread_uid']);
  142. $spread['integral'] = $spread['integral'] + $s_price; //积分增加
  143. $spread->save();
  144. \app\models\user\UserBill::create([
  145. 'uid' => $spread['uid'],
  146. 'link_id' => $id,
  147. 'pm' => 1,
  148. 'title' => '积分增加',
  149. 'category' => 'integral',
  150. 'type' => 'gain',
  151. 'mark' => '卖出商品直推'.$s_price.'积分',
  152. 'add_time' => time(),
  153. 'number' => $s_price,
  154. 'balance' => $spread['integral']
  155. ]);
  156. $user['anticipate'] = $user['anticipate']-number_format($price*($product['deduct']/100),2); // 扣除当前卖出价格百分比的预约卷
  157. $user->save();
  158. \app\models\user\UserBill::create([
  159. 'uid' => $user['uid'],
  160. 'pm' => 0,
  161. 'title' => '预约卷扣除',
  162. 'category' => 'anticipate',
  163. 'type' => 'reduce_anticipate',
  164. 'mark' => '卖出商品扣除'.number_format($price*($product['deduct']/100), 2).'预约卷',
  165. 'add_time' => time(),
  166. 'number' => number_format($price*($product['deduct']/100),2),
  167. 'balance' => $user['anticipate']
  168. ]);
  169. }
  170. $productModel->where('id', $product['id'])->save(['price' => $price, 'hanging_price' => ($price+$price*($product['rise']/100))]); //修改当前画价
  171. }
  172. /**
  173. * 购买成功退预约卷
  174. * @param $id
  175. * @return void
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\DbException
  178. * @throws \think\db\exception\ModelNotFoundException
  179. */
  180. public static function return($id)
  181. {
  182. $data = self::find($id);
  183. $userModel = new User();
  184. $productModel = new AuctionProduct();
  185. $auctionModel = new Auction();
  186. $bookingModel = new AuctionBooking();
  187. $user = $userModel->find($data['uid']);
  188. if ($user['is_new'] == 1){
  189. if ($user['spread_uid']) {
  190. $spread = $userModel->where('uid', $user['spread_uid'])->find();
  191. $spread['green_time'] = strtotime(date('Y-m-d', strtotime('+1 day'))); // 开启明天的绿色通道
  192. }
  193. $orderCount = AuctionOrder::where([['uid', '=', $user['uid']], ['status','=', 3]])->count();
  194. if ($orderCount >= 5){
  195. $user['is_new'] = 0;
  196. }
  197. }
  198. $product = $productModel->where('id', $data['product_id'])->find();
  199. $auction = $auctionModel->where('id', $product['auction_id'])->find();
  200. $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();
  201. if ($booking['status'] > 0){
  202. $booking['status'] = 0;
  203. $booking->save();
  204. $user['anticipate'] = $user['anticipate'] + $auction['anticipate'];// 退还预约卷
  205. $user->save();
  206. \app\models\user\UserBill::create([
  207. 'uid' => $user['uid'],
  208. 'pm' => 1,
  209. 'title' => '预约卷退还',
  210. 'category' => 'anticipate',
  211. 'type' => 'add_anticipate',
  212. 'mark' => '退还预约场馆'.number_format($auction['anticipate'],2).'预约卷',
  213. 'add_time' => time(),
  214. 'number' => number_format($auction['anticipate'],2),
  215. 'balance' => $user['anticipate']
  216. ]);
  217. }
  218. }
  219. /**
  220. * 订单过期
  221. * @return void
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. public static function deduction()
  227. {
  228. $hour = strtotime(date('Y-m-d H:i:s', strtotime('-1 hour'))); // 一小时以前
  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']]])->where('status', '=', 1)->find(); // 找到预约订单
  235. if ($booking){
  236. $user = \app\models\user\User::where('uid', $v['collection_id'])->find();
  237. $user['anticipate'] = $user['anticipate'] + $auction['anticipate']; // 增加预约卷
  238. UserBill::income('增加预约卷', $v['collection_id'], 'anticipate', 'add_anticipate', $auction['anticipate'], $v['uid'], $user['anticipate'], '卖出订单未上传支付凭证,增加'.$auction['anticipate'].'预约卷');
  239. $user->save();
  240. AuctionBooking::where('id', $booking['id'])->update(['status' => 2]); // 修改预约订单状态 为扣除
  241. }
  242. AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
  243. }
  244. }
  245. }
  246. /**
  247. * 退回预约卷
  248. * @return void
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\DbException
  251. * @throws \think\db\exception\ModelNotFoundException
  252. */
  253. public static function th()
  254. {
  255. $auction = Auction::where('rend_time', '<', time())->select();
  256. if ($auction){
  257. foreach ($auction as $k => $v){
  258. $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1]])->select();
  259. if ($booking){
  260. foreach ($booking as $key => $value){
  261. $find = AuctionBooking::find($value['id']);
  262. $find['status'] = 0;
  263. $user = User::where('uid', $value['uid'])->find();
  264. $user['anticipate'] = $user['anticipate'] + $value['anticipate'];
  265. $user->save();
  266. $find->save();
  267. UserBill::income('预约卷增加',$user['uid'], 'anticipate', 'add_anticipate', $value['anticipate'], 0, $user['anticipate'], '预约卷退回');
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }