<?php

namespace app\common\api;

use app\common\state\MainState;
use think\facade\Db;
use app\common\model\HousePurchase as houseModel;
use app\common\model\HousePurchaseChildren as houseChildModel;
use app\common\model\HousePurchaseHouseInfo as houseInfoModel;
use app\common\model\HousePurchaseEnjoyOther as enjoyOtherModel;
use app\common\model\HousePurchaseOtherHouse as otherHouseModel;
use app\common\model\HousePurchaseSpouse as spouseModel;
use app\common\state\HouseStateEnum;

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

    public static function getList($where = [], $field = "*") {
        return houseModel::where($where)->field($field)->select()->order("createTime desc")->toArray();
    }

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

    public static function getFstInfoById($idCard) {
        $where = [];
        $where[] = ["declareType", "=", 1];
        $where[] = ["publicState", "=", 5];
        $where[] = ["checkState", "=", HouseStateEnum::REVIEW_PASS];
        $where[] = ["cashType", "=", 1];
        $where[] = ["idCard", "=", $idCard];
        return houseModel::where($where)->order("firstSubmitTime asc")->find();
    }

    public static function getEnjoyTimesByIdCard($idCard) {
        $where = [];
        $where[] = ["declareType", "=", 1];
        $where[] = ["publicState", "=", 5];
        $where[] = ["cashType", "=", 1];
        $where[] = ["cashIdCards", "like", "%" . $idCard . "%"];
        $count = houseModel::where($where)->order("year desc")->count();
        return $count;
    }

    public static function getHouseInfo($idCard) {
        if (\StrUtil::isEmpOrNull($idCard)) {
            return null;
        }
        $where = [];
        $where[] = ["idCard", "=", $idCard];
        return houseInfoModel::where($where)->find();
    }

    public static function getSpouse($id) {
        $where = [];
        $where[] = ["pId", "=", $id];
        return spouseModel::where($where)->select()->toArray();
    }

    public static function getChildren($id) {
        $where = [];
        $where[] = ["pId", "=", $id];
        return houseChildModel::where($where)->select()->toArray();
    }

    public static function getChildrenById($id) {
        return houseChildModel::where($where)->find($id);
    }

    public static function getHistoryEnjoyOtherList($id, $idCard, $spouseIdcard = null) {
        $where = [];
        $idCards = [];
        $idCards[] = $idCard;
        if (\StrUtil::isNotEmpAndNull($spouseIdcard)) {
            $idCards[] = $spouseIdcard;
        }
        $where[] = ["cashType", "=", 1];
        $where[] = ["checkState", "=", HouseStateEnum::REVIEW_PASS];
        $where[] = ["publicState", ">=", 4];
        $where[] = ["id", "<>", $id];
        $whereRaw = sprintf("idCard in ('%s') or spouseIdcard in ('%s')", implode("','", $idCards), implode("','", $idCards));
        $ids = houseModel::field("id")->where($where)->whereRaw($whereRaw)->column("id");
        $historyOtherList = null;
        if ($ids) {
            $where = [];
            $where[] = ["pId", "in", $ids];
            $historyOtherList = enjoyOtherModel::where($where)->select()->toArray();
        }
        return $historyOtherList;
    }

    public static function getEnjoyOtherList($id) {
        $where = [];
        $where[] = ["pId", "=", $id];
        return enjoyOtherModel::where($where)->select()->toArray();
    }

    public static function getOtherHouseList($id, $type = 1) {
        $where = [];
        $where[] = ["pId", "=", $id];
        $where[] = ["type", "=", $type];
        return otherHouseModel::where($where)->select()->toArray();
    }

    /**
     * 部门审核列表
     * @param type $query
     * @param type $company
     * @param type $type
     * @param type $offset
     * @param type $limit
     */
    public static function selectForHousePurchase($query, $company, $type, $offset, $limit) {
        $where = [];
        if ($company["name"] != "super" && $company["code"] != "rsj") {
            $where[] = ["t1.companyId", "=", $company["id"]];
        }
        if (($company["name"] == "super" || $company["code"] == "rsj") && \StrUtil::isNotEmpAndNull($query["companyName"])) {
            $where[] = ["t3.name", "like", "%" . $query["companyName"] . "%"];
        }
        if (\StrUtil::isEmpOrNull($query["year"])) {
            $where[] = ["t2.year", "=", $query["year"]];
        }
        if (\StrUtil::isEmpOrNull($query["name"])) {
            $where[] = ["t2.name", "like", "%" . $query["name"] . "%"];
        }
        if (\StrUtil::isEmpOrNull($query["idCard"])) {
            $where[] = ["t2.idCard", "like", "%" . $query["idCard"] . "%"];
        }
        if (\StrUtil::isEmpOrNull($query["spouseName"])) {
            $where[] = ["t2.spouseName", "like", "%" . $query["spouseName"] . "%"];
        }
        if (\StrUtil::isEmpOrNull($query["spouseIdcard"])) {
            $where[] = ["t2.spouseIdcard", "like", "%" . $query["spouseIdcard"] . "%"];
        }
        if (\StrUtil::isEmpOrNull($query["childName"])) {
            $where[] = ["t2.childName", "like", "%" . $query["childName"] . "%"];
        }
        if (\StrUtil::isEmpOrNull($query["childIdCard"])) {
            $where[] = ["t2.childIdCard", "like", "%" . $query["childIdCard"] . "%"];
        }
        if (\StrUtil::isEmpOrNull($query["talentArrange"])) {
            $where[] = ["t2.talentArrange", "=", $query["talentArrange"]];
        }
        if (\StrUtil::isEmpOrNull($query["marryStatus"])) {
            $where[] = ["t2.marryStatus", "=", $query["marryStatus"]];
        }
        if (\StrUtil::isEmpOrNull($query["state"])) {
            $where[] = ["t1.state", "=", $query["state"]];
        }
        if (\StrUtil::isEmpOrNull($query["isConflict"])) {
            $where[] = ["t2.isConflict", "=", $query["isConflict"]];
        }
        if (\StrUtil::isEmpOrNull($query["isRecover"])) {
            $where[] = ["t2.isRecover", "=", $query["isRecover"]];
        }
        $where[] = ["t1.type", "=", $type];
        $where[] = ["t2.type", "=", $query["type"]];
        $count = Db::table("un_talent_depcheckstate")->alias("t1")
                ->leftJoin("un_housepurchase t2", "t2.id=t1.mainId")
                ->leftJoin("sys_company t3", "t3.id=t1.companyId")
                ->where($where)
                ->count();

        $list = Db::table("un_talent_depcheckstate")->alias("t1")
                        ->field("t2.*,t1.companyId,t1.state,t3.`name` AS companyName,t5.description AS checkMsg,")
                        ->leftJoin("un_housepurchase t2", "t2.id=t1.mainId")
                        ->leftJoin("sys_company t3", "t3.id=t1.companyId")
                        ->leftJoin("(SELECT t4.mainId,t4.createTime,t4.companyId,t4.description FROM (SELECT * FROM un_talent_checklog WHERE type = #{type} AND step = 2 AND state != 8 ORDER BY createTime DESC LIMIT 100000) t4 GROUP BY t4.mainId,t4.companyId ORDER BY t4.createTime DESC) t5", "t1.mainId = t5.mainId AND t1.companyId = t5.companyId")
                        ->where($where)
                        ->limit($offset, $limit)
                        ->order("t1.createTime DESC")
                        ->select()->toArray();

        return ["total" => $count, "rows" => $list];
    }

    public static function deleteById($id) {
        $data["id"] = $id;
        $data["delete"] = 1;
        $data["deleteUser"] = session("user")["uid"];
        $data["deleteTime"] = date("Y-m-d H:i:s");
        if (houseModel::update($data)) {
            enjoyOtherModel::where("pId", $id)->delete();
            return true;
        }
        return false;
    }

    public static function deleteChildrenById($id) {
        return houseChildModel::where("id", $id)->delete();
    }

}
