<?php


namespace app\admin\controller;


use app\admin\model\ActionLog;
use think\App;
use think\facade\Db;

//金价
class Gold extends \app\BaseController
{
    public $post=[];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->post=$this->request->post();
    }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
        $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
        $type = isset($this->post['type']) && $this->post['type'] !== "" ? intval($this->post['type']) : "";
        $condition=[];
        if($type!=''){
            $condition = [['type',"=",$type]];
        }
        $count = Db::name("gold_price")->where($condition)->count();
        $total = ceil($count/$size);
        $page = $page >= $total ? $total : $page;
        $list =Db::name("gold_price")->where($condition)->page($page,$size)->order("addtime desc")->select();
        $data=[];
        foreach ($list as $value){
            $value['gold'] = $value['type']==1?'18K':($value['type']==2?"24K":'白银');
            $data[]=$value;
        }
        return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
    }

    public  function add(){
        $token=isset($this->post['token'])&&$this->post['token']!=''?trim($this->post['token']):"";
        if($token==""){
            return error_show(1004,"参数token不能为空");
        }
        $userinfo = GetUserInfo($token);
        if(empty($userinfo)||$userinfo['code']!=0){
            return error_show(1002,"申请人数据不存在");
        }
        $uiq=md5(time());
        $rid= isset($userinfo["data"]['id']) ?  $userinfo["data"]['id'] : "";
        $rname= isset($userinfo["data"]['nickname']) ?  $userinfo["data"]['nickname'] : "";
        $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
        if($type==""){
            return error_show(1004,"参数type不能为空");
        }
        $price =  isset($this->post['price'])&&$this->post['price']!=''?floor($this->post['price']):"";
        if($price==''){
            return error_show(1004,"参数price不能为空");
        }
        $gold =[
            "action_name"=>$rname,
            "type"=>$type,
            "price"=>$price,
            "uiq"=>$uiq,
            "addtime"=>date("Y-m-d H:i:s")
        ];
        $int = Db::name("gold_price")->insert($gold);
        if($int){
            $orde = ["order_code"=>$type,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
            ActionLog::logAdd($this->post['token'],$orde,'gold_price',0,$orde);
            return app_show(0,"新建成功");
        }else{
            return error_show(1004,"新建失败");
        }
    }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function GetByType(){
        $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
        if($type==""){
            return error_show(1004,"参数type不能为空");
        }
        $price = Db::name("gold_price")->where(["type"=>$type])->order("addtime desc")->find();
        if(empty($price)){
            return error_show(1004,"未找到数据");
        }
        $price['gold'] = $price['type']==1?'18K':($price['type']==2?"24K":'白银');
        return app_show(0,"获取成功",$price);
    }

}