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

    <resultMap type="PostCheckedProblem" id="PostCheckedProblemResult">
        <result property="id"    column="id"    />
        <result property="status"    column="status"    />
        <result property="problemNo"    column="problem_no"    />
        <result property="checkWorkgroupId"    column="check_workgroup_id"    />
        <result property="checkGroupId"    column="check_group_id"    />
        <!--<result property="checkUserId"    column="check_user_id"    />-->
        <result property="checkUserName"    column="check_user_name"    />
        <result property="deptApproveUserId"    column="dept_approve_user_id"    />
        <result property="deptApproveTime"    column="dept_approve_time"    />
        <result property="specialityDepartmentReviewerId"    column="speciality_department_reviewer_id"    />
        <result property="specialityDepartmentReviewTime"    column="speciality_department_review_time"    />
        <result property="problemLevel"    column="problem_level"    />
        <result property="problemAttribute"    column="problem_attribute"    />
        <result property="problemCategory"    column="problem_category"    />
        <result property="description"    column="description"    />
        <result property="problemCause"    column="problem_cause"    />
        <result property="isImmediatelyCorrected"    column="is_immediately_corrected"    />
        <result property="rectificationMeasure"    column="rectification_measure"    />
        <result property="checkedDeptId"    column="checked_dept_id"    />
        <result property="checkedPostId"    column="checked_post_id"    />
        <result property="rectificationResponsibleUserId"    column="rectification_responsible_user_id"    />
        <result property="rectificationPeriod"    column="rectification_period"    />
        <result property="isRectificationCompleted"    column="is_rectification_completed"    />
        <result property="rectificationCompletionTime"    column="rectification_completion_time"    />
        <result property="rectificationCompletionImg"    column="rectification_completion_img"    />
        <result property="reviewDepartmentReviewerId"    column="review_department_reviewer_id"    />
        <result property="reviewDepartmentReviewerTime"    column="review_department_reviewer_time"    />
        <result property="reviewConclusion"    column="review_conclusion"    />
        <result property="reason"    column="reason"    />
        <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="checkTime"    column="check_time"    />
        <result property="dutyType"    column="duty_type"    />
        <!--
        <result property="checkedDeptName"    column="dept_name"/>
        <result property="checkedPostName"    column="post_name"/>
        -->


    </resultMap>

    <sql id="selectPostCheckedProblemVo">
         select
            t1.id,
            t1.status,
            t1.problem_no,
            t1.check_workgroup_id,
            t1.check_group_id,
            t1.check_user_id,
            t1.check_user_name,
            t1.dept_approve_user_id,
            t1.dept_approve_time,
            t1.speciality_department_reviewer_id,
            t1.speciality_department_review_time,
            t1.problem_level,
            t1.problem_attribute,
            t1.problem_category,
            t1.description,
            t1.problem_cause,
            t1.is_immediately_corrected,
            t1.rectification_measure,
            t1.checked_dept_id,
            t1.checked_post_id,
            t1.rectification_responsible_user_id,
            t1.rectification_period,
            t1.is_rectification_completed,
            t1.rectification_completion_time,
            t1.rectification_completion_img,
            t1.review_department_reviewer_id,
            t1.review_department_reviewer_time,
            t1.review_conclusion, reason,
            t1.remark,
            t1.del_flag,
            t1.create_time,
            t1.create_by,
            t1.update_time,
            t1.update_by,
            t1.create_user_id,
            t1.check_time,
            t1.duty_type,

            t2."dept_name" as checkedDeptName,
            t3."post_name" as checkedPostName,
            t4."nick_name" AS rectificationResponsibleUserName
        from post_checked_problem t1
        LEFT JOIN "sys_dept" t2 ON t2."dept_id" = t1."checked_dept_id"
        LEFT JOIN "sys_post" t3 ON t3."post_id" = t1."checked_post_id"
        LEFT JOIN "public"."sys_users" t4 ON  t4."user_id" = t1."rectification_responsible_user_id"
    </sql>

    <select id="selectPostCheckedProblemList" parameterType="PostCheckedProblem" resultMap="PostCheckedProblemResult">
        <include refid="selectPostCheckedProblemVo"/>
        <where>
            <if test="status != null "> and status = #{status}</if>
            <if test="problemNo != null  and problemNo != ''"> and problem_no = #{problemNo}</if>
            <if test="checkWorkgroupId != null "> and check_workgroup_id = #{checkWorkgroupId}</if>
            <if test="checkGroupId != null "> and check_group_id = #{checkGroupId}</if>
            <!--<if test="checkUserId != null "> and check_user_id = #{checkUserId}</if>-->
            <if test="checkUserName != null and checkUserName != ''"> and check_user_name = #{checkUserName}</if>
            <if test="deptApproveUserId != null "> and dept_approve_user_id = #{deptApproveUserId}</if>
            <if test="deptApproveTime != null "> and dept_approve_time = #{deptApproveTime}</if>
            <if test="specialityDepartmentReviewerId != null "> and speciality_department_reviewer_id = #{specialityDepartmentReviewerId}</if>
            <if test="specialityDepartmentReviewTime != null "> and speciality_department_review_time = #{specialityDepartmentReviewTime}</if>
            <if test="problemLevel != null  and problemLevel != ''"> and problem_level = #{problemLevel}</if>
            <if test="problemAttribute != null"> and problem_attribute = #{problemAttribute}</if>
            <if test="problemCategory != null"> and problem_category = #{problemCategory}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="problemCause != null  and problemCause != ''"> and problem_cause = #{problemCause}</if>
            <if test="isImmediatelyCorrected != null "> and is_immediately_corrected = #{isImmediatelyCorrected}</if>
            <if test="rectificationMeasure != null  and rectificationMeasure != ''"> and rectification_measure = #{rectificationMeasure}</if>
            <if test="checkedDeptId != null "> and checked_dept_id = #{checkedDeptId}</if>
            <if test="checkedPostId != null "> and checked_post_id = #{checkedPostId}</if>
            <if test="rectificationResponsibleUserId != null "> and rectification_responsible_user_id = #{rectificationResponsibleUserId}</if>
            <if test="rectificationPeriod != null "> and rectification_period = #{rectificationPeriod}</if>
            <if test="isRectificationCompleted != null "> and is_rectification_completed = #{isRectificationCompleted}</if>
            <if test="rectificationCompletionTime != null "> and rectification_completion_time = #{rectificationCompletionTime}</if>
            <if test="rectificationCompletionImg != null  and rectificationCompletionImg != ''"> and rectification_completion_img = #{rectificationCompletionImg}</if>
            <if test="reviewDepartmentReviewerId != null "> and review_department_reviewer_id = #{reviewDepartmentReviewerId}</if>
            <if test="reviewDepartmentReviewerTime != null "> and review_department_reviewer_time = #{reviewDepartmentReviewerTime}</if>
            <if test="reviewConclusion != null  and reviewConclusion != ''"> and review_conclusion = #{reviewConclusion}</if>
            <if test="reason != null  and reason != ''"> and reason = #{reason}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>

            <!--<if test="checkTime != null "> and check_time = #{checkTime}</if>-->
            <if test="checkTime != null "> and TO_DATE(t1.check_time, 'yyyy-mm-dd') &gt;= TO_DATE(#{checkTime}, 'yyyy-mm-dd')</if>

            <if test="dutyType != null "> and duty_type = #{dutyType}</if>

            <!-- 开始时间检索 -->
            <if test="params.beginTime != null and params.beginTime != ''">
                AND TO_DATE(t1.check_time, 'yyyy-mm-dd') &gt;= TO_DATE(#{params.beginTime}, 'yyyy-mm-dd')
            </if>
            <!-- 结束时间检索 -->
            <if test="params.endTime != null and params.endTime != ''">
                AND TO_DATE(t1.check_time, 'yyyy-mm-dd') &lt;= TO_DATE(#{params.endTime}, 'yyyy-mm-dd')
            </if>
        </where>
    </select>

    <select id="selectPostCheckedProblemById" parameterType="Long" resultMap="PostCheckedProblemResult">
        <include refid="selectPostCheckedProblemVo"/>
        where id = #{id}
    </select>

    <insert id="insertPostCheckedProblem" parameterType="PostCheckedProblem">
        insert into post_checked_problem
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="status != null">status,</if>
            <if test="problemNo != null and problemNo != ''">problem_no,</if>
            <if test="checkWorkgroupId != null">check_workgroup_id,</if>
            <if test="checkGroupId != null">check_group_id,</if>
            <!--<if test="checkUserId != null">check_user_id,</if>-->
            <if test="checkUserName != null and checkUserName != ''">check_user_name,</if>
            <if test="deptApproveUserId != null">dept_approve_user_id,</if>
            <if test="deptApproveTime != null">dept_approve_time,</if>
            <if test="specialityDepartmentReviewerId != null">speciality_department_reviewer_id,</if>
            <if test="specialityDepartmentReviewTime != null">speciality_department_review_time,</if>
            <if test="problemLevel != null and problemLevel != ''">problem_level,</if>
            <if test="problemAttribute != null">problem_attribute,</if>
            <if test="problemCategory != null">problem_category,</if>
            <if test="description != null">description,</if>
            <if test="problemCause != null">problem_cause,</if>
            <if test="isImmediatelyCorrected != null">is_immediately_corrected,</if>
            <if test="rectificationMeasure != null">rectification_measure,</if>
            <if test="checkedDeptId != null">checked_dept_id,</if>
            <if test="checkedPostId != null">checked_post_id,</if>
            <if test="rectificationResponsibleUserId != null">rectification_responsible_user_id,</if>
            <if test="rectificationPeriod != null">rectification_period,</if>
            <if test="isRectificationCompleted != null">is_rectification_completed,</if>
            <if test="rectificationCompletionTime != null">rectification_completion_time,</if>
            <if test="rectificationCompletionImg != null">rectification_completion_img,</if>
            <if test="reviewDepartmentReviewerId != null">review_department_reviewer_id,</if>
            <if test="reviewDepartmentReviewerTime != null">review_department_reviewer_time,</if>
            <if test="reviewConclusion != null">review_conclusion,</if>
            <if test="reason != null">reason,</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="checkTime != null">check_time,</if>
            <if test="dutyType != null">duty_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="status != null">#{status},</if>
            <if test="problemNo != null and problemNo != ''">#{problemNo},</if>
            <if test="checkWorkgroupId != null">#{checkWorkgroupId},</if>
            <if test="checkGroupId != null">#{checkGroupId},</if>
            <!--<if test="checkUserId != null">#{checkUserId},</if>-->
            <if test="checkUserName != null and checkUserName != ''">#{checkUserName},</if>
            <if test="deptApproveUserId != null">#{deptApproveUserId},</if>
            <if test="deptApproveTime != null">#{deptApproveTime},</if>
            <if test="specialityDepartmentReviewerId != null">#{specialityDepartmentReviewerId},</if>
            <if test="specialityDepartmentReviewTime != null">#{specialityDepartmentReviewTime},</if>
            <if test="problemLevel != null and problemLevel != ''">#{problemLevel},</if>
            <if test="problemAttribute != null">#{problemAttribute},</if>
            <if test="problemCategory != null and problemCategory != ''">#{problemCategory},</if>
            <if test="description != null">#{description},</if>
            <if test="problemCause != null">#{problemCause},</if>
            <if test="isImmediatelyCorrected != null">#{isImmediatelyCorrected},</if>
            <if test="rectificationMeasure != null">#{rectificationMeasure},</if>
            <if test="checkedDeptId != null">#{checkedDeptId},</if>
            <if test="checkedPostId != null">#{checkedPostId},</if>
            <if test="rectificationResponsibleUserId != null">#{rectificationResponsibleUserId},</if>
            <if test="rectificationPeriod != null">#{rectificationPeriod},</if>
            <if test="isRectificationCompleted != null">#{isRectificationCompleted},</if>
            <if test="rectificationCompletionTime != null">#{rectificationCompletionTime},</if>
            <if test="rectificationCompletionImg != null">#{rectificationCompletionImg},</if>
            <if test="reviewDepartmentReviewerId != null">#{reviewDepartmentReviewerId},</if>
            <if test="reviewDepartmentReviewerTime != null">#{reviewDepartmentReviewerTime},</if>
            <if test="reviewConclusion != null">#{reviewConclusion},</if>
            <if test="reason != null">#{reason},</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="checkTime != null">#{checkTime},</if>
            <if test="dutyType != null">#{dutyType},</if>
        </trim>
    </insert>

    <update id="updatePostCheckedProblem" parameterType="PostCheckedProblem">
        update post_checked_problem
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
            <if test="problemNo != null and problemNo != ''">problem_no = #{problemNo},</if>
            <if test="checkWorkgroupId != null">check_workgroup_id = #{checkWorkgroupId},</if>
            <if test="checkGroupId != null">check_group_id = #{checkGroupId},</if>
            <!--<if test="checkUserId != null">check_user_id = #{checkUserId},</if>-->
            <if test="checkUserName != null and checkUserName != ''">check_user_name = #{checkUserName},</if>
            <if test="deptApproveUserId != null">dept_approve_user_id = #{deptApproveUserId},</if>
            <if test="deptApproveTime != null">dept_approve_time = #{deptApproveTime},</if>
            <if test="specialityDepartmentReviewerId != null">speciality_department_reviewer_id = #{specialityDepartmentReviewerId},</if>
            <if test="specialityDepartmentReviewTime != null">speciality_department_review_time = #{specialityDepartmentReviewTime},</if>
            <if test="problemLevel != null and problemLevel != ''">problem_level = #{problemLevel},</if>
            <if test="problemAttribute != null">problem_attribute = #{problemAttribute},</if>
            <if test="problemCategory != null">problem_category = #{problemCategory},</if>
            <if test="description != null">description = #{description},</if>
            <if test="problemCause != null">problem_cause = #{problemCause},</if>
            <if test="isImmediatelyCorrected != null">is_immediately_corrected = #{isImmediatelyCorrected},</if>
            <if test="rectificationMeasure != null">rectification_measure = #{rectificationMeasure},</if>
            <if test="checkedDeptId != null">checked_dept_id = #{checkedDeptId},</if>
            <if test="checkedPostId != null">checked_post_id = #{checkedPostId},</if>
            <if test="rectificationResponsibleUserId != null">rectification_responsible_user_id = #{rectificationResponsibleUserId},</if>
            <if test="rectificationPeriod != null">rectification_period = #{rectificationPeriod},</if>
            <if test="isRectificationCompleted != null">is_rectification_completed = #{isRectificationCompleted},</if>
            <if test="rectificationCompletionTime != null">rectification_completion_time = #{rectificationCompletionTime},</if>
            <if test="rectificationCompletionImg != null">rectification_completion_img = #{rectificationCompletionImg},</if>
            <if test="reviewDepartmentReviewerId != null">review_department_reviewer_id = #{reviewDepartmentReviewerId},</if>
            <if test="reviewDepartmentReviewerTime != null">review_department_reviewer_time = #{reviewDepartmentReviewerTime},</if>
            <if test="reviewConclusion != null">review_conclusion = #{reviewConclusion},</if>
            <if test="reason != null">reason = #{reason},</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="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="checkTime != null">check_time = #{checkTime},</if>
            <if test="dutyType != null">duty_type = #{dutyType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePostCheckedProblemById" parameterType="Long">
        delete from post_checked_problem where id = #{id}
    </delete>

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

    <select id="getPostTotalByDeptId" resultType="java.math.BigDecimal">
        SELECT COUNT(0)
        FROM "public"."sys_user_post" a LEFT JOIN "public"."sys_user_role" sur ON a."user_id"  = sur."user_id"
                                        LEFT JOIN "public"."sys_role_dept" srd ON sur."role_id"  = srd."role_id"
        WHERE srd."dept_id" = #{deptId} GROUP BY a."post_id"
    </select>

    <select id="selectCheckedPostCount" resultType="java.lang.Integer">
        SELECT COUNT(0)
        FROM "public"."post_plan" a INNER JOIN "public"."post_plan_info" ppi ON a."id"  = ppi."post_plan_id" WHERE ppi."status" = #{status}

    </select>

    <!--按问题属性分组统计问题类别-->
    <select id="problemCategoryByAttribute" resultType="ProblemCategory">
        SELECT
            problem_attribute,
            COUNT(t1.id) AS problemTotal,
            SUM(
                 case
                    when t1.problem_category = 1 then 1
                    else 0
                 END
            ) AS problemCategoryOne,

            SUM(
                 case
                    when t1.problem_category = 2 then 1
                    else 0
                 END
            ) AS problemCategoryTwo,

            SUM(
                 case
                    when t1.problem_category = 3 then 1
                    else 0
                 END
            ) AS problemCategoryThree,


            SUM(
                 case
                    when t1.problem_category = 4 then 1
                    else 0
                 END
            ) AS problemCategoryFour,

            SUM(
                 case
                    when t1.problem_category = 5 then 1
                    else 0
                 END
            ) AS problemCategoryFive,

            SUM(
                 case
                    when t1.problem_category = 6 then 1
                    else 0
                 END
            ) AS problemCategorySix
        FROM post_checked_problem t1
        GROUP BY problem_attribute
        ORDER BY t1.problem_attribute
    </select>
</mapper>
