<?php

namespace app\admin\controller;

use app\admin\common\Ocr;
use app\admin\model\ChangeLog;
use think\App;
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 = new \app\admin\common\User();
        $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 = new \app\admin\common\User();
        $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 = new \app\admin\common\User();
        $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, "数据标题不能为空");
        }
//        $rename = Db::name('supplier')->where(['is_del' => 0, 'name' => $name])->where([["id", "<>", $id]])->find();

//        if ($rename != false) {
//            return error_show(1002, "公司名称已存在");
//        }
//        $nature = isset($this->post['nature']) && $this->post['nature']!==""? trim($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'] !== "" ? trim($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']!==""?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,
            "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,
            "is_del" => 0,
            // "status"=>$info['license_img']==$supplier_img ? $info['status']:0,
            "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['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;
            }
        }
//            $ino = array_diff($data, $info);
//            $tem = json_encode($ino, JSON_UNESCAPED_UNICODE);
//            $jsp = json_encode($info, JSON_UNESCAPED_UNICODE);


//            $join = Db::name('supplier')->where(['id' => $id, 'is_del' => 0])->save($data);
//            if ($join) {
//                ChangeLog::logAdd("1", $info['code'], $jsp, $tem, $this->post['token'], $this->post);
//                $item = Db::name('supplier_contact')->where(['code' => $info['code']])->find();
        $temp = [];
//                isset($item['id']) && $item['id'] !== "" ? $temp['id'] = $item['id'] : '';
        $temp['code'] = $info['code'];
        $temp['contactor'] = $contactor;
        $temp['mobile'] = $mobile;
        $temp['position'] = $position;
        $temp['telephone'] = $telephone;
        $temp['email'] = $email;
        $temp['is_del'] = 0;
//                isset($item['id']) && $item['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
        $temp['updatetime'] = date("Y-m-d H:i:s");
//                    $st = array_diff($item,$temp);
//                    $snm =json_encode($st);
//                $vp = Db::name('supplier_contact')->save($temp);
//                if ($vp == "") {
//                    ChangeLog::logAdd("1",$item['code'],$item,$snm,$this->post['token'],$this->post);
//                    Db::rollback();
//                    return error_show(1002, "更新失败");
//                }

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

//                Db::commit();
//                return app_show(0, "编辑成功");
//            } else {
//                Db::rollback();
//                return error_show(1002, "编辑失败");
//            }
//        } catch (\Exception $e) {
//            Db::rollback();
//            return error_show(1003, $e->getMessage());
//        }
    }

    public function info()
    {
        $code = $this->request->only(['code' => ''], 'post', 'trim');
        $userCommon = new \app\admin\common\User();
        $rs = $userCommon->handle('sInfo', $code);

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


//        if ($code == "") {
//            return error_show(1002, "参数code不能为空");
//        }
//        $info = Db::name('supplier')->where(['code' => $code, 'is_del' => 0])->find();
//        if ($info == "") {
//            return error_show(1002, "未找到数据");
//        }
//        $contact = Db::name("supplier_contact")->where(["code" => $info['code'], "is_del" => 0])->find();
//        $info['contactor'] = isset($contact['contactor']) ? $contact['contactor'] : "";
//        $info['mobile'] = isset($contact['mobile']) ? $contact['mobile'] : "";
//        $info['position'] = isset($contact['position']) ? $contact['position'] : "";
//        $info['email'] = isset($contact['email']) ? $contact['email'] : "";
//        $info['telephone'] = isset($contact['telephone']) ? $contact['telephone'] : "";
//        $info['supplier_img'] = isset($info['license_img']) ? $info['license_img'] : "";
//        return app_show(0, "获取成功", $info);
    }

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

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

//        $sup = Db::name("supplier")
//            ->where(['id' => $id, 'is_del' => 0])
//            ->find();
//        if (empty($sup)) return error_show(1005, '未找到对应数据');
//
//        Db::startTrans();
//        try {
//            //维护供应商表
//            $supp = ['is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")];
//            Db::name("supplier")
//                ->where(['id' => $id, 'is_del' => 0])
//                ->update($supp);
//
//            $vam = array_intersect_key($sup, $supp);//比较两个数组的键名,并返回交集

//            ChangeLog::logAdd("1", $sup['code'], json_encode($sup, JSON_UNESCAPED_UNICODE), json_encode($vam, JSON_UNESCAPED_UNICODE), $this->post['token'], $this->post);

//            Db::commit();
//            return app_show(0, '删除成功');
//        } catch (\Exception $exception) {
//            Db::rollback();
//            return error_show(1005, '删除失败' . $exception->getMessage());
//        }


//        $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
//        $sup = Db::name('supplier')->where(["is_del"=>0,'id'=>$id])->find();
//        if($sup==false){
//            return error_show(1002,"供应商信息不存在");
//        }
//        $supp= Db::name('supplier')->update(['id'=>$id,'is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
//        $vam = array_diff($sup,$supp);
//        $vap = json_encode($vam);
//        if($supp){
//            ChangeLog::logAdd("1",$sup['code'],$sup,$vap,$this->post['token'],$this->post);
//
//            //维护联系人账号
//            $res = SupplierUser::where([
//                'is_del' => SupplierUser::$is_del_normal,
//                'status' => SupplierUser::$status_normal,
//                'type' => SupplierUser::$type_default
//            ])->whereFindInSet('supplierNos', $sup['code'])->save([
//                'is_del' => SupplierUser::$is_del_deleted,
//                'updatetime' => date('Y-m-d H:i:s')
//            ]);
//            if (!$res) return error_show('删除联系人账号失败');
//
//            return error_show(0,"删除成功");
//        }else{
//            return error_show(1002,"删除失败");
//        }
    }

    //启/禁用 供应商
    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 = new \app\admin\common\User();

        $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']);

//        $param = $this->request->filter('trim')->only(['id', 'status'], 'post');
//
//        $val = Validate::rule(['id' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1,2']);
//
//        if (!$val->check($param)) return error_show(1005, $val->getError());

//        $msg = $param['status'] == 1 ? "启用" : "禁用";

//        $info = Db::name("supplier")
//            ->alias('a')
//            ->field('a.id,a.code,a.status,b.mobile')
//            ->leftJoin('supplier_contact b', 'b.code=a.code')
//            ->where(['a.id' => $param['id'], 'a.is_del' => 0])
//            ->find();
//        if (empty($info)) return error_show(1005, '未找到对应数据');
//        if ($info['status'] == $param['status']) return error_show(1005, '不能重复' . $msg);

//        Db::startTrans();
//        try {
//            //维护供应商表
//            Db::name("supplier")
//                ->where('id', $param['id'])
//                ->where('status', '<>', $param['status'])
//                ->save([
//                    'status' => $param['status'],
//                    'updatetime' => date("Y-m-d H:i:s"),
//                ]);
//
//            Db::commit();
//            return app_show(0, $msg . '成功');
//        } catch (\Exception $exception) {
//            Db::rollback();
//            return error_show(1005, $exception->getMessage());
//        }

//        $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();
//        if(!$info){
//            return error_show(1002,"未找到对应数据");
//        }
//        $status =  isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
//        if($status===""){
//            return error_show(1002,"参数status不能为空");
//        }
//        if(!in_array($status,[0,1,2])){
//            return error_show(1002,"参数status无效");
//        }
//        $info['status']=$status;
//        $info['updatetime']=date("Y-m-d H:i:s");
//        $msg = $status==1?"启用":"禁用";
//        $update = Db::name("supplier")->save($info);
//        return  $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");

    }

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

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

        $userCommon = new \app\admin\common\User();

        $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']);

    }
}