<?php

namespace app\admin\controller;

use app\admin\common\Ocr;
use app\admin\model\ChangeLog;
use think\App;
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\Validate;


//供应商
class Suppler extends Base
{
    public function create()
    {
        $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
        if ($name == "") {
            return error_show(1002, "参数name不能为空");
        }
//        $rename = Db::name('supplier')->field('id')->where(['is_del' => 0, 'name' => $name])->find();
//        if (!empty($rename)) {
//            return error_show(1002, "公司名称已存在");
//        }


        $tr = makeNo("QS");
        $source = isset($this->post['source']) && $this->post['source'] !== "" ? intval($this->post['source']) : "";
        if ($source === "") {
            return error_show(1002, "供应商来源不能为空");
        }
        $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 = $this->uid;
        $creater = $this->uname;
        $personid = isset($this->post['personid']) && $this->post['personid'] !== "" ? intval($this->post['personid']) : "";
        if ($personid == "") {
            return error_show(1002, "参数personid不能为空");
        }
        $to = GetInfoById($token, ['id' => $personid]);
        if (empty($to) || $to['code'] != 0) {
            return app_show(1002, "负责人信息不存在");
        }
        $person = isset($to['data']['nickname']) && $to['data']['nickname'] != "" ? $to['data']['nickname'] : "";
        $type = isset($this->post['type']) && $this->post['type'] !== "" ? intval($this->post['type']) : "";
        if ($type === "") {
            return error_show(1002, "申请类型不能为空");
        }
        $coop_state = isset($this->post['coop_state']) && $this->post['coop_state'] !== "" ? intval($this->post['coop_state']) : "";
        if ($coop_state === "") {
            return error_show(1002, "参数coop_state不能为空");
        }
        $category = isset($this->post['category']) && $this->post['category'] !== "" ? intval($this->post['category']) : "";
        if ($category === "") {
            return error_show(1002, "参数category不能为空");
        }
        $delivery_way = isset($this->post['delivery_way']) && $this->post['delivery_way'] !== "" ? intval($this->post['delivery_way']) : "";
        if ($delivery_way === "") {
            return error_show(1002, "参数delivery_way不能为空");
        }
        $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "";
        if ($level === "") {
            return error_show(1002, "参数level不能为空");
        }
        $supplier_type = isset($this->post['supplier_type']) && $this->post['supplier_type'] !== "" ? intval($this->post['supplier_type']) : "";
        if ($supplier_type === "") {
            return error_show(1002, "参数supplier_type不能为空");
        }
        $pay_type = isset($this->post['pay_type']) && $this->post['pay_type'] !== "" ? intval($this->post['pay_type']) : "";
        if ($pay_type === "") {
            return error_show(1002, "参数pay_type不能为空");
        }
        $prove_img = isset($this->post['prove_img']) && $this->post['prove_img'] !== "" ? trim($this->post['prove_img']) : "";

        // $area = isset($this->post['area']) && $this->post['area']!==""? trim($this->post['area']) :"";
        // $city = isset($this->post['city']) && $this->post['city']!==""? trim($this->post['city']) :"";
        $nature = isset($this->post['nature']) && $this->post['nature'] !== "" ? intval($this->post['nature']) : "";
        $is_platform = isset($this->post['is_platform']) && $this->post['is_platform'] !== "" ? intval($this->post['is_platform']) : "0";
        $ticket_type = isset($this->post['ticket_type']) && $this->post['ticket_type'] !== "" ? intval($this->post['ticket_type']) : "";
        $legaler = isset($this->post['legaler']) && $this->post['legaler'] !== "" ? trim($this->post['legaler']) : "";
        $addr = isset($this->post['addr']) && $this->post['addr'] !== "" ? trim($this->post['addr']) : "";
        $registercode = isset($this->post['registercode']) && $this->post['registercode'] !== "" ? trim($this->post['registercode']) : "";
        $capital = isset($this->post['capital']) && $this->post['capital'] !== "" ? intval($this->post['capital']) : "";
        $product_category = isset($this->post['product_category']) && $this->post['product_category'] !== "" ? trim($this->post['product_category']) : "";
        $is_payment = isset($this->post['is_payment']) && $this->post['is_payment'] !== "" ? trim($this->post['is_payment']) : "";
        $notice_mark = isset($this->post['notice_mark']) && $this->post['notice_mark'] !== "" ? trim($this->post['notice_mark']) : "";
        $agreement = isset($this->post['agreement']) && $this->post['agreement'] !== "" ? trim($this->post['agreement']) : "";
        $warehouse = isset($this->post['warehouse']) && $this->post['warehouse'] !== "" ? trim($this->post['warehouse']) : "";
        $pay_day = isset($this->post['pay_day']) && $this->post['pay_day'] !== "" ? trim($this->post['pay_day']) : "";
        $scope = isset($this->post['scope']) && $this->post['scope'] !== "" ? trim($this->post['scope']) : "";
        // $status= isset($this->post['status']) && $this->post['status']!==""?intval($this->post['status']) :"1";
        //$registertime= isset($this->post['registertime']) && $this->post['registertime']!==""?
        // $this->post['registertime'] :"";
        $supplier_img = isset($this->post['supplier_img']) && $this->post['supplier_img'] !== "" ? trim($this->post['supplier_img']) : "";
        $contactor = isset($this->post['contactor']) && $this->post['contactor'] !== "" ? trim($this->post['contactor']) : "";
        if ($contactor == "") {
            return error_show(1002, "参数contactor不能为空");
        }
        $mobile = isset($this->post['mobile']) && $this->post['mobile'] !== "" ? trim($this->post['mobile']) : "";
        if ($mobile == "") {
            return error_show(1002, "参数mobile不能为空");
        }
        $telephone = isset($this->post['telephone']) && $this->post['telephone'] !== "" ? trim($this->post['telephone']) : "";
        $position = isset($this->post['position']) && $this->post['position'] !== "" ? trim($this->post['position']) : "";
        if ($position == "") {
            return error_show(1002, "参数position不能为空");
        }
        $email = isset($this->post['email']) && $this->post['email'] !== "" ? trim($this->post['email']) : "";
//        Db::startTrans();
//        try {

        $date = date('Y-m-d H:i:s');
        $data = [
            "name" => $name,
            "source" => $source,
            "coop_state" => $coop_state,
            "category" => $category,
            "delivery_way" => $delivery_way,
            "level" => $level,
            "supplier_type" => $supplier_type,
            "pay_type" => $pay_type,
            "registercode" => $registercode,
            "capital" => $capital,
            "product_category" => $product_category,
            "is_payment" => $is_payment,
            "notice_mark" => $notice_mark,
            "agreement" => $agreement,
            "warehouse" => $warehouse,
            "pay_day" => $pay_day,
            "scope" => $scope,
            "code" => $tr,
            "type" => $type,
            "nature" => $nature,
            "legaler" => $legaler,
            "addr" => $addr,
            "status" => 0,
            "is_del" => 0,
            "remark" => '',
            "ticket_type" => $ticket_type,
            "addtime" => $date,
            "updatetime" => $date,
            "is_platform" => $is_platform,
            "prove_img" => $prove_img,
            "license_img" => $supplier_img,
            "creater" => $creater,
            "createrid" => $createrid,
            "registertime" => $date,
            "person" => $person,
            "personid" => $personid,

        ];
        if ($supplier_img !== "") {
            $ocr = new Ocr();
            $datwa = $ocr->query("", $supplier_img);
            if ($datwa['code'] == 0 && isset($datwa['data']['result'])) {
                $license = $datwa['data']['result'];
                $data['legaler'] = isset($license['legal_representative']) ? $license['legal_representative'] : "";
                $data['addr'] = isset($license['address']) ? $license['address'] : "";
                $data['registertime'] = isset($license['found_date']) ? $license['found_date'] : "";
                $data['scope'] = isset($license['business_scope']) ? $license['business_scope'] : "";
                $data['capital'] = isset($license['registered_capital']) ? $license['registered_capital'] : "";
                $data['nature'] = isset($license['type']) ? $license['type'] : "";
                $data['registercode'] = isset($license['registration_number']) ? $license['registration_number'] : "";
                $data['ocr_status'] = 1;
                if ($name != $license['name']) {
                    $data['remark'] = "企业名称与营业执照不匹配";
                    $data['status'] = 0;
                    $data['ocr_status'] = 1;
                }
                if ($registercode != $license['registration_number'] && $registercode != "") {
                    $data['remark'] .= "企业名称与营业执照不匹配";
                    $data['status'] = 0;
                    $data['ocr_status'] = 1;
                }
            } else {
                $data['remark'] = "营业执照识别失败";
                $data['status'] = 0;
                $data['ocr_status'] = 2;
            }
        }


//            $join = Db::name('supplier')->insert($data, true);
//            if ($join > 0) {
        $temp = [];
        $temp['code'] = $tr;
        $temp['contactor'] = $contactor;
        $temp['mobile'] = $mobile;
        $temp['position'] = $position;
        $temp['telephone'] = $telephone;
        $temp['email'] = $email;
        $temp['is_del'] = 0;
        $temp['addtime'] = $date;
        $temp['updatetime'] = $date;
//                $vp = Db::name('supplier_contact')->insert($temp);


        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('sCreate', ['data' => $data, 'contact' => $temp]);

        return json_show($rs['code'], $rs['message'], $rs['data']);

//                if ($vp == "") {
//                    Db::rollback();
//                    return error_show(1002, "添加失败");
//                }

//                Db::commit();
//                return app_show(0, "添加成功", ["id" => $join]);
//            } else {
//                Db::rollback();
//                return error_show(1002, "添加失败");
//            }
//        } catch (\Exception $e) {
//            Db::rollback();
//            return error_show(1003, $e->getMessage());
//        }
    }

