<?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.xingxi.master.merchant.mapper.MerchantMapper">
    <resultMap type="Merchant" id="MerchantResult">
        <result property="mercId"    column="mercId"    />
        <result property="mercName"    column="mercName"    />
        <result property="mercStatus"    column="mercStatus"    />
        <result property="mercType"    column="mercType"    />
        <result property="contact"    column="contact"    />
        <result property="mail"    column="mail"    />
        <result property="mobile"    column="mobile"    />
        <result property="address"    column="address"    />
        <result property="personName"    column="personName"    />
        <result property="idNumber"    column="idNumber"    />
        <result property="companyName"    column="companyName"    />
        <result property="companyCode"    column="companyCode"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectMerchant">
        select mercId, mercName, mercStatus, mercType, contact, mail, mobile, address, personName, idNumber, companyName, companyCode, delFlag, createUser, createTime, updateUser, updateTime from m_merchant
    </sql>

    <select id="selectMerchantList" parameterType="Merchant" resultMap="MerchantResult">
        <include refid="selectMerchant"/>
        <where>  
            <if test="mercName != null  and mercName != ''"> and mercName like concat('%', #{mercName}, '%')</if>
            <if test="mercStatus != null  and mercStatus != ''"> and mercStatus = #{mercStatus}</if>
            <if test="mercType != null  and mercType != ''"> and mercType = #{mercType}</if>
            <if test="contact != null  and contact != ''"> and contact = #{contact}</if>
            <if test="mail != null  and mail != ''"> and mail = #{mail}</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="personName != null  and personName != ''"> and personName like concat('%', #{personName}, '%')</if>
            <if test="idNumber != null  and idNumber != ''"> and idNumber = #{idNumber}</if>
            <if test="companyName != null  and companyName != ''"> and companyName like concat('%', #{companyName}, '%')</if>
            <if test="companyCode != null  and companyCode != ''"> and companyCode = #{companyCode}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </select>
    
    <select id="selectMerchantByMercId" parameterType="Long" resultMap="MerchantResult">
        <include refid="selectMerchant"/>
        where mercId = #{mercId}
    </select>
        
    <insert id="insertMerchant" parameterType="Merchant" useGeneratedKeys="true" keyProperty="mercId">
        insert into m_merchant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mercName != null">mercName,</if>
            <if test="mercStatus != null">mercStatus,</if>
            <if test="mercType != null">mercType,</if>
            <if test="contact != null">contact,</if>
            <if test="mail != null">mail,</if>
            <if test="mobile != null">mobile,</if>
            <if test="address != null">address,</if>
            <if test="personName != null">personName,</if>
            <if test="idNumber != null">idNumber,</if>
            <if test="companyName != null">companyName,</if>
            <if test="companyCode != null">companyCode,</if>
            <if test="delFlag != null">delFlag,</if>
            <if test="createUser != null">createUser,</if>
            <if test="createTime != null">createTime,</if>
            <if test="updateUser != null">updateUser,</if>
            <if test="updateTime != null">updateTime,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mercName != null">#{mercName},</if>
            <if test="mercStatus != null">#{mercStatus},</if>
            <if test="mercType != null">#{mercType},</if>
            <if test="contact != null">#{contact},</if>
            <if test="mail != null">#{mail},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="address != null">#{address},</if>
            <if test="personName != null">#{personName},</if>
            <if test="idNumber != null">#{idNumber},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="companyCode != null">#{companyCode},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertMerchant">
        insert into m_merchant( mercName, mercStatus, mercType, contact, mail, mobile, address, personName, idNumber, companyName, companyCode, delFlag, createUser, createTime, updateUser, updateTime) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.mercName}, #{item.mercStatus}, #{item.mercType}, #{item.contact}, #{item.mail}, #{item.mobile}, #{item.address}, #{item.personName}, #{item.idNumber}, #{item.companyName}, #{item.companyCode}, #{item.delFlag}, #{item.createUser}, #{item.createTime}, #{item.updateUser}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateMerchant" parameterType="Merchant">
        update m_merchant
        <trim prefix="SET" suffixOverrides=",">
            <if test="mercName != null">mercName = #{mercName},</if>
            <if test="mercStatus != null">mercStatus = #{mercStatus},</if>
            <if test="mercType != null">mercType = #{mercType},</if>
            <if test="contact != null">contact = #{contact},</if>
            <if test="mail != null">mail = #{mail},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="address != null">address = #{address},</if>
            <if test="personName != null">personName = #{personName},</if>
            <if test="idNumber != null">idNumber = #{idNumber},</if>
            <if test="companyName != null">companyName = #{companyName},</if>
            <if test="companyCode != null">companyCode = #{companyCode},</if>
            <if test="delFlag != null">delFlag = #{delFlag},</if>
            <if test="createUser != null">createUser = #{createUser},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
            <if test="updateUser != null">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where mercId = #{mercId}
    </update>

    <delete id="deleteMerchantByMercId" parameterType="Long">
        delete from m_merchant where mercId = #{mercId}
    </delete>

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

    <update id="logicDeleteMerchantByMercId" parameterType="Long">
        update m_merchant
            set delFlag = '1'
        where mercId = #{mercId}
    </update>

    <update id="logicDeleteMerchantByMercIds" parameterType="String">
        update m_merchant
        set delFlag = '1'
        where mercId in
        <foreach item="mercId" collection="array" open="(" separator="," close=")">
            #{mercId}
        </foreach>
    </update>

    <update id="logicDeleteMerchantByCondition" parameterType="Merchant">
        update m_merchant
        set delFlag = '1'
        <where>
            <if test="mercName != null  and mercName != ''"> and mercName like concat('%', #{mercName}, '%')</if>
            <if test="mercStatus != null  and mercStatus != ''"> and mercStatus = #{mercStatus}</if>
            <if test="mercType != null  and mercType != ''"> and mercType = #{mercType}</if>
            <if test="contact != null  and contact != ''"> and contact = #{contact}</if>
            <if test="mail != null  and mail != ''"> and mail = #{mail}</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="personName != null  and personName != ''"> and personName like concat('%', #{personName}, '%')</if>
            <if test="idNumber != null  and idNumber != ''"> and idNumber = #{idNumber}</if>
            <if test="companyName != null  and companyName != ''"> and companyName like concat('%', #{companyName}, '%')</if>
            <if test="companyCode != null  and companyCode != ''"> and companyCode = #{companyCode}</if>
            <if test="delFlag != null  and delFlag != ''"> and delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and createUser = #{createUser}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and updateTime = #{updateTime}</if>
        </where>
    </update>
</mapper>