<?php

namespace app\common\api;

use app\common\model\EnterpriseVerifyMgr;

class EnterpriseVerifyMgrApi {

    public static function getOne($id) {
        return EnterpriseVerifyMgr::findOrEmpty($id);
    }

    public static function getList($params) {
        $order = $params["order"] ?: "desc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        $where[] = ["m.delete", "=", 0];
        $count = EnterpriseVerifyMgr::alias("m")->leftJoin("sys_company c", "c.id=m.companyId")->where($where)->count();
        $list = EnterpriseVerifyMgr::alias("m")->leftJoin("sys_company c", "c.id=m.companyId")->where($where)->limit($offset, $limit)->order("m.updateTime desc,m.createTime desc")->field("m.*,c.name as companyName")->select()->toArray();
        $enterpriseTags = DictApi::selectByParentCode("enterprise_tag");
        $organizationTags = DictApi::selectByParentCode("organization_tag");
        $institutionTags = DictApi::selectByParentCode("institution_tag");
        foreach ($list as $key => $item) {
            $list[$key]["enterpriseTagName"] = $enterpriseTags[$item["enterpriseTag"]];
            $list[$key]["organizationTagName"] = $organizationTags[$item["organizationTag"]];
            $list[$key]["institutionTagName"] = $institutionTags[$item["institutionTag"]];
        }
        return ["total" => $count, "rows" => $list];
    }

    public static function getAll() {
        $where[] = ["delete", "=", 0];
        $list = EnterpriseVerifyMgr::where($where)->order("sn asc")->select()->toArray();
        return $list;
    }

    public static function edit($params) {
        $data["enterpriseTag"] = $params["enterpriseTag"];
        $data["organizationTag"] = $params["organizationTag"];
        $data["institutionTag"] = $params["institutionTag"];
        $data["companyId"] = $params["companyId"];
        $data["description"] = $params["description"];
        $data["uniCode"] = implode(",", array_unique(array_filter(explode(",", $params["uniCode"]))));
        if ($params["id"]) {
            $data["id"] = $params["id"];
            $data["updateTime"] = date("Y-m-d H:i:s");
            $data["updateUser"] = session("user")["uid"];
            return EnterpriseVerifyMgr::update($data);
        } else {
            $data["id"] = getStringId();
            $data["createTime"] = date("Y-m-d H:i:s");
            $data["createUser"] = session("user")["uid"];
            return EnterpriseVerifyMgr::insert($data);
        }
    }

    public static function delete($id) {
        $data["id"] = $id;
        $data["delete"] = 1;
        $data["updateUser"] = session("user")["uid"];
        $data["updateTime"] = date("Y-m-d H:i:s");
        return EnterpriseVerifyMgr::update($data);
    }

    public static function chkExist($tag, $id = 0) {
        $where = [];
        if ($id) {
            $where[] = ["id", "<>", $id];
        }
        $where[] = ["delete", "=", 0];
        $where[] = ["enterpriseTag", "=", $tag];
        return EnterpriseVerifyMgr::where($where)->find();
    }

    public static function chkUnicodeExist($codes, $id = 0) {
        $where = [];
        if ($id) {
            $where[] = ["id", "<>", $id];
        }
        $codes = array_unique(array_filter(explode(",", $codes)));
        $where[] = ["delete", "=", 0];
        $whereRaw = sprintf("concat(',',uniCode,',') regexp(',%s,')", implode(",|,", $codes));
        return EnterpriseVerifyMgr::where($where)->whereRaw($whereRaw)->find();
    }

}
