<?php
namespace app\admin\controller\auction;

use app\admin\controller\AuthController;
use app\admin\controller\Union;
use app\admin\model\user\User;
use app\admin\model\user\UserBill;
use crmeb\services\{ExpressService,
    JsonService,
    MiniProgramService,
    upload\Upload,
    WechatService,
    FormBuilder as Form,
    CacheService,
    UtilService as Util,
    JsonService as Json};
use app\admin\model\system\{
    SystemAttachment as SystemAttachmentModel, SystemAttachmentCategory as Category
};
use app\admin\model\auction\AuctionOrder as model;
use think\facade\Route as Url;

/**
 * 竞拍管理
 * Class StoreOrder
 * @package app\admin\controller\store
 */
class AuctionOrder extends AuthController
{

    public function index()
    {

        $list = \app\admin\model\auction\Auction::select();

        $this->assign([
            'year' => get_month(),
            'auction'=> $list,

        ]);
        return $this->fetch();
    }


    /**
     * 获取列表
     * @return void
     */
    public function list()
    {
        $where = Util::getMore([
            ['is_show', ''],
            ['page', 1],
            ['limit', 20],
            ['auction_id', ''],
            ['store_name', ''],
            ['data', ''],
            ['status', '']
        ]);
        $data = model::list($where);
        foreach ($data['data'] as $key => $val){
            if ($data['data'][$key]['uid'] == 0) $data['data'][$key]['nickname'] = '管理';
        }


        return Json::successlayui($data);
    }

    public function auction_list()
    {
        $list = model::select()->toArray();

        Json::successful($list);
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create($id = 0)
    {
        $data = \app\admin\model\auction\Auction::select()->toArray();

        $this->assign(['id' => $id, 'list' => $data]);
        return $this->fetch();
    }


    /**
     * 添加
     * @param $id
     * @return void
     */
    public function save($id)
    {
        $data = Util::postMore([
            'name',
            'is_show',
            'image',
            'price',
            'deduct',
            'rise',
            'info',
            'sort',
            'auction_id'
        ]);
        $model = new model();
        $res = $model->save($data);
        if ($res){
            return Json::success('添加成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }

    }

    /**
     * 删除
     * @param $id
     * @return void
     * @throws \Exception
     */
    public function delete($id)
    {
        if (!$id) Json::fail('删除失败');

        $res = model::where('id', $id)->delete();
        if ($res){
            return Json::success('删除成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }

    }


    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return void
     */
    public function set_status($id, $status)
    {

        if (empty($id)) Json::fail('修改失败');

        $res = model::update(['is_show' => $status, 'id' => $id]);
        if ($res){
            return Json::success('修改成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }
    }

    /**
     * 编辑页面
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($id)
    {
        if (!$id) Json::fail('数据不存在');
        $data = \app\admin\model\auction\Auction::select()->toArray();

        $this->assign(['id' => $id, 'list' => $data]);
        return $this->fetch();
    }


    public function get_list($id)
    {
        if (!$id) Json::fail('数据不存在');
        $model = new model();
        $info = $model->find($id);

        return JsonService::successful($info);
    }

    /**
     * 修改
     * @param $id
     * @return void
     */
    public function update($id)
    {
        $data = Util::postMore([
            'id',
            'name',
            'is_show',
            'image',
            'price',
            'deduct',
            'rise',
            'info',
            'sort',
            'auction_id'
        ]);
        $res = model::update($data);
        if ($res){
            return Json::success('修改成功!');
        }else{
            return Json::fail(\app\admin\model\auction\Auction::getErrorInfo());
        }


    }


    /**
     * 通过
     * @param $id
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function qd($id)
    {
        if (!$id) return Json::fail('没有数据');
        $data = model::find($id);// 订单数据

        if($data['status'] < 3 and $data['status'] > 0){

            \app\admin\model\auction\AuctionProduct::beginTrans();

            $product = \app\admin\model\auction\AuctionProduct::find($data['product_id']);
            if (!$product)return Json::fail('数据不存在');
            $uid = $product['uid']; // 所属人id

            $product['uid'] = $data['uid'];// 商品拥有人更新
            $res = $product->save();
            if ($res){
                if ($uid > 0){
                    model::earn($uid,$data['price'] ,$product); // 卖家
                }
                model::return($id); // 买家
                $data['status'] = 3;
                $data->save();
                \app\admin\model\auction\AuctionProduct::commitTrans();
				// 计算总消费金额
				$prices=model::where('uid',$data['uid'])->where('status',3)->sum('price');//历史完成订单
                $sum=floatval($prices+$data['price']);
                //如果总金额大于等于三万
                if($sum>=30000){
                    $user=User::where('uid',$data['uid'])->find();//获取用户信息
                    if($user['f_integral']==3000){//如果积分依旧冻结
                        //释放冻结积分
                        $integral=floatval($user['integral']+3000);
                        $res=User::where('uid',$data['uid'])->update(['f_integral' =>0,'integral'=>$integral]);
                        //积分释放流水
                        $res3=UserBill::income('冻结积分释放',$data['uid'], 'integral', 'release',3000, $id,$integral, '释放3000积分');
						if(!$res3){
							return self::setErrorInfo('操作失败');
				}

                    }
                }

                return Json::successful('审核通过！');
            }else{


                \app\admin\model\auction\AuctionProduct::rollbackTrans();
                return Json::successful('审核失败！');
            }

        }
        return Json::fail('已通过订单');

    }

}