<?php

/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/02
 */

namespace app\admin\model\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;


    public static function list($where)
    {
        $model = self::alias('a')
            ->order('a.id', 'desc')
            ->field('a.*, u.account, u.nickname,p.name, p.image')
            ->leftJoin('user u', 'a.uid = u.uid')
            ->leftJoin('auction_product p', 'a.product_id = p.id');



        if (trim($where['store_name']) != '') $model->where('a.id|u.account|u.nickname|a.order_id', 'like', '%'.$where['store_name'].'%');
        if (trim($where['status']) != '') $model->where('a.status', $where['status']);
        if (trim($where['auction_id']) != '') $model->where('a.auction_id', $where['auction_id']);
        if (trim($where['data']) != '') $model = self::getModelTime($where, $model,  'a.create_time');

        if ($where['page'] && $where['limit']){
            $model->page($where['page'], $where['limit']);
        }else{
            $model->page(20, 1);
        }

        $data['count'] = $model->count();
        $list = $model->select();

        $data['data'] = $list;
        return $data;

    }

    /**
     * 卖家操作
     * @param $id  //商品所属人
     * @param $price //卖出价格
     * @param $product //商品详情
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function earn($id, $price,$product)
    {
        $userModel = new User();
        $billModel = new UserBill();
        $productModel = new AuctionProduct();
        $user = $userModel->find($id);

        if ($user['spread_uid'] > 0){
            $s_price = ($price - $product['price']) * 0.1; // 卖出价格减去购买价格的百分之十 为上级直推奖励
            $spread = $userModel->find($user['spread_uid']);
            $spread['integral'] = $spread['integral'] + $s_price; //积分增加
            $spread->save();
            $billModel->save([
                'uid' => $spread['uid'],
                'pm' => 1,
                'title' => '积分增加',
                'category' => 'integral',
                'type' => 'gain',
                'mark' => '直推积分',
                'add_time' => time(),
                'number' => $s_price,
                'balance' => $spread['integral']

            ]);
        }
        $user['anticipate'] = $user['anticipate']-$price*($product['deduct']/100); // 扣除当前卖出价格百分比的预约卷
        $user->save();
        UserBill::expend('预约卷扣除', $user['uid'], 'anticipate','reduce_anticipate', $price*($product['deduct']/100), 0, $user['anticipate'] ,'卖出扣除预约卷');
        $productModel->where('id', $product['id'])->save(['price' => $price, 'hanging_price' => ($price+$price*($product['rise']/100))]); //修改当前画价

    }

    /**
     * 购买成功退预约卷
     * @param $id
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function return($id)
    {
        $data = self::find($id);
        $userModel = new User();
        $productModel = new AuctionProduct();
        $auctionModel = new Auction();
        $bookingModel = new AuctionBooking();


        $user = $userModel->find($data['uid']);
        if ($user['is_new'] == 1){

            if ($user['spread_uid']) {
                $spread = $userModel->where('uid', $user['spread_uid'])->find();
                $spread['green_time'] = strtotime(date('Y-m-d', strtotime('+1 day'))); // 开启明天的绿色通道
                $spread->save();
            }
            $orderCount =AuctionOrder::where([['uid', '=',  $user['uid']], ['status', '=', 3]])->count();
            if ($orderCount >= 5){
                $user['is_new'] = 0;
            }


        }
        $product = $productModel->where('id', $data['product_id'])->find();
        $auction = $auctionModel->where('id', $product['auction_id'])->find();
        $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();
        if ($booking['status'] > 0){
            $booking['status'] = 0;
            $booking->save();
            $user['anticipate'] = $user['anticipate'] + $auction['anticipate'];// 退还预约卷
            $user->save();


                UserBill::create([
                'uid' => $user['uid'],
                'pm' => 1,
                'title' => '预约卷退还',
                'category' => 'anticipate',
                'type' => 'add_anticipate',
                'mark' => '退还预约卷',
                'add_time' => time(),
                'number' => $auction['anticipate'],
                'balance' => $user['anticipate']
            ]);
        }
    }





}