<?php

namespace app\common\api;

use app\common\model\LivingAllowance as LaModel;
use app\common\state\LivingAllowanceState as LaState;
use think\facade\Db;
use app\common\state\CommonConst;

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

    public static function getList($params) {
        $order = trim($params["order"]) ?: "desc";
        $offset = trim($params["offset"]) ?: 0;
        $limit = trim($params["limit"]) ?: 10;
        $where = [];
        if ($_where = self::setLivingAllowanceCheckStateCondition($params)) {
            $where = array_merge($where, $_where);
        }
        if ($_where = self::getWhereByParams($params)) {
            $where = array_merge($where, $_where);
        }
        $type = session("user")["type"];
        if ($type == CommonConst::ENTERPRISE_NORMAL) {
            $where[] = ["type", "in", [CommonConst::ENTERPRISE_NORMAL, CommonConst::ENTERPRISE_WJ, CommonConst::ENTERPRISE_GJ]];
        } else if ($type == CommonConst::ENTERPRISE_JC) {
            $where[] = ["type", "=", $type];
        } else {
            $where[] = ["type", "=", "you have no power"];
        }
        $count = laModel::where($where)->count();
        $list = laModel::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        $masterTypes = DictApi::selectByParentCode("un_master_education"); //申报对象类型
        $degrees = DictApi::selectByParentCode("highest_degree"); //最高学历
        foreach ($list as $key => $item) {
            $list[$key]["declareTypeName"] = $masterTypes[$item["declareType"]];
            $list[$key]["highEducation"] = $degrees[$item["highEducation"]];
        }
        return ["total" => $count, "rows" => $list];
    }

    public static function setLivingAllowanceCheckStateCondition($params) {
        $where = [];
        $checkState = $params["checkState"];
        $process = $params["process"];
        if ($checkState) {
            switch ($process) {
                case -1:
                    if ($checkState == 7) {
                        $where[] = ["checkState", ">=", $checkState];
                    } else {
                        $where[] = ["checkState", "=", $checkState];
                    }
                    break;
                case 1:
                    if ($checkState == LaState::LA_NEED_DEP_CHECK) {
                        $where[] = ["checkState", "in", [15, 25, 30, 35]];
                    } else if ($checkState == 5) {
                        $where[] = ["checkState", "=", LaState::LA_NEED_FIRST_CHECK];
                        $where[] = ["highProcess", ">=", 1];
                    } else if ($checkState == LaState::LA_NEED_FIRST_CHECK) {
                        $where[] = ["checkState", "=", LaState::LA_NEED_FIRST_CHECK];
                        $where[] = ["highProcess", "<", 1];
                    } else {
                        $where[] = ["checkState", "=", $checkState];
                    }
                    break;
                case 2:
                    if ($checkState == LaState::LA_BEFORE_REJECT) {
                        $where[] = ["checkState", "=", 15];
                        $where[] = ["highProcess", ">=", 2];
                    } else if ($checkState == LaState::LA_NEED_FIRST_CHECK) {
                        $where[] = ["checkState", "=", 15];
                        $where[] = ["highProcess", "<", 2];
                    } else if ($checkState == LaState::LA_FIRST_REJECT) {               //待复核                        
                        $where[] = ["checkState", "in", [1, 3, 5, 7, 10, 20]];
                    } else if ($checkState == LaState::LA_NEED_DEP_CHECK) {
                        $where[] = ["checkState", ">=", 25];
                    } else {
                        $where[] = ["checkState", "=", $checkState];
                    }
                    break;
                case 3:
                    if ($checkState == LaState::LA_THIRD_REJECT) {
                        $where [] = ["checkState", "in", [7, 10, 15, 20, 30]];
                    } else if ($checkState == LaState::LA_BEFORE_REJECT) {            //重新提交
                        $where[] = ["checkState", "=", LaState::LA_NEED_THIRD];
                        $where[] = ["highProcess", ">=", 3];
                    } else if ($checkState == LaState::LA_NEED_THIRD) {               //待复核
                        $where[] = ["checkState", "=", $checkState];
                        $where[] = ["highProcess", "<", 3];
                    } else {
                        $where[] = ["checkState", "=", $checkState];
                    }
                    break;
                case 4:
                    $where[] = ["checkState", "=", $checkState];
                    break;
                case 5:
                    break;
            }
        }
        return $where;
    }

    public static function getWhereByParams($params) {
        foreach ($params as &$param) {
            $param = trim($param);
        }unset($param);
        $where = [];
        if ($params["name"]) {
            $where[] = ["name", "like", "%" . $params["name"] . "%"];
        }
        if ($params["idCard"]) {
            $where[] = ["idCard", "like", "%" . $params["idCard"] . "%"];
        }
        if ($params["introductionMethod"]) {
            $where[] = ["introductionMethod", "=", $params["introductionMethod"]];
        }
        if ($params["sex"]) {
            $where[] = ["sex", "=", $params["sex"]];
        }
        if ($params["declareType"]) {
            $where[] = ["declareType", "=", $params["declareType"]];
        }
        if ($params["nation"]) {
            $where[] = ["nation", "=", $params["nation"]];
        }
        if ($params["nationality"]) {
            $where[] = ["nationality", "=", $params["nationality"]];
        }
        if ($params["provinceCode"]) {
            $where[] = ["provinceCode", "=", $params["provinceCode"]];
        }
        if ($params["politics"]) {
            $where[] = ["politics", "=", $params["politics"]];
        }
        if (session("user")["usertype"] == 2) {
            $where[] = ["enterpriseId", "=", session("user")["uid"]];
        } else {
            if ($params["enterpriseId"]) {
                $where[] = ["enterpriseId", "=", $params["enterpriseId"]];
            }
        }
        if ($params["industryFieldNew"]) {
            $where[] = ["industryFieldNew", "=", $params["industryFieldNew"]];
        }
        if ($params["industryField"]) {
            $where[] = ["industryField", "=", $params["industryField"]];
        }
        if ($params["introductionMode"]) {
            $where[] = ["introductionMode", "=", $params["introductionMode"]];
        }
        if ($params["highEducation"]) {
            $where[] = ["highEducation", "=", $params["highEducation"]];
        }
        if ($params["major"]) {
            $where[] = ["major", "like", "%" . $params["major"] . "%"];
        }
        if ($params["title"]) {
            $where[] = ["title", "like", "%" . $params["title"] . "%"];
        }
        if ($params["studyAbroad"]) {
            $where[] = ["studyAbroad", "=", $params["studyAbroad"]];
        }
        if ($params["phone"]) {
            $where[] = ["phone", "like", "%" . $params["phone"] . "%"];
        }
        if ($params["email"]) {
            $where[] = ["email", "like", "%" . $params["email"] . "%"];
        }
        if ($params["address"]) {
            $where[] = ["address", "=", $params["address"]];
        }
        if ($params["isPublic"]) {
            $where[] = ["isPublic", "=", $params["isPublic"]];
        }
        if ($params["year"]) {
            $where[] = ["year", "=", $params["year"]];
        }
        switch ($params["process"]) {
            case 1:
                $where[] = ["checkState", "not in", [3, 5]];
                break;
            case 2:
                $where[] = ["firstPassTime", "EXP", Db::raw("is not null")];
                break;
            case 3:
                $where[] = ["firstDepPassTime", "EXP", Db::raw("is not null")];
                break;
            case 4:
                $where[] = ["checkState", "in", [-1, 35]];
                break;
        }
        return $where;
    }

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

    public static function getApplyCountByIdCard($idCard) {
        $where = [];
        $where[] = ["idCard", "=", $idCard];
        $where[] = ["checkState", "<>", LaState::LA_NOTPASS];
        $list = LaModel::where($where)->distinct(true)->field("substr(year,1,4) as year")->select()->toArray();
        $years = array_column($list, "year");
        return $years;
    }

    public static function getPassYearsByIdCard($idCard) {
        $where = [];
        $where[] = ["idCard", "=", $idCard];
        $where[] = ["checkState", "=", LaState::LA_PASS];
        $list = LaModel::where($where)->distinct(true)->field("substr(year,1,4) as year")->select()->toArray();
        $passYears = array_column($list, "year");
        return $passYears;
    }

}
