AuctionOrder.php 12 KB

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