<?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.bonus.gs.sub.evaluate.evaluate.dao.TeamEvaluateDao">
    <!--评价/审核/重新评价-->
    <update id="evaluateData">
        <if test="operType == 1 or operType == 3">
            UPDATE pt_team_exit SET manager = #{manager},level = #{level},mark = #{mark},ev_status = '1',check_status = '0',
            ev_user_id = #{evUserId},ev_user_name = #{evUserName},reject_reason = null WHERE id = #{id}
        </if>
        <if test="operType == 2">
            <if test="roleId == 5">
                UPDATE pt_team_exit SET check_status_two = #{checkStatus},reject_reason_two = #{rejectReason},
                check_user_id_two = #{checkUserId},check_user_name_two = #{checkUserName}, check_status = #{checkStatus}  WHERE id = #{id}
            </if>
            <if test="roleId == 6">
                UPDATE pt_team_exit SET check_status = #{checkStatus},reject_reason =
                #{rejectReason},
                <if test="checkStatus == 2 || checkStatus == '2'">
                    reject_reason_two = '',
                    check_status_two = null,
                </if>
                <if test="checkStatus == 1 || checkStatus == '1'">
                    reject_reason_two = '',
                    check_status_two = 0,
                </if>
                check_user_id = #{checkUserId},check_user_name = #{checkUserName} WHERE id = #{id}
            </if>

        </if>
    </update>

    <!--operType 1.评价列表 2.审核评价列表 3.所有评价列表-->
    <select id="findByPage" parameterType="com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean"
            resultType="com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean">
        SELECT
        pte.id,
        pte.team_name AS teamName,
        pte.user_name AS userName,
        pte.phone AS phone,
        pte.sub_name AS subName,
        pte.pro_name AS proName,
        pte.manager,
        DATE_FORMAT(pte.evaluate_time,'%Y-%m-%d %H:%i:%s') AS evaluateTime,
        CASE pte.level WHEN '1' THEN '优' WHEN '2' THEN '良' WHEN '3' THEN '中' WHEN '4' THEN '差' END AS level,
        CASE pte.ev_status WHEN '0' THEN '待评价' WHEN '1' THEN '已评价' ELSE '待评价' END AS evStatus,
        CASE pte.check_status WHEN '0' THEN '待审核' WHEN '1' THEN '已通过' WHEN '2' THEN '评价驳回' ELSE '待审核' END
        AS checkStatus,
        pte.reject_reason AS rejectReason,
        pu.TELPHONE AS sendPhone,
        pte.parent_id AS parentId
        FROM pt_team_exit pte
        LEFT JOIN pm_user pu ON pte.ev_user_id = pu.ID
        <where>
            <if test="param.operType == 1">
                AND pte.parent_id = #{param.parentId}
            </if>
            <if test="param.operType == 2">
                AND pte.ev_status = '1'
            </if>
            <if test="param.keyWord!=null and param.keyWord!=''">
                AND (
                INSTR(pte.team_name,#{param.keyWord}) > 0 OR
                INSTR(pte.phone,#{param.keyWord}) > 0 OR
                INSTR(pte.user_name,#{param.keyWord}) > 0 OR
                INSTR(pte.sub_name,#{param.keyWord}) > 0 OR
                INSTR(pte.pro_name,#{param.keyWord}) > 0 OR
                INSTR(pte.manager,#{param.keyWord}) > 0
                )
            </if>
            <if test="param.evStatus == 1">
                AND (pte.ev_status = '0' OR pte.ev_status IS NULL)
            </if>
   		    <if test="param.evStatus == 2">
                AND pte.ev_status = '1' AND pte.check_status != '2'
            </if>
            <if test="param.evStatus == 3">
                AND pte.check_status = '2'
            </if>
            <if test="param.checkStatus !=null and param.checkStatus!=''">
                AND pte.check_status = #{param.checkStatus}
            </if>
            <if test="param.startDate!=null and param.startDate!='' and param.endDate!=null and param.endDate!=''">
                AND DATE_FORMAT(pte.evaluate_time, '%Y-%m-%d') BETWEEN #{param.startDate} AND #{param.endDate}
            </if>
        </where>
    </select>
    <!--获取评价详情-->
    <select id="getEvaluateData" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean">
        SELECT pte.id,
            pte.manager AS manager,
            DATE_FORMAT(pte.evaluate_time,'%Y-%m-%d') AS evaluateTime,
            pte.level,
            pte.mark,
            pte.team_name AS teamName,
            pte.user_name AS userName,
   		    pte.phone AS phone,
   		    pte.sub_name AS subName,
   		    pte.pro_name AS proName,
   		    pte.manager
        FROM pt_team_exit pte
        WHERE pte.id = #{id}
    </select>
    <!--获取班组评价列表-->
    <select id="getExportExcelData" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean">
        SELECT
   		    pte.id,
   		    pte.team_name AS teamName,
   		    pte.user_name AS userName,
   		    pte.phone AS phone,
   		    pte.sub_name AS subName,
   		    pte.pro_name AS proName,
   		    pte.manager,
   		    DATE_FORMAT(pte.evaluate_time,'%Y-%m-%d %H:%i:%s') AS evaluateTime,
   		    CASE pte.level WHEN '1' THEN '优' WHEN '2' THEN '良' WHEN '3' THEN '中' WHEN '4' THEN '差' END AS level,
   		    CASE pte.ev_status WHEN '0' THEN '待评价' WHEN '1' THEN '已评价' ELSE '待评价' END AS evStatus,
   		    CASE pte.check_status WHEN '0' THEN '待审核' WHEN '1' THEN '已通过' WHEN '2' THEN '评价驳回' ELSE '待审核' END AS checkStatus,
   		    pte.reject_reason AS rejectReason
   		FROM pt_team_exit pte
   		<where>
            <if test="param.operType == 1">
               AND pte.parent_id = #{param.parentId}
            </if>
            <if test="param.operType == 2">
               AND pte.ev_status = '1'
            </if>
            <if test="param.keyWord!=null and param.keyWord!=''">
               AND (
                INSTR(pte.team_name,#{param.keyWord}) > 0 OR
                INSTR(pte.phone,#{param.keyWord}) > 0 OR
                INSTR(pte.user_name,#{param.keyWord}) > 0 OR
                INSTR(pte.sub_name,#{param.keyWord}) > 0 OR
                INSTR(pte.pro_name,#{param.keyWord}) > 0 OR
                INSTR(pte.manager,#{param.keyWord}) > 0
                )
            </if>
   		    <if test="param.evStatus == 1">
                AND (pte.ev_status = '0' OR pte.ev_status IS NULL)
            </if>
   		    <if test="param.evStatus == 2">
                AND pte.ev_status = '1' AND pte.check_status != '2'
            </if>
            <if test="param.evStatus == 3">
                AND pte.check_status = '2'
            </if>
   		    <if test="param.checkStatus !=null and param.checkStatus!=''">
                AND pte.check_status = #{param.checkStatus}
            </if>
   		    <if test="param.startDate!=null and param.startDate!='' and param.endDate!=null and param.endDate!=''">
                AND DATE_FORMAT(pte.evaluate_time, '%Y-%m-%d') BETWEEN #{param.startDate} AND #{param.endDate}
            </if>
        </where>
    </select>
    <!--获取项目经理的手机号-->
    <select id="getSendPhone" resultType="java.lang.String">
        SELECT GROUP_CONCAT(TELPHONE)
        FROM gs_exam.pm_user WHERE dept_id = #{parentId}
        GROUP BY dept_id
    </select>
    <select id="getTeamEvaluateList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean">
        SELECT
        pte.id,
        pte.team_name AS teamName,
        pte.user_name AS userName,
        pte.phone AS phone,
        pte.sub_name AS subName,
        pte.pro_name AS proName,
        pte.manager,
        su.phone AS sendPhone,
        DATE_FORMAT(pte.evaluate_time,'%Y-%m-%d %H:%i:%s') AS evaluateTime,
        CASE pte.level WHEN '1' THEN '优' WHEN '2' THEN '良' WHEN '3' THEN '中' WHEN '4' THEN '差' END AS level,
        CASE pte.ev_status WHEN '0' THEN '待评价' WHEN '1' THEN '已评价' ELSE '待评价' END AS evStatus,
        <if test="roleId == 6">
            case pte.check_status
            WHEN '0' THEN '待审核'
            WHEN '1' THEN '已通过'
            WHEN '2' THEN '评价驳回'
            ELSE '待审核'
            END AS checkStatus,
            pte.reject_reason AS rejectReason,
        </if>
        <if test="roleId == 5">
            case (if(pte.check_status = '1',pte.check_status_two,pte.check_status))
            WHEN '0' THEN '待审核'
            WHEN '1' THEN '已通过'
            WHEN '2' THEN '评价驳回'
            ELSE '待审核'
            END AS checkStatus,
            pte.reject_reason_two AS rejectReason,
        </if>
        <if test="roleId != 5 and roleId != 6">
            case (if(pte.check_status = '1',pte.check_status_two,pte.check_status))
            WHEN '0' THEN '待审核'
            WHEN '1' THEN '已通过'
            WHEN '2' THEN '评价驳回'
            ELSE '待审核'
            END AS checkStatus,
            IF(pte.reject_reason is not null and pte.reject_reason != '' , pte.reject_reason ,pte.reject_reason_two) AS rejectReason,
        </if>
        pte.check_status as status,
        pu.TELPHONE AS sendPhone,
        pte.parent_id AS parentId
        FROM pt_team_exit pte
        LEFT JOIN gs_exam.pm_user pu ON pte.ev_user_id = pu.ID
        LEFT JOIN pm_org_info poi ON poi.id = pte.parent_id
        LEFT JOIN pm_org_info poi1 ON poi1.id = poi.parent_id
        left join sys_user su on su.id = pte.ev_user_id
        <where>
            <if test="operType == 1">
                AND poi1.user_id = #{evUserId}
            </if>
            <if test="operType == 2">
                AND pte.ev_status = '1'
                <if test="roleId == 5">
                    and pte.check_status  is not null
                </if>
                <if test="roleId == 1">
                    and 1 = 1
                </if>
                <if test="roleId != 1 and  roleId != 5 and roleId != 6">
                    and pte.check_status = 10
                </if>
            </if>
            <if test="keyWord!=null and keyWord!=''">
                AND (
                INSTR(pte.team_name,#{keyWord}) > 0 OR
                INSTR(pte.phone,#{keyWord}) > 0 OR
                INSTR(pte.user_name,#{keyWord}) > 0 OR
                INSTR(pte.sub_name,#{keyWord}) > 0 OR
                INSTR(pte.pro_name,#{keyWord}) > 0 OR
                INSTR(pte.manager,#{keyWord}) > 0
                )
            </if>
            <if test="evStatus == 4">
                AND (pte.ev_status = '1'  and (check_status=0  or check_status_two=0 ))
            </if>
            <if test="evStatus == 6">
                AND (pte.ev_status = '1'  and (check_status=1  and check_status_two=1 ))
            </if>
            <if test="evStatus == 7">
                AND (pte.ev_status = '1'  and (check_status=2  or check_status_two=2 ))
            </if>
            <if test="evStatus == 1">
                AND (pte.ev_status = '0' OR pte.ev_status IS NULL)
            </if>
            <if test="evStatus == 2">
                AND pte.ev_status = '1' AND pte.check_status != '2'
            </if>
            <if test="evStatus == 3">
                AND pte.check_status = '2'
            </if>
            <if test="checkStatus !=null and checkStatus!=''">
                AND pte.check_status = #{checkStatus}
            </if>
            <if test="startDate!=null and startDate!='' and endDate!=null and endDate!=''">
                AND DATE_FORMAT(pte.evaluate_time, '%Y-%m-%d') BETWEEN #{startDate} AND #{endDate}
            </if>
        </where>
    </select>
</mapper>
