AuctionOrder.php 11 KB

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