<?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.PostPlanMapper">

    <resultMap type="PostPlan" id="PostPlanResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="content"    column="content"    />
        <result property="status"    column="status"    />
        <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="createUserId"    column="create_user_id"    />
        <result property="checkGroupId"    column="check_group_id"    />
        <result property="checkTeamId"    column="check_team_id"    />
        <result property="statusIcfa"    column="status_icfa"    />
        <result property="dateIcfa"    column="date_icfa"    />
        <result property="reasonIcfa"    column="reason_icfa"    />
        <result property="statusHr"    column="status_hr"    />
        <result property="dateHr"    column="date_hr"    />
        <result property="reasonHr"    column="reason_hr"    />
        <result property="statusTime"    column="status_time"    />
        <result property="statusUserId"    column="status_user_id"    />
        <result property="statusIcfaUserId"    column="status_icfa_user_id"    />
        <result property="statusHrUserId"    column="status_hr_user_id"    />
        <result property="step"    column="step"    />
        <result property="publish"    column="publish"    />
    </resultMap>

    <sql id="selectPostPlanVo">
        select
            id, name, content,
             status, remark, del_flag, create_time, create_by, update_time, update_by, create_user_id,
            check_group_id,
            check_team_id,
            status_icfa, date_icfa, reason_icfa, status_hr, date_hr, reason_hr, status_time, status_user_id, status_icfa_user_id,
            status_hr_user_id, step,
            publish
        from post_plan
    </sql>

    <select id="selectPostPlanList" parameterType="PostPlan" resultMap="PostPlanResult">
        SELECT
            t1.id, t1.name, t1.content,
            t1.status,
            t1.remark, t1.del_flag, t1.create_time, t1.create_by, t1.update_time, t1.update_by, t1.create_user_id,
            t1.check_group_id,
            t1.check_team_id,
            t1.status_icfa, t1.date_icfa, t1.reason_icfa,
            t1.status_hr, t1.date_hr, t1.reason_hr,
            t1.status_time, t1.status_user_id, t1.status_icfa_user_id, t1.status_hr_user_id,
            t1.step,
            t1.publish,

            pt1.name AS checkGroupName,
            pt2.name AS checkTeamName,
            GROUP_CONCAT (DISTINCT t2.checked_dept_name) AS checkedDeptNames,
            GROUP_CONCAT (DISTINCT t2.checked_post_name) AS checkedPostNames,
            GROUP_CONCAT (DISTINCT t3.checked_user_name) AS checkedUserNames
        FROM post_plan t1
        INNER JOIN post_plan_info t2 ON t2.post_plan_id  = t1.id
        LEFT JOIN post_plan_check_user t3 ON t3.post_plan_id  = t1.id
        LEFT JOIN post_team pt1 ON pt1.id = t1.check_group_id
        LEFT JOIN post_team pt2 ON pt2.id = t1.check_team_id
        <where>
            <if test="id != null "> and t1.id = #{id}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="checkGroupId != null "> and check_group_id = #{checkGroupId}</if>
            <if test="checkTeamId != null "> and check_team_id = #{checkTeamId}</if>
            <if test="statusIcfa != null "> and status_icfa = #{statusIcfa}</if>
            <if test="dateIcfa != null  and dateIcfa != ''"> and date_icfa = #{dateIcfa}</if>
            <if test="reasonIcfa != null  and reasonIcfa != ''"> and reason_icfa = #{reasonIcfa}</if>
            <if test="statusHr != null "> and status_hr = #{statusHr}</if>
            <if test="dateHr != null  and dateHr != ''"> and date_hr = #{dateHr}</if>
            <if test="reasonHr != null  and reasonHr != ''"> and reason_hr = #{reasonHr}</if>
            <if test="statusTime != null  and statusTime != ''"> and status_time = #{statusTime}</if>
            <if test="statusUserId != null "> and status_user_id = #{statusUserId}</if>
            <if test="statusIcfaUserId != null "> and status_icfa_user_id = #{statusIcfaUserId}</if>
            <if test="statusHrUserId != null "> and status_hr_user_id = #{statusHrUserId}</if>

            <if test="publish != null "> and t1.publish = #{publish}</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>

            <if test="checkGroupId != null  "> and t1."check_group_id" = #{checkGroupId}</if>
            <if test="checkTeamId != null  "> and t1."check_team_id" = #{checkTeamId}</if>
            <if test="checkGroupName != null  and checkGroupName != ''"> and pt1."name" like concat('%', #{checkGroupName}, '%')</if>
            <if test="checkTeamName != null  and checkTeamName != ''"> and pt2."name" like concat('%', #{checkTeamName}, '%')</if>

            <if test="checkedDeptId != null "> and t2.checked_dept_id = #{checkedDeptId}</if>
            <if test="checkedPostId != null "> and t2.checked_post_id = #{checkedPostId}</if>
            <if test="checkedDeptNames != null  and checkedDeptNames != ''"> and t2.checked_dept_name like concat('%', #{checkedDeptNames}, '%')</if>
            <if test="checkedPostNames != null  and checkedPostNames != ''"> and t2.checked_post_name like concat('%', #{checkedPostNames}, '%')</if>
        </where>
        GROUP BY t1.id
        <!--
        select
            t1.id, t1.name, t1.content,
            t1.status,
            t1.remark, t1.del_flag, t1.create_time, t1.create_by, t1.update_time, t1.update_by, t1.create_user_id,
            t1.check_group_id,
            t1.check_team_id,
            t1.status_icfa, t1.date_icfa, t1.reason_icfa,
            t1.status_hr, t1.date_hr, t1.reason_hr,
            t1.status_time, t1.status_user_id, t1.status_icfa_user_id, t1.status_hr_user_id,
            t1.step,
            t1.publish,

            pt1.name AS checkGroupName,
            pt2.name AS checkTeamName,
            GROUP_CONCAT (DISTINCT sd.dept_name) AS checkedDeptNames,
            GROUP_CONCAT (DISTINCT sp.post_name) AS checkedPostNames,
            GROUP_CONCAT (DISTINCT su."nick_name") AS checkedUserNames
        from post_plan t1
        INNER JOIN post_plan_info t2 ON t2.post_plan_id  = t1.id
        LEFT JOIN post_plan_check_user t3 ON t3.post_plan_id  = t1.id
        LEFT JOIN "public"."sys_users" su ON su."user_id" = t3."checked_user_id"
        LEFT JOIN post_team pt1 ON pt1.id = t1.check_group_id
        LEFT JOIN post_team pt2 ON pt2.id = t1.check_team_id
        LEFT JOIN sys_dept sd ON sd.dept_id = t2.checked_dept_id
        LEFT JOIN sys_post sp ON sp.post_id = t2.checked_post_id
        <where>
            <if test="id != null "> and t1.id = #{id}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="checkGroupId != null "> and check_group_id = #{checkGroupId}</if>
            <if test="checkTeamId != null "> and check_team_id = #{checkTeamId}</if>
            <if test="statusIcfa != null "> and status_icfa = #{statusIcfa}</if>
            <if test="dateIcfa != null  and dateIcfa != ''"> and date_icfa = #{dateIcfa}</if>
            <if test="reasonIcfa != null  and reasonIcfa != ''"> and reason_icfa = #{reasonIcfa}</if>
            <if test="statusHr != null "> and status_hr = #{statusHr}</if>
            <if test="dateHr != null  and dateHr != ''"> and date_hr = #{dateHr}</if>
            <if test="reasonHr != null  and reasonHr != ''"> and reason_hr = #{reasonHr}</if>
            <if test="statusTime != null  and statusTime != ''"> and status_time = #{statusTime}</if>
            <if test="statusUserId != null "> and status_user_id = #{statusUserId}</if>
            <if test="statusIcfaUserId != null "> and status_icfa_user_id = #{statusIcfaUserId}</if>
            <if test="statusHrUserId != null "> and status_hr_user_id = #{statusHrUserId}</if>

            <if test="publish != null "> and publish = #{publish}</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>

        <if test="checkGroupId != null  "> and t1."check_group_id" = #{checkGroupId}</if>
        <if test="checkTeamId != null  "> and t1."check_team_id" = #{checkTeamId}</if>
        <if test="checkGroupName != null  and checkGroupName != ''"> and pt1."name" like concat('%', #{checkGroupName}, '%')</if>
        <if test="checkTeamName != null  and checkTeamName != ''"> and pt2."name" like concat('%', #{checkTeamName}, '%')</if>

        <if test="checkedDeptId != null "> and sd.dept_id = #{checkedDeptId}</if>
        <if test="checkedPostId != null "> and sp.post_id = #{checkedPostId}</if>
        <if test="checkedDeptNames != null  and checkedDeptNames != ''"> and sd.dept_name like concat('%', #{checkedDeptNames}, '%')</if>
        <if test="checkedPostNames != null  and checkedPostNames != ''"> and sp.post_name like concat('%', #{checkedPostNames}, '%')</if>
    </where>
    GROUP BY t1.id
        -->
    </select>

    <select id="selectPostPlanById" parameterType="Long" resultMap="PostPlanResult">
        <include refid="selectPostPlanVo"/>
        where id = #{id}
    </select>

    <insert id="insertPostPlan" parameterType="PostPlan">
        insert into post_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null and delFlag != ''">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="checkGroupId != null">check_group_id,</if>
            <if test="checkTeamId != null">check_team_id,</if>
            <if test="statusIcfa != null">status_icfa,</if>
            <if test="dateIcfa != null">date_icfa,</if>
            <if test="reasonIcfa != null">reason_icfa,</if>
            <if test="statusHr != null">status_hr,</if>
            <if test="dateHr != null">date_hr,</if>
            <if test="reasonHr != null">reason_hr,</if>
            <if test="statusTime != null">status_time,</if>
            <if test="statusUserId != null">status_user_id,</if>
            <if test="statusIcfaUserId != null">status_icfa_user_id,</if>
            <if test="statusHrUserId != null">status_hr_user_id,</if>
            <if test="publish != null">publish,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null and delFlag != ''">#{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="checkGroupId != null">#{checkGroupId},</if>
            <if test="checkTeamId != null">#{checkTeamId},</if>
            <if test="statusIcfa != null">#{statusIcfa},</if>
            <if test="dateIcfa != null">#{dateIcfa},</if>
            <if test="reasonIcfa != null">#{reasonIcfa},</if>
            <if test="statusHr != null">#{statusHr},</if>
            <if test="dateHr != null">#{dateHr},</if>
            <if test="reasonHr != null">#{reasonHr},</if>
            <if test="statusTime != null">#{statusTime},</if>
            <if test="statusUserId != null">#{statusUserId},</if>
            <if test="statusIcfaUserId != null">#{statusIcfaUserId},</if>
            <if test="statusHrUserId != null">#{statusHrUserId},</if>
            <if test="publish != null">#{publish},</if>
        </trim>
    </insert>

    <update id="updatePostPlan" parameterType="PostPlan">
        update post_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="content != null and content != ''">content = #{content},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="checkGroupId != null">check_group_id = #{checkGroupId},</if>
            <if test="checkTeamId != null">check_team_id = #{checkTeamId},</if>
            <if test="statusIcfa != null">status_icfa = #{statusIcfa},</if>
            <if test="dateIcfa != null">date_icfa = #{dateIcfa},</if>
            <if test="reasonIcfa != null">reason_icfa = #{reasonIcfa},</if>
            <if test="statusHr != null">status_hr = #{statusHr},</if>
            <if test="dateHr != null">date_hr = #{dateHr},</if>
            <if test="reasonHr != null">reason_hr = #{reasonHr},</if>
            <if test="statusTime != null">status_time = #{statusTime},</if>
            <if test="statusUserId != null">status_user_id = #{statusUserId},</if>
            <if test="statusIcfaUserId != null">status_icfa_user_id = #{statusIcfaUserId},</if>
            <if test="statusHrUserId != null">status_hr_user_id = #{statusHrUserId},</if>
            <if test="step != null">step = #{step},</if>
            <if test="publish != null">publish = #{publish},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePostPlanById" parameterType="Long">
        delete from post_plan where id = #{id}
    </delete>

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

    <!--从岗检计划中查询，岗检记录上传的列表-->
    <select id="selectPostPlanJoinCheckedList" parameterType="PostPlanJoinChecked" resultType="PostPlanJoinChecked">
        SELECT
            t1.check_group_id AS checkGroupId,
            t1.check_team_id AS checkTeamId,
            t1.status AS planStatus,
            pt1.name AS checkGroupName,
            pt2.name AS checkTeamName,
            t2.id AS planInfoId,
            t2.checked_user_name AS checkedUserName,
            t2.checked_dept_id AS checkedDeptId,
            t2.checked_post_id AS checkedPostId,
            t2.check_time AS checkTime,
            t2.checked_img AS checkedImg,
            t2.status AS status,
            case t2.checked_dept_type
                when 1 then '职能部门'
                when 2 then '生产部门'
                else 'Other'
                end  AS checkedDeptTypeView,
            t2.checked_dept_name AS checkedDeptName,
            GROUP_CONCAT (DISTINCT t2.checked_post_name) AS checkedPostNames,
            GROUP_CONCAT (DISTINCT t3.checked_user_name) AS checkedUserNames
            <!--
             case sd.type
                when 1 then '职能部门'
                when 2 then '生产部门'
                else 'Other'
                end  AS checkedDeptTypeView,
            sd.dept_name AS checkedDeptName,
            GROUP_CONCAT (DISTINCT sp.post_name) AS checkedPostNames,
            GROUP_CONCAT (DISTINCT su."nick_name") AS checkedUserNames
            -->
        FROM post_plan t1
        INNER JOIN post_plan_info t2 ON t2."post_plan_id"  = t1.id
        LEFT JOIN post_plan_check_user t3 ON t3.post_plan_id  = t1.id
        LEFT JOIN post_team pt1 ON pt1.id = t1.check_group_id
        LEFT JOIN post_team pt2 ON pt2.id = t1.check_team_id
        <!--
        LEFT JOIN "public"."sys_users" su ON su."user_id" = t3."checked_user_id"
        LEFT JOIN sys_dept sd ON sd.dept_id = t2.checked_dept_id
        LEFT JOIN sys_post sp ON sp.post_id = t2.checked_post_id
        -->
        <where>
            <if test="planStatus != null  and planStatus != 0"> and t1."status" = #{planStatus} and t1.publish > 0</if>


            <if test="status != null  and status != 0"> and t2."status" = #{status}</if>

            <if test="checkGroupName != null  and checkGroupName != ''"> and pt1."name" like concat('%', #{checkGroupName}, '%')</if>
            <if test="checkTeamName != null  and checkTeamName != ''"> and pt2."name" like concat('%', #{checkTeamName}, '%')</if>
            <if test="checkedDeptName != null  and checkedDeptName != ''"> and t2."checked_dept_name" like concat('%', #{checkedDeptName}, '%')</if>
            <if test="checkedPostNames != null  and checkedPostNames != ''"> and t2."checked_post_name" like concat('%', #{checkedPostNames}, '%')</if>

            <if test="checkedDeptId != null"> and t2."checked_dept_id" = #{checkedDeptId}</if>
            <if test="checkGroupId != null  and checkGroupId != ''"> and t1.check_group_id = #{checkGroupId}</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 t1."id" ,t2.checked_dept_id, t2."checked_post_id"
        ORDER BY t1.create_time, t2.create_time, t2.id
    </select>

    <!--从岗检计划中查询，岗检问题录入的列表-->
    <select id="selectPostPlanJoinProblemList" parameterType="PostPlanJoinProblem" resultType="PostPlanJoinProblem">
        SELECT
            t1."id" AS planId,
            t1.check_group_id AS checkGroupId,
            t1.check_team_id AS checkTeamId,
            t2.start_time as checkTime,
            t2.id AS planInfoId,
            t2.checked_dept_id AS checkedDeptId,
            t2.checked_post_id AS checkedPostId,
            <!-- 取最小值，部门下未全部检查完时，就认为是未检 -->
            MIN(t2.status) AS status,
            pt1.name AS checkGroupName,
            pt2.name AS checkTeamName,
            t2.checked_dept_name AS checkedDeptName,
            GROUP_CONCAT (DISTINCT t2.checked_post_name) AS checkedPostNames,
            GROUP_CONCAT (DISTINCT t3.checked_user_name) AS checkedUserNames
        FROM post_plan t1
        INNER JOIN post_plan_info t2 ON t2.post_plan_id  = t1.id
        LEFT JOIN post_plan_check_user t3 ON t3.post_plan_id  = t1.id
        LEFT JOIN post_team pt1 ON pt1.id = t1.check_group_id
        LEFT JOIN post_team pt2 ON pt2.id = t1.check_team_id
        <!--
        LEFT JOIN "public"."sys_users" su ON su."user_id" = t3."checked_user_id"
        LEFT JOIN sys_dept sd ON sd.dept_id = t2.checked_dept_id
        LEFT JOIN sys_post sp ON sp.post_id = t2.checked_post_id
        -->
        <where>
            <if test="planInfoId != null">
                and t2.id = #{planInfoId}
            </if>
            <if test="status != null  and status != 0"> and t2."status" = #{status}  and t1.publish > 0</if>

            <if test="checkGroupName != null  and checkGroupName != ''"> and pt1."name" like concat('%', #{checkGroupName}, '%')</if>
            <if test="checkTeamName != null  and checkTeamName != ''"> and pt2."name" like concat('%', #{checkTeamName}, '%')</if>
            <if test="checkedDeptName != null  and checkedDeptName != ''"> and t2."checked_dept_name" like concat('%', #{checkedDeptName}, '%')</if>
            <if test="checkedPostNames != null  and checkedPostNames != ''"> and t2."checked_post_name" like concat('%', #{checkedPostNames}, '%')</if>

            <if test="checkedDeptId != null"> and t2."checked_dept_id" = #{checkedDeptId}</if>
            <if test="checkGroupId != null  and checkGroupId != ''"> and t1.check_group_id = #{checkGroupId}</if>
        </where>
        GROUP BY t1.id, t2.checked_dept_id
        ORDER BY t1.create_time, t2.create_time, t2.id
    </select>

</mapper>
