<?php

namespace app\common\api;

use app\common\model\EducationSchool as EduModel;
use app\common\state\MainState;
use think\facade\Db;

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

    public static function getList($params) {
        $user = session("user");
        $order = trim($params["order"]) ?: "desc";
        $offset = trim($params["offset"]) ?: 0;
        $limit = trim($params["limit"]) ?: 10;
        $where = [];
        if ($user["usertype"] == 3) {
            $where[] = ["personId", "=", $user["uid"]];
        } else if ($user["usertype"] == 1) {
            //$where[] = ["type", "=", $user["type"]];
        } else {
            return [];
        }
        if ($_where = self::getWhereByParams($params)) {
            $where = array_merge($where, $_where);
        }
        $count = EduModel::where($where)->count();
        $list = EduModel::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        $levelMap = DictApi::selectByParentCode("talent_arrange");
        $streetMap = DictApi::selectByParentCode("street");
        $relationMap = DictApi::selectByParentCode("education_relation");
        $gradeMap = DictApi::selectByParentCode("education_grade");
        $educationProjectMap = DictApi::selectByParentCode("education_project");
        $schoolMap = DictApi::selectByParentCode("education_school_pool");
        foreach ($list as $key => $item) {
            $list[$key]["talentArrangeName"] = $levelMap[$item["talentArrange"]];
            $list[$key]["houseStreetName"] = $streetMap[$item["houseStreet"]];
            $list[$key]["companyStreetName"] = $streetMap[$item["companyStreet"]];
            $list[$key]["cRelationName"] = $relationMap[$item["cRelation"]];
            $list[$key]["nowGradeName"] = $gradeMap[$item["nowGrade"]];
            $list[$key]["projectName"] = $educationProjectMap[$item["project"]];
            $list[$key]["applySchoolName"] = $schoolMap[$item["applySchool"]];
        }
        return ["total" => $count, "rows" => $list];
    }

    public static function getWhereByParams($params) {
        foreach ($params as $key => $param) {
            $params[$key] = trim(\StrUtil::getRequestDecodeParam($params, $key));
        }unset($param);
        $where = [];
        if ($params["year"]) {
            $where[] = ["year", "=", $params["year"]];
        }
        if ($params["enterpriseName"]) {
            $where[] = ["enterpriseName", "like", "%" . $params["enterpriseName"] . "%"];
        }
        if ($params["pName"]) {
            $where[] = ["pName", "like", "%" . $params["pName"] . "%"];
        }
        if ($params["pSex"]) {
            $where[] = ["pSex", "=", $params["pSex"]];
        }
        if ($params["pIdcard"]) {
            $where[] = ["pIdcard", "like", "%" . $params["pIdcard"] . "%"];
        }
        if ($params["talentArrange"]) {
            $where[] = ["talentArrange", "=", $params["talentArrange"]];
        }
        if ($params["certificateNo"]) {
            $where[] = ["certificateNo", "like", "%" . $params["certificateNo"] . "%"];
        }
        if ($params["address"]) {
            $where[] = ["address", "like", "%" . $params["address"] . "%"];
        }
        if ($params["phone"]) {
            $where[] = ["phone", "like", "%" . $params["phone"] . "%"];
        }
        if ($params["project"]) {
            $where[] = ["project", "=", $params["project"]];
        }
        if ($params["cName"]) {
            $where[] = ["cName", "like", "%" . $params["cName"] . "%"];
        }
        if ($params["cSex"]) {
            $where[] = ["cSex", "=", $params["cSex"]];
        }
        if ($params["cIdcard"]) {
            $where[] = ["cIdcard", "like", "%" . $params["cIdcard"] . "%"];
        }
        if ($params["cRelation"]) {
            $where[] = ["cRelation", "=", $params["cRelation"]];
        }
        if ($params["nowSchool"]) {
            $where[] = ["nowSchool", "like", "%" . $params["nowSchool"] . "%"];
        }
        if ($params["nowGrade"]) {
            $where[] = ["nowGrade", "=", $params["nowGrade"]];
        }
        if ($params["applySchool"]) {
            $where[] = ["applySchool", "=", $params["applySchool"]];
        }
        if ($params["companyStreet"]) {
            $where[] = ["companyStreet", "=", $params["companyStreet"]];
        }
        if ($params["houseStreet"]) {
            $where[] = ["houseStreet", "=", $params["houseStreet"]];
        }
        if ($params["checkState"]) {
            $where[] = ["checkState", "=", $params["checkState"]];
        }
        return $where;
    }

    public static function getInfoById($id) {
        return EduModel::findOrEmpty($id)->toArray();
    }

}
