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

    <insert id="addEvalSummary">
        insert into pj_offline(type, file_path, evaluate_title, year_and_month, upload_time, update_time)
        values (#{evaluateDept}, #{pathName}, #{evaluateTitle}, #{evaluateMonth}, now(),now())
    </insert>
    <delete id="deleteOffEvaluateById">
        update pj_offline set is_active = 0 where offline_id = #{id}
    </delete>
    <select id="getEvalSummaryList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OutSourceBean">
        select
        per.evaluate_id as id,
        ped.details_id as detailsId,
        per.template_id as templateId,
        poi.name as evaluateDept,
        per.evaluate_name as evaluateTitle,
        per.year_and_month as evaluateMonth,
        per.create_time as createTime,
        r.proNum as projectNum,
        r.subNum as outsourcerNum,
        ifnull(GROUP_CONCAT(ped.post_id),'') as deptId,
        check_one as isApprove,
        IF(rr.num > 0,1,if(rr.num = 0 and rr.failNum = 0,0,2)) as isAudit,
        if(m.num = m.total,1,if(m.num > 0,2,0)) as status,
        rr.rejectReason as rejectReason
        from pj_evaluate_record per
        left join (
        select
        evaluate_id,
        count(distinct sub_id) as subNum,
        count(distinct pro_id) as proNum
        from pj_evaluate_sub pes where pes.is_active = 1
        group by evaluate_id
        )r on per.evaluate_id = r.evaluate_id
        left join pj_template_config pt on pt.config_id = per.template_id and pt.is_active = 1
        left join pm_org_info poi on per.dept_id = poi.id and poi.status = 1
        left join pj_evaluate_details ped on ped.evaluate_id = per.evaluate_id
        left join (
            select
                count(if(check_three > 0,1,null)) as num,
                count(1) as total,
                evaluate_id
            from pj_evaluate_details
            group by evaluate_id
        ) m on m.evaluate_id = per.evaluate_id
        left join(
        SELECT
        count(IF(check_three is null or check_three = 0 or check_three = '',1,null)) as num,
        GROUP_CONCAT(DISTINCT IF(reject_three is null or reject_three = '',null,reject_three)) as rejectReason,
        count(if(check_three = 2,1,null)) as failNum,
        evaluate_id
        from pj_evaluate_result
        GROUP BY evaluate_id
        )rr on rr.evaluate_id = per.evaluate_id
        where per.is_active = 1
        <if test="evaluateDeptId != null and evaluateDeptId != ''">
            and per.dept_id = #{evaluateDeptId}
        </if>
        <if test="searchDate != null and searchDate != ''">
            and per.year_and_month = #{searchDate}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and per.evaluate_name like concat('%', #{keyWord}, '%' )
        </if>
        group by per.evaluate_id
        <if test="status != null and status != ''">
            having status = #{status}
        </if>
        <if test="type == 'view'">
            having status = 1
        </if>

        order by MAX(per.create_time) desc
    </select>
    <select id="getOfflineSummaryList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OutSourceBean">
        select
            offline_id as id,
            poi.name as evaluateDept,
            year_and_month as evaluateMonth,
            evaluate_title as evaluateTitle,
            file_path as filePath,
            type as type,
            upload_time as createTime
        from pj_offline po
        left join pm_org_info poi on po.type = poi.id
        where po.is_active = 1
        <if test="evaluateDeptId != null and evaluateDeptId != ''">
            and type = #{evaluateDeptId}
        </if>
        <if test="evaluateTitle != null and evaluateTitle != ''">
            and evaluateTitle like concat('%', #{evaluateTitle}, '%' )
        </if>

    </select>
    <select id="dedicatedEvalSummaryList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.OutSourceBean">
        select
        per.evaluate_id as id,
        ped.details_id as detailsId,
        per.template_id as templateId,
        poi.name as evaluateDept,
        per.evaluate_name as evaluateTitle,
        per.year_and_month as evaluateMonth,
        per.create_time as createTime,
        r.proNum as projectNum,
        r.subNum as outsourcerNum,
        ifnull(GROUP_CONCAT(ped.post_id),'') as deptId,
        check_one as isApprove,
        IF(rr.num > 0,1,if(rr.num = 0 and rr.failNum = 0,0,2)) as isAudit,
        if(m.num = m.total,1,if(m.num > 0,2,0)) as status,
        rr.rejectReason as rejectReason
        from pj_evaluate_record per
        left join (
        select
        evaluate_id,
        count(distinct sub_id) as subNum,
        count(distinct pro_id) as proNum
        from pj_evaluate_sub pes where pes.is_active = 1
        group by evaluate_id
        )r on per.evaluate_id = r.evaluate_id
        left join pj_template_config pt on pt.config_id = per.template_id and pt.is_active = 1
        left join pm_org_info poi on per.dept_id = poi.id and poi.status = 1
        left join pj_evaluate_details ped on ped.evaluate_id = per.evaluate_id
        left join (
        select
        count(if(check_two > 0,1,null)) as num,
        count(1) as total,
        evaluate_id
        from pj_evaluate_details
        group by evaluate_id
        ) m on m.evaluate_id = per.evaluate_id
        left join(
        SELECT
        count(IF(check_two is null or check_two = 0 or check_two = '',1,null)) as num,
        GROUP_CONCAT(DISTINCT IF(reject_two is null or reject_two = '',null,reject_two)) as rejectReason,
        count(if(check_two = 2,1,null)) as failNum,
        evaluate_id
        from pj_evaluate_result
        GROUP BY evaluate_id
        )rr on rr.evaluate_id = per.evaluate_id
        where per.is_active = 1
        <if test="evaluateDeptId != null and evaluateDeptId != ''">
            and per.dept_id = #{evaluateDeptId}
        </if>
        <if test="searchDate != null and searchDate != ''">
            and per.year_and_month = #{searchDate}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and per.evaluate_name like concat('%', #{keyWord}, '%' )
        </if>
        group by per.evaluate_id
        <if test="status != null and status != ''">
            having status = #{status}
        </if>
        <if test="type == 'view'">
            having status = 1
        </if>

        order by MAX(per.create_time) desc
    </select>
</mapper>
