<?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.aiccs.market.mapper.AreaSliceMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.market.entity.AreaSlice">
        <id column="slice_id" property="sliceId" />
        <result column="org_number" property="orgNumber" />
        <result column="slice_name" property="sliceName" />
        <result column="slice_addr_key" property="sliceAddrKey" />
        <result column="slice_addr" property="sliceAddr" />
        <result column="supervisor_id" property="supervisorId" />
        <result column="supervisor_name" property="supervisorName" />
        <result column="sub_supervisor_id" property="subSupervisorId" />
        <result column="sub_supervisor_name" property="subSupervisorName" />
        <result column="remark" property="remark" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="creator_id" property="creatorId" />
        <result column="state" property="state" />
        <result column="is_deleted" property="isDeleted" />
        <result column="sub_use" property="subUse" />
        <result column="sub_start_time" property="subStartTime" />
        <result column="sub_end_time" property="subEndTime" />
        <result column="isInit" property="isInit" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        slice_id, org_number, slice_name, slice_addr_key, slice_addr, supervisor_id, sub_supervisor_id, remark, create_time, update_time, creator_id, state, is_deleted, sub_use, sub_start_time, sub_end_time, slice_type, p_slice_id
    </sql>
    <sql id="selectWithOrgQuery">
        select <include refid="Base_Column_List"/>, u1.realname as supervisor_name, u2.realname as sub_supervisor_name,
        if(exists(select 1 from area_slice_polygon p where p.slice_id = a.slice_id), '1', '0') isInit
        from aiccs.area_slice a left join cxaicorg.t_users u1 on a.supervisor_id = u1.userid
        left join cxaicorg.t_users u2 on a.sub_supervisor_id = u2.userid
    </sql>

    <select id="getListByOrg" resultType="com.chinaweal.aiccs.aiccs.dict.entity.CascadeTreeNode" >
        select slice_id as id, slice_name as label, slice_id as value
        from aiccs.area_slice where is_deleted = '0' and state = '1'
        <if test="org != null and org != ''">
            and org_number = #{org}
        </if>
        <if test="org == null or org == ''">
            and slice_type = '0'
        </if>
    </select>

    <select id="selectByParentId" resultType="com.chinaweal.aiccs.aiccs.dict.entity.CascadeTreeNode" >
        select slice_id as id, slice_name as label, slice_id as value
        from aiccs.area_slice where p_slice_id = #{pid}
    </select>

    <select id="selectIdsByParentId" resultType="java.lang.String" >
        select slice_id from aiccs.area_slice where p_slice_id = #{pid}
    </select>

    <select id="selectPageWithOrg" resultMap="BaseResultMap">
        <include refid="selectWithOrgQuery"/>
        <where>
            a.is_deleted = '0'
            <if test="params != null">
                <if test="params.sliceName != null and params.sliceName != ''">
                    and slice_name like concat('%',#{params.sliceName}, '%')
                </if>
                <if test="params.supervisorName != null and params.supervisorName != ''">
                    and u1.realname like concat('%',#{params.supervisorName}, '%')
                </if>
                <if test="params.orgNumber != null and params.orgNumber != ''">
                    and a.org_number = #{params.orgNumber}
                </if>
            </if>
        </where>
    </select>

</mapper>
