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

    <resultMap type="com.kmall.admin.entity.StoreEntity" id="storeMap">
        <result property="id" column="id"/>
		<result property="merchSn" column="merch_sn"/>
		<result property="merchName" column="merch_name"/>
        <result property="storeName" column="store_name"/>
        <result property="storeNumber" column="store_number"/>
        <result property="storeAddress" column="store_address"/>
        <result property="provinceName" column="province_name"/>
        <result property="cityName" column="city_name"/>
        <result property="countyName" column="county_name"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
        <result property="coverRadius" column="cover_radius"/>
        <result property="remark" column="remark"/>
		<result column="creater_sn" property="createrSn"/>
		<result column="create_time" property="createTime"/>
		<result column="moder_sn" property="moderSn"/>
		<result column="mod_time" property="modTime"/>
		<result column="tstm" property="tstm"/>

		<result property="thirdPartyMerchCode" column="third_party_merch_code"/>
		<result property="thirdPartyMerchName" column="third_party_merch_name"/>
		<result property="exprAgreementType" column="expr_agreement_type"/>
		<result property="isValid" column="is_valid"/>

    </resultMap>

	<select id="queryObject" resultType="com.kmall.admin.entity.StoreEntity">
		select
			`id`,
			`merch_sn`,
			`merch_name`,
			`store_name`,
			`store_number`,
			`store_address`,
			`province_name`,
			`city_name`,
			`county_name`,
			`latitude`,
			`longitude`,
			`cover_radius`,expr_agreement_type,
			`remark`,merch_sn, merch_name, creater_sn, create_time,third_party_merch_code,third_party_merch_name,
		moder_sn, mod_time, tstm,is_valid
		from mall_store
		where id = #{id}
	</select>

	<select id="queryObjectByName" resultType="com.kmall.admin.entity.StoreEntity">
		select
		`id`,
		`merch_sn`,
		`merch_name`,
		`store_name`,
		`store_number`,
		`store_address`,
		`province_name`,
		`city_name`,
		`county_name`,
		`latitude`,
		`longitude`,
		`cover_radius`,expr_agreement_type,
		`remark`,merch_sn, merch_name, creater_sn, create_time,third_party_merch_code,third_party_merch_name,
    moder_sn, mod_time, tstm,is_valid
		from mall_store
		where store_name = #{storeName}
	</select>

	<!--<select id="queryObjectByDistId" resultType="com.kmall.admin.entity.StoreEntity">-->
		<!--select-->
		<!--`id`,-->
		<!--`merch_sn`,-->
		<!--`merch_name`,-->
		<!--`store_name`,-->
		<!--`store_number`,-->
		<!--`store_address`,-->
		<!--`province_name`,-->
		<!--`city_name`,-->
		<!--`county_name`,-->
		<!--`latitude`,-->
		<!--`longitude`,-->
		<!--`cover_radius`,expr_agreement_type,-->
		<!--`remark`,merch_sn, merch_name, creater_sn, create_time,third_party_merch_code,third_party_merch_name,-->
		<!--moder_sn, mod_time, tstm,is_valid,dist_id-->
		<!--from mall_store-->
		<!--where dist_id = #{distId}-->
	<!--</select>-->

	<select id="queryList" resultType="com.kmall.admin.entity.StoreEntity">
		select
		s.`id`,
		s.`merch_sn`,
		s.`merch_name`,
		s.`store_name`,
		s.`store_number`,
		s.`store_address`,
		s.`province_name`,
		s.`city_name`,
		s.`county_name`,
		s.`latitude`,
		s.`longitude`,
		s.`cover_radius`,
		s.`remark`,s.merch_sn, s.merch_name, s.creater_sn, s.create_time,s.third_party_merch_code,t.third_party_merch_name,s.expr_agreement_type,
		s.moder_sn, s.mod_time, s.tstm,s.is_valid
		from mall_store s left join third_merchant_biz t on s.third_party_merch_code = t.third_party_merch_code
		WHERE 1=1
		<if test="merchSn != null and merchSn.trim() != ''">
			AND s.merch_sn = #{merchSn}
		</if>
		<if test="storeName != null and storeName.trim() != ''">
			AND s.store_name LIKE concat('%',#{storeName},'%')
		</if>
		<if test="storeNumber != null and storeNumber.trim() != ''">
			AND s.storeNumber LIKE concat('%',#{store_number},'%')
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND s.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn.trim() != ''">
			AND t.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="isSupplierGoods != null and isSupplierGoods.trim() != ''">
			AND t.is_supplier_goods = #{isSupplierGoods}
		</if>
		<if test="id != null">
			AND s.id = #{id}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by s.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_store s left join third_merchant_biz t on s.third_party_merch_code = t.third_party_merch_code
		WHERE 1=1
		<if test="merchSn != null and merchSn.trim() != ''">
			AND s.merch_sn = #{merchSn}
		</if>
		<if test="storeName != null and storeName.trim() != ''">
			AND s.store_name LIKE concat('%',#{storeName},'%')
		</if>
		<if test="storeNumber != null and storeNumber.trim() != ''">
			AND s.storeNumber LIKE concat('%',#{store_number},'%')
		</if>
		<if test="thirdPartyMerchCode != null and thirdPartyMerchCode.trim() != ''">
			AND s.third_party_merch_code = #{thirdPartyMerchCode}
		</if>
		<if test="thirdMerchSn != null and thirdMerchSn.trim() != ''">
			AND t.third_merch_sn = #{thirdMerchSn}
		</if>
		<if test="id != null">
			AND s.id = #{id}
		</if>
	</select>

	<insert id="save" parameterType="com.kmall.admin.entity.StoreEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_store(
			`merch_sn`,
			`merch_name`,
			`store_name`,
			`store_number`,
			`store_address`,
			`province_name`,
			`city_name`,
			`county_name`,
			`latitude`,
			`longitude`,
			`cover_radius`,
			is_valid,
			`remark`,
			third_party_merch_code,
			third_party_merch_name,
			expr_agreement_type,
			creater_sn,
			create_time,
			moder_sn,
			mod_time
		)
		values(
			#{merchSn},
			#{merchName},
			#{storeName},
			#{storeNumber},
			#{storeAddress},
			#{provinceName},
			#{cityName},
			#{countyName},
			#{latitude},
			#{longitude},
			#{coverRadius},
			#{isValid},
			#{remark},
			#{thirdPartyMerchCode},
			#{thirdPartyMerchName},
			#{exprAgreementType},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime}
		)
	</insert>
	 
	<update id="update" parameterType="com.kmall.admin.entity.StoreEntity">
		update mall_store
		<set>
			<if test="merchSn != null">`merch_sn` = #{merchSn}, </if>
			<if test="merchName != null">`merch_name` = #{merchName}, </if>
			<if test="storeName != null">`store_name` = #{storeName}, </if>
			<if test="storeNumber != null">`store_number` = #{storeNumber}, </if>
			<if test="storeAddress != null">`store_address` = #{storeAddress}, </if>
			<if test="provinceName != null">`province_name` = #{provinceName}, </if>
			<if test="cityName != null">`city_name` = #{cityName}, </if>
			<if test="countyName != null">`county_name` = #{countyName}, </if>
			<if test="latitude != null">`latitude` = #{latitude}, </if>
			<if test="longitude != null">`longitude` = #{longitude}, </if>
			<if test="coverRadius != null">`cover_radius` = #{coverRadius}, </if>
			<if test="isValid != null">`is_valid` = #{isValid}, </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="thirdPartyMerchCode != null" >third_party_merch_code = #{thirdPartyMerchCode},</if>
			<if test="thirdPartyMerchName != null" >third_party_merch_name = #{thirdPartyMerchName},</if>
			<if test="exprAgreementType != null" >expr_agreement_type = #{exprAgreementType},</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 mall_store where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_store where id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>