<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chinaweal.aiccs.crgs.movein.mapper.TbZcEntityMoveinMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.crgs.movein.entity.TbZcEntityMovein">
        <id column="MIID" property="miid" />
        <result column="PRIPID" property="pripid" />
        <result column="MINLETNUM" property="minletnum" />
        <result column="MINREA" property="minrea" />
        <result column="MINDATE" property="mindate" />
        <result column="MOUTAREA" property="moutarea" />
        <result column="MOUTAREREGORG" property="moutareregorg" />
        <result column="MINSIGN" property="minsign" />
        <result column="SCBZ" property="scbz" />
        <result column="ZHXGSJ" property="zhxgsj" />
    </resultMap>

    <resultMap id="InfoCrgsDto" type="com.chinaweal.aiccs.crgs.movein.entity.dto.InfoCrgsDto">
        <id column="pripid" property="pripid" />
        <result column="entName" property="entName" />
        <result column="uniscid" property="uniscid" />
        <result column="regNo" property="regNo" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        MIID, PRIPID, MINLETNUM, MINREA, MINDATE, MOUTAREA, MOUTAREREGORG, MINSIGN, SCBZ, ZHXGSJ
    </sql>
    <select id="searchPageWithBaseInfo"
            resultType="com.chinaweal.aiccs.crgs.movein.entity.dto.MoveInEntityDto">
        select m.PRIPID pripid, m.ENTNAME entName, m.UNISCID uniscid, m.regNo,
        m.regOrg, m.regOrgCn, m.moveInDate, m.syncDate, m.miid
        from(
        select e.PRIPID pripid, e.ENTNAME entName, e.UNISCID uniscid, e.REGNO regNo,
        e.REGORG regOrg, e.REGORG_CN regOrgCn, mv.MINDATE moveInDate, mv.ZHXGSJ syncDate, mv.MIID miid
        from aiccs.e_baseinfo e left join tb_zc_entity_movein mv on e.PRIPID = mv.PRIPID
        where mv.MIID is not null and mv.minsign = '2'
        <if test="param != null">
            <if test="param.regOrg != null and param.regOrg != ''">
                and e.REGORG = #{param.regOrg}
            </if>
            <if test="param.uniscid != null and param.uniscid != ''">
                and e.UNISCID = #{param.uniscid}
            </if>
            <if test="param.regno != null and param.regno != ''">
                and e.UNISCID = #{param.regno}
            </if>
            <if test="param.entName != null and param.entName != ''">
                and e.ENTNAME like concat('%', #{param.entName}, '%')
            </if>
            <if test=" param.moveInDateOn != null and param.moveInDateEnd != '' ">
                AND mv.MINDATE between #{param.moveInDateOn} and #{param.moveInDateEnd}
            </if>
        </if>
        union
        select e.PRIPID pripid, e.ENTNAME entName, e.UNISCID uniscid, e.REGNO regNo,
        e.REGORG regOrg, e.REGORG_CN regOrgCn, mv.MINDATE moveInDate, mv.ZHXGSJ syncDate, mv.MIID miid
        from aiccs.e_baseinfo e left join tb_zc_entity_movein mv on e.PRIPID = mv.PRIPID
        where mv.MIID is not null and mv.minsign = '5'
        <if test="param != null">
            <if test="param.regOrg != null and param.regOrg != ''">
                and e.REGORG = #{param.regOrg}
            </if>
            <if test="param.uniscid != null and param.uniscid != ''">
                and e.UNISCID = #{param.uniscid}
            </if>
            <if test="param.regno != null and param.regno != ''">
                and e.UNISCID = #{param.regno}
            </if>
            <if test="param.entName != null and param.entName != ''">
                and e.ENTNAME like concat('%', #{param.entName}, '%')
            </if>
            <if test=" param.moveInDateOn != null and param.moveInDateEnd != '' ">
                AND mv.MINDATE between #{param.moveInDateOn} and #{param.moveInDateEnd}
            </if>
        </if>
        )m
        where 1=1
        <if test="param != null and param.orderType == 'asc'">
            ORDER BY m.moveInDate ASC
        </if>
        <if test="param != null and param.orderType == 'desc'">
            ORDER BY m.moveInDate DESC
        </if>
        <if test="param != null and param.orderType == null">
            ORDER BY m.moveInDate DESC
        </if>
    </select>

    <select id="queryAllInfo" resultMap="BaseResultMap">
        select * from crgs.tb_zc_entity_movein where minsign = '5' or minsign = '2'
    </select>

    <select id="queryAllByPage" resultMap="BaseResultMap">
        select * from crgs.tb_zc_entity_movein
        where (minsign = '5' or minsign = '2')
        <if test="beginId != null and beginId != ''">
            and MIID &gt; #{beginId}
        </if>
        order by MIID asc
        limit ${pageSize}
    </select>

    <select id="queryOrgNumber" resultType="String">
        select left(tog.orgNumber,6) from cxaicorg.t_users tu
 		left join cxaicorg.t_orgunits tog on tu.orgUnitID = tog.orgUnitID
 		where userID = #{userID}
    </select>
</mapper>
