<?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.lz.modules.sys.dao.app.ResultDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.sys.entity.app.ResultDetail">
        <id column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="type" property="type"/>
        <result column="target" property="target"/>
        <result column="key_result" property="keyResult"/>
        <result column="key_result_3_5" property="keyResult35"/>
        <result column="key_result_3_7" property="keyResult37"/>
        <result column="check_weight" property="checkWeight"/>
        <result column="check_result" property="checkResult"/>
        <result column="super_score" property="superScore"/>
        <result column="acquire_score" property="acquireScore"/>
        <result column="score_comment" property="scoreComment"/>
        <result column="record_id" property="recordId"/>
        <result column="staff_id" property="staffId"/>
        <result column="priority" property="priority"/>
        <result column="model_id" property="modelId"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, type AS type, target AS target, key_result AS keyResult, key_result_3_5 AS keyResult35, key_result_3_7 AS keyResult37, check_weight AS checkWeight, check_result AS checkResult, super_score AS superScore, acquire_score AS acquireScore, score_comment AS scoreComment, record_id AS recordId, staff_id AS staffId, priority AS priority, model_id AS modelId
    </sql>




    <select id="selectResultDetailById" resultType="ResultDetail" >
        select * from lz_result_detail  where id=#{id} and is_delete = 0 limit 1
    </select>


    <insert id="insertResultDetail" parameterType="ResultDetail" useGeneratedKeys="true" keyProperty="id" >
        insert into lz_result_detail(
        <if test="type != null">type, </if>
        <if test="target != null">target, </if>
        <if test="keyResult != null">key_result, </if>
        <if test="keyResult35 != null">key_result_3_5, </if>
        <if test="keyResult37 != null">key_result_3_7, </if>
        <if test="checkWeight != null">check_weight, </if>
        <if test="checkResult != null">check_result, </if>
        <if test="superScore != null">super_score, </if>
        <if test="acquireScore != null">acquire_score, </if>
        <if test="scoreComment != null">score_comment, </if>
        <if test="recordId != null">record_id, </if>
        <if test="staffId != null">staff_id, </if>
        <if test="priority != null">priority, </if>
        <if test="modelId != null">model_id, </if>
        is_delete,
        gmt_create,
        gmt_modified
        )values(
        <if test="type != null">#{ type}, </if>
        <if test="target != null">#{ target}, </if>
        <if test="keyResult != null">#{ keyResult}, </if>
        <if test="keyResult35 != null">#{ keyResult35}, </if>
        <if test="keyResult37 != null">#{ keyResult37}, </if>
        <if test="checkWeight != null">#{ checkWeight}, </if>
        <if test="checkResult != null">#{ checkResult}, </if>
        <if test="superScore != null">#{ superScore}, </if>
        <if test="acquireScore != null">#{ acquireScore}, </if>
        <if test="scoreComment != null">#{ scoreComment}, </if>
        <if test="recordId != null">#{ recordId}, </if>
        <if test="staffId != null">#{ staffId}, </if>
        <if test="priority != null">#{ priority}, </if>
        <if test="modelId != null">#{ modelId}, </if>
        0,
        now(),
        now()
        )
    </insert>


    <update id="updateResultDetailById" parameterType="ResultDetail" >
        update
        lz_result_detail
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="type != null">type = #{type},</if>
            <if test="target != null">target = #{target},</if>
            <if test="keyResult != null">key_result = #{keyResult},</if>
            <if test="keyResult35 != null">key_result_3_5 = #{keyResult35},</if>
            <if test="keyResult37 != null">key_result_3_7 = #{keyResult37},</if>
            <if test="checkWeight != null">check_weight = #{checkWeight},</if>
            <if test="checkResult != null">check_result = #{checkResult},</if>
            <if test="superScore != null">super_score = #{superScore},</if>
            <if test="acquireScore != null">acquire_score = #{acquireScore},</if>
            <if test="scoreComment != null">score_comment = #{scoreComment},</if>
            <if test="recordId != null">record_id = #{recordId},</if>
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="modelId != null">model_id = #{modelId}</if>
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverResultDetailById" parameterType="ResultDetail" >
        update
            lz_result_detail
        set
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            type = #{type},
            target = #{target},
            key_result = #{keyResult},
            key_result_3_5 = #{keyResult35},
            key_result_3_7 = #{keyResult37},
            check_weight = #{checkWeight},
            check_result = #{checkResult},
            super_score = #{superScore},
            acquire_score = #{acquireScore},
            score_comment = #{scoreComment},
            record_id = #{recordId},
            staff_id = #{staffId},
            priority = #{priority},
            model_id = #{modelId}
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="deleteResultDetailById" parameterType="java.lang.Long">
        update lz_result_detail set is_delete = 1 where id=#{id} limit 1
    </update>



    <select id="selectByRecordId" resultType="com.lz.modules.sys.entity.app.ResultDetail">
        select * from lz_result_detail  where record_id=#{recordResultId} and is_delete = 0 order by type asc ,priority desc
    </select>

    <select id="selectByRecordIdType" resultType="com.lz.modules.sys.entity.app.ResultDetail">
        select * from lz_result_detail  where record_id=#{recordResultId} and type = #{type} and is_delete = 0 order by type asc ,priority desc
    </select>


    <select id="calculateScore" resultType="BigDecimal">
        SELECT sum(check_weight * acquire_score) from lz_result_detail where is_delete = 0
        and record_id = #{recordId} and staff_id = #{staffId} and type = #{type}
    </select>

    <insert id="insertResultDetails" parameterType="ResultDetail" useGeneratedKeys="true" keyProperty="id" >
        insert into lz_result_detail(
        type,
        target,
        key_result,
        check_weight,
        check_result,
        super_score,
        acquire_score,
        score_comment,
        record_id,
        staff_id,
        priority,
        model_id,
        is_delete
        )values
        <foreach collection="list" item="item" separator=",">
        (
        #{ item.type},
        #{ item.target},
        #{ item.keyResult},
        #{ item.checkWeight},
        #{ item.checkResult},
        #{ item.superScore},
        #{ item.acquireScore},
        #{ item.scoreComment},
        #{ item.recordId},
        #{ item.staffId},
        #{ item.priority},
        #{item.modelId},
        0
        )
        </foreach>
    </insert>

    <select id="selectDtosByRecordId" resultType="com.lz.modules.flow.model.ResultDetailDto">
        select * from lz_result_detail  where record_id=#{recordResultId} and model_id = #{modelId} and is_delete = 0 order by priority asc
    </select>

</mapper>

