<?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.kmall.admin.dao.FreightDao">

    <resultMap type="com.kmall.admin.entity.FreightEntity" id="freightMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="templateType" column="template_type"/>
        <result property="pricingManner" column="pricing_manner"/>
		<result property="defaultFreight" column="default_freight"/>
        <result property="isDefault" column="is_default"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.FreightEntity">
		select
			`id`,
			`name`,
			`template_type`,
			`pricing_manner`,
			`default_freight`,
			`is_default`
		from mall_freight
		where id = #{id}
	</select>

	<select id="queryObjectByName" resultType="com.kmall.admin.entity.FreightEntity">
		select
		`id`,
		`name`,
		`template_type`,
		`pricing_manner`,
		`default_freight`,
		`is_default`
		from mall_freight
		where default_freight = #{defaultFreight}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.FreightEntity">
		select
    		`id`,
    		`name`,
    		`template_type`,
    		`pricing_manner`,
			`default_freight`,
    		`is_default`
		from mall_freight
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
		<if test="id != null and id != ''">
			AND id LIKE concat('%',#{id},'%')
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="queryEntity" resultType="com.kmall.admin.entity.FreightEntity">
		select
		`id`,
		`name`,
		`template_type`,
		`pricing_manner`,
		`default_freight`,
		`is_default`
		from mall_freight
		WHERE 1=1
		<if test="name != null and name != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
		<if test="id != null and id != ''">
			AND id NOT LIKE concat('%',#{id},'%')
		</if>
		<choose>
			<when test="sidx != null and sidx != ''">
				order by ${sidx} ${order}
			</when>
			<otherwise>
				order by id desc
			</otherwise>
		</choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mall_freight
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
		<if test="id != null and id != ''">
			AND id NOT LIKE concat('%',#{id},'%')
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.FreightEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_freight(
			`name`,
			`template_type`,
			`pricing_manner`,
			`default_freight`,
			`is_default`)
		values(
			#{name},
			#{templateType},
			#{pricingManner},
			#{defaultFreight},
			#{isDefault})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.FreightEntity">
		update mall_freight 
		<set>
			<if test="name != null">`name` = #{name}, </if>
			<if test="templateType != null">`template_type` = #{templateType}, </if>
			<if test="pricingManner != null">`pricing_manner` = #{pricingManner}, </if>
			<if test="defaultFreight != null">`default_freight` = #{defaultFreight},</if>
			<if test="isDefault != null">`is_default` = #{isDefault}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from mall_freight where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_freight where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

	<select id="queryMaxId" resultType="java.lang.Integer" parameterType="map">
		SELECT MAX(id) FROM mall_freight
	</select>
</mapper>