<?php

namespace app\common\api;

use app\common\model\Dict;
use think\Facade\Db;

/**
 * Description of DictApi
 *
 * @author sgq
 */
class DictApi {

    public static function create($params) {
        $data["code"] = $params["dictCode"];
        $data["name"] = $params["dictName"];
        $data["tips"] = $params["dictTips"];
        $data["pid"] = 0;
        $values = $params["dictValues"];
        if (self::isExistByCode($data["code"]))
            return ["msg" => sprintf("编码[%s]已经存在", $data["code"])];
        $items = array_filter(explode(";", $values));
        $id = Dict::insertGetId($data);
        $childs = [];
        for ($i = 0; $i < count($items); $i++) {
            $fields = array_filter(explode(":", $items[$i]));
            $childs[] = ["pid" => $id, "code" => $fields[0], "name" => $fields[1], "num" => $fields[2]];
        }
        Dict::insertAll($childs);
        return ["code" => 200, "msg" => "成功"];
    }

    public static function update($params) {
        $data["id"] = $params["dictId"];
        $data["code"] = $params["dictCode"];
        $data["name"] = $params["dictName"];
        $data["tips"] = $params["dictTips"];
        $data["pid"] = 0;
        $values = $params["dictValues"];
        if (self::isExistByCode($data["code"], $params["dictId"]))
            return ["msg" => sprintf("编码[%s]已经存在", $data["code"])];
        $items = array_filter(explode(";", $values));
        Dict::update($data);
        //删除原来的子项
        Dict::where(["pid" => $params["dictId"]])->delete();
        $childs = [];
        for ($i = 0; $i < count($items); $i++) {
            $fields = array_filter(explode(":", $items[$i]));
            $childs[] = ["pid" => $params["dictId"], "code" => $fields[0], "name" => $fields[1], "num" => $fields[2]];
        }
        Dict::insertAll($childs);
        return ["code" => 200, "msg" => "成功"];
    }

    public static function delete($id) {
        Dict::where(["id" => $id])->delete();
        Dict::where(["pid" => $id])->delete();
        return ["code" => 200, "msg" => "成功"];
    }

    public static function isExistByCode($code, $id = 0) {
        $where = [];
        if ($id > 0) {
            $where[] = ["id", "<>", $id];
        }
        $where[] = ["code", "=", $code];
        $dict = Dict::where($where)->findOrEmpty()->toArray();
        return $dict;
    }

    /**
     * 获取一条
     * @param type $id
     * @return type
     */
    public static function getOne($id) {
        $info = Dict::where("id", $id)->findOrEmpty()->toArray();
        if ($info) {
            $items = Dict::where("pid", "=", $id)->select()->toArray();
            $info["children"] = $items;
        }
        return $info;
    }

    /**
     * 获取列表
     * @param type $request
     * @return type
     */
    public static function getList($request) {
        $order = trim($request->param("order")) ?: "desc";
        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;
        $name = trim($request->param("condition"));
        $where = [];
        $where[] = ["pid", "=", "0"];
        if ($name) {
            $where[] = ["name", "like", "%" . $name . "%"];
        }
        $count = Dict::where($where)->count();
        $list = Dict::where($where)->limit($offset, $limit)->order("id " . $order)->select()->toArray();
        foreach ($list as &$item) {
            $items = Dict::where("pid", "=", $item["id"])->column("name");
            $item["detail"] = implode(",", $items);
        }
        return ["total" => $count, "rows" => $list];
    }

    /**
     * 
     * @param type $code
     * @return type
     */
    public static function selectByParentCode($code) {
        $parent = Dict::where("code", $code)->findOrEmpty()->toArray();
        $dictList = Dict::where("pid", $parent["id"])->select()->toArray();
        $tmp = [];
        foreach ($dictList as $dict) {
            $tmp[$dict["code"]] = $dict["name"];
        }
        return $tmp;
    }

    /**
     * 
     * @param type $code
     * @return type
     */
    public static function findChildDictByCode($code) {
        $parent = Dict::where("code", $code)->findOrEmpty()->toArray();
        $dictList = Dict::where("pid", $parent["id"])->select()->toArray();
        return $dictList;
    }

    public static function findDictByCode($code) {
        $dict = Dict::where("code", $code)->findOrEmpty();
        return $dict;
    }

