<?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.xinyilu.system.mapper.testItemCountryScoreinfo.TestItemCountryScoreMapper">

    <resultMap type="TestItemCountryScoreVO" id="TestItemCountryScoreResult">
        <result property="id" column="id"/>
        <result property="itemId" column="item_id"/>
        <result property="itemAvgScore" column="item_avg_score"/>
        <result property="itemAvgText" column="item_avg_text"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="itemName" column="item_name"/>
        <result property="dictValue" column="dict_value"/>
        <result property="sex" column="sex"/>
    </resultMap>

    <sql id="selectTestItemCountryScoreVo">
        select id,
               item_id,
               sex,
               item_avg_score,
               item_avg_text,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from test_item_country_score
    </sql>

    <select id="selectTestItemCountryScoreList" parameterType="testItemCountryScoreVO"
            resultMap="TestItemCountryScoreResult">
        select s.id,
        s.item_id,
        s.sex,
        s.item_avg_score,
        s.item_avg_text,
        s.del_flag,
        s.create_by,
        s.create_time,
        s.update_by,
        s.update_time,
        i.item_name,
        d.dict_value
        from test_item_country_score s
        left join test_items i on s.item_id=i.id
        left join sys_dict_data d on d.dict_code=i.result_dict_type
        <where>
            <if test="itemId != null ">
                and s.item_id = #{itemId}
            </if>
            <if test="itemAvgScore != null ">
                and s.item_avg_score = #{itemAvgScore}
            </if>
            <if test="itemAvgScore != null ">
                and s.item_avg_text = #{itemAvgText}
            </if>
            <if test="itemName != null  and itemName != ''">
                and i.item_name like concat('%', #{itemName}, '%')
            </if>
            and s.del_flag = 0
        </where>
        order by s.create_time desc
    </select>

    <select id="selectTestItemCountryScoreById" parameterType="Long"
            resultMap="TestItemCountryScoreResult">
        select s.id,
               s.item_id,
               s.sex,
               s.item_avg_score,
               s.item_avg_text,
               s.del_flag,
               s.create_by,
               s.create_time,
               s.update_by,
               s.update_time,
               i.item_name,
               d.dict_value
        from test_item_country_score s
        left join test_items i on s.item_id=i.id
        left join sys_dict_data d on d.dict_code=i.result_dict_type
        where s.id = #{id} and s.del_flag = 0
    </select>

    <insert id="insertTestItemCountryScore" parameterType="TestItemCountryScore" useGeneratedKeys="true"
            keyProperty="id">
        insert into test_item_country_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,
            </if>
            <if test="sex != null">sex,
            </if>
            <if test="itemAvgScore != null">item_avg_score,
            </if>
            <if test="itemAvgText != null">item_avg_text,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null">#{itemId},
            </if>
            <if test="sex != null">#{sex},
            </if>
            <if test="itemAvgScore != null">#{itemAvgScore},
            </if>
            <if test="itemAvgText != null">#{itemAvgText},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateTestItemCountryScore" parameterType="TestItemCountryScore">
        update test_item_country_score
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id =
                #{itemId},
            </if>
            <if test="sex != null">sex =
                #{sex},
            </if>
            <if test="itemAvgScore != null">item_avg_score =
                #{itemAvgScore},
            </if>
            <if test="itemAvgText != null">item_avg_text =
                #{itemAvgText},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTestItemCountryScoreById" parameterType="Long">
        delete
        from test_item_country_score
        where id = #{id}
    </delete>

    <delete id="deleteTestItemCountryScoreByIds" parameterType="String">
        delete from test_item_country_score where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="updateTestItemCountryScoreByIds" parameterType="String">
        update test_item_country_score set del_flag = 1 ,update_time = now() where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getItemType" parameterType="long" resultType="string">
        SELECT
            IFNULL(sd.dict_value, '')AS dictValue
        FROM
            test_items ti
                INNER JOIN sys_dict_data sd ON ti.result_dict_type = sd.dict_code
        where ti.id = #{itemId}
    </select>


    <update id="updateTestItemCountryScoreById" parameterType="long">
        update test_item_country_score set item_avg_score = null, update_time = now() where id = #{id}
    </update>

    <update id="updateTestItemCountryTextById" parameterType="long">
        update test_item_country_score set item_avg_text = null, update_time = now() where id = #{id}
    </update>

    <select id="getTestItemCountryScore" resultType="testItemCountryScore">
        select
        s.item_id,
        s.sex,
        s.item_avg_score,
        s.item_avg_text
        from test_item_country_score s
        <where>
            s.del_flag = 0
            <if test="itemId != null">
                and s.item_id = #{itemId}
            </if>
            <if test="sex != null and sex != ''">
                and s.sex = #{sex}
            </if>
        </where>
    </select>

    <select id="getItemCountryScoreIds" resultType="long">
        select s.id from test_item_country_score s
        <where>
            s.del_flag = 0
            <if test="itemId != null">
                and s.item_id = #{itemId}
            </if>
            <if test="sex != null and sex != ''">
                and s.sex = #{sex}
            </if>
        </where>
    </select>
</mapper>
