<?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.riskSource.dao.TSafeRiskSourceRecordDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.ddd.safetyControl.infrastructure.repository.riskSource.entity.TSafeRiskSourceRecordDO">
        <id column="id" property="id"/>
        <id column="org_id" property="orgId"/>
        <id column="user_id" property="userId"/>
        <id column="name" property="name"/>
        <id column="operate_type" property="operateType"/>
        <id column="source_id" property="sourceId"/>
    </resultMap>

    <sql id="select_base_column">
        rs.id, rs.org_id orgId, rs.user_id userId, rs.name , rs.operate_type operateType,
        rs.source_id sourceId, rs.remarks, rs.create_date createDate
    </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.name!='' and query.name!=null">-->
<!--            and rs.name like concat('%',#{query.name},'%')-->
<!--        </if>-->
        <if test="query.operateType!='' and query.operateType!=null">
            and rs.operate_type=#{query.operateType}
        </if>
        <if test="query.sourceId!='' and query.sourceId!=null">
            and rs.source_id=#{query.sourceId}
        </if>
    </sql>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskSource.TSafeRiskSourceRecordVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskSource.TSafeRiskSourceRecordDTOParam" databaseId="mysql">
        select su.user_name as userName,rso.org_name as regionOrgName,bso.org_name as basicOrgName,se.name as sourceName,
        <include refid="select_base_column" />
        from t_safe_risk_source_record rs
        left join t_sys_user su on su.user_id = rs.user_id
        left join t_safe_risk_source se on rs.source_id = se.id
        left join t_sys_org rso on rso.org_id = se.region_org_id
        left join t_sys_org bso on bso.org_id = se.basic_org_id
        <where>
            <if test="query.sourceOrgId!=null and query.sourceOrgId!=''">
                and  (se.org_id = #{query.sourceOrgId} or se.org_all_path like concat('%',#{query.sourceOrgId},'%'))
            </if>
            <if test="query.name!='' and query.name!=null">
                and se.name like concat('%',#{query.name},'%')
            </if>
            <if test="query.year!='' and query.year!=null">
                and rs.create_date like concat(#{query.year},'%')
            </if>
            <include refid="common_where_if"></include>
        </where>
        order by rs.create_date desc

    </select>
    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskSource.TSafeRiskSourceRecordVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskSource.TSafeRiskSourceRecordDTOParam" databaseId="kingbase">
        select su.user_name as userName,rso.org_name as regionOrgName,bso.org_name as basicOrgName,se.name as sourceName,
        <include refid="select_base_column" />
        from t_safe_risk_source_record rs
        left join t_sys_user su on su.user_id = rs.user_id
        left join t_safe_risk_source se on rs.source_id = se.id
        left join t_sys_org rso on rso.org_id = se.region_org_id
        left join t_sys_org bso on bso.org_id = se.basic_org_id
        <where>
            <if test="query.sourceOrgId!=null and query.sourceOrgId!=''">
                and  (se.org_id = #{query.sourceOrgId} or se.org_all_path like concat('%',#{query.sourceOrgId},'%'))
            </if>
            <if test="query.name!='' and query.name!=null">
                and se.name like concat('%',#{query.name},'%')
            </if>
            <if test="query.year!='' and query.year!=null">
                and   EXTRACT(YEAR FROM rs.create_date) = #{query.year}
            </if>
            <include refid="common_where_if"></include>
        </where>
        order by rs.create_date desc

    </select>

</mapper>