<?php

namespace app\admin\controller;

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

//咨询单
class Consult extends Base
{
    public $noble=[];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->noble =\think\facade\Config::get("noble");
    }

    public function conlist(){
        $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";
        $where=[['a.is_del',"=",0],['b.is_del',"=",0]];
        $infoNo = isset($this->post['zxNo']) && $this->post['zxNo'] !== "" ? trim($this->post['zxNo']) : "";
        if ($infoNo !== "") {
            $where[] = ['a.infoNo','like','%'.$infoNo.'%'];
        }

        $salesman = isset($this->post['salesman']) && $this->post['salesman'] !== "" ? trim($this->post['salesman']) : "";
        if ($salesman !== "") {
            //$where["salesman"] =Db::Raw("like '%$salesman%'");
            $where[]= ["b.salesman",'like',"%$salesman%"];
        }
        $cpName = isset($this->post['cpName']) && $this->post['cpName'] !== "" ? trim($this->post['cpName']) : "";
        if ($cpName !== "") {
            //$where["cpName"]= Db::Raw("like '%$cpName%'");
            $where[]= ["a.good_Name",'like',"%$cpName%"];
        }
        $khNo = isset($this->post['khNo']) && $this->post['khNo'] !== "" ? trim($this->post['khNo']) : "";
        if ($khNo !== "") {
            // $where['khNo'] = $khNo;
            $where[]= ["b.khNo",'like',"%$khNo%"];
        }
        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
        if ($companyNo !== "") {
            // $where['khNo'] = $khNo;
            $where[]= ["b.companyNo",'like',"%$companyNo%"];
        }
        $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
        if ($start !="") {
            //$where = ["zxtime"=>Db::raw(">= '{$start}'")];
            $where[]= ["a.addtime",'>=',$start];
        }
        $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
        if($end !=""){
            $where[]= ["a.addtime",'<=',$end];
        }
        $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
        if ($status !== "") {
            $where[]= ["a.status",'=',$status];
        }
        $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
        if ($company_name !== "") $where[] = ["b.createrid", 'in', get_company_item_user_by_name($company_name)];

        $role=$this->checkRole();
        if(!empty($role['write'])){
            $where[]=["b.createrid","in",$role['write']];
        }
//        if(!empty($role['platform']) ){
//            $where[]=["b.platform_code","in",$role['platform']];
//        }
        $count = Db::name('consult_info')->alias("a")->leftJoin("consult_order b","a.zxNo=b.zxNo")->where($where)
            ->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('consult_info')
            ->alias("a")
            ->leftJoin("consult_order b", "a.zxNo=b.zxNo")
            ->leftJoin("depart_user u", "u.uid=b.createrid AND u.is_del=0")
            ->where($where)
            ->page($page, $size)
            ->field("a.*,b.createrid,b.creater,b.is_project,b.projectNo,b.khNo,b.endtime,b.platform_code,b.saleid,b.salesman,b.depart,u.itemid")
            ->order("a.id desc")
            ->cursor();

        $data=[];
        foreach ($list as $value){
            $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
            $value['specinfo']=isset($value['specinfo'])&&$value['specinfo']!=""? json_decode($value['specinfo'],
                true):[];
            $brand = Db::name("brand")->where(['id'=>$value['brand_id']])->find();
            $value['brand_name']= isset($brand['brand_name']) && $brand['brand_name']!='' ? $brand['brand_name']:"";
            $value['khname']="";
            if($value['khNo']!==""){
                $kh=Db::name("customer_info")->where(['companyNo'=>$value['khNo']])->find();
                $value['khname']= isset($kh['companyName'])?$kh['companyName']:"";
            }
            $value['metal_name']=isset($value['metal_id'])&& $value['metal_id']!=0?$this->noble[$value['metal_id']]:"";
            $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
            $data[]=$value;
        }
        return app_show(0,"获取成功",['count'=>$count,'list'=>$data]);
    }
    public function info(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) : "";
        if($id==""){
            return error_show(1002,"咨询单编号不能为空");
        }
        $fo = Db::name('consult')->where(['id'=>$id,'is_del'=>0])->find();
        $in= isset($fo['cat_id']) && $fo['cat_id'] !=0 ? made($fo['cat_id']):[];
        $fo['can']=$in;
        if(empty($fo)){
            return error_show(1002,"未找到咨询单编号");
        }else{
            return app_show(0,"获取成功",$fo);
        }
    }
    public function del(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) : "";
        $de = Db::name('consult')->where(['id'=>$id,'is_del'=>0])->find();
        if($de==false){
            return error_show(1002,'咨询单信息未找到');
        }
        $dell= Db::name('consult')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
        if($dell){
            return error_show(0,"删除成功");
        }else
            return error_show(1002,"删除失败");
    }

    public function create(){
        $is_project= isset($this->post['is_project']) && $this->post['is_project'] !=="" ? intval($this->post['is_project']) : "";
        if($is_project===""){
            return error_show(1002,"参数is_project不能为空");
        }

        if($is_project==1){
            $projectNo= isset($this->post['projectNo']) && $this->post['projectNo'] !=="" ? trim($this->post['projectNo']) : "";
            if($projectNo===""){
                return error_show(1002,"参数projectNo不能为空");
            }
        }
        $khNo =  isset($this->post['khNo']) && $this->post['khNo'] !=="" ? trim($this->post['khNo']) : "";
        if($khNo===""){
            return error_show(1002,"参数khNo不能为空");
        }
        $companyNo =  isset($this->post['companyNo']) && $this->post['companyNo'] !=="" ? trim($this->post['companyNo']) : "";
        if($companyNo===""){
            return error_show(1002,"参数companyNo不能为空");
        }
        $endtime =  isset($this->post['endtime']) && $this->post['endtime'] !=="" ? $this->post['endtime'] : "";
        if($endtime===""){
            return error_show(1002,"参数endtime不能为空");
        }

        $platform_code =  isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code']) :
            "";
        if($platform_code===""){
            return error_show(1002,"参数platform_code不能为空");
        }
        $ladder = isset($this->post['ladder'])&&!empty($this->post['ladder']) ? $this->post['ladder']:"";
        if($ladder==""){
            return error_show(1005,"参数ladder不能为空");
        }
        $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
        if($token==''){
            return error_show(105,"参数token不能为空");
        }

        $user =GetUserInfo($token);
        if(empty($user)||$user['code']!=0){
            return error_show(102,"用户数据不存在");
        }
        $createrid= isset($user["data"]['id']) ?  $user["data"]['id'] : "";
        $creater= isset($user["data"]['nickname']) ?  $user["data"]['nickname'] : "";
        $depart= Db::name("depart_user")->where(["uid"=>$createrid,"status"=>1,"is_del"=>0])->find();
        $zxNo=makeNo("ZX");
        Db::startTrans();
        try{
            $data=[
                "zxNo"=>$zxNo,
                "createrid"=>$createrid,
                "creater"=>$creater,
                "is_project"=>$is_project,
                "projectNo"=>isset($projectNo) ? $projectNo:"",
                "khNo"=>$khNo,
                "companyNo"=>$companyNo,
                "endtime"=>$endtime,
                "salesman"=>$creater,
                "platform_code"=>$platform_code,
                "saleid"=>$createrid,
                "depart"=>isset($depart['itemid'])?$depart['itemid']:0,
                "status"=>0,
                "is_del"=>0,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s"),
            ];
            $zx = Db::name("consult_order")->insert($data);
            if($zx){
                foreach ($ladder as $value){
                    $infoNo=makeNo("IF");
                    $specinfo=[];
                    if(isset($value['specinfo']) &&!empty($value['specinfo'])){
                        foreach ($value["specinfo"] as $v){
                            $spec =Db::name("specs")->where(["id"=>$v['specid']])->find();
                            $spec_value =Db::name("spec_value")->where(["id"=>$v['spec_value_id']])->find();
                            $v['spec_name']=isset($spec['spec_name'])? $spec['spec_name']:"";
                            $v['spec_value_name']=isset($spec_value['spec_value'])?$spec_value['spec_value']:"" ;
                            $specinfo[]=$v;
                        }
                    }
                    $info=[
                        "infoNo"=>$infoNo,
                        "zxNo"=>$zxNo,
                        "spuCode"=>"",
                        "good_name"=>$value['good_name'],
                        "good_img"=>$value['good_img'],
                        "cat_id"=>$value['cat_id'],
                        "unit"=>$value['unit'],
                        "brand_id"=>$value['brand_id'],
                        "arrival_time"=>$value['arrival_time'],
                        "specinfo"=>empty($specinfo)?"" : json_encode($specinfo),
                        "cost_desc" => isset($value['cost_desc']) ? $value['cost_desc'] : '',
                        "is_addrs"=>$value['is_addrs'],
                        "is_custom"=>$value['is_custom'],
                        "metal_id"=>$value['metal_id'],
                        "config"=>isset($value['config'])?$value['config']:"",
                        "other_config"=>isset($value['other_config'])?$value['other_config']:"",
                        "specs_weight"=>isset($value['specs_weight'])?$value['specs_weight']:"0",
                        "gold_price"=>isset($value['gold_price'])?$value['gold_price']:"0",
                        "is_gold_price"=>isset($value['is_gold_price'])?$value['is_gold_price']:"0",
                        "total_weight"=>isset($value['total_weight'])?$value['total_weight']:"0",
                        "budget_price"=>$value['budget_price'],
                        "num"=>$value['num'],
                        "pgNo"=>isset($value['pgNo'])&&$value['pgNo']!==""?trim($value['pgNo']):"",
                        "use_desc"=>$value['use_desc'],
                        "remark"=>$value['remark'],
                        "status"=>1,
                        "is_del"=>0,
                        "addtime"=>date("Y-m-d H:i:s"),
                        "updatetime"=>date("Y-m-d H:i:s"),
                        'enclosure_file'=>isset($value['enclosure_file'])?$value['enclosure_file']:'',
                    ];
                    $up = Db::name("consult_info")->insert($info);
                    if(!$up){
                        Db::rollback();
                        return error_show(1004,'添加失败');
                    }else{
                        //修改状态,添加待办
                        ActionLog::logAdd($this->post['token'], [
                            "order_code" => $infoNo,//咨询单详情编号
                            "status" => 1,//这里的status是之前的值
                            "action_remark" => '',//备注
                            "action_type" => "create"//新建create,编辑edit,更改状态status
                        ], "ZXD", 1, $info);

//                        ProcessOrder::AddProcess($this->post['token'], [
//                            "order_type" => 'ZXD',
//                            "order_code" => $infoNo,//咨询单详情编号
//                            "order_id" => 0,
//                            "order_status" => 1
//                        ]);
                    }
                }
                Db::commit();
                return  app_show(0,"新建成功",["zxNo"=>$zxNo]);
            }else{
                Db::rollback();
                return error_show(1004,"新建失败");
            }
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1003,$e->getMessage());
        }

    }


    

    public function zxinfo(){
        $infoNo= isset($this->post['infoNo'])&&$this->post['infoNo']!=""?trim($this->post['infoNo']):"";
        if($infoNo==""){
            return error_show(1004,"参数infoNo不能为空");
        }
        $zxinfo = Db::name("consult_info")->where(["infoNo"=>$infoNo,"is_del"=>0])->find();
        if($zxinfo==false){
            return error_show(1004,"未找到信息数据");
        }
        $zxorder = Db::name("consult_order")->where(["zxNo"=>$zxinfo['zxNo'],"is_del"=>0])->find();
        if($zxorder==false){
            return error_show(1004,"未找到咨询信息数据");
        }
        $cat =Db::name("cat")->where(["id"=>$zxinfo['cat_id']])->find();
        $budget = isset($cat['order_rate']) ? $cat['order_rate']:0;
        $zxinfo['can']= isset($zxinfo['cat_id']) && $zxinfo['cat_id'] !=0 ? made($zxinfo['cat_id'],[]):[];
        $zxinfo['khname']="";
        $zxinfo['original_price']=sprintf("%.2f",$zxinfo['budget_price']* (1 -$budget/100));
        $zxinfo['khNo']="";
        $zxinfo['brand_id']=$zxinfo['brand_id']==0 ?"":$zxinfo['brand_id'];
        $zxinfo['brand']="";
        $zxinfo['companyNo']=isset($zxorder['companyNo'])?$zxorder['companyNo']:"";
        $zxinfo['projectNo']=isset($zxorder['projectNo'])?$zxorder['projectNo']:"";
        $zxinfo['is_project']=isset($zxorder['is_project'])?$zxorder['is_project']:"";
        $zxinfo['company']="";
        $zxinfo['endtime']=isset($zxorder['endtime'])?$zxorder['endtime']:"";
        $zxinfo['platform_code'] =isset($zxorder['platform_code'])?$zxorder['platform_code']:"";
        $zxinfo['salesman'] =isset($zxorder['salesman'])?$zxorder['salesman']:"";
        $zxinfo['saleid'] =isset($zxorder['saleid'])?$zxorder['saleid']:"";
        $zxinfo['depart'] =isset($zxorder['depart'])?$zxorder['depart']:"";
        $zxinfo['project_name']="";
        $zxinfo['budget_total']="";
        $zxinfo['departinfo']=[];
        $zxinfo['unit_name']="";
        $zxinfo['metal_name']=isset($zxinfo['metal_id'])&& $zxinfo['metal_id']!=0?$this->noble[$zxinfo['metal_id']]:"";
        $zxinfo['specinfo']=json_decode($zxinfo['specinfo'],true);

        if($zxorder['khNo']!==""){
            $kh=Db::name("customer_info")->where(['companyNo'=>$zxorder['khNo']])->find();
            $zxinfo['khname']= isset($kh['companyName'])?$kh['companyName']:"";
            $zxinfo['khNo']= $zxorder['khNo'];
        }
        if($zxinfo['unit']!==""){
            $unit=Db::name("unit")->where(['id'=>$zxinfo['unit']])->find();
            $zxinfo['unit_name']= isset($unit['unit'])?$unit['unit']:"";
        }
        if($zxinfo['brand_id']!==""){
            $unit=Db::name("brand")->where(['id'=>$zxinfo['brand_id']])->find();
            $zxinfo['brand']= isset($unit['brand_name'])?$unit['brand_name']:"";
        }
        if($zxorder['companyNo']!==""){
            $kh=Db::name("business")->where(['companyNo'=>$zxorder['companyNo']])->find();
            $zxinfo['company']= isset($kh['company'])?$kh['company']:"";
        }
        if($zxorder['is_project']==1 && $zxorder['projectNo']!==""){
            $project = Db::name("project")->where(["projectNo"=>$zxorder['projectNo'],"is_del"=>0])->find();
            $zxinfo['project_name']=isset($project['project_name'])?$project['project_name']:"";
            $zxinfo['budget_total']=isset($project['budget_total'])?$project['budget_total']:"";
            $zxinfo['project_use_desc']=isset($project['use_desc'])?$project['use_desc']:"";
            $projectinfo = Db::name("project_info")->where(["pgNo"=>$zxinfo['pgNo']])->find();
            $zxinfo['pgNo'] = isset($projectinfo['pgNo']) ? $projectinfo['pgNo']:"";
        }
        if($zxorder['depart']>0){
            $zxinfo['departinfo']=GetPart($zxorder['depart']);
        }
        if($zxorder['platform_code']>0){
            $palt=Db::name("platform")->where(["id"=>$zxorder['platform_code']])->find();
            $zxinfo['platform_name']=isset($palt['platform_name'])?$palt['platform_name']:"";
        }
        return app_show(0,"获取成功",$zxinfo);
    }

    public function zxedit(){
        $infoNo= isset($this->post['infoNo'])&&$this->post['infoNo']!=""?trim($this->post['infoNo']):"";
        if($infoNo==""){
            return error_show(1004,"参数infoNo不能为空");
        }
        $zxinfo = Db::name("consult_info")->where(["infoNo"=>$infoNo,"is_del"=>0])->find();
        if($zxinfo==false){
            return error_show(1004,"未找到信息数据");
        }

        $enclosure_file =isset($this->post['enclosure_file'])&&$this->post['enclosure_file']!=""?trim($this->post['enclosure_file']):$zxinfo['enclosure_file'];

        $good_name= isset($this->post['good_name'])&&$this->post['good_name']!=""?trim($this->post['good_name']):"";
        if($good_name==""){
            return error_show(1003,"参数good_name不能为空");
        }

        $good_img= isset($this->post['good_img'])&&$this->post['good_img']!=""?trim($this->post['good_img']):"";
        if($good_img==""){
            return error_show(1003,"参数good_img不能为空");
        }

        $cat_id= isset($this->post['cat_id'])&&$this->post['cat_id']!=""?intval($this->post['cat_id']):"";
        if($cat_id==""){
            return error_show(1003,"参数cat_id不能为空");
        }

        $unit= isset($this->post['unit'])&&$this->post['unit']!=""?trim($this->post['unit']):"";
        if($unit==""){
            return error_show(1003,"参数unit不能为空");
        }
        $brand_id= isset($this->post['brand_id'])&&$this->post['brand_id']!==""?intval($this->post['brand_id']):"0";

        $arrival_time= isset($this->post['arrival_time'])&&$this->post['arrival_time']!=""?$this->post['arrival_time']:"";
        if($arrival_time==""){
            return error_show(1003,"参数arrival_time不能为空");
        }
        $specin= isset($this->post['specinfo'])&&!empty($this->post['specinfo'])?$this->post['specinfo']:"";
        if($specin==""){
            return error_show(1003,"参数specinfo不能为空");
        }
        $cost_desc= isset($this->post['cost_desc'])&&$this->post['cost_desc']!=""?trim($this->post['cost_desc']):"";
//        if($cost_desc==""){
//            return error_show(1003,"参数cost_desc不能为空");
//        }
        $is_addrs= isset($this->post['is_addrs'])&&$this->post['is_addrs']!==""?intval($this->post['is_addrs']):"";
        if($is_addrs===""){
            return error_show(1003,"参数is_addrs不能为空");
        }
        $is_custom= isset($this->post['is_custom'])&&$this->post['is_custom']!==""?trim($this->post['is_custom']):"";
        if($is_custom===""){
            return error_show(1003,"参数is_custom不能为空");
        }
        $metal_id= isset($this->post['metal_id'])&&$this->post['metal_id']!==""?intval($this->post['metal_id']):"";
        if($metal_id===""){
            return error_show(1003,"参数metal_id不能为空");
        }
        $config = isset($this->post['config'])&& $this->post['config']!=""?trim($this->post['config']):"";
        $other_config = isset($this->post['other_config'])&& $this->post['other_config']!=""?trim($this->post['other_config']):"";
        $specs_weight = isset($this->post['specs_weight'])&& $this->post['specs_weight']!==""?floatval($this->post['specs_weight']):"";
        $gold_price = isset($this->post['gold_price'])&& $this->post['gold_price']!==""?floatval($this->post['gold_price']):"";
        $is_gold_price = isset($this->post['is_gold_price'])&& $this->post['is_gold_price']!==""?intval($this->post['is_gold_price']):"0";
        $budget_price = isset($this->post['budget_price'])&& $this->post['budget_price']!==""?floatval($this->post['budget_price']):"";
        $total_weight = isset($this->post['total_weight'])&& $this->post['total_weight']!==""?floatval($this->post['total_weight']):"";
        $num = isset($this->post['num'])&& $this->post['num']!==""?intval($this->post['num']):"";
        if($num===""){
            return error_show(1003,"参数num不能为空");
        }
        if($budget_price===""){
            return error_show(1003,"参数budget_price不能为空");
        }
        $pgNo = isset($this->post['pgNo'])&& $this->post['pgNo']!="" ? trim($this->post['pgNo']):"";
        $use_desc = isset($this->post['use_desc'])&& $this->post['use_desc']!="" ? trim($this->post['use_desc']):"";
        if($use_desc==""){
            return error_show(1003,"参数use_desc不能为空");
        }
        $remark = isset($this->post['remark'])&& $this->post['remark']!="" ? trim($this->post['remark']):"";
        if($remark==""){
            return error_show(1003,"参数remark不能为空");
        }
        $endtime = isset($this->post['endtime'])&& $this->post['endtime']!="" ? trim($this->post['endtime']):"";
        if($endtime==""){
            return error_show(1003,"参数endtime不能为空");
        }
        if($metal_id!=0){
            if($config==""){
                return error_show(1003,"参数config不能为空");
            }
            if($other_config==""){
                return error_show(1003,"参数other_config不能为空");
            }
            if($specs_weight===""){
                return error_show(1003,"参数specs_weight不能为空");
            }
            if($gold_price===""){
                return error_show(1003,"参数gold_price不能为空");
            }
        }
        $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
        if($token==''){
            return error_show(102,"参数token不能为空");
        }
        $user =GetUserInfo($token);
        if(empty($user)||$user['code']!=0){
            return error_show(105,"用户数据不存在");
        }
        $specinfo=[];
        foreach ($specin as $v){
            $spec =Db::name("specs")->where(["id"=>$v['specid']])->find();
            $spec_value =Db::name("spec_value")->where(["id"=>$v['spec_value_id']])->find();
            $v['spec_name']=isset($spec['spec_name'])?$spec['spec_name']:"";
            $v['spec_value_name']=isset($spec_value['spec_value'])?$spec_value['spec_value']:"";
            $specinfo[]=$v;
        }
        $createrid= isset($user["data"]['id']) ?  $user["data"]['id'] : "";
        $creater= isset($user["data"]['nickname']) ?  $user["data"]['nickname'] : "";
        $bidlist = Db::name("consult_bids")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->select();
        Db::startTrans();
        try{
            $innew =makeNo("IF");
            $info=[
                "infoNo"=>$innew,
                "zxNo"=>$zxinfo['zxNo'],
                "spuCode"=>"",
                "good_name"=>$good_name,
                "good_img"=>$good_img,
                "cat_id"=>$cat_id,
                "unit"=>$unit,
                "brand_id"=>$brand_id,
                "arrival_time"=>$arrival_time,
                "specinfo"=>json_encode($specinfo),
//                "color"=>$color,
//                "material"=>$material,
                "cost_desc"=>$cost_desc,
                "is_addrs"=>$is_addrs,
                "is_custom"=>$is_custom,
                "metal_id"=>$metal_id,
                "config"=>$config,
                "other_config"=>$other_config,
                "specs_weight"=>$specs_weight===""?0:$specs_weight,
                "gold_price"=>$gold_price===""? 0 : $gold_price,
                "is_gold_price"=>$is_gold_price===""?0:$is_gold_price,
                "total_weight"=>$total_weight===""? $num*$specs_weight :$total_weight,
                "budget_price"=>$budget_price,
                "num"=>$num,
                "pgNo"=>$pgNo,
                "use_desc"=>$use_desc,
                "remark"=>$remark,
                "status"=>1,
                "is_del"=>0,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s"),
                'bargain_status'=>1,
                'enclosure_file'=>$enclosure_file
            ];
            $up = Db::name("consult_info")->insert($info);
            if($up){

                //操作记录
                ActionLog::logAdd($this->post['token'], [
                    "order_code" => $innew,//咨询单详情编号
                    "status" => 1,//这里的status是之前的值
                    "action_remark" => '',//备注
                    "action_type" => "edit"//新建create,编辑edit,更改状态status
                ], "ZXD", 1, $this->post);

//                ProcessOrder::AddProcess($this->post['token'], [
//                    "order_type" => 'ZXD',
//                    "order_code" => $innew,//咨询单详情编号
//                    "order_id" => 0,
//                    "order_status" => 1
//                ]);

                if (!empty($bidlist)){
                    foreach ($bidlist as $value){
                        unset($value['id']);
                        $value['infoNo']=$innew;
                        $value['bidNo']=makeNo("BD");
                        $value['spuCode']=makeNo("SKU");
                        $value['status'] =0;
                        $value['updatetime'] =date("Y-m-d H:i:s");
                        $in = Db::name("consult_bids")->insert($value);
                        if($in==false){
                            Db::rollback();
                            return error_show(1004,"反馈商品新建失败");
                        }
                        ActionLog::logAdd($this->post['token'], [
                            "order_code" => $innew,//咨询单详情编号
                            "status" => 1,//这里的status是之前的值
                            "action_remark" => '',//备注
                            "action_type" => "edit"//新建create,编辑edit,更改状态status
                        ], "FKD", 1, $this->post);
                    }
                }
                $zxinfo["bargain_status"] =1;
                $zxinfo["bargain_code"] =$innew;
                $zxinfo["updatetime"] =date("Y-m-d H:i:s");
                $bar=Db::name("consult_info")->save($zxinfo);
                if($bar==false){
                    Db::rollback();
                    return error_show(1004,"反馈商品更新失败");
                }else{
                    //修改状态,添加待办
                    ActionLog::logAdd($this->post['token'], [
                        "order_code" => $zxinfo['infoNo'],//咨询单详情编号
                        "status" => $zxinfo['status'],//这里的status是之前的值
                        "action_remark" => '',//备注
                        "action_type" => "edit"//新建create,编辑edit,更改状态status
                    ], "ZXD", $zxinfo['status'], $zxinfo);
                }

                //更新consule_order记录
             $op = Db::name('consult_order')->where('zxNo',$zxinfo['zxNo'])->update(['endtime'=>$endtime,
                    'updatetime'=>date('Y-m-d H:i:s')]);
                if($op==false){
                    Db::rollback();
                    return error_show(1004,'咨询单结束时间更新失败');
                }
                Db::commit();
                return app_show(0,"咨询新建成功",["infoNo"=>$innew]);
            }
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1004,$e->getMessage());
        }

    }
    //** 发布竞标任务 */
    public function  bargain(){
        $infoNo= isset($this->post['infoNo'])&&$this->post['infoNo']!=""?trim($this->post['infoNo']):"";
        if($infoNo==""){
            return error_show(1004,"参数infoNo不能为空");
        }
        $zxinfo = Db::name("consult_info")->where(["infoNo"=>$infoNo,"is_del"=>0])->find();
        if($zxinfo==false){
            return error_show(1004,"未找到信息数据");
        }
        $status =isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
        if($status===""){
            return error_show(1004,"参数status不能为空");
        }
//        if($status==1&&$zxinfo['status']>1){
//            $zxinfo['bargain_num']=+1;
//        }
        $old_zxinfo_status = $zxinfo['status'];
        $zxinfo['status']=$status;
        $zxinfo['updatetime']=date("Y-m-d H:i:s");
        $up = Db::name("consult_info")->save($zxinfo);
        if($up){

            //修改状态,添加待办
            ActionLog::logAdd($this->post['token'], [
                "order_code" => $zxinfo['infoNo'],//咨询单详情编号
                "status" => $old_zxinfo_status,//这里的status是之前的值
                "action_remark" => '',//备注
                "action_type" => "status"//新建create,编辑edit,更改状态status
            ], "ZXD", $zxinfo['status'], $zxinfo);
                        return app_show(0,"状态更新成功");
        }else{
            return error_show(1003,"状态更新失败");
        }
    }

    //反馈商品添加
    public function feadback(){
        $infoNo= isset($this->post['infoNo'])&&$this->post['infoNo']!=""?trim($this->post['infoNo']):"";
        if($infoNo==""){
            return error_show(1002,"参数infoNo不能为空");
        }
        $zxinfo = Db::name("consult_info")->where(["infoNo"=>$infoNo,"is_del"=>0])->find();
        if($zxinfo==false){
            return error_show(1003,"未找到信息数据");
        }
        if($zxinfo['status']!=1){
            return error_show(1004,"任务未进行");
        }
        $zx = Db::name("consult_order")->where(["zxNo"=>$zxinfo['zxNo'],"is_del"=>0])->find();
        if($zx==false){
            return error_show(1003,"未找咨询信息数据");
        }
        $pname = isset($this->post['pname'])&&$this->post['pname']!=""? trim($this->post['pname']):"";
        if($pname==""){
            return error_show(1002,"参数pname不能为空");
        }
        $brandid = isset($this->post['brandid'])&&$this->post['brandid']!=""? intval($this->post['brandid']):"";
        $cat_id = isset($this->post['cat_id'])&&$this->post['cat_id']!=""? trim($this->post['cat_id']):"";
        if($cat_id==""){
            return error_show(1002,"参数cat_id不能为空");
        }
        $specin= isset($this->post['specinfo'])&&!empty($this->post['specinfo'])?$this->post['specinfo']:"";
        if($specin==""){
            return error_show(1003,"参数specinfo不能为空");
        }
        $specinfo=[];
        foreach ($specin as $v){
            $spec =Db::name("specs")->where(["id"=>$v['specid']])->find();
            $spec_value =Db::name("spec_value")->where(["id"=>$v['spec_value_id']])->find();
            $v['spec_name']=isset($spec['spec_name'])?$spec['spec_name']:"";
            $v['spec_value_name']=isset($spec_value['spec_value'])?$spec_value['spec_value']:"";
            $specinfo[]=$v;
        }
        $unit_id = isset($this->post['unit_id'])&&$this->post['unit_id']!=""? intval($this->post['unit_id']):"";
        $cost_desc = isset($this->post['cost_desc'])&&$this->post['cost_desc']!=""? trim($this->post['cost_desc']):"";
        $work_day = isset($this->post['work_day'])&&$this->post['work_day']!=""? intval($this->post['work_day']):"";
        $delivery_day = isset($this->post['delivery_day'])&&$this->post['delivery_day']!=""? intval($this->post['delivery_day']):"";
        $good_img = isset($this->post['good_img'])&&$this->post['good_img']!=""? $this->post['good_img']:[];
        $expire_day = isset($this->post['expire_day'])&&$this->post['expire_day']!=""? intval($this->post['expire_day'])
            :"";
        $origin_place = isset($this->post['origin_place'])&&$this->post['origin_place']!=""? trim($this->post['origin_place']):"";
        $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""? trim($this->post['supplierNo']):"";
        $pay_way = isset($this->post['pay_way'])&&$this->post['pay_way']!=""? intval($this->post['pay_way']):"";
        $tax = isset($this->post['tax'])&&$this->post['tax']!=""? trim($this->post['tax']):"";
        $send_way = isset($this->post['send_way'])&&$this->post['send_way']!=""? intval($this->post['send_way']):"";
        $metal_id = isset($this->post['metal_id'])&&$this->post['metal_id']!=""? intval($this->post['metal_id']):"0";
        $is_gold_price = isset($this->post['is_gold_price'])&&$this->post['is_gold_price']!=""? intval($this->post['is_gold_price']):"0";
        $config = isset($this->post['config'])&&$this->post['config']!=""? $this->post['config']:"";
        $other_config = isset($this->post['other_config'])&&$this->post['other_config']!=""? $this->post['other_config']:"";
        $weight = isset($this->post['weight'])&&$this->post['weight']!==""? floatval($this->post['weight']):"0";
        $good_weight = isset($this->post['noble_weight'])&&$this->post['noble_weight']!==""? floatval($this->post['noble_weight']):"0";
        $demo_fee = isset($this->post['demo_fee'])&&$this->post['demo_fee']!==""? floatval($this->post['demo_fee']):"0";
        $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!==""? floatval($this->post['delivery_fee']):"0";
        $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!==""? floatval($this->post['open_fee']):"0";
        $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!==""? floatval($this->post['pakge_fee']):"0";
        $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!==""? floatval($this->post['nake_fee']):"0";
        $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!==""? floatval($this->post['mark_fee']):"0";
        $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!==""? floatval($this->post['cert_fee']):"0";
        $cost_fee = isset($this->post['cost_fee'])&&$this->post['cost_fee']!==""? floatval($this->post['cost_fee']):"0";
        if($nake_fee===""  ){
            return error_show(1004,"参数nake_fee不能为空");
            }
        $is_diff = isset($this->post['is_diff'])&&$this->post['is_diff']!==""? intval($this->post['is_diff']):"";
        $supply_area = isset($this->post['supply_area'])&&$this->post['supply_area']!==""? intval($this->post['supply_area']):"";
        $remark = isset($this->post['remark'])&&$this->post['remark']!=""? trim($this->post['remark']):"";
        $delivery_place = isset($this->post['delivery_place'])&& $this->post['delivery_place']!='' ? trim($this->post['delivery_place']):"";
        if($delivery_place==''){
            return error_show(1002,"参数delivery_place不能为空");
        }
        $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
        if($token==''){
            return error_show(1002,"参数token不能为空");
        }
        $user =GetUserInfo($token);
        if(empty($user)||$user['code']!=0){
            return error_show(1005,"用户数据不存在");
        }
        $createrid= isset($user["data"]['id']) ?  $user["data"]['id'] : "";
        $creater= isset($user["data"]['nickname']) ?  $user["data"]['nickname'] : "";
        $bidNo=makeNo("BD");
        $spucode=makeNo("SKU");
        $catinfo = Db::name("cat")->where(["id"=>$cat_id])->find();
        $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;

        //部分参数的大小校验
        $vali = Validate::rule([
            'expire_day|有效时长'=>'require|elt:214748364',
            'delivery_day|物流时长'=>'require|elt:214748364',
            'work_day|生产工期'=>'require|elt:214748364',
            'weight|总重量'=>'require|max:10',
        ]);

        $temp = [
            'expire_day'=>$expire_day,
            'delivery_day'=>$delivery_day,
            'work_day'=>$work_day,
            'weight'=>$weight,
        ];

        if(!$vali->check($temp)) return error_show(0,$vali->getError());


        $sale_cost_fee = $cost_fee;//销售工艺费默认取传值

        $cat_top_list = made($cat_id);
        $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
        if ($is_gold_price == 1 && $cat_top_id == 6) {
            $gold = Db::name("gold_price1")->where(["type"=>$metal_id,"is_del"=>0,"status"=>1])->order("addtime desc")
                ->find();

            //$total_fee = $open_fee/$zxinfo['num'] + $weight* $gold["price"] + $cost_fee*$weight+$pakge_fee+$mark_fee+$cert_fee+$nake_fee;
            //$total_fee(成本合计初始价格) =打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价0+运费;
            $total_fee = $demo_fee / $zxinfo['num'] + $open_fee / $zxinfo['num'] + $good_weight * $gold["price"] + $cost_fee * $good_weight + $pakge_fee + $mark_fee + $cert_fee + $nake_fee + $delivery_fee;

//            $saleprice = $open_fee/$zxinfo['num'] + $weight* $gold["price"] + $cost_fee/(1-$budget)* $weight+$pakge_fee+$mark_fee+$cert_fee+$nake_fee;
            //$saleprice(最终售价) = (开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价)/(1-成本售价/100);
//            $saleprice = ($open_fee / $zxinfo['num'] + $weight * $gold["price"] + $cost_fee * $weight + $pakge_fee + $mark_fee + $cert_fee + $nake_fee) / (1 - $budget);
            $saleprice = $total_fee/ (1 - $budget);

            //销售工艺费=((开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价)/(1-成本售价/100)-(打样费/购买数量 + 开模费/购买数量 +包装费+加标费+证书费+产品裸价0+运费) )/商品重量-最新金价
            $sale_cost_fee = ($saleprice - ($demo_fee / $zxinfo['num'] + $open_fee / $zxinfo['num'] + $pakge_fee + $mark_fee + $cert_fee + $nake_fee + $delivery_fee)) /$good_weight - $gold["price"];

        }else{

            if($nake_fee){
                $saleprice = ($demo_fee / $zxinfo['num']+$delivery_fee+$open_fee / $zxinfo['num'] + $cost_fee * $good_weight +
                        $pakge_fee + $mark_fee + $cert_fee +$nake_fee)/ (1 - $budget);
                $total_fee = $demo_fee / $zxinfo['num']+$delivery_fee+$open_fee / $zxinfo['num'] + $cost_fee *$good_weight + $pakge_fee + $mark_fee + $cert_fee + $nake_fee;
            }else{
                $saleprice = ($demo_fee/$zxinfo['num']+$delivery_fee+$open_fee / $zxinfo['num'] + $cost_fee * $good_weight + $pakge_fee + $mark_fee + $cert_fee) /
                    (1 - $budget);
                $total_fee = $demo_fee/$zxinfo['num']+$delivery_fee+$open_fee / $zxinfo['num'] + $cost_fee *$good_weight + $pakge_fee + $mark_fee + $cert_fee;
            }

        }


        $data=[
            "bidNo"=>$bidNo,
            "infoNo"=>$infoNo,
            "zxNo"=>$zxinfo['zxNo'],
            "spuCode"=>$spucode,
            "good_name"=>$pname,
            "brand_id"=>$brandid,
            "cat_id"=>$cat_id,
            "specinfo"=>json_encode($specinfo),
            "unit_id"=>$unit_id,
            "cost_desc"=>$cost_desc,
            "work_day"=>$work_day,
            "delivery_day"=>$delivery_day,
            "good_img"=>is_array($good_img)?implode(',',$good_img):$good_img,
            "expire_day"=>$expire_day,
            "origin_place"=>$origin_place,
            "supplierNo"=>$supplierNo,
            "pay_way"=>$pay_way,
            "tax"=>$tax,
            "send_way"=>$send_way,
            "metal_id"=>$metal_id,
            "is_gold_price"=>$is_gold_price,
            "config"=>$config,
            "other_config"=>$other_config,
            "weight"=>$weight,
            "good_weight"=>$good_weight,
            "gold_price"=>isset($gold["price"])?$gold["price"]:0,
            "is_diff"=>$is_diff,
            "demo_fee"=>$demo_fee,
            "delivery_fee"=>$delivery_fee,
            "open_fee"=>$open_fee,
            "pakge_fee"=>$pakge_fee,
            "nake_fee"=>$nake_fee,
            "mark_fee"=>$mark_fee,
            "cert_fee"=>$cert_fee,
            "cost_fee"=>$cost_fee,
            "total_fee"=>$total_fee,
            "supply_area"=>$supply_area,
            "remark"=>$remark,
            "sale_price"=>$saleprice,
            "sale_cost_fee"=>$sale_cost_fee,
            "origin_cost_fee"=>$sale_cost_fee,
            "origin_price"=>$saleprice,
            "createrid"=>$createrid,
            "creater"=>$creater,
            "status"=>$zxinfo['status']==1 ?1 :2,
            "is_del"=>0,
            "addtime"=>date("Y-m-d H:i:s"),
            "updatetime"=>date("Y-m-d H:i:s"),
            'delivery_place'=>$delivery_place//发货地
        ];
        Db::startTrans();
        try{
            $insert= Db::name("consult_bids")->insert($data);
            if($insert){
                    if($zx['is_project']==1 && $zx['projectNo']!="" && $zxinfo['pgNo']!=""){
                        $projectinfo =Db::name("project_info")->where(["pgNo"=>$zxinfo["pgNo"],"projectNo"=>$zx['projectNo'],"is_del"=>0])->find();
                        if($projectinfo==false){
                            Db::rollback();
                            return error_show(1006,"未找到项目信息");
                        }
                        $temp=[];
                        $temp['spuCode']=$spucode;
                        $temp['skuCode']="";
                        $temp['pgNo']=$zxinfo['pgNo'];
                        $temp['projectNo']=$zx['projectNo'];
                        $temp['good_name']=$pname;
                        $temp['good_type']=$projectinfo['good_type'];
                        $temp['data_source']=2;
                        $temp['cat_id']=$cat_id;
                        $temp['budget_price']=$projectinfo['budget_price'];
                        $temp['sale_price']=$saleprice;
                        $temp['origin_price']=$total_fee;
                        $temp['num']=$zxinfo['num'];
                        $temp['status']=0;
                        $temp['is_del']=0;
                        $temp['creater']=$creater;
                        $temp['createrid']=$createrid;
                        $temp['addtime']=date("Y-m-d H:i:s");
                        $temp['updatetime']=date("Y-m-d H:i:s");
                        $nu = Db::name("project_feedback")->insert($temp);
                        if($nu==false){
                            Db::rollback();
                            return error_show(1006,"项目反馈失败");
                        }else{
                            $old_projectinfo_status = $projectinfo['status'];
                            $projectinfo['status']=2;
                            $projectinfo['updatetime']=date("Y-m-d H:i:s");
                            $up = Db::name("project_info")->save($projectinfo);
                            if($up){
                                //修改状态,添加待办,只记录动作
                                ActionLog::logAdd($this->post['token'], [
                                    "order_code" => $projectinfo['pgNo'],//编码
                                    "status" => $old_projectinfo_status,//这里的status是之前的值
                                    "action_remark" => '',//备注
                                    "action_type" => "status"//新建create,编辑edit,更改状态status
                                ], "PRI", 2, $projectinfo);

                                $count = Db::name("project_info")->where(["projectNo"=>$projectinfo['projectNo'],"status"=>1])
                                    ->count();
                                if($count==0){
                                    $projetc =Db::name("project")->where(["projectNo"=>$projectinfo['projectNo'],"status"=>1])->find();
                                    if($projetc!=false){
                                        $proc =Db::name("project")->where(["projectNo"=>$projectinfo['projectNo'],"status"=>1])->save
                                        (["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
                                        if($proc==false){
                                            Db::rollback();
                                            return error_show(1006,"项目反馈失败");
                                        }else{
                                            //修改状态,添加待办
                                            ActionLog::logAdd($this->post['token'], [
                                                "order_code" => $projetc['projectNo'],//项目编码
                                                "status" => 1,//这里的status是之前的值
                                                "action_remark" => '',//备注
                                                "action_type" => "status"//新建create,编辑edit,更改状态status
                                            ], "PRO", 2, $projetc);

                                            ProcessOrder::AddProcess($this->post['token'], [
                                                "order_type" => 'PRO',
                                                "order_code" => $projetc['projectNo'],
                                                "order_id" => $projetc['id'],
                                                "order_status" => 2,"before_status"=>1
                                            ]);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    Db::commit();
                    return app_show(0,"反馈成功",["bidNo"=>$bidNo]);

            }else{
                Db::rollback();
                return error_show(1006,"反馈失败");
            }
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1006,$e->getMessage());
        }
    }

    //反馈商品详情
    public function feadinfo(){
       $bidNo =isset($this->post['bidNo'])&& $this->post['bidNo']!=""?trim($this->post['bidNo']):"";
       if($bidNo==""){
           return error_show(1004,"参数bidNo不能为空");
       }
       $info =Db::name("consult_bids")->where(['bidNo'=>$bidNo,"is_del"=>0])->find();
       if($info==false){
           return error_show(1004,"未找到商品数据");
       }
        $unit =Db::name("unit")->where(["id"=>$info['unit_id']])->find();
        $info['unit'] = isset($unit['unit'])?$unit['unit']:'';
        $info['cat_info'] = made($info['cat_id'],[]);
        $info['noble_weight'] = $info['good_weight'];
        $supplier = Db::name("supplier")->where(["code"=>$info['supplierNo']])->find();
        $info['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
        if($info['brand_id']!=0){
            $brand=Db::name("brand")->where(["id"=>$info['brand_id']])->find();
            $info["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
        }else{
            $info["brand_name"]="";
            $info["brand_id"]="";
        }
        $catinfo = Db::name("cat")->where(["id"=>$info['cat_id']])->find();
        $bidinfo = Db::name("consult_info")->where(["infoNo"=>$info["infoNo"]])->find();
        $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']:0;
        $god=[
            "metal_id"=>$info['metal_id'],
            "weight"=>$info["good_weight"],
            "demo_fee"=>$info["demo_fee"],
            "delivery_fee"=>$info["delivery_fee"],
            "open_fee"=>$info["open_fee"],
            "packing_fee"=>$info["pakge_fee"],
            "mark_fee"=>$info["mark_fee"],
            "nake_fee"=>$info["nake_fee"],
            "cert_fee"=>$info["cert_fee"],
            "cost_fee"=>$info["cost_fee"],
            "num"=>$bidinfo["num"],
        ];
        if($info['is_gold_price']==1){
            $price = GoldPrice($god, $budget/100);
            $info['sale_price'] =round($price,2);
        }
        $info['specinfo'] = json_decode($info['specinfo'],true);
        $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
        if($info['delivery_place']!=""){
            list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$info['delivery_place']);
        }
        $info['delivery_place_cn']=GetAddr(json_encode($place));
        $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
        if($info['origin_place']!=""){
            list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$info['origin_place']);
        }

        $info['origin_place_cn']=GetAddr(json_encode($place));
        $info['metal_name']=isset($info['metal_id'])&& $info['metal_id']!=0?$this->noble[$info['metal_id']]:"";
        $bargain =Db::name("bargain_order")->where(['bidsNo'=>$bidNo,"is_del"=>0])->find();
        $info['bargain'] =$bargain;
        $info['good_img'] = explode(',',$info['good_img']);
        return app_show(0,"获取成功",$info);
    }

    //反馈商品编辑
    public function feededit(){
        $bidNo= isset($this->post['bidNo'])&&$this->post['bidNo']!=""?trim($this->post['bidNo']):"";
        if($bidNo==""){
            return error_show(1002,"参数bidNo不能为空");
        }
        $bidinfo = Db::name("consult_bids")->where(["bidNo"=>$bidNo,"is_del"=>0])->find();
        if($bidinfo==false){
            return error_show(1003,"未找到信息数据");
        }
        if($bidinfo['status']>=5){
            return error_show(1004,"咨询单已确认无法修改");
        }

        $zxinfo = Db::name("consult_info")->where(["infoNo"=>$bidinfo['infoNo'],"is_del"=>0])->find();
        if($zxinfo==false){
            return error_show(1003,"未找到信息数据");
        }
        $zx = Db::name("consult_order")->where(["zxNo"=>$zxinfo['zxNo'],"is_del"=>0])->find();
        if($zx==false){
            return error_show(1003,"未找咨询信息数据");
        }
        $pname = isset($this->post['pname'])&&$this->post['pname']!=""? trim($this->post['pname']):"";
        if($pname==""){
            return error_show(1002,"参数pname不能为空");
        }
        $brandid = isset($this->post['brandid'])&&$this->post['brandid']!=""? intval($this->post['brandid']):"";
//        if($brandid==""){
//            return error_show(1002,"参数brandid不能为空");
//        }

        $cat_id = isset($this->post['cat_id'])&&$this->post['cat_id']!=""? trim($this->post['cat_id']):"";
        if($cat_id==""){
            return error_show(1002,"参数cat_id不能为空");
        }
        $specin= isset($this->post['specinfo'])&&!empty($this->post['specinfo'])?$this->post['specinfo']:"";
        if($specin==""){
            return error_show(1003,"参数specinfo不能为空");
        }
        $specinfo=[];
        foreach ($specin as $v){
            $spec =Db::name("specs")->where(["id"=>$v['specid']])->find();
            $spec_value =Db::name("spec_value")->where(["id"=>$v['spec_value_id']])->find();
            $v['spec_name']=isset($spec['spec_name'])?$spec['spec_name']:"";
            $v['spec_value_name']=isset($spec_value['spec_value'])?$spec_value['spec_value']:"";
            $specinfo[]=$v;
        }
        $unit_id = isset($this->post['unit_id'])&&$this->post['unit_id']!=""? intval($this->post['unit_id']):"";
        $cost_desc = isset($this->post['cost_desc'])&&$this->post['cost_desc']!=""? trim($this->post['cost_desc']):"";
        $work_day = isset($this->post['work_day'])&&$this->post['work_day']!=""? intval($this->post['work_day']):"";
        $delivery_day = isset($this->post['delivery_day'])&&$this->post['delivery_day']!=""? intval($this->post['delivery_day']):"";
        $good_img = isset($this->post['good_img'])&&$this->post['good_img']!=""? $this->post['good_img']:[];
        $expire_day = isset($this->post['expire_day'])&&$this->post['expire_day']!=""? intval($this->post['expire_day'])
            :"";
        $origin_place = isset($this->post['origin_place'])&&$this->post['origin_place']!=""? trim($this->post['origin_place']):"";
        $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""? trim($this->post['supplierNo']):"";
        $pay_way = isset($this->post['pay_way'])&&$this->post['pay_way']!=""? intval($this->post['pay_way']):"";
        $tax = isset($this->post['tax'])&&$this->post['tax']!=""? trim($this->post['tax']):"";
        $send_way = isset($this->post['send_way'])&&$this->post['send_way']!=""? intval($this->post['send_way']):"0";
        $metal_id = isset($this->post['metal_id'])&&$this->post['metal_id']!=""? intval($this->post['metal_id']):"0";
        $is_gold_price = isset($this->post['is_gold_price'])&&$this->post['is_gold_price']!=""? intval($this->post['is_gold_price']):"0";
        $config = isset($this->post['config'])&&$this->post['config']!=""? $this->post['config']:"";
        $other_config = isset($this->post['other_config'])&&$this->post['other_config']!=""? $this->post['other_config']:"";
        $weight = isset($this->post['weight'])&&$this->post['weight']!=""? floatval($this->post['weight']):"0";
        $noble_weight = isset($this->post['noble_weight'])&&$this->post['noble_weight']!=""? floatval($this->post['noble_weight']):"0";
        $demo_fee = isset($this->post['demo_fee'])&&$this->post['demo_fee']!=""? floatval($this->post['demo_fee']):"0";
        $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=""? floatval($this->post['delivery_fee']):"0";
        $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=""? floatval($this->post['open_fee']):"0";
        $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=""? floatval($this->post['pakge_fee']):"0";
        $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!==""? floatval($this->post['nake_fee']):0;

        $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=""? floatval($this->post['mark_fee']):"0";
        $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=""? floatval($this->post['cert_fee']):"0";
        $cost_fee = isset($this->post['cost_fee'])&&$this->post['cost_fee']!=""? floatval($this->post['cost_fee']):"0";
        $is_diff = isset($this->post['is_diff'])&&$this->post['is_diff']!==""? intval($this->post['is_diff']):"";
        $supply_area = isset($this->post['supply_area'])&&$this->post['supply_area']!==""? intval($this->post['supply_area']):"";
        $remark = isset($this->post['remark'])&&$this->post['remark']!=""? trim($this->post['remark']):"";
        $delivery_place = isset($this->post['delivery_place'])&& $this->post['delivery_place']!='' ? trim($this->post['delivery_place']):"";
        if($delivery_place==''){
            return error_show(1002,"参数delivery_place不能为空");
        }
        $catinfo = Db::name("cat")->where(["id"=>$cat_id])->find();
        $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;

        $sale_cost_fee = $bidinfo['sale_cost_fee'];

        $cat_top_list = made($cat_id);
        $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
        if ($is_gold_price == 1 && $cat_top_id == 6) {
            $gold = Db::name("gold_price1")->where(["type"=>$metal_id,"is_del"=>0,"status"=>1])->order("addtime desc")
                ->find();

            //$total_fee(成本合计初始价格) =打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价0+运费;
            $total_fee = $demo_fee / $zxinfo['num'] + $open_fee / $zxinfo['num'] + $noble_weight * $gold["price"] + $cost_fee * $noble_weight + $pakge_fee + $mark_fee + $cert_fee + $nake_fee + $delivery_fee;

//            $saleprice = $open_fee/$zxinfo['num'] + $weight* $gold["price"] + $cost_fee/(1-$budget)* $weight+$pakge_fee+$mark_fee+$cert_fee+$nake_fee;
            //$saleprice(最终售价) = (开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价)/(1-成本售价/100);
            $saleprice = $total_fee  / (1 - $budget);

            //销售工艺费=((开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价)/(1-成本售价/100)-(打样费/购买数量 + 开模费/购买数量 +包装费+加标费+证书费+产品裸价0+运费) )/商品重量-最新金价
            $sale_cost_fee = ( $saleprice- ($demo_fee / $zxinfo['num'] + $open_fee / $zxinfo['num'] + $pakge_fee + $mark_fee + $cert_fee + $nake_fee + $delivery_fee)) / $noble_weight - $gold["price"];

        }else{
            $saleprice = ($demo_fee / $zxinfo['num']+$delivery_fee+$open_fee / $zxinfo['num'] + $cost_fee*$noble_weight + $pakge_fee +
                    $mark_fee + $cert_fee + $nake_fee) / (1 - $budget);
            $total_fee = $demo_fee/$zxinfo['num']+$delivery_fee+$open_fee/$zxinfo['num'] + $cost_fee * $noble_weight + $pakge_fee + $mark_fee+$cert_fee +$nake_fee;
        }
        Db::startTrans();
        try{
            $data=[
                "good_name"=>$pname,
                "brand_id"=>$brandid,
                "cat_id"=>$cat_id,
                "specinfo"=>json_encode($specinfo),
//            "material"=>$material,
                "unit_id"=>$unit_id,
                "cost_desc"=>$cost_desc,
                "work_day"=>$work_day,
                "delivery_day"=>$delivery_day,
                "good_img"=>is_array($good_img)?implode(',',$good_img):$good_img,
                "expire_day"=>$expire_day,
                "origin_place"=>$origin_place,
                "supplierNo"=>$supplierNo,
                "pay_way"=>$pay_way,
                "tax"=>$tax,
                "send_way"=>$send_way,
                "metal_id"=>$metal_id,
                "is_gold_price"=>$is_gold_price,
                "config"=>$config,
                "other_config"=>$other_config,
                "weight"=>$weight,
                "good_weight"=>$noble_weight,
                "is_diff"=>$is_diff,
                "demo_fee"=>$demo_fee,
                "gold_price"=>isset($gold["price"])?$gold["price"]:0,
                "delivery_fee"=>$delivery_fee,
                "open_fee"=>$open_fee,
                "pakge_fee"=>$pakge_fee,
                "nake_fee"=>$nake_fee,
                "mark_fee"=>$mark_fee,
                "cert_fee"=>$cert_fee,
                "cost_fee"=>$cost_fee,
                "sale_cost_fee"=>$sale_cost_fee,
                "origin_cost_fee"=>$sale_cost_fee,
                "total_fee"=>$total_fee,
                "sale_price"=>$saleprice,
                "origin_price"=>$saleprice,
                "supply_area"=>$supply_area,
                "remark"=>$remark,
                "status"=>0,
                "updatetime"=>date("Y-m-d H:i:s"),
                'delivery_place' => $delivery_place
            ];
            $insert= Db::name("consult_bids")->where($bidinfo)->save($data);
            if($insert){
                if($zxinfo['bargain_status']==1){
                    $zxinfo['bargain_status']=0;
                    $infoip =Db::name("consult_info")->save($zxinfo);
                    if($infoip==false){
                        Db::rollback();
                        return error_show(1006,"咨询信息状态更新失败");
                    }
                }
                if($zx['is_project']==1 && $zx['projectNo']!="" && $zxinfo['pgNo']!=""){
                    $projectinfo =Db::name("project_info")->where(["pgNo"=>$zxinfo["pgNo"],"projectNo"=>$zx['projectNo'],"is_del"=>0])->find();
                    if($projectinfo==false){
                        Db::rollback();
                        return error_show(1006,"未找到项目信息");
                    }
                    $feed = Db::name("project_feedback")->where(["pgNo"=>$zxinfo["pgNo"],"projectNo"=>$zx['projectNo'],"spuCode"=>$bidinfo['spuCode'],"is_del"=>0])->find();
                  if($feed==false){
                      Db::rollback();
                      return error_show(1006,"未找到项目反馈信息");
                  }
                $feed['good_name']=$pname;
                $feed['good_type']=$projectinfo['good_type'];
                $feed['cat_id']=$cat_id;
                $feed['sale_price']=$saleprice;
                $feed['origin_price']=$total_fee;
                $feed['updatetime']=date("Y-m-d H:i:s");

                $temp['updatetime']=date("Y-m-d H:i:s");
                $nu = Db::name("project_feedback")->save($feed);
                if($nu==false){
                    Db::rollback();
                    return error_show(1006,"项目反馈失败");
                }else{
                    $old_projectinfo_status = $projectinfo['status'];
                    $projectinfo['status']=2;
                    $projectinfo['updatetime']=date("Y-m-d H:i:s");
                    $up = Db::name("project_info")->save($projectinfo);
                    if($up){

                        //修改状态,添加待办,只记录动作
                        ActionLog::logAdd($this->post['token'], [
                            "order_code" => $projectinfo['pgNo'],//编码
                            "status" => $old_projectinfo_status,//这里的status是之前的值
                            "action_remark" => '',//备注
                            "action_type" => "status"//新建create,编辑edit,更改状态status
                        ], "PRI", 2, $projectinfo);

                        $count = Db::name("project_info")->where(["projectNo"=>$projectinfo['projectNo'],"status"=>1])->count();

                        if ($count == 0) {
                            //先查询是否存在status==1的项目,有的话再更新,否则跳过
                            $project_id = Db::name("project")->where(["projectNo" => $projectinfo['projectNo'], "status" => 1])->value('id', 0);
                            if ($project_id) {
                                $proc = Db::name("project")->where(["id" => $project_id])->update(["status" => 2, "updatetime" => date("Y-m-d H:i:s")]);
                                if ($proc == false) {
                                    Db::rollback();
                                    return error_show(1006, "项目反馈失败");
                                }else{
                                    //修改状态,添加待办
                                    ActionLog::logAdd($this->post['token'], [
                                        "order_code" => $projectinfo['projectNo'],//项目编码
                                        "status" => 1,//这里的status是之前的值
                                        "action_remark" => '',//备注
                                        "action_type" => "status"//新建create,编辑edit,更改状态status
                                    ], "PRO", 2, $this->post);

                                    ProcessOrder::AddProcess($this->post['token'], [
                                        "order_type" => 'PRO',
                                        "order_code" => $projectinfo['projectNo'],
                                        "order_id" => $project_id,
                                        "order_status" => 2,"before_status"=>1
                                    ]);
                                }

                            }
                        }
                    }
                }
            }
                Db::commit();
                return app_show(0,"编辑成功");
            }else{
                Db::rollback();
                return error_show(1004,"编辑失败");
            }
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1004,$e->getMessage());
        }
    }
    public function bargain_add(){
        $bidNo = isset($this->post['bidNo'])&& $this->post['bidNo']!=""?trim($this->post['bidNo']):"";
        if($bidNo===""){
            return error_show(1004,"参数bidNo不能为空");
        }
        $info =Db::name("consult_bids")->where(['bidNo'=>$bidNo,"is_del"=>0])->find();
        if($info==false){
            return error_show(1004,"未找到商品数据");
        }
        $consult =Db::name("consult_info")->where(['infoNo'=>$info['infoNo'],"is_del"=>0])->find();
        if($consult==false){
            return error_show(1004,"未找到咨询数据");
        }
        $bargain_price =  isset($this->post['bargain_price'])&& $this->post['bargain_price']!=""?floatval($this->post['bargain_price']):"";
        if($bargain_price===""){
            return error_show(1004,"参数bargain_price不能为空");
        }
        $cost_price =  isset($this->post['cost_price'])&& $this->post['cost_price']!=""?floatval($this->post['cost_price']):"";
        if($info['is_gold_price']==1){
            if($cost_price==""){
                return error_show(1004,"参数cost_price不能为空");
            }
            $catinfo = Db::name("cat")->where(["id"=>$info['cat_id']])->find();
            $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;
            $gold = Db::name("gold_price1")->where(["type"=>$info['metal_id'],"is_del"=>0,"status"=>1])->order("addtime desc")
                ->find();

            $bargain_price = ($info['demo_fee']/$consult['num']+$info['delivery_fee']+$info['open_fee']/$consult['num']+$info['good_weight'] * $gold["price"] + $cost_price *$info['good_weight']+$info['pakge_fee']+$info['mark_fee'] +$info['cert_fee'] +$info['nake_fee']) / (1 - $budget);
        }

        $bargain_reason =  isset($this->post['bargain_reason'])&& $this->post['bargain_reason']!=""?trim($this->post['bargain_reason']):"";
//        if($bargain_reason===""){
//            return error_show(1004,"参数bargain_reason不能为空");
//        }
        $infos =Db::name("consult_info")->where(['infoNo'=>$info['infoNo'],"is_del"=>0])->find();
        if($infos==false){
            return error_show(1004,"未找到商品数据");
        }
        if($infos['status']!=2&&$infos['status']!=3&&$infos['status']!=7){
            return error_show(1004,"咨询单招标未结束或已确认商品");
        }
        $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
        if($token==''){
            return error_show(105,"参数token不能为空");
        }
        $user =GetUserInfo($token);
        if(empty($user)||$user['code']!=0){
            return error_show(1002,"用户数据不存在");
        }
        //新增两个字段
        //议价备注
        $bargain_remark = isset($this->post['bargain_remark']) && $this->post['bargain_remark'] != "" ? trim($this->post['bargain_remark']) : "";
        //议价原因id
        $result_info_id = isset($this->post['result_info_id']) && $this->post['result_info_id'] != "" ? $this->post['result_info_id'] : "";
        if ($result_info_id != '') {
            $temp = Db::name('result_info')->field('id')->where(['result_code' => $result_info_id, 'type' => 7, 'status' => 1, 'is_del' => 0])->find();//type 7议价原因
            if (empty($temp)) return error_show(1004, "议价原因选择不正确");
        }

        $createrid= isset($user["data"]['id']) ?  $user["data"]['id'] : "";
        $creater= isset($user["data"]['nickname']) ?  $user["data"]['nickname'] : "";
        $bargainNo =makeNo("BA");
        Db::startTrans();//::todo 金价 毛利计算
        try{
            $data=[
                "bidsNo"=>$bidNo,
                "infoNo"=>$info['infoNo'],
                "zxNo"=>$info['zxNo'],
                "bargainNo"=>$bargainNo,
                "bargain_type"=>1,
                "bargain_reason"=>$bargain_reason,
                "sale_price"=>$info['sale_price'],
                "cost_before_price"=>$info['sale_cost_fee'],
                "cost_after_price"=>$cost_price,
                "before_rate"=>round(($info['origin_price']-$info['total_fee'])/$info['origin_price'],2),
                "after_rate"=>round(($bargain_price-$info['total_fee'])/$bargain_price,2),
                "after_price"=>$bargain_price,
                "exam_info"=>"",
                "status"=>0,
                "is_del"=>0,
                "creater"=>$creater,
                "createrid"=>$createrid,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s"),
                'bargain_remark'=>$bargain_remark,
                'result_info_id'=>$result_info_id,
            ];
            $int = Db::name("bargain_order")->insert($data);
            if($int){
                //修改状态,添加待办
                ActionLog::logAdd($this->post['token'], [
                    "order_code" => $bargainNo,//咨询单详情编号
                    "status" => 0,//这里的status是之前的值
                    "action_remark" => '',//备注
                    "action_type" => "status"//新建create,编辑edit,更改状态status
                ], "YJD", $data['status'], $data);

                ProcessOrder::AddProcess($this->post['token'], [
                    "order_type" => 'YJD',
                    "order_code" => $bargainNo,
                    "order_id" =>Db::name("bargain_order")->getLastInsID(),
                    "order_status" =>0,"before_status"=>0
                ]);
                $odlstatus=$info['status'];
                $info['status']=3;
                $info['updatetime']=date("Y-m-d H:i:s");
                $up =Db::name("consult_bids")->save($info);
               if($up){
                   //修改状态,添加待办
                   ActionLog::logAdd($this->post['token'], [
                       "order_code" => $bidNo,//咨询单详情编号
                       "status" => $odlstatus,//这里的status是之前的值
                       "action_remark" => '',//备注
                       "action_type" => "status"//新建create,编辑edit,更改状态status
                   ], "FKD", $info['status'], $info);
                   $old_infos_status = $infos['status'];
                   $infos['status']=3;
                   $infos['updatetime']=date("Y-m-d H:i:s");
                   $ups =Db::name("consult_info")->save($infos);
                   if($ups){

                       //修改状态,添加待办
                       ActionLog::logAdd($this->post['token'], [
                           "order_code" => $infos['infoNo'],//咨询单详情编号
                           "status" => $old_infos_status,//这里的status是之前的值
                           "action_remark" => '',//备注
                           "action_type" => "status"//新建create,编辑edit,更改状态status
                       ], "ZXD", $infos['status'], $infos);
                       Db::commit();
                       return app_show(0,"议价单生成成功",["bargainNo"=>$bargainNo]);
                   }
               }
            }
            Db::rollback();
            return error_show(1004,"议价单新建失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1004,$e->getMessage());
        }
    }

    //选择商品
    public function bids_check(){
        $bidNo = isset($this->post['bidNo'])&& $this->post['bidNo']!=""?trim($this->post['bidNo']):"";
        if($bidNo===""){
            return error_show(1004,"参数bidNo不能为空");
        }
        $info =Db::name("consult_bids")->where(['bidNo'=>$bidNo,"is_del"=>0])->find();
        if($info==false){
            return error_show(1004,"未找到商品数据");
        }
        $infos =Db::name("consult_info")->where(['infoNo'=>$info['infoNo'],"is_del"=>0])->find();
        if($infos==false){
            return error_show(1004,"未找到商品数据");
        }
        if($infos['status']!=2&&$infos['status']!=3&&$infos['status']!=7){
            return error_show(1004,"咨询单招标未结束或已确认商品");
        }
        Db::startTrans();
        try {
                $oldstattus=$info['status'];
                $info['status']=6;
                $info['updatetime']=date("Y-m-d H:i:s");
                $bids = Db::name("consult_bids")->save($info);
                if($bids){
                    //修改状态,添加待办
                    ActionLog::logAdd($this->post['token'], [
                        "order_code" =>$bidNo,//咨询单详情编号
                        "status" => $oldstattus,//这里的status是之前的值
                        "action_remark" => '',//备注
                        "action_type" => "status"//新建create,编辑edit,更改状态status
                    ], "FKD", $info['status'], $info);
                    $old_infos_status = $infos['status'];
                    $infos['status']=4;
                    $infos['updatetime']=date("Y-m-d H:i:s");
                    $se = Db::name("consult_info")->save($infos);
                    if($se){

                        //修改状态,添加待办
                        ActionLog::logAdd($this->post['token'], [
                            "order_code" => $infos['infoNo'],//咨询单详情编号
                            "status" => $old_infos_status,//这里的status是之前的值
                            "action_remark" => '',//备注
                            "action_type" => "status"//新建create,编辑edit,更改状态status
                        ], "ZXD", $infos['status'], $infos);
                        Db::commit();
                        return app_show(0,"商品确定成功");
                    }
                }
            Db::rollback();
            return error_show(1004,"商品确认失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1004,$e->getMessage());
        }
    }

    //议价审核
    /**
     * status:
     * 0待审核 1业务主管审核通过 2 业务税率未通过 3 业务驳回 4 专员通过 5 专员驳回 6 财务通过 7 财务税率未通过 8财务驳回 9 boss通过 10 boss 税率越线通过 11 驳回
     */
    public function bargain_status(){
        $bargainNo = isset($this->post['bargainNo'])&& $this->post['bargainNo']!=""?trim($this->post['bargainNo']):"";
        if($bargainNo===""){
            return error_show(1004,"参数bargainNo不能为空");
        }
        $info =Db::name("bargain_order")->where(['bargainNo'=>$bargainNo,"is_del"=>0])->find();
        if($info==false){
            return error_show(1004,"未找到议价数据");
        }
        $bids=Db::name("consult_bids")->where(["bidNo"=>$info["bidsNo"],"is_del"=>0])->find();
        if($bids==false){
            return error_show(1004,"未找到商品数据");
        }
        $coninfo=Db::name("consult_info")->where(["infoNo"=>$bids["infoNo"],"is_del"=>0])->find();
        if($coninfo==false){
            return error_show(1004,"未找到咨询单数据");
        }
        $status = isset($this->post['status'])&& $this->post['status']!=='' ? intval($this->post['status']):"";
        if($status===''){
            return error_show(1005,"参数status不能为空");
        }
        $catinfo = Db::name("cat")->where(["id"=>$bids['cat_id']])->find();
        $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']/100:0;
        $rate = isset($catinfo['rate']) ? $catinfo['rate']/100:0; //业务主管利率
        $money_rate = isset($catinfo['money_rate']) ? $catinfo['money_rate']/100:0;//财务主管利率
        $lower_rate = isset($catinfo['lower_rate']) ? $catinfo['lower_rate']/100:0;//boss主管利率
        if($bids['is_gold_price']==1){
            $gold = Db::name("gold_price1")->where(["type"=>$bids['metal_id'],"is_del"=>0,"status"=>1])->order("addtime desc")
                ->find();
//            $saleprice = $open_fee/$zxinfo['num'] + $weight* $gold["price"] + $cost_fee/(1-$budget)* $weight+$pakge_fee+$mark_fee+$cert_fee+$nake_fee;
            //$saleprice(最终售价) = (开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价)/(1-成本售价/100);
            $total_fe= $bids['demo_fee'] / $coninfo['num']+$bids['delivery_fee']+$bids['open_fee'] / $coninfo['num'] +
                $bids['good_weight']*$gold["price"] +$bids['cost_fee']*$bids['good_weight'] + $bids['pakge_fee'] +$bids['mark_fee'] + $bids['cert_fee'] + $bids['nake_fee'];
            $saleprice =$total_fe / (1 - $budget);
            $rate_price = $total_fe / (1 - $rate);
            $money_price =$total_fe / (1 - $money_rate);
            $lower_price =$total_fe / (1 - $lower_rate);
        }else{
            $total_fe= $bids['demo_fee']/$coninfo['num']+$bids['delivery_fee']+$bids['open_fee'] /
                $coninfo['num']+$bids['cost_fee']*$bids['good_weight']+$bids['pakge_fee'] +$bids['mark_fee']+  $bids['cert_fee'] +  $bids['nake_fee'];
            if($bids['nake_fee']!=0){
                $saleprice = ($total_fe)/ (1 - $budget);
                $rate_price = ($total_fe)/ (1 - $rate);
                $money_price =($total_fe)/ (1 - $money_rate);
                $lower_price = ($total_fe) / (1 - $lower_rate);

            }else{
                $saleprice = $total_fe / (1 - $budget);
                $rate_price =$total_fe/ (1 - $rate);
                $money_price =$total_fe / (1 - $money_rate);
                $lower_price = $total_fe/ (1 - $lower_rate);

            }

        }
        $ra_status = $info['status'];
        if($status==1){
            if($info['status']==0){
                if($info['after_price']<$rate_price){
                    $info['status']=2;
                }else{
                    $info['status']=1;
                }
            }elseif($info['status']==2){
               $info['status']=4;
            }elseif($info['status']==4||$info['status']==5){
                if($info['after_price']<$money_price){
                    $info['status']=7;
                }else{
                    $info['status']=6;
                }
            }elseif($info['status']==7){
                $info['lower_price'] =$lower_price;
                if($info['after_price']<$lower_price){
                    $info['status']=10;
                }else{
                    $info['status']=9;
                }
            }
        }else{
            if($info['status']==0){
                    $info['status']=3;
            }elseif($info['status']==2){
                $info['status']=4;
            }elseif($info['status']==4||$info['status']==5){
              $info['status']=8;
            }elseif($info['status']==7){
                $info['lower_price'] =$lower_price;
                $info['status']=11;
            }
        }
        $remark = isset($this->post['remark'])&& $this->post['remark']!=""?trim($this->post['remark']):"";
        $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
        if($token==''){
            return error_show(105,"参数token不能为空");
        }
        $user =GetUserInfo($token);
        if(empty($user)||$user['code']!=0){
            return error_show(1002,"用户数据不存在");
        }
        $createrid= isset($user["data"]['id']) ?  $user["data"]['id'] : "";
        $creater= isset($user["data"]['nickname']) ?  $user["data"]['nickname'] : "";
        Db::startTrans();
        try{
            $examinfo = isset($info['exam_info'])&&$info['exam_info']!="" ? json_decode($info['exam_info'],true):[];
            $examinfo[]=["status"=>$status,"info_status"=>$ra_status,"remark"=>$remark,"createrid"=>$createrid,
                "creater"=>$creater];
//            $info['status'] =$status;
            $info['remark'] =$remark;
            $info['exam_info']=json_encode($examinfo) ;
            $info['updatetime'] =date("Y-m-d H:i:s");
            $up =Db::name("bargain_order")->save($info);
            if($up){
                //修改状态,添加待办
                ActionLog::logAdd($this->post['token'], [
                    "order_code" =>  $bargainNo,//咨询单详情编号
                    "status" =>$ra_status,//这里的status是之前的值
                    "action_remark" => '',//备注
                    "action_type" => "status"//新建create,编辑edit,更改状态status
                ], "YJD", $info['status'], $info);
                $process=["order_code"=>$bargainNo,"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>'YJD',"before_status"=>$ra_status];
                ProcessOrder::AddProcess($token,$process);
                if($info['status']==1||$info['status']==3||$info['status']==8||$info['status']==6||$info['status']==9||$info['status']==10||$info['status']==11){
                    $bidstatus=$bids['status'];
                    $bids['status']=5;
                       if($info['status']==1||$info['status']==6||$info['status']==9||$info['status']==10){
                           $bids['sale_price']=$info['after_price'];
                           if($bids['is_gold_price']==1){
//                               $gold = Db::name("gold_price1")->where(["type"=>$bids['metal_id'],"is_del"=>0,"status"=>1])->order("addtime desc")
//                                   ->find();
//                               $bids['sale_price']= ($bids['open_fee'] / $coninfo['num'] + $bids['weight'] * $gold["price"] + $info['cost_after_price'] *$bids['weight']  +
//                                       $bids['pakge_fee'] + $bids['mark_fee'] +$bids['cert_fee'] +$bids['nake_fee']) /(1 - $budget);
                               $bids['sale_cost_fee'] = $info['cost_after_price'];
                               $bids['gold_price'] =isset($gold["price"])?$gold["price"]:0;
                           }
                       }
                    $bids['updatetime']=date("Y-m-d H:i:s");
                    $up =Db::name("consult_bids")->save($bids);
                    if($up==false){
                        Db::rollback();
                        return error_show(1004,"数据更新失败");
                    }
                    ActionLog::logAdd($this->post['token'], [
                        "order_code" => $info["bidsNo"],//咨询单详情编号
                        "status" => $bidstatus,//这里的status是之前的值
                        "action_remark" => '',//备注
                        "action_type" => "status"//新建create,编辑edit,更改状态status
                    ], "FKD",  $bids['status'], $bids);
                }
                Db::commit();
                return app_show(0,"更新成功");
            }
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1004,$e->getMessage());
        }
    }
    public  function  bargain_info(){
        $bargainNo = isset($this->post['bargainNo'])&& $this->post['bargainNo']!=""?trim($this->post['bargainNo']):"";
        if($bargainNo===""){
            return error_show(1004,"参数bargainNo不能为空");
        }
        $info =Db::name("bargain_order")->where(['bargainNo'=>$bargainNo,"is_del"=>0])->find();
        if($info==false){
            return error_show(1004,"未找到议价数据");
        }
        $bidinfos =   Db::name("consult_bids")->where([["bidNo","=",$info['bidsNo']],["is_del","=",0]])->find();
        if($bidinfos==false){
            return error_show(1004,"未找到商品数据");
        }
        $consultinfos =   Db::name("consult_info")->where([["infoNo","=",$bidinfos['infoNo']],["is_del","=",0]])
            ->find();
        if($consultinfos==false){
            return error_show(1004,"未找到咨询商品数据");
        }
        $info['spuCode'] = isset($bidinfos['spuCode'])?$bidinfos['spuCode']:"";
        $info['good_name'] = isset($bidinfos['good_name'])?$bidinfos['good_name']:"";
        $info['good_img'] = isset($bidinfos['good_img'])?$bidinfos['good_img']:"";
        $info['brand_id'] = isset($bidinfos['brand_id'])?$bidinfos['brand_id']:"";
        $info['good_num'] = isset($consultinfos['num'])?$consultinfos['num']:"";
        $info['before_rate'] =round($info['before_rate']*100,2);
        $info['sale_price'] =round($bidinfos['origin_price'],2);
        $info['cost_before_price'] =round($bidinfos['origin_cost_fee'],2);
        $info['after_rate'] = round($info['after_rate']*100,2);
        $info['exam_info'] = isset($info['exam_info'])&&$info['exam_info']!=""?json_decode($info['exam_info'],true):"";
        if($bidinfos['brand_id']!=0){
            $info['brand_id'] = $bidinfos['brand_id'];
            $brand=Db::name("brand")->where(["id"=>$bidinfos['brand_id']])->find();
            $info["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
        }else{
            $info["brand_name"]="";
            $info["brand_id"]="";
        }
        $catinfo = Db::name("cat")->where(["id"=>$bidinfos['cat_id']])->find();
        $info['can']= isset($bidinfos['cat_id']) && $bidinfos['cat_id'] !=0 ? made($bidinfos['cat_id']):[];
        $unit =Db::name("unit")->where(["id"=>$bidinfos['unit_id']])->find();
        $info['unit'] = isset($unit['unit'])?$unit['unit']:'';
        $bidinfoe = Db::name("consult_info")->where(["infoNo"=>$info["infoNo"]])->find();
        $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']:0;
        $lower_rate = isset($catinfo['lower_rate']) ? $catinfo['lower_rate']/100:0;//boss主管利率
        $info['budget'] =$budget;
        $info['rate_status'] =0; //利率状态 0 业务主管利率无法通过 1 财务主管利率无法通过 2 boos利率无法通过 3 完全通过
        if($info['after_rate']>=$catinfo['rate']){
            //业务主管利率
            $info['rate_status'] =1;
        }
        if ($info['after_rate']>=$catinfo['money_rate']){
            //财务主管利率
            $info['rate_status'] =2;
        }
        if ($info['after_rate']>=$catinfo['lower_rate']){
            //boss利率
            $info['rate_status'] =3;
        }


        $god=[
            "metal_id"=>$bidinfos['metal_id'],
            "weight"=>$bidinfos["good_weight"],
            "demo_fee"=>$bidinfos["demo_fee"],
            "delivery_fee"=>$bidinfos["delivery_fee"],
            "open_fee"=>$bidinfos["open_fee"],
            "packing_fee"=>$bidinfos["pakge_fee"],
            "mark_fee"=>$bidinfos["mark_fee"],
            "nake_fee"=>$bidinfos["nake_fee"],
            "cert_fee"=>$bidinfos["cert_fee"],
            "cost_fee"=>$bidinfos["cost_fee"],
            "num"=>$bidinfoe["num"],
        ];
        $info['gold_price']=0;
        $info['noble_weight']=$bidinfos['good_weight'];
            if($bidinfos['is_gold_price']==1 &&$bidinfos['metal_id']>0){
                $gold = Db::name("gold_price1")->where(["type"=>$bidinfos["metal_id"],"is_del"=>0,"status"=>1])->order("addtime desc")
                    ->find();
                $info['gold_price'] =$gold['price'];
                $lower_price = ($bidinfos['demo_fee']/$consultinfos['num']+$bidinfos['delivery_fee']+$bidinfos['open_fee']/$consultinfos['num'] + $bidinfos['good_weight']* $gold["price"] +$bidinfos['cost_fee']*$bidinfos['good_weight'] + $bidinfos['pakge_fee'] +$bidinfos['mark_fee'] + $bidinfos['cert_fee'] + $bidinfos['nake_fee']) / (1 - $lower_rate);

            }else{
                if($bidinfos['nake_fee']!=0){
                          $lower_price =($bidinfos['demo_fee']/$consultinfos['num']+$bidinfos['delivery_fee']+$bidinfos['open_fee']/$consultinfos['num']+$bidinfos['cost_fee']*$bidinfos['good_weight']+$bidinfos['pakge_fee'] +$bidinfos['mark_fee']+  $bidinfos['cert_fee'] +  $bidinfos['nake_fee']) / (1 - $lower_rate);

                }else{
                       $lower_price = ($bidinfos['demo_fee']/$consultinfos['num']+$bidinfos['delivery_fee']+$bidinfos['open_fee']/$consultinfos['num']+$bidinfos['cost_fee']*$bidinfos['good_weight']+$bidinfos['pakge_fee'] +$bidinfos['mark_fee']+  $bidinfos['cert_fee'])/ (1 - $lower_rate);
                }
            }
        $info['cost_desc'] =$bidinfos['cost_desc'];
        $info['work_day'] =$bidinfos['work_day'];
        $info['delivery_day'] =$bidinfos['delivery_day'];
        $info['expire_day'] =$bidinfos['expire_day'];
        $info['origin_place'] =$bidinfos['origin_place'];
        $info['origin_addr'] ="";
        if($bidinfos['origin_place']!=''){
            $code = explode(",",$bidinfos['origin_place']);
            $temp=[];
            $temp['provice_code']=isset($code[0])?$code[0]:"";
            $temp['city_code']=isset($code[1])?$code[1]:"";
            $temp['area_code']=isset($code[2])?$code[2]:"";
            $addr= GetAddr(json_encode($temp));
            $info['origin_addr'] = $addr;
        }
        $info['supplierNo'] =$bidinfos['supplierNo'];
        $supplier = Db::name("supplier")->where(["code"=>$info['supplierNo']])->find();
        $info['supplier_name'] = isset($supplier['name'])?$supplier['name']:"";
        $info['pay_way'] =$bidinfos['pay_way'];
        $info['tax'] =$bidinfos['tax'];
        $info['send_way'] =$bidinfos['send_way'];
        $info['metal_id'] =$bidinfos['metal_id'];
        $info['is_gold_price'] =$bidinfos['is_gold_price'];
        $info['config'] =$bidinfos['config'];
        $info['other_config'] =$bidinfos['other_config'];
        $info['weight'] =$bidinfos['weight'];
        $info['good_weight'] =$bidinfos['good_weight'];
        $info['is_diff'] =$bidinfos['is_diff'];
        $info['demo_fee'] =$bidinfos['demo_fee'];
        $info['delivery_fee'] =$bidinfos['delivery_fee'];
        $info['open_fee'] =$bidinfos['open_fee'];
        $info['pakge_fee'] =$bidinfos['pakge_fee'];
        $info['nake_fee'] =$bidinfos['nake_fee'];
        $info['mark_fee'] =$bidinfos['mark_fee'];
        $info['cert_fee'] =$bidinfos['cert_fee'];
        $info['cost_fee'] =$bidinfos['cost_fee'];
        $info['total_fee'] =$bidinfos['total_fee'];
        $info['supply_area'] =$bidinfos['supply_area'];
        $info['remark'] =$bidinfos['remark'];
        $info['is_addrs'] =$consultinfos['is_addrs'];
        $info['is_custom'] =$consultinfos['is_custom'];

        $info['metal_name']=isset($bidinfos['metal_id'])&& $bidinfos['metal_id']!=0?$this->noble[$bidinfos['metal_id']]:"";
//        $info['sale_price'] =round($bidinfos['sale_price'],2);//议价前价格
        $info['specinfo'] = isset($bidinfos['specinfo'])&&$bidinfos['specinfo']!=""?json_decode($bidinfos['specinfo'],true):[];
        if($info['status']==7){
            $info['lower_price'] =round($lower_price,2);
        }
        //获取议价原因
        if ($info['result_info_id'] != 0) {
            $info['result_info'] = Db::name('result_info')->where(['result_code' => $info['result_info_id'], 'type' => 7, 'status' => 1, 'is_del' => 0])->value('result');
        } else {
            $info['result_info'] = '';
        }
        return app_show(0,"获取成功",$info);
    }

    public function bidlist(){
        $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";
        $where=[['is_del',"=",0]];
        $zxNo = isset($this->post['zxNo']) && $this->post['zxNo'] !== "" ? trim($this->post['zxNo']) : "";
        if ($zxNo !== "") {
            $where[] = ['zxNo','like','%'.$zxNo.'%'];
        }
        $infoNo = isset($this->post['infoNo']) && $this->post['infoNo'] !== "" ? trim($this->post['infoNo']) : "";
        if ($infoNo !== "") {
            $where[] = ['infoNo','like','%'.$infoNo.'%'];
        }
        $bidNo = isset($this->post['bidNo']) && $this->post['bidNo'] !== "" ? trim($this->post['bidNo']) : "";
        if ($bidNo !== "") {
            $where[] = ['bidNo','like','%'.$bidNo.'%'];
        }
        $pgNo = isset($this->post['pgNo']) && $this->post['pgNo'] !== "" ? trim($this->post['pgNo']) : "";
        if ($pgNo !== "") {
            $bidinfo = Db::name("consult_info")->where([["pgNo","=",$pgNo],["is_del","=",0]])->column("infoNo");
            $where[] = ['infoNo','in',$bidinfo];
        }
        $bargain_status = isset($this->post['bargain_status']) && $this->post['bargain_status'] !== "" ? intval($this->post['bargain_status']) : "";
        if ($bargain_status !== "") {
            $bidinfo = Db::name("consult_info")->where([["bargain_status","=",$bargain_status],["is_del","=",0]])->column("infoNo");
            $where[] = ['infoNo','in',$bidinfo];
        }
        $projectNo = isset($this->post['projectNo']) && $this->post['projectNo'] !== "" ? trim($this->post['projectNo']) : "";
        if ($projectNo !== "") {
            $zxlist = Db::name("consult_order")->where(["projectNo"=>$projectNo,"is_del"=>0,"is_project"=>1])->column
            ("zxNo");
            $where[] = ['zxNo','in',$zxlist];
        }
        $is_own = isset($this->post['is_own']) ? intval($this->post['is_own']) : "0";
        if($is_own==1){
             $token = isset($this->post['token']) ? trim($this->post['token']) : "";
            if($token==""){
                return error_show(101,'token不能为空');
            }
            $apply_id =GetUserInfo($token);
            if(empty($apply_id)||$apply_id['code']!=0){
                return error_show(1002,"申请人数据不存在");
            }
            $rm= isset($apply_id["data"]['id']) ?  $apply_id["data"]['id'] : "";
            $where[]=['createrid',"=",$rm];
        }

        $count = Db::name('consult_bids')->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list =Db::name('consult_bids')->where($where)->page($page,$size)->select()->toArray();
        $data=[];
        foreach ($list as $value){
            $catinfo = Db::name("cat")->where(["id"=>$value['cat_id']])->find();
            $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
            $unit =Db::name("unit")->where(["id"=>$value['unit_id']])->find();
            $value['unit'] = isset($unit['unit'])?$unit['unit']:'';
            $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
            $value['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
            if($value['brand_id']!=0){
                $brand=Db::name("brand")->where(["id"=>$value['brand_id']])->find();
                $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
            }else{
                $value["brand_name"]="";
                $value["brand_id"]="";
            }
            $bidinfo = Db::name("consult_info")->where(["infoNo"=>$value["infoNo"]])->find();
            $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']:0;
                $god=[
                    "metal_id"=>$value['metal_id'],
                    "weight"=>$value["good_weight"],
                    "demo_fee"=>$value["demo_fee"],
                    "delivery_fee"=>$value["delivery_fee"],
                    "open_fee"=>$value["open_fee"],
                    "packing_fee"=>$value["pakge_fee"],
                    "mark_fee"=>$value["mark_fee"],
                    "nake_fee"=>$value["nake_fee"],
                    "cert_fee"=>$value["cert_fee"],
              //      "cost_fee"=>$value["cost_fee"],
                   "cost_fee"=>$value["sale_cost_fee"]!=$value['origin_cost_fee']? $value["sale_cost_fee"]: $value["cost_fee"],
                    //暂时注掉,搞不清楚原因@吴广广
                    "num"=>$bidinfo["num"],
                ];

            $top_cat = made($value['cat_id']);
            $top_cat_id = isset($top_cat[0]['id'])?$top_cat[0]['id']:0;

            if($value['is_gold_price']==1  && $top_cat_id==6 ){
                $price = GoldPrice($god, $budget/100);
                $value['sale_price'] = round($price,2);
                $value['origin_price'] = $value["sale_cost_fee"]!=$value['origin_cost_fee']? $value["sale_price"]:
                    $value["origin_price"];
            }
            $value['cost_fee'] =$value['origin_cost_fee'];
            $value['specinfo'] = json_decode($value['specinfo'],true);
            $value['metal_name']=isset($value['metal_id'])&& $value['metal_id']!=0?$this->noble[$value['metal_id']]:"";
            $value['bargain_status']=$bidinfo['bargain_status'];

            //reject驳回原因,status==5表示驳回
            $value['reject']=[];
            if ($value['status'] >=5) {
                $value['reject'] = Db::name('bargain_order')
                    ->field('id,remark,status')
                    ->where(['infoNo' => $infoNo, 'bidsNo' => $value['bidNo']])
                    ->whereIn('status', [3,8,11])->order("id desc")
                    ->find();
            }
            //产地
            $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
            if($value['delivery_place']!=""){
                list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$value['delivery_place']);
            }
            $value['delivery_place_cn']=GetAddr(json_encode($place));
            $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
            if($value['origin_place']!=""){
                list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$value['origin_place']);
            }
            $value['origin_addr']=GetAddr(json_encode($place));
//            if($value['origin_place']!=''){
//                $code = explode(",",$value['origin_place']);
//                $temp=[];
//                $temp['provice_code']=isset($code[0])?$code[0]:"";
//                $temp['city_code']=isset($code[1])?$code[1]:"";
//                $temp['area_code']=isset($code[2])?$code[2]:"";
//                $addr= GetAddr(json_encode($temp));
//                $value['origin_addr'] = $addr;
//            }
            $data[] = $value;
        }
        return app_show(0,"获取成功",['count'=>$count,'list'=>$data]);
    }

    public function feedlist(){
        $where=[['is_del',"=",0]];
        $zxNo = isset($this->post['zxNo']) && $this->post['zxNo'] !== "" ? trim($this->post['zxNo']) : "";
        if ($zxNo !== "") {
            $where[] = ['zxNo','=',$zxNo];
        }
        $infoNo = isset($this->post['infoNo']) && $this->post['infoNo'] !== "" ? trim($this->post['infoNo']) : "";
        if ($infoNo !== "") {
            $where[] = ['infoNo','=',$infoNo];
        }
        $bidNo = isset($this->post['bidNo']) && $this->post['bidNo'] !== "" ? trim($this->post['bidNo']) : "";
        if ($bidNo !== "") {
            $where[] = ['bidNo','=',$bidNo];
        }
        $projectNo = isset($this->post['projectNo']) && $this->post['projectNo'] !== "" ? trim($this->post['projectNo']) : "";
        if ($projectNo !== "") {
            $zxlist = Db::name("consult_order")->where(["projectNo"=>$projectNo,"is_del"=>0,"is_project"=>1])->column
            ("zxNo");
            $where[] = ['zxNo','in',$zxlist];
        }
        $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
        if($token==''){
            return error_show(102,"参数token不能为空");
        }
        $is_own = isset($this->post['is_own']) ? intval($this->post['is_own']) : "0";
        if($is_own==1){
            $token = isset($this->post['token']) ? trim($this->post['token']) : "";
            if($token==""){
                return error_show(101,'token不能为空');
            }
            $apply_id =GetUserInfo($token);
            if(empty($apply_id)||$apply_id['code']!=0){
                return error_show(1002,"申请人数据不存在");
            }
            $rm= isset($apply_id["data"]['id']) ?  $apply_id["data"]['id'] : "";
            $where[]=['createrid',"=",$rm];
        }
        $list =Db::name('consult_bids')->where($where)->select();
        $data=[];
        foreach ($list as $value){
            $catinfo = Db::name("cat")->where(["id"=>$value['cat_id']])->find();

            $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
            $unit =Db::name("unit")->where(["id"=>$value['unit_id']])->find();
            $value['unit'] = isset($unit['unit'])?$unit['unit']:'';
            $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
            $value['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
            if($value['brand_id']!=0){
                $brand=Db::name("brand")->where(["id"=>$value['brand_id']])->find();
                $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
            }else{
                $value["brand_name"]="";
                $value["brand_id"]="";
            }
            $bidinfo = Db::name("consult_info")->where(["infoNo"=>$value["infoNo"]])->find();
            $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']:0;
            $god=[
                "metal_id"=>$value['metal_id'],
                "weight"=>$value["good_weight"],
                "demo_fee"=>$value["demo_fee"],
                "delivery_fee"=>$value["delivery_fee"],
                "open_fee"=>$value["open_fee"],
                "packing_fee"=>$value["pakge_fee"],
                "mark_fee"=>$value["mark_fee"],
                "nake_fee"=>$value["nake_fee"],
                "cert_fee"=>$value["cert_fee"],
                "cost_fee"=>$value["cost_fee"],
                "num"=>$bidinfo["num"],
            ];
            if ($value['is_gold_price'] == 1 ) {
                $gold = Db::name("gold_price1")->where(["type"=>$value['metal_id'],"is_del"=>0,"status"=>1])->order("addtime desc")
                    ->find();

                //$total_fee(成本合计初始价格) =打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价0+运费;
                //$saleprice(最终售价) = (开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价)/(1-成本售价/100);
                $saleprice = ($value['demo_fee']/$bidinfo['num']+$value['delivery_fee']+$value["open_fee"]/$bidinfo['num'] +$value["good_weight"] *$gold["price"] +
                $value["cost_fee"] * $value["good_weight"] + $value["pakge_fee"]+ $value["mark_fee"] +$value["cert_fee"] +$value["nake_fee"]) / (1 - $budget);

//                //销售工艺费=((开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价)/(1-成本售价/100)-(打样费/购买数量 + 开模费/购买数量 +包装费+加标费+证书费+产品裸价0+运费) )/商品重量-最新金价
//                $sale_cost_fee = (($open_fee / $zxinfo['num'] + $weight * $gold["price"] + $cost_fee * $weight + $pakge_fee + $mark_fee + $cert_fee + $nake_fee) / (1 - $budget) - ($demo_fee / $zxinfo['num'] + $open_fee / $zxinfo['num'] + $pakge_fee + $mark_fee + $cert_fee + $nake_fee + $delivery_fee)) / $weight - $gold["price"];

            }else{

                if($value["nake_fee"]){
                    $saleprice = ($value['demo_fee']/$bidinfo['num']+$value['delivery_fee']+$value["open_fee"]/$bidinfo['num']+$value["cost_fee"] * $value["good_weight"]+  $value["pakge_fee"]+ $value["mark_fee"] +$value["cert_fee"] +$value["nake_fee"])/ (1 - $budget);

                }else{
                    $saleprice = ($value['demo_fee']/$bidinfo['num']+$value['delivery_fee']+$value["open_fee"]/$bidinfo['num']+$value["cost_fee"] * $value["good_weight"]+  $value["pakge_fee"]+ $value["mark_fee"] +$value["cert_fee"] +$value["nake_fee"]) /
                        (1 - $budget);

                }

            }
            $value['metal_name']=isset($value['metal_id'])&& $value['metal_id']!=0?$this->noble[$value['metal_id']]:"";

            $value['specinfo'] = json_decode($value['specinfo'],true);
            $value['bargain_status']=$bidinfo['bargain_status'];
            $data[]=$value;
        }
        return app_show(0,"获取成功",$data);
    }
    public function bragain_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";
        $where=[['bo.is_del',"=",0]];
        $bidsNo = isset($this->post['bidsNo'])&&$this->post['bidsNo']!=""?trim($this->post['bidsNo']):"";
        if($bidsNo!=""){
            $where[]=["bo.bidsNo","=",$bidsNo];
        }
        $zxNo = isset($this->post['zxNo']) && $this->post['zxNo'] !== "" ? trim($this->post['zxNo']) : "";
        if ($zxNo !== "") {
            $where[] = ['bo.zxNo','=',$zxNo];
        }
        $bargainNo = isset($this->post['bargainNo']) && $this->post['bargainNo'] !== "" ? trim($this->post['bargainNo']) : "";
        if ($bargainNo !== "") {
            $where[] = ['bo.bargainNo','=',$bargainNo];
        }

        $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
        if ($status!== "") {
            $where[] = ['bo.status','=',$status];
        }

        $infoNo = isset($this->post['infoNo']) && $this->post['infoNo'] !== "" ? trim($this->post['infoNo']) : "";
        if ($infoNo !== "") {
            $where[] = ['bo.infoNo','=',$infoNo];
        }

        $projectNo = isset($this->post['projectNo']) && $this->post['projectNo'] !== "" ? trim($this->post['projectNo']) : "";
        if ($projectNo !== "") {
            $zxlist = Db::name("consult_order")->where(["projectNo"=>$projectNo,"is_del"=>0,"is_project"=>1])->column
            ("zxNo");
            $where[] = ['bo.zxNo','in',$zxlist];
        }
        $pgNo = isset($this->post['pgNo']) && $this->post['pgNo'] !== "" ? trim($this->post['pgNo']) : "";
        if ($pgNo !== "") {
            $bidinfo = Db::name("consult_info")->where([["pgNo","=",$pgNo],["is_del","=",0]])->column("infoNo");
            $where[] = ['bo.infoNo','in',$bidinfo];
        }
        $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
        $end= isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
        if($start!=""){
            $where[]=["bo.addtime",">=",date("Y-m-d H:i:s",strtotime($start))];
        }
        if($end!=""){
            $where[]=["bo.addtime","<=",date("Y-m-d H:i:s",strtotime($end))];
        }
        $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
        if ($company_name !== "") $where[] = ["bo.createrid", 'in', get_company_item_user_by_name($company_name)];

        $role=$this->checkRole();
        if(!empty($role['write'])){
            $where[]=["bo.createrid","in",$role['write']];
        }

        $count=Db::name("bargain_order")->alias('bo')->leftJoin("depart_user u", "u.uid=bo.createrid AND u.is_del=0")->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('bargain_order')
            ->alias('bo')
            ->field('bo.*,u.itemid')
            ->leftJoin("depart_user u", "u.uid=bo.createrid AND u.is_del=0")
            ->where($where)
            ->page($page, $size)
            ->order('bo.addtime', 'desc')
            ->cursor();
        $data=[];
        foreach ($list as $value){
           $bidinfos =   Db::name("consult_bids")->where([["bidNo","=",$value['bidsNo']],["is_del","=",0]])->find();
           $value['spuCode'] = isset($bidinfos['spuCode'])?$bidinfos['spuCode']:"";
           $value['good_name'] = isset($bidinfos['good_name'])?$bidinfos['good_name']:"";
           $value['good_img'] = isset($bidinfos['good_img'])?$bidinfos['good_img']:"";
           $value['brand_id'] = isset($bidinfos['brand_id'])?$bidinfos['brand_id']:"";
            if($bidinfos['brand_id']!=0){
                $value['brand_id'] = $bidinfos['brand_id'];
                $brand=Db::name("brand")->where(["id"=>$bidinfos['brand_id']])->find();
                $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
            }else{
                $value["brand_name"]="";
                $value["brand_id"]="";
            }
            $catinfo = Db::name("cat")->where(["id"=>$bidinfos['cat_id']])->find();
            $value['can']= isset($bidinfos['cat_id']) && $bidinfos['cat_id'] !=0 ? made($bidinfos['cat_id']):[];
            $unit =Db::name("unit")->where(["id"=>$bidinfos['unit_id']])->find();
            $value['unit'] = isset($unit['unit'])?$unit['unit']:'';
            $bidinfoe = Db::name("consult_info")->where(["infoNo"=>$value["infoNo"]])->find();
            $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate']:0;
            $value['budget'] =$budget;
            $god=[
                "metal_id"=>$bidinfos['metal_id'],
                "weight"=>$bidinfos["good_weight"],
                "demo_fee"=>$bidinfos["demo_fee"],
                "delivery_fee"=>$bidinfos["delivery_fee"],
                "open_fee"=>$bidinfos["open_fee"],
                "packing_fee"=>$bidinfos["pakge_fee"],
                "mark_fee"=>$bidinfos["mark_fee"],
                "nake_fee"=>$bidinfos["nake_fee"],
                "cert_fee"=>$bidinfos["cert_fee"],
                "cost_fee"=>$bidinfos["cost_fee"],
                "num"=>$bidinfoe["num"],
            ];
//            if($bidinfos['is_gold_price']==1){
//                $price = GoldPrice($god, $budget/100);
//            }else{
//                $price = GoodPrice($god, $budget/100);
//            }
            $value['metal_name']=isset($bidinfos['metal_id'])&& $bidinfos['metal_id']!=0?$this->noble[$bidinfos['metal_id']]:"";
            $value['sale_price'] =round($bidinfos['origin_price'],2);
            $value['cost_before_price'] =round($bidinfos['origin_cost_fee'],2);

            $value['specinfo'] = isset($bidinfos['specinfo'])&&$bidinfos['specinfo']!=""?json_decode($bidinfos['specinfo'],true):[];
            $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
            $data[]=$value;
        }

        return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
    }

    public function  crontablist(){
        $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";
        $where=[['a.is_del',"=",0],['b.is_del',"=",0]];
        $zxNo = isset($this->post['zxNo']) && $this->post['zxNo'] !== "" ? trim($this->post['zxNo']) : "";
        if ($zxNo !== "") {
            $where[] = ['a.zxNo','=',$zxNo];
        }
        $salesman = isset($this->post['salesman']) && $this->post['salesman'] !== "" ? trim($this->post['salesman']) : "";
        if ($salesman !== "") {
            //$where["salesman"] =Db::Raw("like '%$salesman%'");
            $where[]= ["b.salesman",'like',"%$salesman%"];
        }
        $cpName = isset($this->post['cpName']) && $this->post['cpName'] !== "" ? trim($this->post['cpName']) : "";
        if ($cpName !== "") {
            //$where["cpName"]= Db::Raw("like '%$cpName%'");
            $where[]= ["a.good_Name",'like',"%$cpName%"];
        }
        $khNo = isset($this->post['khNo']) && $this->post['khNo'] !== "" ? trim($this->post['khNo']) : "";
        if ($khNo !== "") {
            // $where['khNo'] = $khNo;
            $where[]= ["b.khNo",'like',"%$khNo%"];
        }
        $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
        if ($start !="") {
            //$where = ["zxtime"=>Db::raw(">= '{$start}'")];
            $where[]= ["a.addtime",'>=',$start];
        }
        $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
        if($end !=""){
            $where[]= ["a.addtime",'<=',$end];
        }
        $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
        if ($status !== "") {
            $where[]= ["a.status",'=',$status];
        }
//        $role=$this->checkRole();
//        if(!empty($role['platform']) ){
//            $where[]=["b.platform_code","in",$role['platform']];
//        }
        $count = Db::name('consult_info')->alias("a")->leftJoin("consult_order b","a.zxNo=b.zxNo")->where($where)
            ->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list =Db::name('consult_info')->alias("a")->leftJoin("consult_order b","a.zxNo=b.zxNo")->where($where)->page($page,$size)
            ->field("a.*,b.createrid,b.creater,b.is_project,b.projectNo,b.khNo,b.endtime,b.platform_code,b.saleid,b.salesman,b.depart")->select();
        $data=[];
        foreach ($list as $value){
            $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
            $brand = Db::name("brand")->where(['id'=>$value['brand_id']])->find();
            $value['brand_name']= isset($brand['brand_name']) && $brand['brand_name']!='' ? $brand['brand_name']:"";
            $value['khname']="";
            $value['specinfo']=isset($value['specinfo'])&&$value['specinfo']!=""? json_decode($value['specinfo'],
                true):[];
            if($value['khNo']!==""){
                $kh=Db::name("customer_info")->where(['companyNo'=>$value['khNo']])->find();
                $value['khname']= isset($kh['companyName'])?$kh['companyName']:"";
            }
            $consult =Db::name("consult_bids")->where(["infoNo"=>$value['infoNo'],"createrid"=>$this->uid,"is_del"=>0])
                ->find();
            $consults =Db::name("consult_bids")->where(["infoNo"=>$value['infoNo'],"is_del"=>0])->find();
            $value['is_feed'] =$consult==false?0:1;
            $value['is_all_feed'] =$consults==false?0:1;
            $value['metal_name']=isset($value['metal_id'])&& $value['metal_id']!=0?$this->noble[$value['metal_id']]:"";
            $data[]=$value;
        }
        return app_show(0,"获取成功",['count'=>$count,'list'=>$data]);
    }


    //复制上面的bidlist方法,只更改排序方式
    public function bidlistCopy()
    {
        $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";
        $where = [['cb.is_del', "=", 0]];
        $zxNo = isset($this->post['zxNo']) && $this->post['zxNo'] !== "" ? trim($this->post['zxNo']) : "";
        if ($zxNo !== "") {
            $where[] = ['cb.zxNo', 'like', '%' . $zxNo . '%'];
        }
        $infoNo = isset($this->post['infoNo']) && $this->post['infoNo'] !== "" ? trim($this->post['infoNo']) : "";
        if ($infoNo !== "") {
            $where[] = ['cb.infoNo', 'like', '%' . $infoNo . '%'];
        }
        $bidNo = isset($this->post['bidNo']) && $this->post['bidNo'] !== "" ? trim($this->post['bidNo']) : "";
        if ($bidNo !== "") {
            $where[] = ['cb.bidNo', 'like', '%' . $bidNo . '%'];
        }
        $pgNo = isset($this->post['pgNo']) && $this->post['pgNo'] !== "" ? trim($this->post['pgNo']) : "";
        if ($pgNo !== "") {
            $bidinfo = Db::name("consult_info")->where([["pgNo", "=", $pgNo], ["is_del", "=", 0]])->column("infoNo");
            $where[] = ['cb.infoNo', 'in', $bidinfo];
        }
        $bargain_status = isset($this->post['bargain_status']) && $this->post['bargain_status'] !== "" ? intval($this->post['bargain_status']) : "";
        if ($bargain_status !== "") {
            $bidinfo = Db::name("consult_info")->where([["bargain_status", "=", $bargain_status], ["is_del", "=", 0]])->column("infoNo");
            $where[] = ['cb.infoNo', 'in', $bidinfo];
        }
        $projectNo = isset($this->post['projectNo']) && $this->post['projectNo'] !== "" ? trim($this->post['projectNo']) : "";
        if ($projectNo !== "") {
            $zxlist = Db::name("consult_order")->where(["projectNo" => $projectNo, "is_del" => 0, "is_project" => 1])->column
            ("zxNo");
            $where[] = ['cb.zxNo', 'in', $zxlist];
        }
        $is_own = isset($this->post['is_own']) ? intval($this->post['is_own']) : "0";
        if ($is_own == 1) {
            $token = isset($this->post['token']) ? trim($this->post['token']) : "";
            if ($token == "") {
                return error_show(101, 'token不能为空');
            }
            $apply_id = GetUserInfo($token);
            if (empty($apply_id) || $apply_id['code'] != 0) {
                return error_show(1002, "申请人数据不存在");
            }
            $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
            $where[] = ['cb.createrid', "=", $rm];
        }
        $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
        if ($company_name !== "") $where[] = ["cb.createrid", 'in', get_company_item_user_by_name($company_name)];


        $count = Db::name('consult_bids')->alias('cb')->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('consult_bids')
            ->alias('cb')
            ->field('cb.*,u.itemid')
            ->leftJoin("depart_user u", "u.uid=cb.createrid AND u.is_del=0")
            ->where($where)
            ->page($page, $size)
            ->order('cb.addtime', 'desc')
            ->cursor();
        $data = [];
        foreach ($list as $value) {
            $catinfo = Db::name("cat")->where(["id" => $value['cat_id']])->find();
            $value['can'] = isset($value['cat_id']) && $value['cat_id'] != 0 ? made($value['cat_id']) : [];
            $unit = Db::name("unit")->where(["id" => $value['unit_id']])->find();
            $value['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
            $supplier = Db::name("supplier")->where(["code" => $value['supplierNo']])->find();
            $value['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
            if ($value['brand_id'] != 0) {
                $brand = Db::name("brand")->where(["id" => $value['brand_id']])->find();
                $value["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
            } else {
                $value["brand_name"] = "";
                $value["brand_id"] = "";
            }
            $bidinfo = Db::name("consult_info")->where(["infoNo" => $value["infoNo"]])->find();
            $budget = isset($catinfo['order_rate']) ? $catinfo['order_rate'] : 0;
            $god = [
                "metal_id" => $value['metal_id'],
                "weight" => $value["good_weight"],
                "demo_fee" => $value["demo_fee"],
                "delivery_fee" => $value["delivery_fee"],
                "open_fee" => $value["open_fee"],
                "packing_fee" => $value["pakge_fee"],
                "mark_fee" => $value["mark_fee"],
                "nake_fee" => $value["nake_fee"],
                "cert_fee" => $value["cert_fee"],
                "cost_fee" => $value["sale_cost_fee"]!=$value["origin_cost_fee"] ? $value["sale_cost_fee"] : $value["cost_fee"],
                "num" => $bidinfo["num"],
            ];
            if ($value['is_gold_price'] == 1) {
                $price = GoldPrice($god, $budget / 100);
                $value['sale_price'] = round($price, 2);
            }
            $value['cost_fee'] = $value['origin_cost_fee'];
            $value['specinfo'] = json_decode($value['specinfo'], true);
            $value['metal_name'] = isset($value['metal_id']) && $value['metal_id'] != 0 ? $this->noble[$value['metal_id']] : "";
            $value['bargain_status'] = $bidinfo['bargain_status'];

            //reject驳回原因,status==5表示驳回
            $value['reject'] = [];
            if ($value['status'] >= 5) {
                $value['reject'] = Db::name('bargain_order')
                    ->field('id,remark,status')
                    ->where(['infoNo' => $infoNo, 'bidsNo' => $value['bidNo']])
                    ->whereIn('status', [3, 8, 11])->order("id desc")
                    ->find();
            }
            //产地
            $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
            if ($value['delivery_place'] != "") {
                list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $value['delivery_place']);
            }
            $value['delivery_place_cn'] = GetAddr(json_encode($place));
            $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
            if ($value['origin_place'] != "") {
                list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $value['origin_place']);
            }
            $value['origin_addr'] = GetAddr(json_encode($place));
//            if($value['origin_place']!=''){
//                $code = explode(",",$value['origin_place']);
//                $temp=[];
//                $temp['provice_code']=isset($code[0])?$code[0]:"";
//                $temp['city_code']=isset($code[1])?$code[1]:"";
//                $temp['area_code']=isset($code[2])?$code[2]:"";
//                $addr= GetAddr(json_encode($temp));
//                $value['origin_addr'] = $addr;
//            }
            $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
            $data[] = $value;
        }
        return app_show(0, "获取成功", ['count' => $count, 'list' => $data]);
    }


}