<?php

namespace app\common\api;

use app\admin\controller\EnterpriseChangeRecord;
use app\common\api\CompanyApi;
use app\enterprise\model\EnterpriseRecord;
use think\facade\Db;
use app\admin\model\Enterprise;
use app\common\state\CommonConst;
use app\common\api\Nhc;

class EnterpriseApi {

    public static function getOne($id) {
        $ep = Enterprise::findOrEmpty($id);
        if ($ep->delete == 1) {
            return null;
        }
        $area = array_filter([$ep["provinceName"], $ep["cityName"], $ep["countyName"]]);
        $ep["fullLocationName"] = $area ? implode("", $area) : "";
        $ep["medicalCommunityName"] = $ep["medicalCommunityId"] ? Nhc::getMedicalCommunityMap()[$ep["medicalCommunityId"]] : "";
        return $ep;
    }

    public static function getSimpleList($extra = array()) {
        $where[] = ["active", "=", 1];
        $where[] = ["delete", "=", 0];
        $where[] = ['type', '=', session('user')['type']];
        if ($extra) {
            $where = array_merge($where, $extra);
        }
        return $list = Enterprise::where($where)->order("name", 'asc')->field("name,id")->select()->toArray();
    }

    public static function getMultipleList($typeList = array()) {
        $where[] = ["active", "=", 1];
        $where[] = ["delete", "=", 0];
        $where[] = ['type', 'in', $typeList];
        return $list = Enterprise::where($where)->order("name", 'asc')->field("name,id")->select()->toArray();
    }