    public static function getIntegralFields() {
        return [
            "name" => "姓名",
            "card_type" => "证件类型",
            "card_number" => "证件号码",
            "phone" => "手机号码",
            "email" => "电子邮箱"
        ];
    }

    public static function getTalentFields($step, $isImport = 0) {
        $dict1 = [
            "talent_type" => "人才类型",
            "tax_insurance_month" => "在我市缴交社会保险或个人所得税月份",
            "labor_contract_rangetime" => "劳动合同起止时间",
            "salary_pay_way" => "工资发放渠道",
            "salary_pay_month" => "工资发放月份",
            "pre_import_type" => "预引进类型",
            "return" => "流出晋江满3年后又返回晋江的",
            "name" => "姓名",
            "card_type" => "证件类型",
            "card_number" => "证件号码",
            "sex" => "性别",
            "birthday" => "出生日期",
            "nationality" => "国籍/地区",
            "province" => "籍贯省",
            "city" => "籍贯市",
            "county" => "籍贯县",
            "nation" => "民族",
            "politics" => "政治面貌",
            "fst_work_time" => "来晋工作时间",
            "experience" => "工作简历",
            "education" => "教育背景",
        ];
        $dict2 = [
            "talent_arrange" => "人才层次",
            "talent_arrange_category" => "人才条款",
            "talent_condition" => "认定条件",
            "highest_degree" => "最高学历",
            "graduate_school" => "毕业院校",
            "major" => "专业",
            "professional" => "专业技术职称",
            "pro_qua" => "职业资格",
            "bank" => "开户银行",
            "bank_number" => "银行行号",
            "bank_branch_name" => "开户银行网点",
            "bank_account" => "银行账号",
            "study_abroad" => "是否有留学经历",
            "abroad_school" => "留学毕业院校",
            "abroad_major" => "留学专业",
            "phone" => "手机号码",
            "email" => "电子邮箱",
            "apply_year" => "申报年度",
            "import_way" => "引进方式",
            "cur_entry_time" => "本单位入职时间",
            "position" => "本单位现任职务",
            "source" => "申报来源",
            "source_city" => "入选来源地级市",
            "source_county" => "入选来源县市区",
            //"source_batch" => "入选名单的文件号及批次",
            //"fujian_highcert_pubtime" => "福建省高层次人才证书发证日期",
            //"fujian_highcert_exptime" => "福建省高层次人才证书有效期",
            //"quanzhou_highcert_pubtime" => "泉州高层次人才证书发证日期",
            //"quanzhou_highcert_exptime" => "泉州高层次人才证书有效期",
            "annual_salary" => "上一年度年薪（元）"
        ];
        if ($isImport) {
            $dict2 = array_merge(["isMatchZhiren" => "是否符合直认条件"], $dict2);
        }
        if ($step == 1) {
            return $dict1;
        }
        if ($step == 2) {
            return $dict2;
        }
        if ($step == 3) {
            return null;
        }
        return array_merge($dict1, $dict2);
    }

    public static function getTalentFields_IC() {
        $dict = [
            "name" => "姓名",
            "card_type" => "证件类型",
            "card_number" => "证件号码",
            "sex" => "性别",
            "birthday" => "出生日期",
            "nationality" => "国籍/地区",
            "province" => "籍贯省",
            "city" => "籍贯市",
            "county" => "籍贯县",
            "nation" => "民族",
            "politics" => "政治面貌",
            "break_faith" => "是否曾被相关主管部门列为失信个人",
            "phone" => "手机号码",
            "email" => "电子邮箱",
            "highest_degree" => "最高学历",
            "graduate_school" => "毕业院校",
            "major" => "专业",
            "study_abroad" => "是否有留学经历",
            "position" => "职务",
            "cur_entry_time" => "入职时间",
            "labor_contract_rangetime" => "工作合同起止时间",
            "talent_arrange" => "人才层次",
            "talent_condition" => "认定条件",
            "identifyConditionName" => "认定条件名称",
            "identifyGetTime" => "认定条件证书取得时间",
            "title" => "职称",
            "pro_qua" => "国家职业资格",
            "bank" => "开户银行",
            "bank_branch_name" => "开户银行网点",
            "bank_account" => "银行账号",
            "education" => "教育背景及工作简历",
            "experience" => "主要业绩及取得的荣誉",
            "description" => "备注"
        ];
        return $dict;
    }

