<?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.ruoyi.business.mapper.BusinessTemplatePropValueMapper">
    
    <resultMap type="BusinessTemplatePropValue" id="BusinessTemplatePropValueResult">
        <result property="id"    column="id"    />
        <result property="propId"    column="prop_id"    />
        <result property="propName"    column="prop_name"    />
        <result property="valueKey"    column="value_key"    />
        <result property="valueText"    column="value_text"    />
        <result property="tempId"    column="temp_id"    />
        <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="deleted"    column="deleted"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectBusinessTemplatePropValueVo">
        select id, prop_id, prop_name, value_key, value_text, temp_id, create_by, create_time, update_by, update_time, deleted, remark from business_template_prop_value
    </sql>

    <select id="selectBusinessTemplatePropValueList" parameterType="BusinessTemplatePropValue" resultMap="BusinessTemplatePropValueResult">
        <include refid="selectBusinessTemplatePropValueVo"/>
        <where>  
            <if test="propId != null  and propId != ''"> and prop_id = #{propId}</if>
            <if test="propName != null  and propName != ''"> and prop_name like concat('%', #{propName}, '%')</if>
            <if test="valueKey != null  and valueKey != ''"> and value_key = #{valueKey}</if>
            <if test="valueText != null  and valueText != ''"> and value_text = #{valueText}</if>
            <if test="tempId != null "> and temp_id = #{tempId}</if>
        </where>
    </select>
    
    <select id="selectBusinessTemplatePropValueById" parameterType="Long" resultMap="BusinessTemplatePropValueResult">
        <include refid="selectBusinessTemplatePropValueVo"/>
        where id = #{id}
    </select>

    <!--获取统计数据-->
    <select id="countGroup" resultType="map">
        select
        count(1) as value,
        value_text as name
        from business_template_prop_value
        where survey_id = #{surveyId}
        and prop_type in ('0','1','6')
        and prop_id = #{propId}
        and deleted = '0'
        group by value_key
    </select>

    <insert id="insertBusinessTemplatePropValue" parameterType="BusinessTemplatePropValue" useGeneratedKeys="true" keyProperty="id">
        insert into business_template_prop_value
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="propId != null">prop_id,</if>
            <if test="propName != null">prop_name,</if>
            <if test="valueKey != null">value_key,</if>
            <if test="valueText != null">value_text,</if>
            <if test="tempId != null">temp_id,</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>
            <if test="deleted != null">deleted,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="propId != null">#{propId},</if>
            <if test="propName != null">#{propName},</if>
            <if test="valueKey != null">#{valueKey},</if>
            <if test="valueText != null">#{valueText},</if>
            <if test="tempId != null">#{tempId},</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>
            <if test="deleted != null">#{deleted},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateBusinessTemplatePropValue" parameterType="BusinessTemplatePropValue">
        update business_template_prop_value
        <trim prefix="SET" suffixOverrides=",">
            <if test="propId != null">prop_id = #{propId},</if>
            <if test="propName != null">prop_name = #{propName},</if>
            <if test="valueKey != null">value_key = #{valueKey},</if>
            <if test="valueText != null">value_text = #{valueText},</if>
            <if test="tempId != null">temp_id = #{tempId},</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>
            <if test="deleted != null">deleted = #{deleted},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBusinessTemplatePropValueById" parameterType="Long">
        delete from business_template_prop_value where id = #{id}
    </delete>

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