    public static function getList($request, $isExport = false) {
        $companyId = session('user')['companyId'];
        $company_info = CompanyApi::getOne($companyId);
        $where = [];
        $whereRaw = "";
        $where[] = ['type', '=', session('user')['type']];
        if (session('user')['type'] == CommonConst::ENTERPRISE_WJ) {
            $where[] = ["step", "in", [1, 2]];
        }
        if ($company_info['code'] != 'super' && !self::chkUserInSuperusers()) {
            if (session('user')['type'] == 1) {
                $whr[] = ["companyId", "=", $companyId];
                $whr[] = ["delete", "=", 0];
                $list = \app\common\model\EnterpriseVerifyMgr::where($whr)->select()->toArray();
                $talentType = []; //查找单位对应标签
                $org = [];
                $ins = [];
                $uniCodes = []; //查找单位对应例外
                foreach ($list as $k => $v) {
                    if (!empty($v['enterpriseTag'])) {
                        array_push($talentType, $v['enterpriseTag']);
                    }
                    if (!empty($v['organizationTag'])) {
                        array_push($org, $v['organizationTag']);
                    }
                    if (!empty($v['institutionTag'])) {
                        array_push($ins, $v['institutionTag']);
                    }
                    if (!empty($v["uniCode"])) {
                        $codes = explode(",", $v["uniCode"]);
                        $uniCodes = array_merge($uniCodes, (array) $codes);
                    }
                }

                $_whr[] = ["uniCode", "<>", ""];
                $_whr[] = ["delete", "=", 0];
                $_whr[] = ["companyId", "<>", $companyId];
                $_list = \app\common\model\EnterpriseVerifyMgr::where($_whr)->select()->toArray();
                $expUniCodes = []; //排除其它单位的例外
                foreach ($_list as $_v) {
                    $codes = explode(",", $_v["uniCode"]);
                    $expUniCodes = array_merge($expUniCodes, (array) $codes);
                }

                if (($talentType || $org || $ins) && $uniCodes) {
                    if ($expUniCodes) {
                        $whereRaw = sprintf('(enterpriseTag in ("%s") or organizationTag in ("%s") or institutionTag in ("%s") and idCard not in ("%s")) or idCard in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $expUniCodes), implode('","', $uniCodes));
                    } else {
                        $whereRaw = sprintf('enterpriseTag in ("%s") or organizationTag in ("%s") or institutionTag in ("%s") or idCard in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $uniCodes));
                    }
                }

                if (($talentType || $org || $ins) && !$uniCodes) {
                    if ($expUniCodes) {
                        $whereRaw = sprintf('enterpriseTag in ("%s") or organizationTag in ("%s") or institutionTag in ("%s") and idCard not in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $expUniCodes));
                    } else {
                        $whereRaw = sprintf('enterpriseTag in ("%s") or organizationTag in ("%s") or institutionTag in ("%s") ', implode('","', $talentType), implode('","', $org), implode('","', $ins));
                    }
                }

                if (!$talentType && !$org && !$ins && $uniCodes) {
                    $where[] = ["idCard", "in", $uniCodes];
                }
            }
        }

        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;

        $name = urldecode(trim($request->param("name")));
        $idCard = urldecode(trim($request->param("idCard")));
        $legal = urldecode(trim($request->param("legal")));
        $medicalCommunityId = urldecode(trim($request->param("medicalCommunityId")));
        $isGeneral = urldecode(trim($request->param("isGeneral")));
        $ephone = urldecode(trim($request->param("ephone")));
        $agentName = urldecode(trim($request->param("agentName")));
        $agentPhone = urldecode(trim($request->param("agentPhone")));
        $checkState = urldecode(trim($request->param("checkState")));
        $active = urldecode(trim($request->param("active")));
        $street = urldecode(trim($request->param("street")));
        $special = urldecode(trim($request->param("special")));
        $agencyType = urldecode(trim($request->param("agencyType")));
        $industryFieldNew = urldecode(trim($request->param("industryFieldNew")));
        $industryFieldOld = urldecode(trim($request->param("industryFieldOld")));
        $enterpriseTag = urldecode(trim($request->param("enterpriseTag")));
        $enterpriseType = urldecode(trim($request->param("enterpriseType")));

        $where[] = ["delete", "=", 0];
        if ($name) {
            $where[] = ["name", "like", "%{$name}%"];
        }
        if ($idCard) {
            $where[] = ["idCard", "like", "%{$idCard}%"];
        }
        if ($legal) {
            $where[] = ["legal", "like", "%{$legal}%"];
        }
        if ($medicalCommunityId) {
            $where[] = ["medicalCommunityId", "=", $medicalCommunityId];
        }
        if ($isGeneral) {
            $where[] = ["isGeneral", "=", $isGeneral];
        }
        if ($ephone) {
            $where[] = ["ephone", "like", "%{$ephone}%"];
        }
        if ($agentName) {
            $where[] = ["agentName", "like", "%{$agentName}%"];
        }
        if ($agentPhone) {
            $where[] = ["agentPhone", "like", "%{$agentPhone}%"];
        }
        if ($checkState) {
            $where[] = ["checkState", "=", "{$checkState}"];
        }
        if ($active) {
            $where[] = ["active", "=", "{$active}"];
        }
        if ($street) {
            $where[] = ["street", "=", "{$street}"];
        }
        if ($special !== "") {
            $where[] = ["special", "=", "{$special}"];
        }
        if ($agencyType) {
            $where[] = ["agencyType", "=", "{$agencyType}"];
        }
        if ($industryFieldNew) {
            $where[] = ["industryFieldNew", "=", "{$industryFieldNew}"];
        }
        if ($industryFieldOld) {
            $where[] = ["industryFieldOld", "=", "{$industryFieldOld}"];
        }
        if ($enterpriseTag) {
            if ($special == 1) {
                $where[] = ["institutionTag", "=", "{$enterpriseTag}"];
            } else if ($special == 3) {
                $where[] = ["organizationTag", "=", "{$enterpriseTag}"];
            } else {
                $where[] = ["enterpriseTag", "=", "{$enterpriseTag}"];
            }
        }
        if ($enterpriseType) {
            $where[] = ["enterpriseType", "=", "{$enterpriseType}"];
        }

        if ($whereRaw) {
            $count = Enterprise::where($where)->whereRaw($whereRaw)->count();
        } else {
            $count = Enterprise::where($where)->count();
        }

        if ($count > 0) {
            $talentTypeList = DictApi::selectByParentCode("enterprise_tag");
            $orgList = DictApi::selectByParentCode("organization_tag");
            $insList = DictApi::selectByParentCode("institution_tag");
            $industryFieldNewList = DictApi::selectByParentCode("industry_field");
            $streetList = DictApi::selectByParentCode("street");
            $medicalCommunityList = Nhc::getMedicalCommunityMap();
            //dd($talentTypeList);
            if ($whereRaw) {
                if ($isExport) {
                    $list = Enterprise::where($where)->whereRaw($whereRaw)->order("createTime", 'desc')->select()->toArray();
                } else {
                    $list = Enterprise::where($where)->whereRaw($whereRaw)->limit($offset, $limit)->order("createTime", 'desc')->select()->toArray();
                }
            } else {
                if ($isExport) {
                    $list = Enterprise::where($where)->order("createTime", 'desc')->select()->toArray();
                } else {
                    $list = Enterprise::where($where)->limit($offset, $limit)->order("createTime", 'desc')->select()->toArray();
                }
            }
            foreach ($list as $k => &$v) {
                unset($v['password']);
                $v['enterpriseTagName'] = array_key_exists($v['enterpriseTag'], $talentTypeList) ? $talentTypeList[$v['enterpriseTag']] : ''; //此处旧字段为talentType,新字段为enterpriseTag，为防止数据污染与丢失，因而这样写
                $v['organizationTagName'] = array_key_exists($v['organizationTag'], $orgList) ? $orgList[$v['organizationTag']] : '';
                $v['institutionTagName'] = array_key_exists($v['institutionTag'], $orgList) ? $orgList[$v['institutionTag']] : '';
                $v['industryFieldNewName'] = $industryFieldNewList[$v['industryFieldNew']];
                $v['streetName'] = $streetList[$v['street']];
                $v['medicalCommunityName'] = $v['medicalCommunityId'] ? $medicalCommunityList[$v['medicalCommunityId']] : "";
            }
        } else {
            $list = [];
        }
        return ["total" => $count, "rows" => $list];
    }

    public static function getRecordList($request) {
        $companyId = session('user')['companyId'];
        $company_info = CompanyApi::getOne($companyId);
        $where = [];
        $whereRaw = "";
        $where[] = ['r.type', '=', session('user')['type']];
        if ($company_info['code'] != 'super' && !self::chkUserInSuperusers()) {
            if (session('user')['type'] == 1) {
                $whr[] = ["companyId", "=", $companyId];
                $whr[] = ["delete", "=", 0];
                $list = \app\common\model\EnterpriseVerifyMgr::where($whr)->select()->toArray();
                $talentType = []; //查找单位对应标签
                $org = [];
                $ins = [];
                $uniCodes = [];
                foreach ($list as $k => $v) {
                    if (!empty($v['enterpriseTag'])) {
                        array_push($talentType, $v['enterpriseTag']);
                    }
                    if (!empty($v['organizationTag'])) {
                        array_push($org, $v['organizationTag']);
                    }
                    if (!empty($v['institutionTag'])) {
                        array_push($ins, $v['institutionTag']);
                    }
                    if (!empty($v["uniCode"])) {
                        $codes = explode(",", $v["uniCode"]);
                        $uniCodes = array_merge($uniCodes, (array) $codes);
                    }
                }


                $_whr[] = ["uniCode", "<>", ""];
                $_whr[] = ["delete", "=", 0];
                $_whr[] = ["companyId", "<>", $companyId];
                $_list = \app\common\model\EnterpriseVerifyMgr::where($_whr)->select()->toArray();
                $expUniCodes = []; //排除其它单位的例外
                foreach ($_list as $_v) {
                    $codes = explode(",", $_v["uniCode"]);
                    $expUniCodes = array_merge($expUniCodes, (array) $codes);
                }

                if (($talentType || $org || $ins) && $uniCodes) {
                    if ($expUniCodes) {
                        $whereRaw = sprintf('(newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") and newIdCard not in ("%s")) or newIdCard in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $expUniCodes), implode('","', $uniCodes));
                    } else {
                        $whereRaw = sprintf('newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") or newIdCard in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $uniCodes));
                    }
                }
                if (($talentType || $org || $ins) && !$uniCodes) {
                    if ($expUniCodes) {
                        $whereRaw = sprintf('newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") and newIdCard not in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $expUniCodes));
                    } else {
                        $whereRaw = sprintf('newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") ', implode('","', $talentType), implode('","', $org), implode('","', $ins));
                    }
                }
            }
        }

        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;

        $oldName = trim($request->param("oldName"));
        $oldIdCard = trim($request->param("oldIdCard"));
        $oldLegal = trim($request->param("oldLegal"));
        $oldMedicalCommunityId = trim($request->param("oldMedicalCommunityId"));
        $oldIsGeneral = trim($request->param("oldIsGeneral"));
        $oldStreet = trim($request->param("oldStreet"));
        $oldEnterpriseTag = trim($request->param("oldEnterpriseTag"));
        $oldIndustryFieldNew = trim($request->param("oldIndustryFieldNew"));
        $newName = trim($request->param("newName"));
        $newIdCard = trim($request->param("newIdCard"));
        $newMedicalCommunityId = trim($request->param("newMedicalCommunityId"));
        $newIsGeneral = trim($request->param("newIsGeneral"));
        $newAgentName = trim($request->param("newAgentName"));
        $newStreet = trim($request->param("newStreet"));
        $newEnterpriseTag = trim($request->param("newEnterpriseTag"));
        $newIndustryFieldNew = trim($request->param("newIndustryFieldNew"));
        $checkState = trim($request->param("checkState"));

        if ($oldName) {
            $where[] = ["r.oldName", "like", "%{$oldName}%"];
        }
        if ($oldIdCard) {
            $where[] = ["r.oldIdCard", "like", "%{$oldIdCard}%"];
        }
        if ($oldLegal) {
            $where[] = ["r.oldLegal", "like", "%{$oldLegal}%"];
        }
        if ($oldMedicalCommunityId) {
            $where[] = ["r.oldMedicalCommunityId", "=", $oldMedicalCommunityId];
        }
        if ($oldIsGeneral) {
            $where[] = ["r.oldIsGeneral", "=", $oldIsGeneral];
        }
        if ($oldStreet) {
            $where[] = ["r.oldStreet", "=", "{$oldStreet}"];
        }
        if ($oldEnterpriseTag) {
            $where[] = ["r.oldEnterpriseTag", "=", "{$oldEnterpriseTag}"];
        }
        if ($oldIndustryFieldNew) {
            $where[] = ["r.oldIndustryFieldNew", "=", "{$oldIndustryFieldNew}"];
        }
        if ($newName) {
            $where[] = ["r.newName", "like", "%{$newName}%"];
        }
        if ($newIdCard) {
            $where[] = ["r.newIdCard", "like", "%{$newIdCard}%"];
        }
        if ($newMedicalCommunityId) {
            $where[] = ["r.newMedicalCommunityId", "=", $newMedicalCommunityId];
        }
        if ($newIsGeneral) {
            $where[] = ["r.newIsGeneral", "=", $newIsGeneral];
        }
        if ($newAgentName) {
            $where[] = ["r.newAgentName", "like", "%{$newAgentName}%"];
        }
        if ($checkState) {
            $where[] = ["r.checkState", "=", "{$checkState}"];
        } else {
            $where[] = ['r.checkState', '>', 1];
        }
        if ($newStreet) {
            $where[] = ["r.newStreet", "=", "{$newStreet}"];
        }
        if ($newEnterpriseTag) {
            $where[] = ["r.newEnterpriseTag", "=", "{$newEnterpriseTag}"];
        }
        if ($newIndustryFieldNew) {
            $where[] = ["r.newIndustryFieldNew", "=", "{$newIndustryFieldNew}"];
        }
        $where[] = ["e.delete", "=", 0];
        if ($whereRaw) {
            $count = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->where($where)->whereRaw($whereRaw)->count();
        } else {
            $count = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->where($where)->count();
        }
        if ($count > 0) {
            $talentTypeList = DictApi::selectByParentCode("enterprise_tag");
            $industryFieldNewList = DictApi::selectByParentCode("industry_field");
            $streetList = DictApi::selectByParentCode("street");
            $medicalCommunityList = Nhc::getMedicalCommunityMap();
            if ($whereRaw) {
                $list = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->field("r.*")->where($where)->whereRaw($whereRaw)->limit($offset, $limit)->order("r.createTime", 'desc')->select()->toArray();
            } else {
                $list = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->field("r.*")->where($where)->limit($offset, $limit)->order("r.createTime", 'desc')->select()->toArray();
            }
            foreach ($list as $k => &$v) {
                $v['oldStreetName'] = $streetList[$v['oldStreet']];
                $v['newStreetName'] = $streetList[$v['newStreet']];
                $v['oldEnterpriseTagName'] = $talentTypeList[$v['oldEnterpriseTag']];
                $v['newEnterpriseTagName'] = $talentTypeList[$v['newEnterpriseTag']]; //此处旧字段为talentType,新字段为enterpriseTag，为防止数据污染与丢失，因而这样写
                $v['oldIndustryFieldNewName'] = $industryFieldNewList[$v['oldIndustryFieldNew']];
                $v['newIndustryFieldNewName'] = $industryFieldNewList[$v['newIndustryFieldNew']];
                $v['oldMedicalCommunityName'] = $medicalCommunityList[$v['oldMedicalCommunityId']];
                $v['newMedicalCommunityName'] = $medicalCommunityList[$v['newMedicalCommunityId']];
            }
        } else {
            $list = [];
        }
        return ["total" => $count, "rows" => $list];
    }

    public static function getExportList($request) {
        $companyId = session('user')['companyId'];
        $company_info = CompanyApi::getOne($companyId);
        $where = [];
        $whereRaw = "";
        $where[] = ['r.type', '=', session('user')['type']];
        if ($company_info['code'] != 'super' && !self::chkUserInSuperusers()) {
            if (session('user')['type'] == 1) {
                $whr[] = ["companyId", "=", $companyId];
                $whr[] = ["delete", "=", 0];
                $list = \app\common\model\EnterpriseVerifyMgr::where($whr)->select()->toArray();
                $talentType = []; //查找单位对应标签
                $org = [];
                $ins = [];
                $uniCodes = [];
                foreach ($list as $k => $v) {
                    if (!empty($v['enterpriseTag'])) {
                        array_push($talentType, $v['enterpriseTag']);
                    }
                    if (!empty($v['organizationTag'])) {
                        array_push($org, $v['organizationTag']);
                    }
                    if (!empty($v['institutionTag'])) {
                        array_push($ins, $v['institutionTag']);
                    }
                    if (!empty($v["uniCode"])) {
                        $codes = explode(",", $v["uniCode"]);
                        $uniCodes = array_merge($uniCodes, (array) $codes);
                    }
                }


                $_whr[] = ["uniCode", "<>", ""];
                $_whr[] = ["delete", "=", 0];
                $_whr[] = ["companyId", "<>", $companyId];
                $_list = \app\common\model\EnterpriseVerifyMgr::where($_whr)->select()->toArray();
                $expUniCodes = []; //排除其它单位的例外
                foreach ($_list as $_v) {
                    $codes = explode(",", $_v["uniCode"]);
                    $expUniCodes = array_merge($expUniCodes, (array) $codes);
                }

                if (($talentType || $org || $ins) && $uniCodes) {
                    if ($expUniCodes) {
                        $whereRaw = sprintf('(newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") and newIdCard not in ("%s")) or newIdCard in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $expUniCodes), implode('","', $uniCodes));
                    } else {
                        $whereRaw = sprintf('newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") or newIdCard in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $uniCodes));
                    }
                }
                if (($talentType || $org || $ins) && !$uniCodes) {
                    if ($expUniCodes) {
                        $whereRaw = sprintf('newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") and newIdCard not in ("%s")', implode('","', $talentType), implode('","', $org), implode('","', $ins), implode('","', $expUniCodes));
                    } else {
                        $whereRaw = sprintf('newEnterpriseTag in ("%s") or newOrganizationTag in ("%s") or newInstitutionTag in ("%s") ', implode('","', $talentType), implode('","', $org), implode('","', $ins));
                    }
                }
            }
        }

        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;

        $oldName = urldecode(trim($request->param("oldName")));
        $oldIdCard = urldecode(trim($request->param("oldIdCard")));
        $oldLegal = urldecode(trim($request->param("oldLegal")));
        $oldMedicalCommunityId = trim($request->param("oldMedicalCommunityId"));
        $oldIsGeneral = trim($request->param("oldIsGeneral"));
        $oldStreet = urldecode(trim($request->param("oldStreet")));
        $oldEnterpriseTag = urldecode(trim($request->param("oldEnterpriseTag")));
        $oldIndustryFieldNew = urldecode(trim($request->param("oldIndustryFieldNew")));
        $newName = urldecode(trim($request->param("newName")));
        $newIdCard = urldecode(trim($request->param("newIdCard")));
        $newMedicalCommunityId = trim($request->param("newMedicalCommunityId"));
        $newIsGeneral = trim($request->param("newIsGeneral"));
        $newAgentName = urldecode(trim($request->param("newAgentName")));
        $newStreet = urldecode(trim($request->param("newStreet")));
        $newEnterpriseTag = urldecode(trim($request->param("newEnterpriseTag")));
        $newIndustryFieldNew = urldecode(trim($request->param("newIndustryFieldNew")));
        $checkState = urldecode(trim($request->param("checkState")));

        if ($oldName) {
            $where[] = ["oldName", "like", "%{$oldName}%"];
        }
        if ($oldIdCard) {
            $where[] = ["oldIdCard", "like", "%{$oldIdCard}%"];
        }
        if ($oldLegal) {
            $where[] = ["oldLegal", "like", "%{$oldLegal}%"];
        }
        if ($oldMedicalCommunityId) {
            $where[] = ["oldMedicalCommunityId", "=", $oldMedicalCommunityId];
        }
        if ($oldIsGeneral) {
            $where[] = ["oldIsGeneral", "=", $oldIsGeneral];
        }
        if ($oldStreet) {
            $where[] = ["oldStreet", "=", "{$oldStreet}"];
        }
        if ($oldEnterpriseTag) {
            $where[] = ["oldEnterpriseTag", "=", "{$oldEnterpriseTag}"];
        }
        if ($oldIndustryFieldNew) {
            $where[] = ["oldIndustryFieldNew", "=", "{$oldIndustryFieldNew}"];
        }
        if ($newName) {
            $where[] = ["newName", "like", "%{$newName}%"];
        }
        if ($newIdCard) {
            $where[] = ["newIdCard", "like", "%{$newIdCard}%"];
        }
        if ($newMedicalCommunityId) {
            $where[] = ["newMedicalCommunityId", "=", $newMedicalCommunityId];
        }
        if ($newIsGeneral) {
            $where[] = ["newIsGeneral", "=", $newIsGeneral];
        }
        if ($newAgentName) {
            $where[] = ["newAgentName", "like", "%{$newAgentName}%"];
        }
        if ($checkState) {
            $where[] = ["r.checkState", "=", "{$checkState}"];
        } else {
            $where[] = ['r.checkState', '>', 1];
        }
        if ($newStreet) {
            $where[] = ["newStreet", "=", "{$newStreet}"];
        }
        if ($newEnterpriseTag) {
            $where[] = ["newEnterpriseTag", "=", "{$newEnterpriseTag}"];
        }
        if ($newIndustryFieldNew) {
            $where[] = ["newIndustryFieldNew", "=", "{$newIndustryFieldNew}"];
        }
        $where[] = ["e.delete", "=", 0];
        if ($whereRaw) {
            $count = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->where($where)->whereRaw($whereRaw)->count();
        } else {
            $count = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->where($where)->count();
        }
        if ($count > 0) {
            $organizationTagList = DictApi::selectByParentCode("organization_tag");
            $institutionTag = DictApi::selectByParentCode("institution_tag");
            $talentTypeList = DictApi::selectByParentCode("enterprise_tag");
            $enterpriseTypeList = DictApi::selectByParentCode("enterprise_type");
            $industryFieldNewList = DictApi::selectByParentCode("industry_field");
            $streetList = DictApi::selectByParentCode("street");
            $agencyTypeList = DictApi::selectByParentCode("agency_type");
            $medicalCommunityList = Nhc::getMedicalCommunityMap();
            if ($whereRaw) {
                $list = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->field("r.*")->where($where)->whereRaw($whereRaw)->order("createTime", 'desc')->select()->toArray();
            } else {
                $list = EnterpriseRecord::alias("r")->leftJoin("un_enterprise e", "e.id=r.mainId")->field("r.*")->where($where)->order("createTime", 'desc')->select()->toArray();
            }
            foreach ($list as $k => &$v) {
                if ($v["special"] == 1) {
                    $v["typeName"] = "事业单位";
                    $v["oldAgencyTagName"] = $institutionTag[$v["oldInstitutionTag"]];
                    $v["newAgencyTagName"] = $institutionTag[$v["newInstitutionTag"]];
                } else if ($v["special"] == 2) {
                    $v["typeName"] = "民办非企业";
                    $v["oldAgencyTagName"] = $organizationTagList[$v["oldOrganizationTag"]];
                    $v["newAgencyTagName"] = $organizationTagList[$v["newOrganizationTag"]];
                } else {
                    $v["typeName"] = "企业用户";
                    if ($v["type"] == CommonConst::ENTERPRISE_NORMAL) {
                        $v["typeName"] .= "(晋江市现代产业体系人才)";
                    } else if ($v["type"] == CommonConst::ENTERPRISE_JC) {
                        $v["typeName"] .= "(集成电路优秀人才)";
                    } else if ($v["type"] == CommonConst::ENTERPRISE_WJ) {
                        $v["typeName"] .= "(卫健医院)";
                    } else if ($v["type"] == CommonConst::ENTERPRISE_GJ) {
                        $v["typeName"] .= "(高教学校)";
                    }
                }
                $v['oldStreetName'] = $streetList[$v['oldStreet']];
                $v['newStreetName'] = $streetList[$v['newStreet']];
                $v['oldEnterpriseTagName'] = $talentTypeList[$v['oldEnterpriseTag']];
                $v['newEnterpriseTagName'] = $talentTypeList[$v['newEnterpriseTag']]; //此处旧字段为talentType,新字段为enterpriseTag，为防止数据污染与丢失，因而这样写
                $v['oldIndustryFieldNewName'] = $industryFieldNewList[$v['oldIndustryFieldNew']];
                $v['newIndustryFieldNewName'] = $industryFieldNewList[$v['newIndustryFieldNew']];
                $v["oldIndustryFieldOldName"] = DictApi::findDictByCode($v['oldIndustryFieldOld'])["name"];
                $v["newIndustryFieldOldName"] = DictApi::findDictByCode($v['newIndustryFieldOld'])["name"];
                $v["oldEnterpriseTypeName"] = $enterpriseTypeList[$v["oldEnterpriseType"]];
                $v["newEnterpriseTypeName"] = $enterpriseTypeList[$v["newEnterpriseType"]];
                $v["oldAgencyTypeName"] = $agencyTypeList[$v["oldAgencyType"]];
                $v["newAgencyTypeName"] = $agencyTypeList[$v["newAgencyType"]];
                $v['oldMedicalCommunityName'] = $medicalCommunityList[$v['oldMedicalCommunityId']];
                $v['newMedicalCommunityName'] = $medicalCommunityList[$v['newMedicalCommunityId']];
                switch ($v["checkState"]) {
                    case 1:
                        $v['checkStateName'] = '保存未提交审核';
                        break;
                    case 2:
                        $v['checkStateName'] = '待审核';
                        break;
                    case 3:
                        $v['checkStateName'] = "审核驳回";
                        break;
                    case 4:
                        $v['checkStateName'] = "审核通过";
                        break;
                    case 5:
                        $v['checkStateName'] = '重新提交';
                        break;
                    case 6:
                        $v['checkStateName'] = '初审驳回';
                        break;
                    case 7:
                        $v['checkStateName'] = '初审通过';
                        break;
                }
            }
        } else {
            $list = [];
        }
        return $list;
    }

    public static function updateById($data) {
        return Enterprise::update($data);
    }

    public static function getOneRecord($id) {
        return EnterpriseRecord::findOrEmpty($id);
    }

    private static function getSuperusersForEnterprise() {
        $superusers = getJsonConfig("../sys_config.json", "super_users_for_enterprise");
        return $superusers;
    }

    public static function chkUserInSuperusers() {
        $superusers = self::getSuperusersForEnterprise();
        $account = session("user")["account"];
        return in_array($account, $superusers);
    }

}
