123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/11/02
- */
- namespace app\models\auction;
- use app\admin\model\user\User;
- use app\admin\model\user\UserBill;
- use crmeb\services\product\Product;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- /**
- * 预约 Model
- * Class WechatNews
- * @package app\admin\model\wechat
- */
- class AuctionOrder extends BaseModel
- {
- use ModelTrait;
- protected $pk = 'id';
- protected $name = 'auction_order';
- protected $autoWriteTimestamp = true;
- /**
- * 用户订单
- * @param $data
- * @param $uid
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function userOrder($data,$uid)
- {
- if ($data['order_id']){
- $list = self::alias('a')
- ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
- ->leftJoin('user u', 'a.collection_id = u.uid')
- ->where('a.order_id', $data['order_id'])->find(); //详细订单
- }else{
- if ($data['type'] == 1){
- $list = self::where([['uid', '=', $uid], ['status', '=', 1]])->order('id DESC')->page($data['page'], $data['limit'])->select(); //待支付
- }else if($data['type'] == 2){
- $list = self::where([['uid', '=', $uid],['status', '=', 2]])->order('id DESC')->page($data['page'], $data['limit'])->select(); //完成订单
- }else if($data['type'] == 3){
- $list = AuctionProduct::alias('a')
- ->field('a.*, au.name as au_name')
- ->leftJoin('auction au', 'au.id = a.auction_id')
- ->order('a.id DESC')
- ->where('a.uid', $uid)
- ->where('a.is_show', 0)
- ->select();
- }else{
- $list = self::where([['uid', '=', $uid], ['status', '<', 1]])->order('id DESC')->page($data['page'], $data['limit'])->select(); //过期订单
- }
- }
- $list = !empty($list)? $list->toArray(): [];
- return $list;
- }
- /**
- * 卖家订单
- * @param $data
- * @param $uid
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function seller_list($data,$uid)
- {
- if ($data['order_id']){
- $list = self::alias('a')
- ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
- ->leftJoin('user u', 'a.uid = u.uid')
- ->where('a.order_id', $data['order_id'])->find(); //详细订单
- }else{
- if ($data['type'] == 3){
- $list = AuctionProduct::alias('a')
- ->field('a.*, au.name as au_name')
- ->leftJoin('auction au', 'au.id = a.auction_id')
- ->order('a.id DESC')
- ->where('a.uid', $uid)
- ->where('a.is_show', 1)
- ->select();
- }else{
- if ($data['type'] == 1){
- $status = 1;
- }elseif ($data['type'] == 2){
- $status = 2;
- }else{
- $status = 0;
- }
- $list = self::alias('a')
- ->field('a.*,u.nickname,u.avatar,u.uid as user_id')
- ->leftJoin('user u', 'a.uid = u.uid')
- ->order('a.id DESC')
- ->where([['a.collection_id', '=', $uid], ['a.status', '=', $status]])
- ->page($data['page'], $data['limit'])
- ->select(); //待支付订单
- }
- }
- $list = !empty($list)? $list->toArray(): [];
- return $list;
- }
- /**
- * 订单过期
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function deduction()
- {
- $hour = strtotime(date('Y-m-d H:i:s', strtotime('-30 minute'))); // 30分钟以前
- $order = AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->select(); // 查询不在当前30分钟前的订单
- if ($order){
- foreach ($order as $K => $v){
- $product = AuctionProduct::where('id', $v['product_id'])->find();
- $auction = Auction::where('id', $product['auction_id'])->find();
- $booking = AuctionBooking::where([['uid', '=', $v['uid']], ['status', '=', 1], ['auction_id', '=', $auction['id']]])->where('status', '=', 1)->find(); // 找到预约订单
- if ($booking){
- AuctionBooking::where('id', $booking['id'])->update(['status' => 2]); // 修改预约订单状态 为扣除
- }
- AuctionOrder::where('create_time', '<', $hour)->where('status', '=', 1)->update(['status' => 0]); // 修改为已过期订单
- }
- }
- }
- /**
- * 退回广告值
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function th()
- {
- $auction = Auction::select();
- if ($auction){
- foreach ($auction as $k => $v){
- if (strtotime($v['rend_time'])+1800 < time()){
- // 结束后半个小时退回广告值
- $booking = AuctionBooking::where([['auction_id', '=', $v['id']], ['status','=' , 1], ['frequency', '=', $v['frequency']]])->select();
- if ($booking){
- foreach ($booking as $key => $value){
- $product = AuctionProduct::where('auction_id', $value['auction_id'])->column('id');
- $order = AuctionOrder::where([['product_id', 'in', $product], ['frequency', '=', $value['frequency']], ['uid', '=', $value['uid']]])->find();
- if (!$order){
- $userBok = AuctionBooking::where('uid', '=',$value['uid'])->where('deduct_advert', '>', 0)->count(); //预约未购买退回的次数
- $bf = (0.1*$userBok >= 1)? 1: 0.1*$userBok == 0? 0.1 : 0.1*$userBok; // 0.1 * 退回次数
- $find = AuctionBooking::find($value['id']);
- $find['status'] = 0;
- $find['deduct_advert'] = $value['advert']*$bf; // 扣除广告书
- $user = User::where('uid', $value['uid'])->find();
- $user['aid_val'] = $user['aid_val'] + ($value['advert'] - ($value['advert'] * $bf)); // 增加用户广告值
- $user->save();
- $find->save();
- UserBill::income('未购买解冻但扣除'. $find['deduct_advert'].'广告值',$user['uid'], 'aid_val', 'add_aid_val',$value['advert'] - ($value['advert'] * $bf), 0, $user['aid_val'], '未购买解冻但扣除'. $find['deduct_advert'].'广告值');
- }
- }
- }
- }
- }
- }
- }
- /**
- * 支付订单
- * @param $order
- * @param $uid
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function pay($order, $uid)
- {
- $user = User::where('uid', $uid)->find();
- $user['now_money'] -= $order['actual_price']; // 扣除用户余额
- AuctionProduct::where('id', $order['product_id'])->update(['uid' => $uid, 'is_show' => 0]); // 修改商品拥有人,商品下架等待挂售
- $user->save();
- if ($order['collection_id'] > 0){
- self::m_user($order['collection_id'], $order['actual_price']);
- }
- AuctionOrder::where('id', $order['id'])->update(['status' => 2]); // 修改订单状态
- UserBill::expend('购买商品支付', $uid, 'now_money', 'sub_money', $order['actual_price'], $user['spread_uid'], $user['now_money'], '购买商品支付订单');
- }
- /**
- * 卖家增加余额
- * @param $uid
- * @param $price
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function m_user($uid, $price)
- {
- $user = User::where('uid', $uid)->find();
- $user['now_money'] += $price; // 卖家增加余额
- $user->save();
- UserBill::income('卖出商品', $uid, 'now_money', 'add_money', $price, $user['spread_uid'], $user['now_money'], '商品卖出');
- }
- }
|