<?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.module.safeCheck.dao.TSafeHazardCheckPlanDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.safeCheck.model.domain.TSafeHazardCheckPlan">
        <id column="id" property="id"/>
        <id column="org_id" property="orgId"/>
        <id column="checked_org_id" property="checkedOrgId"/>
        <id column="name" property="name"/>
        <id column="code" property="code"/>
        <id column="start_date" property="startDate"/>
        <id column="end_date" property="endDate"/>
        <id column="in_weekend" property="inWeekend"/>
        <id column="in_holiday" property="inHoliday"/>
        <id column="advance_hour" property="advanceHour"/>
        <id column="checked_org_parent_ids" property="checkedOrgParentIds"/>
    </resultMap>

    <sql id="select_base_column">
        rs.id, rs.org_id orgId, rs.checked_org_id checkedOrgId, rs.name, rs.code,
       rs.start_date startDate, rs.end_date endDate, rs.in_weekend inWeekend, rs.in_holiday inHoliday, rs.advance_hour advanceHour,
        rs.checked_org_parent_ids checkedOrgParentIds,
        rs.status, rs.remarks, rs.create_by createBy, rs.create_date createDate,
        rs.update_by as updateBy, rs.update_date updateDate
    </sql>

    <sql id="common_where_if">
        <if test="query.id!=null and query.id!=''">
            and rs.id=#{query.id}
        </if>
        <if test="query.orgId!=null and query.orgId!=''">
            and rs.org_id=#{query.orgId}
        </if>
        <if test="query.checkedOrgId!='' and query.checkedOrgId!=null">
            and rs.checked_org_id=#{query.checkedOrgId}
        </if>
        <if test="query.name!='' and query.name!=null">
            and rs.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.code!='' and query.code!=null">
            and rs.code=#{query.code}
        </if>

        <if test="query.startDateStart!=null">
            and rs.start_date >= #{query.startDateStart}
        </if>
        <if test="query.startDateEnd!=null">
            and #{query.startDateEnd} >= rs.start_date
        </if>
    </sql>

    <select id="findById" resultType="com.testor.module.safeCheck.model.vo.SafeHazardCheckPlanDetailVO">
        select so.org_name as orgName,dpo.org_name as checkedOrgName,
        <include refid="select_base_column" />
        from t_safe_hazard_check_plan rs
        left join t_sys_org so on rs.org_id = so.org_id
        left join t_sys_org dpo on dpo.org_id = rs.checked_org_id
        <where>
            rs.status = '0'
            and rs.id = #{planId}
        </where>
    </select>


        <select id="listPage" resultType="com.testor.module.safeCheck.model.vo.TSafeHazardCheckPlanVO"
            parameterType="com.testor.module.safeCheck.model.dto.TSafeHazardCheckPlanParam">
        select
        rs.id, rs.org_id orgId, rs.checked_org_id checkedOrgId, rs.name, rs.code,
        rs.start_date startDate, rs.end_date endDate, rs.advance_hour advanceHour,
        so.org_name as orgName,dpo.org_name as checkedOrgName
        from t_safe_hazard_check_plan rs
        left join t_sys_org so on rs.org_id = so.org_id
        left join t_sys_org dpo on dpo.org_id = rs.checked_org_id
        left join t_safe_hazard_check_object hco on rs.id = hco.plan_id
        <where>
            rs.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.checkObjectType!='' and query.checkObjectType!=null">
                and hco.type=#{query.checkObjectType}
            </if>
            <if test="query.checkObjectId!='' and query.checkObjectId!=null">
                and hco.obj_id=#{query.checkObjectId}
            </if>
            <if test="query.checkObjectTypeId!='' and query.checkObjectTypeId!=null">
                and hco.obj_type_parent_ids like concat('%',#{query.checkObjectTypeId},'%')
            </if>
            <if test="query.standardId!='' and query.standardId!=null">
                and hco.standard_id=#{query.standardId}
            </if>
                <if test='query.planStatus == 0'>
                    and  rs.start_date > now()
                </if>
                <if test='query.planStatus == 1'>
                    and (now() >= rs.start_date and rs.end_date >= now() )
                </if>
                <if test='query.planStatus == 2'>
                    and (now() > rs.end_date)
                </if>
                <if test="query.condOrgIdList!=null and query.condOrgIdList.size>0">
                    and rs.org_id in
                    <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
        </where>
        group by rs.id,so.org_name,dpo.org_name
        <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="findByObjectInfo" resultType="com.testor.module.safeCheck.model.domain.TSafeHazardCheckPlan"
            parameterType="com.testor.module.safeCheck.model.domain.TSafeHazardCheckObject">
        select
        <include refid="select_base_column"></include>
        from t_safe_hazard_check_plan rs
        inner join t_safe_hazard_check_object so on rs.id = so.plan_id
        <where>
            rs.status = '0'
            <if test="query.standardId!='' and query.standardId!=null">
                and so.standard_id=#{query.standardId}
            </if>
            <if test="query.objId!='' and query.objId!=null">
                and so.obj_id=#{query.objId}
            </if>
           and rs.end_date > now()
        </where>
    </select>
</mapper>