<?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.equipment.dao.TSafeEquipmentDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap"
               type="com.testor.ddd.safetyControl.infrastructure.repository.equipment.entity.TSafeEquipmentDO">
        <id column="id" property="id"/>
        <id column="org_id" property="orgId"/>
        <id column="sys_type_id" property="sysTypeId"/>
        <id column="equipment_type_id" property="equipmentTypeId"/>
        <id column="name" property="name"/>
        <id column="code" property="code"/>
        <id column="specification" property="specification"/>
        <id column="space_id" property="spaceId"/>
        <id column="space_area_id" property="spaceAreaId"/>
        <id column="manufacturer" property="manufacturer"/>
        <id column="des" property="des"/>
        <id column="validity_date" property="validityDate"/>
        <id column="is_special" property="isSpecial"/>
        <id column="img_url" property="imgUrl"/>
    </resultMap>

    <sql id="select_base_column">
        se.id, se.org_id orgId, se.sys_type_id sysTypeId, se.equipment_type_id equipmentTypeId, se.name, se.code,
        se.specification, se.space_id spaceId, se.space_area_id spaceAreaId, se.manufacturer, se.des, se.validity_date validityDate, se.is_special isSpecial,se.img_url imgUrl,
        se.status, se.remarks, se.create_by createBy, se.create_date createDate,
        se.update_by as updateBy, se.update_date updateDate
    </sql>

    <sql id="common_where_if">
        <if test="query.id!=null and query.id!=''">
            and se.id=#{query.id}
        </if>
        <if test="query.orgId!=null and query.orgId!=''">
            and se.org_id=#{query.orgId}
        </if>
        <if test="query.sysTypeId!='' and query.sysTypeId!=null">
            and se.sys_type_id=#{query.sysTypeId}
        </if>
        <if test="query.equipmentTypeId!='' and query.equipmentTypeId!=null">
            and se.equipment_type_id=#{query.equipmentTypeId}
        </if>
        <if test="query.spaceAreaId!='' and query.spaceAreaId!=null">
            and se.space_area_id=#{query.spaceAreaId}
        </if>
        <if test="query.spaceId!='' and query.spaceId!=null">
            and se.space_id=#{query.spaceId}
        </if>
        <if test="query.name!='' and query.name!=null">
            and se.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.code!='' and query.code!=null">
            and se.code=#{query.code}
        </if>
        <if test="query.manufacturer!='' and query.manufacturer!=null">
            and se.manufacturer=#{query.manufacturer}
        </if>
        <if test="query.specification!='' and query.specification!=null">
            and se.specification=#{query.specification}
        </if>
        <if test="query.des!='' and query.des!=null">
            and se.des like concat('%',#{query.des},'%')
        </if>
        <if test="query.validityDate!='' and query.validityDate!=null">
            and se.validity_date=#{query.validityDate}
        </if>
        <if test="query.isSpecial!='' and query.isSpecial!=null">
            and se.is_special=#{query.isSpecial}
        </if>
    </sql>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.equipment.TSafeEquipmentVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.equipment.TSafeEquipmentDTOParam">
        select   st.name as sysTypeName,
        et.name as equipmentTypeName,
        so.org_name as orgName,
        <include refid="select_base_column" />
        from t_safe_equipment se
        left join t_safe_equipment_type st on st.id = se.sys_type_id
        left join t_safe_equipment_type et on se.equipment_type_id = et.id
        left join t_sys_org so on so.org_id = se.org_id
        <where>
            se.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.condOrgIdList!=null and query.condOrgIdList.size>0">
                and se.org_id in
                <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condSysTypeIdList!=null and query.condSysTypeIdList.size>0">
                and pss.sys_type_id in
                <foreach collection="query.condSysTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condEquipmentTypeIdList!=null and query.condEquipmentTypeIdList.size>0">
                and se.equipment_type_id in
                <foreach collection="query.condEquipmentTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.validityStartDate!=null">
                and se.validity_date >= #{query.validityStartDate}
            </if>
            <if test="query.validityEndDate!=null">
                and #{query.validityEndDate} >= se.validity_date
            </if>
        </where>
        <if test="query.orderBy!='' and query.orderBy!=null and query.orderType!='' and query.orderType!=null" >
            order by ${query.orderBy} ${query.orderType}
        </if>
    </select>

    <select id="findDetailById" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.equipment.TSafeEquipmentVO">
        select   st.name as sysTypeName,
        et.name as equipmentTypeName,
        so.org_name as orgName,
        ss.name as spaceAreaName,
        pss.name as spaceName,
        <include refid="select_base_column" />
        from t_safe_equipment se
        left join t_safe_equipment_type st on st.id = se.sys_type_id
        left join t_safe_equipment_type et on se.equipment_type_id = et.id
        left join t_sys_org so on so.org_id = se.org_id
        left join t_safe_space ss on ss.id = se.space_area_id
        left join t_safe_space pss on pss.id = se.space_id
        <where>
            se.status = '0' and se.id=#{id}
        </where>
    </select>

    <select id="listForRisk" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.equipment.TSafeEquipmentVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.equipment.TSafeEquipmentDTOParam">
        select   st.name as sysTypeName,
        et.name as equipmentTypeName,
        ss.name as spaceAreaName,
        pss.name as spaceName,
        ptss.name as spaceTypeName,
        ptss.id as spaceTypeId,
        <include refid="select_base_column" />
        from t_safe_equipment se
        left join t_safe_equipment_type st on st.id = se.sys_type_id
        left join t_safe_equipment_type et on se.equipment_type_id = et.id
        left join t_safe_space ss on ss.id = se.space_area_id
        left join t_safe_space pss on pss.id = se.space_id
        left join t_safe_space_type ptss on ptss.id = pss.space_type_id
        <where>
            se.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.condOrgIdList!=null and query.condOrgIdList.size>0">
                and se.org_id in
                <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condSysTypeIdList!=null and query.condSysTypeIdList.size>0">
                and pss.sys_type_id in
                <foreach collection="query.condSysTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condEquipmentTypeIdList!=null and query.condEquipmentTypeIdList.size>0">
                and se.equipment_type_id in
                <foreach collection="query.condEquipmentTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.validityStartDate!='' and query.validityStartDate!=null">
                and se.validity_date > #{query.validityStartDate}
            </if>
            <if test="query.validityEndDate!='' and query.validityEndDate!=null">
                and se.validity_date=#{query.validityEndDate}
            </if>
        </where>
    </select>

    <select id="countEquipment" resultType="java.lang.Integer"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.equipment.TSafeEquipmentDTO">
        select count(se.id)
        from t_safe_equipment se
        left join t_partner_type st on st.id = se.sys_type_id
        left join t_partner_type et on se.equipment_type_id = et.id
        left join t_sys_org so on so.org_id = se.org_id
        <where>
            se.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.condOrgIdList!=null and query.condOrgIdList.size>0">
                and se.org_id in
                <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condSysTypeIdList!=null and query.condSysTypeIdList.size>0">
                and pss.sys_type_id in
                <foreach collection="query.condSysTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.condEquipmentTypeIdList!=null and query.condEquipmentTypeIdList.size>0">
                and se.equipment_type_id in
                <foreach collection="query.condEquipmentTypeIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="query.validityStartDate!=null">
                and se.validity_date >= #{query.validityStartDate}
            </if>
            <if test="query.validityEndDate!=null">
                and  #{query.validityEndDate} >= se.validity_date
            </if>
        </where>
    </select>

    <update id="replaceCode">
        update t_safe_equipment
        set code = replace(code,#{oldCode}, #{newCode})
        where status = '0' and (sys_type_id = #{typeId}  or equipment_type_id=#{typeId})
    </update>
</mapper>