<?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.zcxk.evaluation.dao.EvaluationResultMapper" >

    <sql id="evaluation_result_list" >
           a.ID,
           a.TENANT_ID,
           a.COMPANY_ORG_ID,
           a.DEPT_ORG_ID,
           a.EVALUATION_BY,
           a.EVALUATION_BE,
           a.EVALUATION_BE_USER_ID,
           a.STATE,
           a.CYCLE,
           a.YEAR,
           a.MONTH,
           a.EVALUATION_GRADE,
           a.VALUE,
           a.DATE_START,
           a.DATE_END,
           a.DATE_EVALUATION
    </sql>

    <sql id="permissonSql">
        <if test="dto.permissonType == 5 or dto.permissonType == 2">
            and ( a.DEPT_ORG_ID in
            <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
            or
            a.COMPANY_ORG_ID in
            <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
            )
        </if>
        <if test="dto.permissonType == 4 or dto.permissonType == 3">
            and a.DEPT_ORG_ID in
            <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
        </if>
        <if test="dto.permissonType == 1">
            and a.COMPANY_ORG_ID in
            <foreach collection="dto.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
            and (a.DEPT_ORG_ID is null or a.DEPT_ORG_ID = 0)
        </if>
    </sql>

    <insert id="insertEvaluationResult" useGeneratedKeys="true"  keyProperty="id" parameterType="com.zcxk.evaluation.entity.EvaluationResultEntity">
        insert sc_evaluation_result (TENANT_ID,COMPANY_ORG_ID,DEPT_ORG_ID,EVALUATION_BE,EVALUATION_BE_USER_ID,`STATE`,`CYCLE`,`YEAR`,`MONTH`,EVALUATION_GRADE,
                `VALUE`,DATE_START,DATE_END,DATE_EVALUATION,STATUS,DATE_CREATE,CREATE_BY,DATE_UPDATE,UPDATE_BY)
        values
            (
            #{item.tenantId}, #{item.companyOrgId}, #{item.deptOrgId}, #{item.evaluationBe}, #{item.evaluationBeUserId},#{item.state},
            #{item.cycle}, #{item.year}, #{item.month}, #{item.evaluationGrade}, #{item.value},
            #{item.dateStart}, #{item.dateEnd}, #{item.dateEvaluation}, #{item.status}, #{item.dateCreate},
            #{item.createBy}, #{item.dateUpdate}, #{item.updateBy})
    </insert>

    <select id="findPage" resultType="com.zcxk.evaluation.vo.EvaluationResultVo">
        select
        <include refid="evaluation_result_list"></include>
        from sc_evaluation_result a
        <where>
            a.tenant_id = #{dto.tenantId}
            <if test="dto.userType != null and dto.userType != -999 and dto.userType != -9999 and  dto.programItems != null and dto.programItems.size() > 0">
                <include refid="permissonSql"></include>
            </if>
            <if test="dto.userName != null">
                and a.EVALUATION_BE like CONCAT('%',#{dto.userName,jdbcType=VARCHAR},'%')
            </if>
            <if test="dto.companyOrgId != null">
                and a.COMPANY_ORG_ID = #{dto.companyOrgId}
            </if>
            <if test="dto.deptOrgId != null">
                and a.DEPT_ORG_ID = #{dto.deptOrgId}
            </if>
            <if test="dto.sort == 1 and dto.state != null">
                and a.`STATE` = #{dto.state}
            </if>
            <if test="dto.sort == 2">
                and a.`STATE` = 1
            </if>
            and a.`YEAR`  = #{dto.year}
            and a.`cycle` = #{dto.cycle}
            <if test="dto.cycle != 2">
                and a.`month` = #{dto.month}
            </if>
            and a.status = 1
        </where>
        <if test="dto.sort == 1">
            order by a.date_start desc,a.`state`
        </if>
        <if test="dto.sort == 2">
            order by a.date_start desc,a.`VALUE` desc
        </if>
    </select>

    <select id="findEvaluationState" resultType="java.lang.Integer">
        select `state` from sc_evaluation_result where id =#{evaluationResultId}
    </select>

    <update id="updateEvaluationScore">
        update  sc_evaluation_result
        set `VALUE` = #{score}, `state` = 1, EVALUATION_GRADE = #{evaluationGrade},UPDATE_BY = #{updateBy},
            DATE_UPDATE = #{dateUpdate},DATE_EVALUATION =#{dateEvaluation},EVALUATION_BY =#{evaluationBy}
        where id = #{evaluationResultId}
    </update>
    
    <select id="findRanking" resultType="com.zcxk.evaluation.vo.EvaluationReportRankingVo">
        select a.EVALUATION_BE,a.VALUE,a.EVALUATION_GRADE,t.COMPLETE_COUNT,t.COMPLETION_RATE,t.DELAY_COMPLETE_COUNT,t.TOTAL_COUNT
        from sc_evaluation_result a
        inner join sc_evaluation_result_task t on a.id = t.EVALUATION_RESULT_ID
        <where>
            a.status = 1 and a.tenant_id = #{dto.tenantId} and t.status = 1
            <if test="dto.userType != null and dto.userType != -999 and dto.userType != -9999 and  dto.programItems != null and dto.programItems.size() > 0">
                <include refid="permissonSql"></include>
            </if>
            <if test="dto.companyOrgId != null">
                and a.COMPANY_ORG_ID = #{dto.companyOrgId}
            </if>
            <if test="dto.deptOrgId != null">
                and a.DEPT_ORG_ID = #{dto.deptOrgId}
            </if>
            and a.`STATE` = 1
            and a.`cycle` = #{dto.cycle}
            and a.`YEAR`  = #{dto.year}
            <if test="dto.cycle != 2">
                and a.`month` = #{dto.month}
            </if>
        </where>
        order by a.VALUE desc
    </select>

    <select id="findGradePercentage" resultType="com.zcxk.evaluation.vo.EvaluationGradePercentageVo">
        select a.EVALUATION_GRADE, count(1) as "percentage"
        from sc_evaluation_result a
        <where>
            a.status = 1 and a.tenant_id = #{dto.tenantId}
                <if test="dto.userType != null and dto.userType != -999 and dto.userType != -9999 and  dto.programItems != null and dto.programItems.size() > 0">
                    <include refid="permissonSql"></include>
                </if>
                <if test="dto.companyOrgId != null">
                    and a.COMPANY_ORG_ID = #{dto.companyOrgId}
                </if>
                <if test="dto.deptOrgId != null">
                    and a.DEPT_ORG_ID = #{dto.deptOrgId}
                </if>
                and a.`STATE` = 1
                and a.`cycle` = #{dto.cycle}
                and a.`YEAR`  = #{dto.year}
                <if test="dto.cycle != 2">
                    and a.`month` = #{dto.month}
                </if>
        </where>
        group by a.EVALUATION_GRADE
    </select>

    <select id="countQualifiedNumber" resultType="com.zcxk.evaluation.vo.EvaluationQualifiedNumberVo">
        select a.month, count(1) as "qualifiedNumber"
        from sc_evaluation_result a
        <where>
            a.status = 1 and a.tenant_id = #{dto.tenantId}
            <if test="dto.userType != null and dto.userType != -999 and dto.userType != -9999 and  dto.programItems != null and dto.programItems.size() > 0">
                <include refid="permissonSql"></include>
            </if>
            <if test="dto.companyOrgId != null">
                and a.COMPANY_ORG_ID = #{dto.companyOrgId}
            </if>
            <if test="dto.deptOrgId != null">
                and a.DEPT_ORG_ID = #{dto.deptOrgId}
            </if>
            and a.`STATE` = 1
            and a.`cycle` = #{dto.cycle}
            and a.`YEAR`  = #{dto.year}
            <if test="dto.months != null">
                and a.`MONTH` in
                <foreach collection="dto.months" item="month" open="(" close=")" separator=",">
                    #{month}
                </foreach>
            </if>
            and a.VALUE  <![CDATA[ >= ]]>  #{qualifiedValue}
        </where>
        group by a.`YEAR`,a.`MONTH`
    </select>

    <select id="countPerformanceAvg" resultType="com.zcxk.evaluation.vo.EvaluationPerformanceAvgVo">
        select a.month, ROUND( SUM(a.value) / count(1), 2 ) AS "avg"
        from sc_evaluation_result a
        <where>
            a.status = 1 and a.tenant_id = #{dto.tenantId}
            <if test="dto.userType != null and dto.userType != -999 and dto.userType != -9999 and  dto.programItems != null and dto.programItems.size() > 0">
                <include refid="permissonSql"></include>
            </if>
            <if test="dto.companyOrgId != null">
                and a.COMPANY_ORG_ID = #{dto.companyOrgId}
            </if>
            <if test="dto.deptOrgId != null">
                and a.DEPT_ORG_ID = #{dto.deptOrgId}
            </if>
            and a.`STATE` = 1
            and a.`cycle` = #{dto.cycle}
            and a.`YEAR`  = #{dto.year}
            <if test="dto.months != null">
                and a.`MONTH` in
                <foreach collection="dto.months" item="month" open="(" close=")" separator=",">
                    #{month}
                </foreach>
            </if>
        </where>
        group by a.`YEAR`,a.`MONTH`
    </select>

    <select id="listEvaluationResult" resultType="com.zcxk.evaluation.vo.EvaluationResultVo">
        select
        <include refid="evaluation_result_list"></include>
        from sc_evaluation_result a
        <where>
            <if test="dto.ids != null">
                and a.id in
                <foreach collection="dto.ids" item="resultId" open="(" close=")" separator=",">
                    #{resultId}
                </foreach>
            </if>
            <if test="dto.ids == null">
                a.status = 1 and a.tenant_id = #{dto.tenantId}
                <if test="dto.userType != null and dto.userType != -999 and dto.userType != -9999 and  dto.programItems != null and dto.programItems.size() > 0">
                    <include refid="permissonSql"></include>
                </if>

                <if test="dto.userName != null">
                    and a.EVALUATION_BE like CONCAT('%',#{dto.userName,jdbcType=VARCHAR},'%')
                </if>
                <if test="dto.companyOrgId != null">
                    and a.COMPANY_ORG_ID = #{dto.companyOrgId}
                </if>
                <if test="dto.deptOrgId != null">
                    and a.DEPT_ORG_ID = #{dto.deptOrgId}
                </if>
                <if test="dto.sort == 1 and dto.state != null">
                    and a.`STATE` = #{dto.state}
                </if>
                <if test="dto.sort == 2">
                    and a.`STATE` = 1
                </if>
                and a.`cycle` = #{dto.cycle}
                and a.`YEAR`  = #{dto.year}
                <if test="dto.cycle != 2">
                    and a.`month` = #{dto.month}
                </if>
            </if>
        </where>
        <if test="dto.sort == 1">
            order by a.date_start desc,a.`state`
        </if>
        <if test="dto.sort == 2">
            order by a.date_start desc,a.`VALUE` desc
        </if>
    </select>
</mapper>