<?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.riskOwner.dao.TSafeRiskOwnerDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.ddd.safetyControl.infrastructure.repository.riskOwner.entity.TSafeRiskOwnerDO">
        <id column="id" property="id"/>
        <id column="risk_id" property="riskId"/>
        <id column="user_id" property="userId"/>
        <id column="org_id" property="orgId"/>
        <id column="department_id" property="departmentId"/>
        <id column="type" property="type"/>
    </resultMap>

    <sql id="select_base_column">
        rs.id, rs.risk_id riskId, rs.user_id userId, rs.org_id orgId, rs.department_id departmentId,rs.type,
        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.departmentId!='' and query.departmentId!=null">
            and rs.department_id=#{query.departmentId}
        </if>
        <if test="query.riskId!='' and query.riskId!=null">
            and rs.risk_id=#{query.riskId}
        </if>
        <if test="query.type!='' and query.type!=null">
            and rs.type=#{query.type}
        </if>
        <if test="query.userId!='' and query.userId!=null">
            and rs.user_id=#{userId}
        </if>
    </sql>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskOwner.TSafeRiskOwnerVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskOwner.TSafeRiskOwnerDTOParam">
        select so.org_name as orgName,dpo.org_name as departmentName,
        su.user_name as userName,
        <include refid="select_base_column" />
        from t_safe_risk_owner rs
        left join t_sys_user su on rs.user_id = su.user_id
        left join t_sys_org so on so.org_id = rs.org_id
        left join t_sys_org dpo on dpo.org_id = rs.department_id
        <where>
            rs.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.userName!='' and query.userName!=null">
                and su.user_name like concat('%',#{userName},'%')
            </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="listAll"
            resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskOwner.TSafeRiskOwnerVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskOwner.TSafeRiskOwnerDTOParam">
        select so.org_name as orgName,dpo.org_name as departmentName,
        su.user_name as userName,
        <include refid="select_base_column" />
        from t_safe_risk_owner rs
        left join t_sys_user su on rs.user_id = su.user_id
        left join t_sys_org so on so.org_id = rs.org_id
        left join t_sys_org dpo on dpo.org_id = rs.department_id
        <where>
            rs.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.userName!='' and query.userName!=null">
                and su.user_name like concat('%',#{userName},'%')
            </if>
        </where>
    </select>
</mapper>