<?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.testor.ddd.safetyControl.infrastructure.repository.space.dao.TSafeSpaceDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap"
               type="com.testor.ddd.safetyControl.infrastructure.repository.space.entity.TSafeSpaceDO">
        <id column="id" property="id"/>
        <id column="org_id" property="orgId"/>
        <id column="space_type_id" property="spaceTypeId"/>
        <id column="space_id" property="spaceId"/>
        <id column="name" property="name"/>
        <id column="code" property="code"/>
        <id column="level" property="level"/>
    </resultMap>

    <sql id="select_base_column">
        ss.id, ss.org_id orgId, ss.space_type_id spaceTypeId, ss.space_id spaceId, ss.name, ss.code, ss.level,
        ss.status, ss.remarks, ss.create_by createBy, ss.create_date createDate,
        ss.update_by as updateBy, ss.update_date updateDate
    </sql>

    <sql id="common_where_if">
        <if test="query.id!=null and query.id!=''">
            and ss.id=#{query.id}
        </if>
        <if test="query.orgId!=null and query.orgId!=''">
            and ss.org_id=#{query.orgId}
        </if>
        <if test="query.spaceTypeId!='' and query.spaceTypeId!=null">
            and ss.space_type_id=#{query.spaceTypeId}
        </if>
        <if test="query.spaceId!='' and query.spaceId!=null">
            and ss.space_id=#{query.spaceId}
        </if>
        <if test="query.name!='' and query.name!=null">
            and ss.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.code!='' and query.code!=null">
            and ss.code=#{query.code}
        </if>
        <if test="query.level!='' and query.level!=null">
            and ss.level=#{query.level}
        </if>
    </sql>

    <select id="listPageByWorkshop" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.space.TSafeSpaceVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.space.TSafeSpaceDTOParam">
        select py.name as spaceTypeName,
        so.org_name as orgName,
        <include refid="select_base_column" />
        from t_safe_space ss
        left join t_safe_space_type py on ss.space_type_id = py.id
        left join t_sys_org so on so.org_id = ss.org_id
        <where>
            ss.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.condOrgIdList!=null and query.condOrgIdList.size>0">
                and ss.org_id in
                <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condSpaceTypeIdList!=null and query.condSpaceTypeIdList.size>0">
                and ss.space_type_id in
                <foreach collection="query.condSpaceTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
<!--            <if test="query.condSpaceIdList!=null">-->
<!--                and ss.space_id in-->
<!--                <foreach collection="query.condSpaceIdList" index="index" item="item" open="(" separator="," close=")">-->
<!--                    #{item}-->
<!--                </foreach>-->
<!--            </if>-->
        </where>
        order by ss.space_type_id, ss.code asc
    </select>

    <select id="listPageByWorkArea" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.space.TSafeSpaceVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.space.TSafeSpaceDTOParam">
        select   py.name as spaceTypeName,
        pss.name as spaceName,
        pss.code as spaceCode,
        so.org_name as orgName,
        concat(py.name,'/',pss.name,'/',ss.name) as structureName,
        <include refid="select_base_column" />
        from t_safe_space ss
        left join t_safe_space pss on ss.space_id = pss.id
        left join t_safe_space_type py on ss.space_type_id = py.id
        left join t_sys_org so on so.org_id = ss.org_id
        <where>
            ss.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.condOrgIdList!=null and query.condOrgIdList.size>0">
                and ss.org_id in
                <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condSpaceTypeIdList!=null and query.condSpaceTypeIdList.size>0">
                and pss.space_type_id in
                <foreach collection="query.condSpaceTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condSpaceIdList!=null and query.condSpaceIdList.size>0">
                and ss.space_id in
                <foreach collection="query.condSpaceIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.spaceName!='' and query.spaceName!=null">
                and pss.name like concat('%',#{query.spaceName},'%')
            </if>
        </where>
        order by  createDate desc,py.create_date desc,pss.name desc
    </select>

    <select id="listByCode" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.space.TSafeSpaceVO">
        select   py.name as spaceTypeName,
        pss.name as spaceName,
        pss.code as spaceCode,
        so.org_name as orgName,
        <include refid="select_base_column" />
        from t_safe_space ss
        left join t_safe_space pss on ss.space_id = pss.id
        left join t_safe_space_type py on ss.space_type_id = py.id
        left join t_sys_org so on so.org_id = ss.org_id
       where
            ss.status = '0' and ss.code=#{spaceCode}
        order by ss.space_id, ss.code asc
    </select>
    <select id="getSpaceNameById" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.space.TSafeSpaceVO">
        select
        concat(pss.name,'/',ss.name) as structureName
        from t_safe_space ss
        left join t_safe_space pss on ss.space_id = pss.id
        where ss.id = #{spaceId}
    </select>
    <select id="listSafeSpaceAll"
            resultType="com.testor.ddd.safetyControl.interfaces.model.vo.space.TSafeSpaceVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.space.TSafeSpaceDTOParam">
        select py.name as spaceTypeName,
        pss.name as spaceName,
        pss.code as spaceCode,
        so.org_name as orgName,
        concat(py.name,'/',pss.name,'/',ss.name) as structureName,
        <include refid="select_base_column" />
        from t_safe_space ss
        left join t_safe_space pss on ss.space_id = pss.id
        left join t_safe_space_type py on ss.space_type_id = py.id
        left join t_sys_org so on so.org_id = ss.org_id
        <where>
            ss.status = '0'
            <include refid="common_where_if"></include>
        </where>
    </select>

    <update id="replaceCode">
        update t_safe_space
        set code = replace(code,#{oldCode}, #{newCode})
        where status = '0' and space_type_id = #{spaceTypeId}
    </update>
</mapper>