<?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.nantian.cloud.jly.mapper.CheckPlanMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jly.entity.CheckPlan">
        <id column="id" property="id"/>
        <result column="num" property="num"/>
        <result column="report_id" property="reportId"/>
        <result column="name" property="name"/>
        <result column="year" property="year"/>
        <result column="check_purpose" property="checkPurpose"/>
        <result column="check_method" property="checkMethod"/>
        <result column="check_type" property="checkType"/>
        <result column="check_status" property="checkStatus"/>
        <result column="begin_time" property="beginTime"/>
        <result column="end_time" property="endTime"/>
        <result column="check_region_id" property="checkRegionId"/>
        <result column="comment" property="comment"/>
        <result column="send_flag" property="sendFlag"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="del_flag" property="delFlag"/>
        <result column="dept_id" property="deptId"/>
        <result column="meaning" property="meaning"/>
        <result column="basis_file" property="basisFile"/>
    </resultMap>

    <sql id="base_sql">
        i.id, i.num, i.report_id, i.name, year, check_purpose, check_method, check_type, check_status,
         begin_time, end_time, check_region_id, comment, send_flag, userid, update_time, del_flag, dept_id
    </sql>

    <!--select <include refid="base_sql"></include>-->
    <select id="getByPage" resultMap="BaseResultMap">
        select i.id, i.num, i.report_id, i.name, i.year, i.check_purpose, i.check_method, i.check_type, i.check_status,
        i.begin_time, i.end_time, i.check_region_id, i.comment, i.send_flag, i.userid, i.update_time, i.del_flag,
        i.dept_id,r.meaning,i.basis_file
        from check_plan i
        LEFT JOIN dict_region r ON r.id = i.dept_id
        <trim prefix="where" prefixOverrides="and">
            and (i.id in(
                select distinct e1.check_plan_id from check_enterprice e1
                left join enterprise_info e2 on e1.epc = e2.enterprise_code
                where e1.del_flag = '1' and e2.region_code like concat('%',#{deptId},'%')
            ) or i.dept_id like concat('%',#{deptId},'%'))
            <if test="plan.name!=null">
                and name like concat("%",#{plan.name},"%")
            </if>
            <if test="plan.checkMethod != null">
                and check_method = #{plan.checkMethod}
            </if>
            <if test="plan.checkType != null">
                and check_type = #{plan.checkType}
            </if>
            <if test="plan.checkStatus != null">
                and check_status = #{plan.checkStatus}
            </if>
            <if test="plan.endTime != null">
                and #{plan.endTime} >= end_time
            </if>
        </trim>
        and i.del_flag = "1"
    </select>

    <select id="getPageByEpc" resultType="com.nantian.cloud.jly.entity.CheckPlan">
        select  p.id, p.num, p.report_id, p.name, p.year, p.check_purpose, p.check_method, p.check_type, p.check_status,
         p.begin_time, p.end_time, p.check_region_id, p.comment, p.send_flag
        from check_plan as p
        inner join check_enterprice as e on e.check_plan_id = p.id
        and e.epc = #{epc} and p.del_flag = "1"
    </select>


</mapper>
