<?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.diaoyun.zion.chinafrica.dao.TbCfExpCatRelDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfExpCatRelEntity" id="tbCfExpCatRelMap">
        <result property="relateId" column="relate_id"/>
        <result property="templateId" column="template_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="creatTime" column="creat_time"/>
    </resultMap>

	<select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfExpCatRelEntity">
		select
			`relate_id`,
			`template_id`,
			`category_id`,
			`creat_time`
		from tb_cf_exp_cat_rel
		where relate_id = #{id}
	</select>

	<select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfExpCatRelEntity">
		select
    		`relate_id`,
    		`template_id`,
    		`category_id`,
    		`creat_time`
		from tb_cf_exp_cat_rel
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by relate_id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from tb_cf_exp_cat_rel
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>
	 
	<insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfExpCatRelEntity">
		insert into tb_cf_exp_cat_rel(
			`relate_id`,
			`template_id`,
			`category_id`,
			`creat_time`)
		values(
			#{relateId},
			#{templateId},
			#{categoryId},
			#{creatTime})
	</insert>
	 
	<update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfExpCatRelEntity">
		update tb_cf_exp_cat_rel 
		<set>
			<if test="templateId != null">`template_id` = #{templateId}, </if>
			<if test="categoryId != null">`category_id` = #{categoryId}, </if>
			<if test="creatTime != null">`creat_time` = #{creatTime}</if>
		</set>
		where relate_id = #{relateId}
	</update>
	
	<delete id="delete">
		delete from tb_cf_exp_cat_rel where relate_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from tb_cf_exp_cat_rel where relate_id in 
		<foreach item="relateId" collection="array" open="(" separator="," close=")">
			#{relateId}
		</foreach>
	</delete>

    <!--关联运费模板和分类-->
	<insert id="buildRelate" parameterType="java.util.List">
        insert into tb_cf_exp_cat_rel(
        `relate_id`,
        `template_id`,
        `category_id`,
        `creat_time`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.relateId},
            #{item.templateId},
            #{item.categoryId},
            #{item.creatTime}
            )
        </foreach>
    </insert>

</mapper>