<?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.ThirdMerchExpressDao">

    <resultMap type="com.kmall.admin.entity.ThirdMerchExpressEntity" id="thirdMerchExpressMap">
        <result property="id" column="id"/>
        <result property="expressAccount" column="express_account"/>
        <result property="expressPwd" column="express_pwd"/>
        <result property="expressCusId" column="express_cus_id"/>
        <result property="thirdPartyMerchCode" column="third_party_merch_code"/>
        <result property="remark" column="remark"/>
        <result property="createrSn" column="creater_sn"/>
        <result property="createTime" column="create_time"/>
        <result property="moderSn" column="moder_sn"/>
        <result property="modTime" column="mod_time"/>
        <result property="tstm" column="tstm"/>
		<result property="thirdPartyMerchName" column="thirdPartyMerchName"/>

    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.ThirdMerchExpressEntity">
		select
			`id`,
			`express_account`,
			`express_pwd`,
			`express_cus_id`,
			`third_party_merch_code`,
			`remark`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from third_merch_express
		where id = #{id}
	</select>

	<select id="getThirdMerchExpressByThirdCode" resultType="com.kmall.admin.entity.ThirdMerchExpressEntity">
		select
		e.`id`,
		e.`express_account`,
		e.`express_pwd`,
		e.`express_cus_id`,
		e.`third_party_merch_code`,
		e.`remark`,
		e.`creater_sn`,
		e.`create_time`,
		e.`moder_sn`,
		e.`mod_time`,
		e.`tstm`,
		b.third_party_merch_name thirdPartyMerchName
		from third_merch_express e left join third_merchant_biz b on e.third_party_merch_code = b.third_party_merch_code
		where e.third_party_merch_code = #{thirdPartyMerchCode}
	</select>

	<select id="queryList" resultType="com.kmall.admin.entity.ThirdMerchExpressEntity">
		select
		e.`id`,
		e.`express_account`,
		e.`express_pwd`,
		e.`express_cus_id`,
		e.`third_party_merch_code`,
		e.`remark`,
		e.`creater_sn`,
		e.`create_time`,
		e.`moder_sn`,
		e.`mod_time`,
		e.`tstm`,
		b.third_party_merch_name thirdPartyMerchName
		from third_merch_express e
		left join third_merchant_biz b on e.third_party_merch_code = b.third_party_merch_code
		WHERE 1=1
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND e.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="merchSn != null and merchSn.trim() != ''">
			AND b.merch_sn = #{merchSn}
		</if>
		<if test="storeId != null and storeId != ''">
			AND e.third_party_merch_code = (select third_party_merch_code from mall_store where id = #{storeId})
		</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="queryTotal" resultType="int">
		select count(*)
		from third_merch_express e
		left join third_merchant_biz b on e.third_party_merch_code = b.third_party_merch_code
		WHERE 1=1
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND e.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="merchSn != null and merchSn.trim() != ''">
			AND b.merch_sn = #{merchSn}
		</if>
		<if test="storeId != null and storeId != ''">
			AND e.third_party_merch_code = (select third_party_merch_code from mall_store where id = #{storeId})
		</if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.admin.entity.ThirdMerchExpressEntity" useGeneratedKeys="true" keyProperty="id">
		insert into third_merch_express(
			`express_account`,
			`express_pwd`,
			`express_cus_id`,
			`third_party_merch_code`,
			`remark`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{expressAccount},
			#{expressPwd},
			#{expressCusId},
			#{thirdPartyMerchCode},
			#{remark},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.ThirdMerchExpressEntity">
		update third_merch_express 
		<set>
			<if test="expressAccount != null">`express_account` = #{expressAccount}, </if>
			<if test="expressPwd != null">`express_pwd` = #{expressPwd}, </if>
			<if test="expressCusId != null">`express_cus_id` = #{expressCusId}, </if>
			<if test="thirdPartyMerchCode != null">`third_party_merch_code` = #{thirdPartyMerchCode}, </if>
			<if test="remark != null">`remark` = #{remark}, </if>
			<if test="createrSn != null">`creater_sn` = #{createrSn}, </if>
			<if test="createTime != null">`create_time` = #{createTime}, </if>
			<if test="moderSn != null">`moder_sn` = #{moderSn}, </if>
			<if test="modTime != null">`mod_time` = #{modTime}, </if>
			<if test="tstm != null">`tstm` = #{tstm}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from third_merch_express where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from third_merch_express where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>