<?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));
        $id = Dict::table('sys_dict')->save($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 getTalentFields($step) {
        $dict1 = [
            "talent_type" => "人才类型",
            "tax_insurance_month" => "在我市缴交社会保险或个人所得税月份",
            "labor_contract_rangetime" => "劳动合同起止时间",
            "name" => "姓名",
            "card_type" => "证件类型",
            "card_number" => "证件号码",
            "sex" => "性别",
            "birthday" => "出生日期",
            "nationality" => "国籍/地区",
            "province" => "籍贯省",
            "city" => "籍贯市",
            "county" => "籍贯县",
            "nation" => "民族",
            "politics" => "政治面貌"
        ];
        $dict2 = [
            "talent_arrange" => "人才层次",
            "talent_condition" => "认定条件",
            "highest_degree" => "最高学历",
            "graduate_school" => "毕业院校",
            "major" => "专业",
            "professional" => "专业技术职称",
            "bank" => "开户银行",
            "bank_number" => "银行行号",
            "bank_branch_name" => "开户银行网点",
            "bank_account" => "银行账号",
            "study_abroad" => "是否有留学经历",
            "abroad_school" => "留学毕业院校",
            "abroad_major" => "留学专业",
            "phone" => "手机号码",
            "email" => "电子邮箱",
            "apply_year" => "申报年度",
            "import_way" => "引进方式",
            "fst_work_time" => "首次来晋工作时间",
            "cur_entry_time" => "本单位入职时间",
            "position" => "本单位现任职务",
            "source" => "申报来源",
            "source_city" => "入选来源地级市",
            "source_county" => "入选来源县市区",
            "source_batch" => "入选名单批次",
            "fujian_highcert_pubtime" => "福建省高层次人才证书发证日期",
            "fujian_highcert_exptime" => "福建省高层次人才证书有效期",
            "quanzhou_highcert_pubtime" => "泉州高层次人才证书发证日期",
            "quanzhou_highcert_exptime" => "泉州高层次人才证书有效期"
        ];
        if ($step == 1) {
            return $dict1;
        }
        if ($step == 2) {
            return $dict2;
        }
        return array_merge($dict1, $dict2);
    }
    
    // 1保存未提交 2已提交未审核 3已审核 4驳回 5保存补充材料未提交 6提交补充材料进入初审 7初审通过 8初审驳回 9部门审核通过 10部门审核驳回 11复核通过 12复核驳回 13复核失败
    public static function getTalentInfoStepByState($state) {
        $stepName = "";
        switch ($state) {
            case 3:
            case 4:
                $stepName = '<span class="label">"基本条件判定"</span>';
                break;
            case 7:
            case 8:
                $stepName = '<span class="label label-primary">初级审核</span>';
                break;
            case 9:
            case 10:
                $stepName = '<span class="label label-primary">部门审核</span>';
                break;
            case 11:
            case 12:
                $stepName = '<span class="label label-danger">复核</span>';
                break;
            case 13:
                $stepName = '<span class="label label-danger">审核不通过</span>';
                break;
            default:
                $stepName = '<span class="label">用户操作</span>';
                break;
        }
        return $stepName;
    }

    public static function getTalentInfoStateName($state) {
        $str = "";
        switch ($state) {
            case 1:
                $str = '<span class="label">待提交基本资料</span>';
                break;
            case 2:
                $str = '<span class="label label-success">待审核</span>';
                break;
            case 3:
                $str = '<span class="label">待提交认定资料</span>';
                break;
            case 4:
                $str = '<span class="label label-danger">初审驳回</span>';
                break;
            case 5:
                $str = '<span class="label">待提交</span>';
                break;
            case 6:
                $str = '<span class="label label-success">待初审</span>';
                break;
            case 7:
                $str = '<span class="label label-success">待复核</span>';
                break;
            case 8:
                $str = '<span class="label label-success">待初审</span>';
                break;
            case 9:
                $str = '<span class="label label-success">待复核</span>';
                break;
            case 10:
                $str = '<span class="label label-success">待初审</span>';
                break;
            case 11:
                $str = '<span class="label label-primary">复核通过</span>';
                break;
            case 12:
                $str = '<span class="label label-danger">待重新审核</span>';
                break;
            case 13:
                $str = '<span class="label label-danger">审核不通过</span>';
                break;
        }
        return $str;
    }

}