    // 1保存未提交 2已提交未审核 3已审核 4驳回 5保存补充材料未提交 6提交补充材料进入初审 7初审通过 8初审驳回 9部门审核通过 10部门审核驳回 11复核通过 12复核驳回 13复核失败
    public static function getCheckLogStepName($state, $step) {
        $stepName = "";
        switch ($state) {
            case -1:
            case 3:
            case 4:
                $stepName = '<span class="label label-primary">基础信息审核</span>';
                break;
            case 5:
            case 6:
            case 7:
                $stepName = '<span class="label label-primary">基础信息复审</span>';
                break;
            case -2:
            case 10:
            case 11:
                if ($step == 3) {
                    $stepName = '<span class="label label-primary">部门审核</span>';
                } else {
                    $stepName = '<span class="label label-primary">初级审核</span>';
                }
                break;
            case 12:
            case 13:
                $stepName = '<span class="label label-primary">部门审核</span>';
                break;
            case 14:
            case 15:
                $stepName = '<span class="label label-info">复核</span>';
                break;
            case 16:
                $stepName = '<span class="label label-danger">审核不通过</span>';
                break;
            case 21:
            case 22:
                $stepName = '<span class="label label-info">核查征信</span>';
                break;
            case 23:
            case 24:
            case 25:
                $stepName = '<span class="label label-info">公示</span>';
                break;
            case 26:
            case 27:
                $stepName = '<span class="label label-info">公布</span>';
                break;
            case 28:
                $stepName = '<span class="label label-inverse">发放人才码</span>';
                break;
            default:
                $stepName = '<span class="label">用户操作</span>';
                break;
        }
        return $stepName;
    }

    public static function getTalentInfoStateName($state, $step = 0, $last_state = 0) {
        $str = "";
        switch ($state) {
            case -2:
                $str = '<span class="label label-danger">初审失败</span>';
                break;
            case -1:
                $str = '<span class="label label-danger">基础信息审核失败</span>';
                break;
            case 1:
                if ($last_state > $state) {
                    $str = '<span class="label label-danger">基础信息审核驳回</span>';
                } else {
                    $str = '<span class="label">待提交基础信息</span>';
                }
                break;
            case 2:
                /* if ($last_state > $state) {
                  $str = '<span class="label label-success">待重审</span>';
                  } else */
                if ($last_state == 3) {
                    $str = '<span class="label label-danger">基础信息复审驳回</span>';
                } else {
                    $str = '<span class="label label-success">待审核</span>';
                }
                break;
            case 3:
                $str = '<span class="label label-primary">基础信息审核通过</span>';
                break;
            case 4:
                $str = '<span class="label">再提交基础信息</span>';
                break;
            case 5:
                $str = '<span class="label label-primary">基础信息复审通过</span>';
                break;
            case 6:
                $str = '<span class="label label-danger">基础信息复审驳回</span>';
                break;
            case 7:
                $str = '<span class="label label-danger">基础信息复审失败</span>';
                break;
            case 8:
                if ($last_state > $state) {
                    $str = '<span class="label label-danger">初审驳回</span>';
                } else {
                    $str = '<span class="label">待提交认定材料</span>';
                }
                break;
            case 9:
                if ($step == 3) {
                    $str = '<span class="label label-danger">部门驳回</span>';
                } else {
                    if ($last_state == TalentState::FST_VERIFY_REJECT) {
                        $str = '<span class="label label-success">待重审</span>';
                    } else if ($last_state == TalentState::DEPT_VERIFY_REJECT) {
                        $str = '<span class="label label-danger">部门驳回</span>';
                    } else if ($last_state == TalentState::REVERIFY_REJECT) {
                        $str = '<span class="label label-danger">复核驳回</span>';
                    } else {
                        $str = '<span class="label label-success">待初审</span>';
                    }
                }
                break;
            case 10:
                if ($step == 3) {
                    $str = '<span class="label label-success">待部门审核</span>';
                } else {
                    $str = '<span class="label label-success">待复核</span>';
                }
                break;
            case 11:
                $str = '<span class="label">再提交认定材料</span>';
                break;
            case 12:
                if ($step == 3) {
                    $str = '<span class="label label-primary">部门通过</span>';
                } else {
                    $str = '<span class="label label-success">待复核</span>';
                }
                break;
            case 13:
                if ($step == 3) {
                    $str = '<span class="label label-danger">部门驳回</span>';
                } else {
                    $str = '<span class="label label-success">待初审</span>';
                }
                break;
            case 14:
                $str = '<span class="label label-success">待核查征信</span>';
                break;
            case 15:
                $str = '<span class="label label-danger">待重新审核</span>';
                break;
            case 16:
                $str = '<span class="label label-danger">复核不通过</span>';
                break;
            case 22:
                $str = '<span class="label label-danger">征信不通过</span>';
                break;
            case 25:
                $str = '<span class="label label-danger">公示审核不通过</span>';
                break;
            case 27:
                $str = '<span class="label label-danger">公布不通过</span>';
                break;
            case 21:
                $str = '<span class="label label-success">待公示</span>';
                break;
            case 23:
                $str = '<span class="label label-success">待公示审核</span>';
                break;
            case 24:
                $str = '<span class="label label-success">待公布</span>';
                break;
            case 26:
                $str = '<span class="label label-success">待发放人才码</span>';
                break;
            case 28:
                $str = '<span class="label label-primary">已发放人才码</span>';
                break;
        }
        return $str;
    }