    public function list()
    {
        $param = $this->request->only(['page' => 1, 'size' => 10, 'name' => '', 'creater' => '', 'person' => '', 'status' => '', 'ocr_status' => '', 'start' => '', 'end' => '', 'company_name' => ''], 'post', 'trim');

        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('sGetList', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);

        /*
        $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 = [['s.is_del', "=", 0]];
        $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
        if ($name !== "") {
            $where[] = ["s.name", "like", "%$name%"];
        }
        $creater = isset($this->post['creater']) && $this->post['creater'] !== "" ? trim($this->post['creater']) : "";
        if ($creater !== "") {
            $where[] = ["s.creater", "like", "%$creater%"];
        }
        $person = isset($this->post['person']) && $this->post['person'] !== "" ? trim($this->post['person']) : "";
        if ($person !== "") {
            $where[] = ["s.person", "like", "%$person%"];
        }
        $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
        if ($status !== "") {
            $where[] = ["s.status", "=", $status];
        }
        $ocr_status = isset($this->post['ocr_status']) && $this->post['ocr_status'] !== "" ? intval($this->post['ocr_status']) : "";
        if ($ocr_status !== "") {
            $where[] = ["s.ocr_status", "=", $ocr_status];
        }
        $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
        if ($start !== "") {
            $where[] = ['s.addtime', ">=", $start];
        }
        $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
        if ($company_name !== "") $where[] = ["s.createrid", 'in', get_company_item_user_by_name($company_name)];

//        $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
//        if($end !=""){
//            $where[]=['s.addtime',"<=",$end];
//        }
//        $contactor = isset($this->post['contactor']) && $this->post['contactor'] !==""? trim($this->post['contactor']):"";
//        if($contactor !=""){
//            $where[]=["s.contactor","like","%$contactor%"];
//        }
//        $mobile = isset($this->post['mobile']) && $this->post['mobile'] !=="" ? intval($this->post['mobile']):"";
//        if($mobile !=""){
//            $where[] = ['s.mobile',"like","%$mobile%"];
//        }
        $count = Db::name('supplier')
            ->alias('s')
            ->where($where)
            ->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
//        $list = Db::name('supplier')->alias('a')
//            ->join("supplier_contact b","b.code=a.code","left")
//            ->where($where)->page($page,$size)->order("a.addtime desc")
//            ->field("a.*,b.contactor,b.mobile")->select();
        $list = Db::name('supplier')
            ->alias('s')
            ->field('s.*,u.itemid')
            ->leftJoin("depart_user u", "u.uid=s.createrid AND u.is_del=0")
            ->where($where)
            ->page($page, $size)
            ->order("addtime desc")
            ->select()
            ->toArray();

        $all_codes = Db::name('supplier_contact')
            ->whereIn('code', array_column($list, 'code'))
            ->column("id,contactor,mobile", 'code');

        //获取开通账号的供应商
        $account = checkHasAccountBySupplierNos(array_column($list,'code'));

        foreach ($list as &$value) {
            $value['contactor'] = isset($all_codes[$value['code']]['contactor']) ? $all_codes[$value['code']]['contactor'] : '';
            $value['mobile'] = isset($all_codes[$value['code']]['mobile']) ? $all_codes[$value['code']]['mobile'] : '';
            $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
            $value['has_account'] = (int)isset($account[$value['code']]);
        }
        return app_show("0", "获取成功", ['list' => $list, 'count' => $count]);
        */
    }

