<?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.ruoyi.postCheck.mapper.PostPlanInfoMapper">

    <resultMap type="PostPlanInfo" id="PostPlanInfoResult">
        <result property="id"    column="id"    />
        <result property="postPlanId"    column="post_plan_id"    />
        <result property="checkedDeptId"    column="checked_dept_id"    />
        <result property="checkedDeptType"    column="checked_dept_type"    />
        <result property="checkedDeptName"    column="checked_dept_name"    />
        <result property="checkedPostId"    column="checked_post_id"    />
        <result property="checkedPostName"    column="checked_post_name"    />
        <result property="checkedPostUserCount"    column="checked_post_user_count"    />
        <result property="checkTime"    column="check_time"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="checkedImg"    column="checked_img"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="checkedTime"    column="checked_time"    />
        <result property="checkedUserName"    column="checked_user_name"    />
        <result property="status"    column="status"    />

        <result property="printUserId"    column="print_user_id"    />
        <result property="printTime"    column="print_time"    />

        <result property="startTime"  column="start_time"/>
        <result property="endTime"    column="end_time"/>
        <result property="postName"    column="post_name"/>
    </resultMap>



    <sql id="selectPostPlanInfoVo">
        select
            t1.id,
            t1.post_plan_id,
            t1.checked_dept_id,
            t1.checked_dept_type,
            t1.checked_dept_name,
            t1.checked_post_id,
            t1.checked_post_name,
            t1.checked_post_user_count,
            t1.check_time,
            t1.remark,
            t1.del_flag,
            t1.create_time,
            t1.create_by,
            t1.update_time,
            t1.update_by,
            t1.create_user_id,
            t1.print_user_id,
            t1.print_time,
            t1.checked_user_name,
            t1.checked_img,
            t1.checked_time,
            t1.status,
            t1.start_time,
            t1.end_time
            <!--
            sd.dept_name AS checkedDeptName,
            sp.post_name AS checkedPostName
            -->
        from post_plan_info t1
        <!--
        LEFT JOIN sys_dept sd ON sd.dept_id = t1.checked_dept_id
        LEFT JOIN sys_post sp ON sp.post_id = t1.checked_post_id
        -->
    </sql>

    <select id="selectPostPlanInfoList" parameterType="PostPlanInfo" resultMap="PostPlanInfoResult">
        <include refid="selectPostPlanInfoVo"/>
        <where>
            <if test="postPlanId != null "> and post_plan_id = #{postPlanId}</if>
            <if test="checkedDeptId != null "> and checked_dept_id = #{checkedDeptId}</if>
            <if test="checkTime != null "> and check_time = #{checkTime}</if>
            <if test="checkedPostId != null "> and checked_post_id = #{checkedPostId}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="checkedUserName != null  and checkedUserName != ''"> and checked_user_name like concat('%', #{checkedUserName}, '%')</if>
            <if test="status != null "> and status = #{status}</if>

            <if test="postPlanIdList != null and postPlanIdList.size() > 0">
                and t1.post_plan_id in
                <foreach collection="postPlanIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectPostPlanInfoById" parameterType="Long" resultMap="PostPlanInfoResult">
        <include refid="selectPostPlanInfoVo"/>
        where id = #{id}
    </select>



    <select id="selectCheckTimeAndUser" parameterType="Long" resultMap="PostPlanInfoResult">
       SELECT
            t2.id,
            t2.checked_dept_type,
            t2.checked_dept_id,
            t2.checked_post_id,
            t2.checked_time,
            t2.checked_user_name
        FROM post_plan t1
        INNER JOIN post_plan_info t2 ON t2.post_plan_id = t1.id
        WHERE t1."id" = #{planId}
        GROUP BY t2.checked_dept_id, t2.checked_post_id
        ORDER BY t1.create_time, t2.create_time, t2.id
    </select>


    <select id="selectShouldCheckedList" parameterType="PostPlanInfo" resultMap="PostPlanInfoResult">
        select
            t1."status" AS planStatus,
            t1.check_group_id as checkGroupId,
            t1.check_team_id as checkTeamId,

            t2."id" ,
            t2."post_plan_id" ,
            t2."checked_dept_type" ,
            t2."checked_dept_id" ,
            t2."checked_dept_name" ,
            t2.checked_post_id,
            t2.checked_post_name,
            t2."start_time" ,
            t2."end_time" ,
            t2."status",
            t2.check_time,
            t2.checked_user_name
            <!--
            sp.post_name
            -->
        FROM post_plan t1
        INNER JOIN post_plan_info t2 ON t2.post_plan_id  = t1.id
        <!--
        INNER JOIN sys_post sp ON sp.post_id = t2.checked_post_id
        -->
        <where>
            <if test="planStatus != null">
                AND t1."status" = #{planStatus}
            </if>
            <!-- 开始时间检索 -->
            <if test="params.beginTime != null and params.beginTime != ''">
                AND TO_DATE(t2.start_time, 'yyyy-mm-dd') &gt;= TO_DATE(#{params.beginTime}, 'yyyy-mm-dd')
            </if>
            <!-- 结束时间检索 -->
            <if test="params.endTime != null and params.endTime != ''">
                AND TO_DATE(t2.start_time, 'yyyy-mm-dd') &lt;= TO_DATE(#{params.endTime}, 'yyyy-mm-dd')
            </if>
        </where>
        GROUP BY t2."id"
        ORDER BY t1.create_time, t2.create_time, t2.id
    </select>



    <insert id="insertPostPlanInfo" parameterType="PostPlanInfo">
        insert into post_plan_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="postPlanId != null">post_plan_id,</if>
            <if test="checkedDeptType != null">checked_dept_type,</if>
            <if test="checkedDeptId != null">checked_dept_id,</if>
            <if test="checkedDeptName != null and checkedDeptName != ''">checked_dept_name,</if>
            <if test="checkTime != null">check_time,</if>
            <if test="checkedPostId != null">checked_post_id,</if>
            <if test="checkedPostName != null and checkedPostName != ''">checked_post_name,</if>
            <if test="checkedPostUserCount != null">checked_post_user_count,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="status != null">status,</if>

            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="postPlanId != null">#{postPlanId},</if>
            <if test="checkedDeptType != null">#{checkedDeptType},</if>
            <if test="checkedDeptId != null">#{checkedDeptId},</if>
            <if test="checkedDeptName != null and checkedDeptName != ''">#{checkedDeptName},</if>
            <if test="checkTime != null">#{checkTime},</if>
            <if test="checkedPostId != null">#{checkedPostId},</if>
            <if test="checkedPostName != null and checkedPostName != ''">#{checkedPostName},</if>
            <if test="checkedPostUserCount != null">#{checkedPostUserCount},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="status != null">#{status},</if>

            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
         </trim>
    </insert>

    <update id="updatePostPlanInfo" parameterType="PostPlanInfo">
        update post_plan_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="postPlanId != null">post_plan_id = #{postPlanId},</if>
            <if test="checkedDeptType != null">checked_dept_type = #{checkedDeptType},</if>
            <if test="checkedDeptId != null">checked_dept_id = #{checkedDeptId},</if>
            <if test="checkedDeptName != null and checkedDeptName != ''">checked_dept_name = #{checkedDeptName},</if>
            <if test="checkTime != null">check_time = #{checkTime},</if>
            <if test="checkedPostId != null">checked_post_id = #{checkedPostId},</if>
            <if test="checkedPostName != null and checkedPostName != ''">checked_post_name = #{checkedPostName},</if>
            <if test="checkedPostUserCount != null">checked_post_user_count = #{checkedPostUserCount},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="checkedImg != null">checked_img = #{checkedImg},</if>
            <if test="checkedUserName != null">checked_user_name = #{checkedUserName},</if>
            <if test="checkedTime != null">checked_time = #{checkedTime},</if>
            <if test="printUserId != null">print_user_id = #{printUserId},</if>
            <if test="printTime != null">print_time = #{printTime},</if>
            <if test="status != null">status = #{status},</if>

            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
        </trim>
        where id = #{id}
    </update>



    <delete id="deletePostPlanInfoById" parameterType="Long">
        delete from post_plan_info where id = #{id}
    </delete>

    <delete id="deletePostPlanInfoByIds" parameterType="String">
        delete from post_plan_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
