AuctionOrder.php 11 KB

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