    public function edit()
    {
        $token = isset($this->post['token']) && $this->post['token'] != '' ? trim($this->post['token']) : "";
        if ($token == '') {
            return error_show(105, "参数token不能为空");
        }
        $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
        if ($id == "") {
            return error_show(1002, "参数id不能为空");
        }
//        $info = Db::name("supplier")->where(['id' => $id, 'is_del' => 0])->find();

        $code = $this->request->only(['code' => ''], 'post', 'trim');
        $userCommon = \app\admin\common\User::getIns();
        $info = $userCommon->handle('sInfo', $code);

        if (!isset($info['code']) || $info['code'] != 0) return json_show($info['code'], $info['message']);
//
        if ($info['data']['status'] == 1) return json_show(1002, "状态是启用状态,无法编辑");

        $source = isset($this->post['source']) && $this->post['source'] !== "" ? trim($this->post['source']) : "";
        if ($source == "") {
            return error_show(1002, "供应商来源不能为空");
        }
        $type = isset($this->post['type']) && $this->post['type'] !== "" ? trim($this->post['type']) : "";
        if ($type == "") {
            return error_show(1002, "申请类型不能为空");
        }
        $coop_state = isset($this->post['coop_state']) && $this->post['coop_state'] !== "" ? trim($this->post['coop_state']) : "";
        if ($coop_state == "") {
            return error_show(1002, "参数coop_state不能为空");
        }
        $category = isset($this->post['category']) && $this->post['category'] !== "" ? trim($this->post['category']) : "";
        if ($category == "") {
            return error_show(1002, "参数category不能为空");
        }
        $delivery_way = isset($this->post['delivery_way']) && $this->post['delivery_way'] !== "" ? trim($this->post['delivery_way']) : "";
        if ($delivery_way == "") {
            return error_show(1002, "参数delivery_way不能为空");
        }
        $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "";
        if ($level == "") {
            return error_show(1002, "参数level不能为空");
        }
        $supplier_type = isset($this->post['supplier_type']) && $this->post['supplier_type'] !== "" ? trim($this->post['supplier_type']) : "";
        if ($supplier_type == "") {
            return error_show(1002, "参数supplier_type不能为空");
        }
        $pay_type = isset($this->post['pay_type']) && $this->post['pay_type'] !== "" ? trim($this->post['pay_type']) : "";
        if ($pay_type == "") {
            return error_show(1002, "参数pay_type不能为空");
        }
        $prove_img = isset($this->post['prove_img']) && $this->post['prove_img'] !== "" ? trim($this->post['prove_img']) : "";

        $personid = isset($this->post['personid']) && $this->post['personid'] !== "" ? intval($this->post['personid']) : "";
        if ($personid == "") {
            return error_show(1002, "参数personid不能为空");
        }
        $to = GetInfoById($token, ['id' => $personid]);
        if (empty($to) || $to['code'] != 0) {
            return app_show(1002, "负责人信息不存在");
        }
        $telephone = isset($this->post['telephone']) && $this->post['telephone'] !== "" ? trim($this->post['telephone']) : "";
        $person = isset($to['data']['nickname']) && $to['data']['nickname'] != "" ? $to['data']['nickname'] : "";
        $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
        if ($name == "") {
            return error_show(1002, "数据标题不能为空");
        }
        $is_platform = isset($this->post['is_platform']) && $this->post['is_platform'] !== "" ? intval($this->post['is_platform']) : "0";
        $ticket_type = isset($this->post['ticket_type']) && $this->post['ticket_type'] !== "" ? trim($this->post['ticket_type']) : "";

        $registercode = isset($this->post['registercode']) && $this->post['registercode'] !== "" ? trim($this->post['registercode']) : "";
//        $capital= isset($this->post['capital']) && $this->post['capital']!==""? intval($this->post['capital']) :"";
        $product_category = isset($this->post['product_category']) && $this->post['product_category'] !== "" ? trim($this->post['product_category']) : "";
        $is_payment = isset($this->post['is_payment']) && $this->post['is_payment'] !== "" ? trim($this->post['is_payment']) : "";
        $notice_mark = isset($this->post['notice_mark']) && $this->post['notice_mark'] !== "" ? trim($this->post['notice_mark']) : "";
        $agreement = isset($this->post['agreement']) && $this->post['agreement'] !== "" ? trim($this->post['agreement']) : "";
        $warehouse = isset($this->post['warehouse']) && $this->post['warehouse'] !== "" ? trim($this->post['warehouse']) : "";
        $pay_day = isset($this->post['pay_day']) && $this->post['pay_day'] !== "" ? trim($this->post['pay_day']) : "";
        $scope= isset($this->post['scope']) && $this->post['scope']!==""?trim($this->post['scope']) :"";
////        $status= isset($this->post['status']) && $this->post['status']!==""?trim($this->post['status']) :"";
//        $registertime= isset($this->post['registertime']) && $this->post['registertime']!==""? $this->post['registertime'] :"";
        $supplier_img = isset($this->post['supplier_img']) && $this->post['supplier_img'] !== "" ? trim($this->post['supplier_img']) : "";
        $contactor = isset($this->post['contactor']) && $this->post['contactor'] !== "" ? trim($this->post['contactor']) : "";
        if ($contactor == "") {
            return error_show(1002, "参数contactor不能为空");
        }
        $mobile = isset($this->post['mobile']) && $this->post['mobile'] !== "" ? trim($this->post['mobile']) : "";
        if ($mobile == "") {
            return error_show(1002, "参数mobile不能为空");
        }
        $position = isset($this->post['position']) && $this->post['position'] !== "" ? trim($this->post['position']) : "";
        if ($position == "") {
            return error_show(1002, "参数position不能为空");
        }
        $email = isset($this->post['email']) && $this->post['email'] !== "" ? trim($this->post['email']) : "";

//        if($registercode==""){
//            return error_show(1002,"参数registercode不能为空");
//        }
//        Db::startTrans();
//        try {
        $data = [
            "id" => $id,
            "source" => $source,
            "name" => $name,
            "code" => $code['code'],
            "coop_state" => $coop_state,
            "category" => $category,
            "delivery_way" => $delivery_way,
            "level" => $level,
            "supplier_type" => $supplier_type,
            "pay_type" => $pay_type,
            "registercode" => $registercode,
            "product_category" => $product_category,
            "is_payment" => $is_payment,
            "notice_mark" => $notice_mark,
            "agreement" => $agreement,
            "warehouse" => $warehouse,
            "pay_day" => $pay_day,
            "type" => $type,
            "license_img" => $supplier_img=="" ?$info['data']['license_img']:"",
            "is_del" => 0,
            "scope"=>$scope,
            "ticket_type" => $ticket_type,
            "updatetime" => date("Y-m-d H:i:s"),
            "is_platform" => $is_platform,
            "prove_img" => $prove_img,
            "person" => $person,
            "personid" => $personid,
            "remark" => "",
            'updaterid' => $this->uid,
            'updater' => $this->uname,
        ];
        if ($supplier_img !== "" && $info['data']['license_img'] != $supplier_img) {
            $ocr = new Ocr();
            $dataw = $ocr->query("", $supplier_img);

            if ($dataw['code'] == 0 && isset($dataw['data']['result'])) {
                $license = $dataw['data']['result'];
                $data['legaler'] = isset($license['legal_representative']) ? $license['legal_representative'] : "";
                $data['addr'] = isset($license['address']) ? $license['address'] : "";
                $data['registertime'] = isset($license['found_date']) ? $license['found_date'] : "";
                $data['scope'] = isset($license['business_scope']) ? $license['business_scope'] : "";
                $data['capital'] = isset($license['registered_capital']) ? $license['registered_capital'] : "";
                $data['nature'] = isset($license['type']) ? $license['type'] : "";
                $data['registercode'] = isset($license['registration_number']) ? $license['registration_number'] : "";//12356
                $data['ocr_status'] = 1;
                if ($name != $license['name']) {
                    $data['remark'] = "企业名称与营业执照不匹配";
                    $data['ocr_status'] = 1;
                }
                if ($registercode != $license['registration_number'] && $registercode != '') {
                    $data['remark'] .= "企业名称与营业执照不匹配";
                    $data['ocr_status'] = 1;
                }
            } else {
                $data['remark'] = "营业执照识别失败";
                $data['ocr_status'] = 2;
            }
        }

        $temp = [];
        $temp['code'] = $info['data']['code'];
        $temp['contactor'] = $contactor;
        $temp['mobile'] = $mobile;
        $temp['position'] = $position;
        $temp['telephone'] = $telephone;
        $temp['email'] = $email;
        $temp['is_del'] = 0;
        $temp['updatetime'] = date("Y-m-d H:i:s");


        $res = $userCommon->handle('sEdit', ['data' => $data, 'contact' => $temp]);

        Db::startTrans();
        try {
            if ($info['data']['personid'] != $personid) {
                $good = Db::name("good_basic")->where(["supplierNo" => $info['data']['code'], "is_del" => 0])->column('id');
                if (!empty($good)) {
                    $up = Db::name("good_basic")->where(["id" => $good])->update(["chargerid" => $personid, "charger" => $person, "updatetime" => date("Y-m-d H:i:s")]);
                    if ($up == false) throw new Exception("商品负责人更新失败");
                }
            }

            //更新同一个供应商下虚拟仓的负责人
            Db::name('warehouse_info')
                ->where(['is_del' => 0, 'supplierNo' => $code['code'], 'wsm_type' => 2])
                ->update(['contactor' => $data['personid'], 'contactor_name' => $data['person']]);

            Db::commit();
            return json_show($res['code'], $res['message'], $res['data']);
        } catch (Exception $exception) {
            Db::rollback();
            return json_show(1004, $exception->getMessage());
        }
    }

