<?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.SetTemplateDao">
    <insert id="addTemplate" keyProperty="id" useGeneratedKeys="true">
        insert into pj_template_config (config_name, create_user_name, version, json, is_active)
        values (#{name}, #{createUser}, '1', #{jsonTxt}, '1')
    </insert>
    <insert id="addTemplateDetails" useGeneratedKeys="true" keyProperty="id">
        insert into pj_template (config_id, template_name, level, child_num, target_name,
                                 target_score,count_type,collect_type,is_active,version)
        values (#{templateId}, #{templateName},'1', #{childNum}, #{indexName},
                #{standardScore},#{total},#{collectType},'1',#{version})
    </insert>
    <insert id="addTwoLevelTemplateDetails">
        insert into pj_template (config_id, template_name, level, child_num, target_name,
                                 target_score,count_type,duty_dept,parent_id,is_active,version,method,standard)
        values (#{templateId}, #{templateName},'2', '0', #{subIndexName},
                #{subStandardScore},#{sub},#{responsibleDepartment},#{id},'1',#{version},#{subIndexDefinition},#{subIntegralStandard})
    </insert>
    <update id="updateTemplate">
        update pj_template_config
        set config_name = #{name},
            json = #{jsonTxt},
            version = version + 1
        where config_id = #{id}
    </update>
    <update id="setTemplatePass">
        update pj_template_config
        set is_enable = 1
        where is_active = '1'
    </update>
    <update id="changeEnable">
        update pj_template_config
        set is_enable = 0
        where config_id = #{configId}
    </update>
    <delete id="deleteTemplateById">
        update pj_template_config set is_active = '0' where config_id = #{id}
    </delete>

    <select id="getTemplateList" resultType="com.bonus.gs.sub.evaluate.evaluate.beans.SetTemplateBean">
        SELECT
        ptc.config_id as id,
        ptc.config_name as name,
        ptc.create_user_name as createUser,
        ptc.create_time as createTime,
        ptc.version as version,
        ptc.json as jsonTxt,
        ptc.is_enable as isEnable,
        ifnull(per.evaluate_id, 0) as isUse
        FROM
            pj_template_config ptc
        left join pj_evaluate_record per on ptc.config_id = per.template_id and per.is_active = '1'
        WHERE
            ptc.is_active = '1'
        <if test="name != null and name != ''">
            AND ptc.config_name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="createUser != null and createUser != ''">
            AND ptc.create_user_name LIKE CONCAT('%', #{createUser}, '%')
        </if>
        ORDER BY
        ptc.config_id DESC
    </select>
    <select id="checkIsExistTemplateName" resultType="java.lang.Integer">
        SELECT
            count(1)
        FROM
            pj_template_config
        WHERE
            config_name = #{name}
            AND is_active = '1'
        <if test="id != null">
            AND config_id != #{id}
        </if>
    </select>
    <select id="getResponsibleDepartmentSelect"
            resultType="com.bonus.gs.sub.evaluate.evaluate.beans.SetTemplateBean">
        select
            '0' as value,
            '项目部' as name
        union all
        select
            '-1' as value,
            '事业部' as name
         union all
        SELECT
            id as value,
            name
        from pm_org_info
        where org_type = '2' and status = '1' and level = '2'
    </select>
</mapper>
