<?php

namespace app\enterprise\api;

use app\common\model\LivingAllowance as laModel;
use app\common\api\DictApi;
use app\common\state\LivingAllowanceState;

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

    /**
     * 判断是否可以编辑
     * @param type $id
     * @return boolean
     */
    public static function checkIsEditable($id) {
        $info = self::getOne($id);
        if (!$info || !in_array($info["checkState"], [0, LivingAllowanceState::FST_SAVE, LivingAllowanceState::BASE_REVERIFY_PASS, LivingAllowanceState::SCND_SAVE]))
            return false;
        return true;
    }

    static public function chkIsOwner($id, $uid) {
        $info = self::getOne($id);
        if ($info["enterprise_id"] != $uid)
            return null;
        return $info;
    }

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

    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("name"));
        $idCard = trim($request->param("idCard"));
        $type = session("user")["type"];
        $where = [];
        if (session("user")["usertype"] == 2) {
            $where[] = ["enterpriseId", "=", session("user")["uid"]];
        }
        //$where[] = ["isImport", "=", $request->param("import") ?: 0];
        if ($name) {
            $where[] = ["name", "like", "%" . $name . "%"];
        }
        if ($idCard) {
            $where[] = ["idCard", "like", "%" . $idCard . "%"];
        }
        $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"); //最高学历
        $enterprise = \app\common\model\Enterprise::find(session("user")["uid"]);
        foreach ($list as $key => $item) {
            $list[$key]["declareTypeName"] = $masterTypes[$item["declareType"]];
            $list[$key]["highEducation"] = $degrees[$item["highEducation"]];
            $list[$key]["enterpriseName"] = $enterprise["name"];
        }
        return ["total" => $count, "rows" => $list];
    }

}