    public function info()
    {
        $code = $this->request->only(['code' => ''], 'post', 'trim');
        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('sInfo', $code);
        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //删除 供应商
    public function del()
    {
        $param = $this->request->only(['id' => 0], 'post', 'trim');

        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('delete', [
            'ids' => [$param['id']],
            'type' => 3,
            'updater' => $this->uname,
            'updaterid' => $this->uid,
        ]);
        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //启/禁用 供应商
    public function status()
    {
        $param = $this->request->only(['id', 'status'], 'post', 'trim');

        $val = Validate::rule([
            'id' => 'require|number|gt:0',
            'status|状态' => 'require|number|in:0,1'
        ]);

        if (!$val->check($param)) return json_show(1005, $val->getError());

        $userCommon = \app\admin\common\User::getIns();

        $rs = $userCommon->handle('status', [
            'id' => $param['id'],
            'status' => $param['status'],
            'type' => 3,
            'updater' => $this->uname,
            'updaterid' => $this->uid,
        ]);
        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //供应商升级成业务公司
    public function upgrade(){

        $param = $this->request->filter('trim')->post();

        $userCommon = \app\admin\common\User::getIns();

        $param['uid']=$this->uid;
        $param['uname']=$this->uname;

        $rs = $userCommon->handle('upgrade', $param);

        if (isset($rs['code']) && $rs['code'] == 0) {
            //更新用户和角色关联表的数据
            Db::name('user_role')
                ->where(['companyNo' => $param['code'], 'company_type' => 1])
                ->update(['company_type' => 2]);
        }

        return json_show($rs['code'], $rs['message'], $rs['data']);

    }
}