    public static function getOldStateNameByState($state) {
        switch ($state) {
            case -2:
                return "<span class='label label-success'>待提交</span>";
            case -1:
                return "<span class='label label-success'>审核不通过</span>";
            case 1:
                return "<span class='label label-success'>待审核</span>";
            case 2:
                return "<span class='label label-danger'>审核驳回</span>";
            case 3:
                return "<span class='label label-primary'>审核通过</span>";
            case 4:
                return "<span class='label label-warm'>上级驳回</span>";
            case 10:
                return "<span class='label label-default'>冻结</span>";
            case 8:
                return "<span class='label label-primary'>提交审核</span>";
            case 11:
                return "<span class='label label-primary'>撤销审核</span>";
            case 9:
                return "<span class='label label-primary'>重新提交</span>";
            default:
                return "";
        }
    }

    public static function getOldStepNameByStep($step) {
        switch ($step) {
            case -1:
                return "<span class='label label-primary'>部门初审</span>";
            case 0:
                return "<span class='label'>用户操作</span>";
            case 1:
                return "<span class='label label-primary'>初级审核</span>";
            case 2:
                return "<span class='label label-success'>部门审核</span>";
            case 3:
                return "<span class='label label-danger'>复核</span>";
            case 4:
                return "<span class='label label-warning'>核查征信</span>";
            case 5:
                return "<span class='label label-warning'>公示</span>";
            case 6:
                return "<span class='label label-success'>公示再审核</span>";
            case 7:
                return "<span class='label label-warning'>公布</span>";
            case 8:
                return "<span class='label label-info'>发证</span>";
            case 11:
                return "<span class='label label-danger'>审核</span>";
            case 12:
                return "<span class='label label-primary'>撤回审核</span>";
            case 20:
                return "<span class='label label-success'>人才层次变更</span>";
            case 21:
                return "<span class='label label-success'>工作单位变更</span>";
            case 22:
                return "<span class='label label-success'>离职变更</span>";
            case 23:
                return "<span class='label label-success'>银行账号变更</span>";
            case 24:
                return "<span class='label label-success'>基础信息变更</span>";
            case 50:
                return "<span class='label label-primary'>兑现</span>";
            case 55:
                return "<span class='label label-primary'>撤销公布</span>";
            case 60:
                return "<span class='label label-primary'>取消优秀人才</span>";
            case 65:
                return "<span class='label label-primary'>恢复优秀人才</span>";
            case 70:
                return "<span class='label label-danger'>设置审核不通过</span>";
            default:
                return "";
        }
    }

    public static function findByParentCodeAndCode($parentCode, $code) {
        $parent_info = Dict::where('code', $parentCode)->findOrEmpty();
        if ($parent_info) {
            return Dict::where('pid', $parent_info['id'])->where('code', $code)->findOrEmpty();
        }
        return false;
    }

    public static function getLayerCatsByLayer($lv) {
        $kv = [1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E", 6 => "F", 7 => "G"];
        $chr = $kv[$lv];
        if ($chr) {
            $parent_info = Dict::where('code', "talent_condition_cats")->findOrEmpty();
            $where[] = ["pid", "=", $parent_info["id"]];
            if ($parent_info) {
                $where[] = ["code", "like", $chr . "%"];
                return Dict::where($where)->select()->toArray();
            }
        }
        return false;
    }

}
