<?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="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.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,
            t1.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.check_user_name,
            t1.duty_type,

            t1.checked_dept_name,
	        t1.checked_post_name,
            t1.rectification_responsible_name,
            t1.dept_approve_user_name,
            t1.speciality_department_reviewer_name,
            t1.review_department_reviewer_name,

            pt1.name AS checkWorkgroupName,
            pt2.name AS checkGroupName
            <!--
            t2."dept_name" as checkedDeptName,
            t3."post_name" as checkedPostName,
            t4."nick_name" AS rectificationResponsibleName,
            t6."nick_name" AS specialityDepartmentReviewerName,
            t5."nick_name" AS reviewDepartmentReviewerName
        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"
        LEFT JOIN "public"."sys_users" t6 ON  t6."user_id" = t1."speciality_department_reviewer_id"
        LEFT JOIN "public"."sys_users" t5 ON  t5."user_id" = t1."review_department_reviewer_id"
            -->
        from post_checked_problem t1
        LEFT JOIN post_team pt1 ON pt1.id = t1.check_workgroup_id
        LEFT JOIN post_team pt2 ON pt2.id = t1.check_group_id
    </sql>

    <select id="selectPostCheckedProblemList" parameterType="PostCheckedProblem" resultMap="PostCheckedProblemResult">
        <include refid="selectPostCheckedProblemVo"/>
        <where>
            <if test="checkWorkgroupName != null  and checkWorkgroupName != ''"> and pt1."name" like concat('%', #{checkWorkgroupName}, '%')</if>
            <if test="checkGroupName != null  and checkGroupName != ''"> and pt2."name" like concat('%', #{checkGroupName}, '%')</if>
            <if test="checkedDeptName != null  and checkedDeptName != ''"> and t1.checked_dept_name like concat('%', #{checkedDeptName}, '%')</if>
            <if test="checkedPostName != null  and checkedPostName != ''"> and t1.checked_post_name like concat('%', #{checkedPostName}, '%')</if>

            <if test="status != null "> and t1.status = #{status}</if>
            <if test="problemNo != null  and problemNo != ''"> and t1.problem_no = #{problemNo}</if>
            <if test="checkWorkgroupId != null "> and t1.check_workgroup_id = #{checkWorkgroupId}</if>
            <if test="checkGroupId != null "> and t1.check_group_id = #{checkGroupId}</if>
            <if test="checkUserName != null and checkUserName != ''"> and t1.check_user_name = #{checkUserName}</if>
            <if test="deptApproveUserId != null "> and t1.dept_approve_user_id = #{deptApproveUserId}</if>
            <if test="deptApproveTime != null "> and t1.dept_approve_time = #{deptApproveTime}</if>
            <if test="specialityDepartmentReviewerId != null "> and t1.speciality_department_reviewer_id = #{specialityDepartmentReviewerId}</if>
            <if test="specialityDepartmentReviewTime != null "> and t1.speciality_department_review_time = #{specialityDepartmentReviewTime}</if>
            <if test="problemLevel != null"> and t1.problem_level = #{problemLevel}</if>
            <if test="problemAttribute != null"> and t1.problem_attribute = #{problemAttribute}</if>
            <if test="problemCategory != null"> and t1.problem_category = #{problemCategory}</if>
            <if test="description != null  and description != ''"> and t1.description = #{description}</if>
            <if test="problemCause != null  and problemCause != ''"> and t1.problem_cause = #{problemCause}</if>
            <if test="isImmediatelyCorrected != null "> and t1.is_immediately_corrected = #{isImmediatelyCorrected}</if>
            <if test="rectificationMeasure != null  and rectificationMeasure != ''"> and rectification_measure = #{rectificationMeasure}</if>
            <if test="checkedDeptId != null "> and t1.checked_dept_id = #{checkedDeptId}</if>
            <if test="checkedPostId != null "> and t1.checked_post_id = #{checkedPostId}</if>
            <if test="rectificationResponsibleUserId != null "> and t1.rectification_responsible_user_id = #{rectificationResponsibleUserId}</if>
            <if test="rectificationPeriod != null "> and t1.rectification_period = #{rectificationPeriod}</if>
            <if test="isRectificationCompleted != null "> and t1.is_rectification_completed = #{isRectificationCompleted}</if>
            <if test="rectificationCompletionTime != null "> and t1.rectification_completion_time = #{rectificationCompletionTime}</if>
            <if test="rectificationCompletionImg != null  and rectificationCompletionImg != ''"> and t1.rectification_completion_img = #{rectificationCompletionImg}</if>
            <if test="reviewDepartmentReviewerId != null "> and t1.review_department_reviewer_id = #{reviewDepartmentReviewerId}</if>
            <if test="reviewDepartmentReviewerTime != null "> and t1.review_department_reviewer_time = #{reviewDepartmentReviewerTime}</if>
            <if test="reviewConclusion != null  and reviewConclusion != ''"> and t1.review_conclusion = #{reviewConclusion}</if>
            <if test="reason != null  and reason != ''"> and t1.reason = #{reason}</if>
            <if test="createUserId != null "> and t1.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 t1.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>
            <if test="problemIdList != null and problemIdList.size() > 0">
                and t1.id IN
                <foreach collection="problemIdList" item="item"  open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="checkedDeptIdList != null and checkedDeptIdList.size() > 0">
                and t1.checked_dept_id IN
                <foreach collection="checkedDeptIdList" item="item"  open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectPostCheckedProblemById" parameterType="Long" resultMap="PostCheckedProblemResult">
        <include refid="selectPostCheckedProblemVo"/>
        where t1.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="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">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>

            <if test="checkedDeptName != null and checkedDeptName != ''">checked_dept_name,</if>
            <if test="checkedPostName != null and checkedPostName != ''">checked_post_name,</if>
            <if test="rectificationResponsibleName != null and rectificationResponsibleName != ''">rectification_responsible_name,</if>
            <if test="deptApproveUserName != null and deptApproveUserName != ''">dept_approve_user_name,</if>
            <if test="specialityDepartmentReviewerName != null and specialityDepartmentReviewerName != ''">speciality_department_reviewer_name,</if>
            <if test="reviewDepartmentReviewerName != null and reviewDepartmentReviewerName != ''">review_department_reviewer_name,</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="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">#{problemLevel},</if>
            <if test="problemAttribute != null">#{problemAttribute},</if>
            <if test="problemCategory != null">#{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>

            <if test="checkedDeptName != null and checkedDeptName != ''">#{checkedDeptName},</if>
            <if test="checkedPostName != null and checkedPostName != ''">#{checkedPostName},</if>
            <if test="rectificationResponsibleName != null and rectificationResponsibleName != ''">#{rectificationResponsibleName},</if>
            <if test="deptApproveUserName != null and deptApproveUserName != ''">#{deptApproveUserName},</if>
            <if test="specialityDepartmentReviewerName != null and specialityDepartmentReviewerName != ''">#{specialityDepartmentReviewerName},</if>
            <if test="reviewDepartmentReviewerName != null and reviewDepartmentReviewerName != ''">#{reviewDepartmentReviewerName},</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="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">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>

            <if test="checkedDeptName != null and checkedDeptName != ''">checked_dept_name = #{checkedDeptName},</if>
            <if test="checkedPostName != null and checkedPostName != ''">checked_post_name = #{checkedPostName},</if>
            <if test="rectificationResponsibleName != null and rectificationResponsibleName != ''">rectification_responsible_name = #{rectificationResponsibleName},</if>
            <if test="deptApproveUserName != null and deptApproveUserName != ''">dept_approve_user_name = #{deptApproveUserName},</if>
            <if test="specialityDepartmentReviewerName != null and specialityDepartmentReviewerName != ''">speciality_department_reviewer_name = #{specialityDepartmentReviewerName},</if>
            <if test="reviewDepartmentReviewerName != null and reviewDepartmentReviewerName != ''">review_department_reviewer_name = #{reviewDepartmentReviewerName},</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 SUM(COUNT(0))
        FROM "public"."sys_post" a
        WHERE a."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 a.status = 3
        and ppi."status" = #{status}
        <!-- 开始时间检索 -->
        <if test="beginTime != null and beginTime != ''">
            AND TO_DATE(ppi.start_time, 'yyyy-mm-dd') &gt;= TO_DATE(#{beginTime}, 'yyyy-mm-dd')
        </if>
        <!-- 结束时间检索 -->
        <if test="endTime != null and endTime != ''">
            AND TO_DATE(ppi.start_time, 'yyyy-mm-dd') &lt;= TO_DATE(#{endTime}, 'yyyy-mm-dd')
        </if>
    </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>

    <select id="getQuarterStatistics" resultType="java.util.Map">
        SELECT "QUARTER",IFNULL(sum(num), '0') "num"
        FROM (SELECT t."QUARTER", (t."sum" - t."two" - t."one") / "total" AS "num"
              FROM (SELECT EXTRACT(
                                   QUARTER
                                   FROM
                                   pp."create_time"
                               )       "QUARTER",
                           sum(pr."full_score"),
                           COUNT(0)    total,
                           SUM(CASE
                               pp."problem_level"
                                   WHEN 2 THEN pr."problem_level_two"
                                   ELSE '0'
                               END) AS "two",
                           sum(CASE
                               pp."problem_level"
                                   WHEN 1 THEN pr."problem_level_one"
                                   ELSE '0'
                               END) AS "one"
                    FROM "public"."post_checked_problem" pp
                             INNER JOIN "public"."post_assessment_rules" pr ON
                                pp."duty_type" = pr."duty_type"
                            AND pr."type" = 1
                    WHERE to_date(pp."create_time", 'YYYY') = to_date(#{year}, 'YYYY')
                    GROUP BY EXTRACT(
                                     QUARTER
                                     FROM
                                     pp."create_time"
                                 )) t
              union all
              SELECT EXTRACT(
                             QUARTER
                             FROM
                             "create_time"
                         ) AS                        "QUARTER",
                     sum("by_dept_score") / count(0) "num"

              FROM "public"."post_assessment_rating_other_info"
              WHERE to_date("create_time", 'YYYY') = to_date(#{year}, 'YYYY')
              GROUP BY EXTRACT(
                               QUARTER
                               FROM
                               "create_time"
                           )) a
        GROUP BY a."QUARTER"
    </select>


    <select id="getYearStatistics" resultType="java.util.Map">
        SELECT "YEAR",IFNULL(sum(num), '0') "num"
        FROM (SELECT t."YEAR", (t."sum" - t."two" - t."one") / "total" AS "num"
              FROM (SELECT EXTRACT(
                                   YEAR
                                   FROM
                                   pp."create_time"
                               )       "YEAR",
                           sum(pr."full_score"),
                           COUNT(0)    total,
                           SUM(CASE
                               pp."problem_level"
                                   WHEN 2 THEN pr."problem_level_two"
                                   ELSE '0'
                               END) AS "two",
                           sum(CASE
                               pp."problem_level"
                                   WHEN 1 THEN pr."problem_level_one"
                                   ELSE '0'
                               END) AS "one"
                    FROM "public"."post_checked_problem" pp
                             INNER JOIN "public"."post_assessment_rules" pr ON
                                pp."duty_type" = pr."duty_type"
                            AND pr."type" = 1
                    GROUP BY EXTRACT(
                                     YEAR
                                     FROM
                                     pp."create_time"
                                 )) t
              union all
              SELECT EXTRACT(
                             YEAR
                             FROM
                             "create_time"
                         ) AS                        "YEAR",
                     sum("by_dept_score") / count(0) "num"

              FROM "public"."post_assessment_rating_other_info"
              GROUP BY EXTRACT(
                               YEAR
                               FROM
                               "create_time"
                           )) a
        GROUP BY a."YEAR"
    </select>

    <select id="getMonthStatistics" resultType="java.util.Map">
        SELECT "MONTH", IFNULL(sum(num), '0') "num"
        FROM (SELECT t."MONTH", (t."sum" - t."two" - t."one") / "total" AS "num"
              FROM (SELECT EXTRACT(
                                   MONTH
                                   FROM
                                   pp."create_time"
                               )       "MONTH",
                           sum(pr."full_score"),
                           COUNT(0)    total,
                           SUM(CASE
                               pp."problem_level"
                                   WHEN 2 THEN pr."problem_level_two"
                                   ELSE '0'
                               END) AS "two",
                           sum(CASE
                               pp."problem_level"
                                   WHEN 1 THEN pr."problem_level_one"
                                   ELSE '0'
                               END) AS "one"
                    FROM "public"."post_checked_problem" pp
                             INNER JOIN "public"."post_assessment_rules" pr ON
                                pp."duty_type" = pr."duty_type"
                            AND pr."type" = 1
                    WHERE to_date(pp."create_time", 'YYYY') = to_date(#{year}, 'YYYY')
                    GROUP BY EXTRACT(
                                     MONTH
                                     FROM
                                     pp."create_time"
                                 )) t
              union all
              SELECT EXTRACT(
                             MONTH
                             FROM
                             "create_time"
                         ) AS                        "MONTH",
                     sum("by_dept_score") / count(0) "num"

              FROM "public"."post_assessment_rating_other_info"
              WHERE to_date("create_time", 'YYYY') = to_date(#{year}, 'YYYY')
              GROUP BY EXTRACT(
                               MONTH
                               FROM
                               "create_time"
                           )) a
        GROUP BY a."MONTH"
    </select>

    <select id="getYearStatisticsV2" resultType="java.util.Map">
        SELECT
            tools."type_view" AS "YEAR",
            ROUND(IFNULL(t1."num", 0), 2)  AS "num"
        FROM post_tools tools
        LEFT JOIN (
            SELECT "YEAR",IFNULL(sum(num), '0') "num"
            FROM (SELECT t."YEAR", (t."sum" - t."two" - t."one") / "total" AS "num"
                  FROM (SELECT EXTRACT(
                                       YEAR
                                       FROM
                                       pp."create_time"
                                   )       "YEAR",
                               sum(pr."full_score"),
                               COUNT(0)    total,
                               SUM(CASE
                                   pp."problem_level"
                                       WHEN 2 THEN pr."problem_level_two"
                                       ELSE '0'
                                   END) AS "two",
                               sum(CASE
                                   pp."problem_level"
                                       WHEN 1 THEN pr."problem_level_one"
                                       ELSE '0'
                                   END) AS "one"
                        FROM "public"."post_checked_problem" pp
                                 INNER JOIN "public"."post_assessment_rules" pr ON
                                    pp."duty_type" = pr."duty_type"
                                AND pr."type" = 1
                        GROUP BY EXTRACT(
                                         YEAR
                                         FROM
                                         pp."create_time"
                                     )) t
                  union all
                  SELECT EXTRACT(
                                 YEAR
                                 FROM
                                 zu."start_time"
                             ) AS                        "YEAR",
                         sum("by_dept_score") / count(0) "num"

                  FROM "public"."post_assessment_rating_other_info"  z LEFT JOIN "public"."post_assessment_rating_other" zu ON z."rating_other_id"  = zu."id"
                  GROUP BY EXTRACT(
                                   YEAR
                                   FROM
                                   zu."start_time"
                               )) a
            GROUP BY a."YEAR"
        )t1 ON t1."YEAR"  = tools."type_view"
        WHERE tools."type" = 1
        AND tools."type_view" >= #{beginYear}
        AND tools."type_view" <![CDATA[<= ]]> #{endYear}
        ORDER BY tools."type_view" DESC
    </select>

    <select id="getQuarterStatisticsV2" resultType="java.util.Map">
        WITH  a as(SELECT
                       COUNT(0) AS "num",
                       a."checked_dept_id"
                   FROM
                       "public"."post_checked_problem" a
                   WHERE
                           EXTRACT(
                                   YEAR
                                   FROM
                                   a."create_time"
                               ) = #{year}
                   GROUP BY
                       a."checked_dept_id")
        SELECT t1."dept_name" as "deptName",IFNULL ("num",'0') as "num" from "public"."sys_dept" t1 LEFT JOIN a ON t1."dept_id" = a."checked_dept_id" WHERE t1."type"  = #{type}
    </select>

    <select id="getMonthStatisticsV2" resultType="java.util.Map">
        SELECT
            tools."type_view" AS "MONTH",
            IFNULL (round(t1."num", 2),0) AS "num"
        FROM post_tools tools
        LEFT JOIN (
            SELECT
                "MONTH",
                IFNULL(sum(num), '0') "num"
            FROM (SELECT t."MONTH", (t."sum" - t."two" - t."one") / "total" AS "num"
                  FROM (SELECT EXTRACT(
                                       MONTH
                                       FROM
                                       pp."create_time"
                                   )       "MONTH",
                               sum(pr."full_score"),
                               COUNT(0)    total,
                               SUM(CASE
                                   pp."problem_level"
                                       WHEN 2 THEN pr."problem_level_two"
                                       ELSE '0'
                                   END) AS "two",
                               sum(CASE
                                   pp."problem_level"
                                       WHEN 1 THEN pr."problem_level_one"
                                       ELSE '0'
                                   END) AS "one"
                        FROM "public"."post_checked_problem" pp
                                 INNER JOIN "public"."post_assessment_rules" pr ON
                                    pp."duty_type" = pr."duty_type"
                                AND pr."type" = 1
                        WHERE to_date(pp."create_time", 'YYYY') = to_date(#{year}, 'YYYY')
                        GROUP BY EXTRACT(
                                         MONTH
                                         FROM
                                         pp."create_time"
                                     )) t
                  union all
                  SELECT EXTRACT(
                                 MONTH
                                 FROM
                                 zu."start_time"
                             ) AS                        "MONTH",
                         sum("by_dept_score") / count(0) "num"

                  FROM "public"."post_assessment_rating_other_info"  z LEFT JOIN "public"."post_assessment_rating_other" zu ON z."rating_other_id"  = zu."id"
                  WHERE to_date(zu."start_time", 'YYYY') = to_date(#{year}, 'YYYY')
                  GROUP BY EXTRACT(
                                   MONTH
                                   FROM
                                   zu."start_time"
                               )) a
            GROUP BY a."MONTH"
        )t1 ON t1."MONTH" = tools."type_view"
        WHERE tools."type" = 3
        ORDER BY tools."type_view"
    </select>

    <select id="selectAllMans" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_users"
    </select>

    <select id="selectAllMansByDeptType" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_users" p
        LEFT JOIN "public"."sys_dept" d ON  p."dept_id" = d."dept_id"
        WHERE d."type" = #{type}
    </select>

    <select id="selectAllKeyMans" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_users" p
        LEFT JOIN "public"."sys_user_post" sup ON p."user_id" = sup."user_id"
        LEFT JOIN "public"."sys_post" d ON  sup."post_id"  = d."post_id"
        LEFT JOIN "public"."sys_dept" dp ON  p."dept_id" = dp."dept_id"
        WHERE d."important_type" = 1
        AND dp."type" = #{type}
    </select>

    <select id="selectPostTolat" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_post"
    </select>

    <select id="seletctPostTolatByDeptType" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_post" sp
        LEFT JOIN "public"."sys_dept" dp ON sp."dept_id" = dp."dept_id"
        WHERE dp."type" = #{type}
    </select>

    <select id="seletctKeyPostTolat" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_post" sp
        LEFT JOIN "public"."sys_dept" dp ON sp."dept_id" = dp."dept_id"
        WHERE dp."type" = #{type}
        AND sp."important_type" = 1
    </select>

    <select id="selectCheaAllMans" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_users" a
        WHERE a."dept_id" IN (SELECT "checked_dept_id" FROM "public"."post_checked_problem" WHERE "create_time" LIKE concat(#{year}, '%'))
    </select>

    <select id="selectCheatAllMansByDeptType" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_users" p
        LEFT JOIN "public"."sys_dept" d ON  p."dept_id" = d."dept_id"
        WHERE d."type" = #{type} and p."dept_id" IN (SELECT "checked_dept_id" FROM "public"."post_checked_problem" WHERE "create_time" LIKE concat(#{year}, '%'))
    </select>

    <select id="selectCheatAllKeyMans" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_users" p
        LEFT JOIN "public"."sys_user_post" sup ON p."user_id" = sup."user_id"
        LEFT JOIN "public"."sys_post" d ON  sup."post_id"  = d."post_id"
        LEFT JOIN "public"."sys_dept" dp ON  p."dept_id" = dp."dept_id"
        WHERE d."important_type" = '1'
        AND dp."type" =  #{type}  and p."dept_id" IN (SELECT "checked_dept_id" FROM "public"."post_checked_problem" WHERE "create_time" LIKE concat(#{year}, '%'))
    </select>

    <select id="selectCheatPostTolat" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_post" a
        WHERE a."dept_id" IN (SELECT "checked_dept_id" FROM "public"."post_checked_problem" WHERE "create_time" LIKE concat(#{year}, '%'))
    </select>

    <select id="seletctCheatPostTolatByDeptType" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_post" sp
        LEFT JOIN "public"."sys_dept" dp ON sp."dept_id" = dp."dept_id"
        INNER JOIN "public"."post_checked_problem" b ON sp."dept_id" = b."checked_dept_id"
        WHERE dp."type" = #{type}
        AND  sp."dept_id" IN (SELECT "checked_dept_id" FROM "public"."post_checked_problem" WHERE "create_time" LIKE concat(#{year}, '%'))
    </select>

    <select id="seletctCheatKeyPostTolat" resultType="int">
        SELECT COUNT(0)
        FROM "public"."sys_post" sp
        LEFT JOIN "public"."sys_dept" dp ON sp."dept_id" = dp."dept_id"
        WHERE dp."type" = #{type}
        AND sp."important_type" = 1
        AND  sp."dept_id" IN (SELECT "checked_dept_id" FROM "public"."post_checked_problem" WHERE "create_time" LIKE concat(#{year}, '%'))
    </select>
</mapper>
