<?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.younike.system.mapper.MerchantMapper">

    <resultMap type="Merchant" id="MerchantResult">
        <result property="merchantId"    column="merchant_id"    />
        <result property="adminId"    column="admin_id"    />
        <result property="merchantType"    column="merchant_type"    />
        <result property="merchantIndex"    column="merchant_index"    />
        <result property="merchantName"    column="merchant_name"    />
        <result property="merchantDesc"    column="merchant_desc"    />
        <result property="merchantPhone"    column="merchant_phone"    />
        <result property="serverPhone"    column="server_phone"    />
        <result property="storeAge"    column="store_age"    />
        <result property="provinceId"    column="province_id"    />
        <result property="cityId"    column="city_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="avatar"    column="avatar"    />
        <result property="detail"    column="detail"    />
        <result property="houseNumber"    column="house_number"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="wxServiceId"    column="wx_service_id"    />
        <result property="aliServiceId"    column="ali_service_id"    />
        <result property="bondMoney"    column="bond_money"    />
        <result property="score"    column="score"    />
        <result property="serviceDistance"    column="service_distance"    />
        <result property="orderTotalAmountShow"    column="order_total_amount_show"  />
        <result property="rate"    column="rate"    />
        <result property="shopState"    column="shop_state"    />
        <result property="examineUser"    column="examine_user"    />
        <result property="sales"    column="sales"    />
        <result property="examineReason"    column="examine_reason"    />
        <result property="examineTime"    column="examine_time"    />
        <result property="closeUser"    column="close_user"    />
        <result property="closeReason"    column="close_reason"    />
        <result property="closeTime"    column="close_time"    />
        <result property="promoter"    column="promoter"    />
        <result property="businessState"    column="business_state"    />
        <result property="commissionRate"    column="commission_rate"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
        <result property="allow_compose"    column="allowCompose"    />
        <result property="bannerImgUrl"    column="banner_img_url"    />
    </resultMap>

    <resultMap type="MerchantSettledVO" id="MerchantListResult">
        <result property="merchantId"    column="merchant_id"    />
        <result property="adminId"    column="admin_id"    />
        <result property="merchantType"    column="merchant_type"    />
        <result property="merchantIndex"    column="merchant_index"    />
        <result property="merchantName"    column="merchant_name"    />
        <result property="merchantDesc"    column="merchant_desc"    />
        <result property="merchantPhone"    column="merchant_phone"    />
        <result property="serverPhone"    column="server_phone"    />
        <result property="storeAge"    column="store_age"    />
        <result property="provinceId"    column="province_id"    />
        <result property="cityId"    column="city_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="avatar"    column="avatar"    />
        <result property="detail"    column="detail"    />
        <result property="houseNumber"    column="house_number"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="wxServiceId"    column="wx_service_id"    />
        <result property="aliServiceId"    column="ali_service_id"    />
        <result property="bondMoney"    column="bond_money"    />
        <result property="score"    column="score"    />
        <result property="rate"    column="rate"    />
        <result property="sales"    column="sales"    />
        <result property="shopState"    column="shop_state"    />
        <result property="examineUser"    column="examine_user"    />
        <result property="examineReason"    column="examine_reason"    />
        <result property="examineTime"    column="examine_time"    />
        <result property="closeUser"    column="close_user"    />
        <result property="closeReason"    column="close_reason"    />
        <result property="closeTime"    column="close_time"    />
        <result property="promoter"    column="promoter"    />
        <result property="businessState"    column="business_state"    />
        <result property="commissionRate"    column="commission_rate"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
        <result property="bannerImgUrl"    column="banner_img_url"    />
        <result property="orderTotalAmountShow"  column="order_total_amount_show" />
        <association property="info" resultMap="com.younike.system.mapper.MerchantInfoMapper.MerchantInfoResult"/>
        <association property="user" resultMap="com.younike.system.mapper.SysUserMapper.SysUserResult"/>
    </resultMap>

    <sql id="selectMerchantVo">
        select merchant_id, admin_id, merchant_type, merchant_index, merchant_name, merchant_desc, merchant_phone, server_phone, store_age, province_id, city_id, area_id, avatar, detail, house_number, longitude, latitude, wx_service_id, ali_service_id, bond_money, score, service_distance, order_total_amount_show, rate, shop_state, examine_user, sales, examine_reason, examine_time, close_user, close_reason, close_time, promoter, business_state, commission_rate, del_flag, login_ip, login_date, create_by, create_time, update_by, update_time, column1, column2, column3, allow_compose, banner_img_url from merchant
    </sql>
    <sql id="selectMerchant">
         b.merchant_id, b.admin_id, b.merchant_type, b.merchant_index, b.merchant_name, b.merchant_desc, b.merchant_phone, b.server_phone, b.store_age, b.province_id, b.city_id, b.area_id, b.avatar, b.detail, b.house_number, b.longitude, b.latitude, b.wx_service_id, b.ali_service_id, b.bond_money, b.score, b.service_distance, b.rate, b.shop_state, b.examine_user, b.sales, b.examine_reason, b.examine_time, b.close_user, b.close_reason, b.close_time, b.promoter, b.business_state, b.commission_rate, b.del_flag, b.login_ip, b.login_date, b.create_by, b.create_time, b.update_by, b.update_time, b.column1, b.column2, b.column3, b.allow_compose, b.banner_img_url
    </sql>

    <select id="selectMerchantList" parameterType="Merchant" resultMap="MerchantResult">
        <include refid="selectMerchantVo"/>
        <where>
            <if test="adminId != null "> and admin_id = #{adminId}</if>
            <if test="merchantType != null "> and merchant_type = #{merchantType}</if>
            <if test="merchantName != null  and merchantName != ''"> and merchant_name like concat('%', #{merchantName}, '%')</if>
            <if test="merchantDesc != null  and merchantDesc != ''"> and merchant_desc = #{merchantDesc}</if>
            <if test="merchantPhone != null  and merchantPhone != ''"> and merchant_phone = #{merchantPhone}</if>
            <if test="serverPhone != null  and serverPhone != ''"> and server_phone = #{serverPhone}</if>
            <if test="storeAge != null "> and store_age = #{storeAge}</if>
            <if test="provinceId != null "> and province_id = #{provinceId}</if>
            <if test="cityId != null "> and city_id = #{cityId}</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
            <if test="detail != null  and detail != ''"> and detail = #{detail}</if>
            <if test="houseNumber != null  and houseNumber != ''"> and house_number = #{houseNumber}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="wxServiceId != null  and wxServiceId != ''"> and wx_service_id = #{wxServiceId}</if>
            <if test="aliServiceId != null  and aliServiceId != ''"> and ali_service_id = #{aliServiceId}</if>
            <if test="bondMoney != null "> and bond_money = #{bondMoney}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="serviceDistance != null "> and service_distance = #{serviceDistance}</if>
            <if test="rate != null "> and rate = #{rate}</if>
            <if test="shopState != null  and shopState != ''"> and shop_state = #{shopState}</if>
            <if test="examineUser != null "> and examine_user = #{examineUser}</if>
            <if test="sales != null "> and sales = #{sales}</if>
            <if test="examineReason != null  and examineReason != ''"> and examine_reason = #{examineReason}</if>
            <if test="examineTime != null "> and examine_time = #{examineTime}</if>
            <if test="closeUser != null "> and close_user = #{closeUser}</if>
            <if test="closeReason != null  and closeReason != ''"> and close_reason = #{closeReason}</if>
            <if test="closeTime != null "> and close_time = #{closeTime}</if>
            <if test="promoter != null  and promoter != ''"> and promoter = #{promoter}</if>
            <if test="businessState != null  and businessState != ''"> and business_state = #{businessState}</if>
            <if test="loginIp != null  and loginIp != ''"> and login_ip = #{loginIp}</if>
            <if test="loginDate != null "> and login_date = #{loginDate}</if>
            <if test="column1 != null  and column1 != ''"> and column1 = #{column1}</if>
            <if test="column2 != null  and column2 != ''"> and column2 = #{column2}</if>
            <if test="column3 != null  and column3 != ''"> and column3 = #{column3}</if>
        </where>
    </select>


    <select id="selectAuditMerchantList" parameterType="Merchant" resultMap="MerchantListResult">
        select m.*, u.*, mi.*
        from merchant m
        left join sys_user u on m.admin_id = u.user_id and u.del_flag = 0
        left join merchant_info mi on m.merchant_id = mi.merchant_id and mi.del_flag = 0
        <where>
            <if test="merchantId != null "> and m.merchant_id = #{merchantId}</if>
            <if test="merchantType != null "> and m.merchant_type = #{merchantType}</if>
            <if test="merchantIndex != null and merchantIndex != '' "> and m.merchant_index = #{merchantIndex}</if>
            <if test="merchantName != null  and merchantName != ''"> and m.merchant_name like concat('%', #{merchantName}, '%')</if>
            <if test="phonenumber != null  and phonenumber != ''">and u.phonenumber like concat('%', #{phonenumber},
                '%')
            </if>
            <if test="userName != null  and userName != ''"> and u.user_name like concat('%', #{userName}, '%')</if>
            <if test="shopState != null  and shopState != ''"> and m.shop_state = #{shopState}</if>
            <if test="promoter != null  and promoter != ''">and m.promoter like concat('%', #{promoter}, '%')</if>
            <if test="beginTime != null and beginTime != ''">
                and date_format(m.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and date_format(m.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            and m.shop_state in ('001', '003')
            ORDER BY m.create_time DESC
        </where>
    </select>

    <select id="selectPassMerchantList" parameterType="Merchant" resultMap="MerchantListResult">
        select m.*, u.*, mi.*
        from merchant m
        left join sys_user u on m.admin_id = u.user_id and u.del_flag = 0
        left join merchant_info mi on m.merchant_id = mi.merchant_id and mi.del_flag = 0
        <where>
            <if test="merchantId != null ">and m.merchant_id = #{merchantId}</if>
            <if test="merchantType != null ">and m.merchant_type = #{merchantType}</if>
            <if test="merchantIndex != null and merchantIndex != '' ">and m.merchant_index like concat('%',
                #{merchantIndex}, '%')
            </if>
            <if test="merchantName != null  and merchantName != ''">and m.merchant_name like concat('%',
                #{merchantName}, '%')
            </if>
            <if test="phonenumber != null  and phonenumber != ''">and u.phonenumber like concat('%', #{phonenumber},
                '%')
            </if>
            <if test="userName != null  and userName != ''">and u.user_name like concat('%', #{userName}, '%')</if>
            <if test="nickName != null  and nickName != ''">and u.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="shopState != null  and shopState != ''">and m.shop_state = #{shopState}</if>
            <if test="businessState != null  and businessState != ''">and m.business_state = #{businessState}</if>
            <if test="promoter != null  and promoter != ''">and m.promoter like concat('%', #{promoter}, '%')</if>
            <if test="beginTime != null and beginTime != ''">
                and date_format(m.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and date_format(m.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            and m.shop_state = '002'
            ORDER BY m.create_time DESC
        </where>
    </select>


    <select id="selectMerchantByMerchantId" parameterType="Long" resultMap="MerchantResult">
        <include refid="selectMerchantVo"/>
        where merchant_id = #{merchantId}
        and del_flag=0
    </select>

    <insert id="insertMerchant" parameterType="Merchant" useGeneratedKeys="true" keyProperty="merchantId">
        insert into merchant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="adminId != null">admin_id,</if>
            <if test="merchantType != null">merchant_type,</if>
            <if test="merchantIndex != null">merchant_index,</if>
            <if test="merchantName != null">merchant_name,</if>
            <if test="merchantDesc != null">merchant_desc,</if>
            <if test="merchantPhone != null">merchant_phone,</if>
            <if test="serverPhone != null">server_phone,</if>
            <if test="storeAge != null">store_age,</if>
            <if test="provinceId != null">province_id,</if>
            <if test="cityId != null">city_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="avatar != null">avatar,</if>
            <if test="detail != null">detail,</if>
            <if test="houseNumber != null">house_number,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="wxServiceId != null">wx_service_id,</if>
            <if test="aliServiceId != null">ali_service_id,</if>
            <if test="bondMoney != null">bond_money,</if>
            <if test="score != null">score,</if>
            <if test="serviceDistance != null">service_distance,</if>
            <if test="rate != null">rate,</if>
            <if test="shopState != null">shop_state,</if>
            <if test="examineUser != null">examine_user,</if>
            <if test="sales != null">sales,</if>
            <if test="examineReason != null">examine_reason,</if>
            <if test="examineTime != null">examine_time,</if>
            <if test="closeUser != null">close_user,</if>
            <if test="closeReason != null">close_reason,</if>
            <if test="closeTime != null">close_time,</if>
            <if test="promoter != null">promoter,</if>
            <if test="businessState != null">business_state,</if>
            <if test="commissionRate != null  and commissionRate != ''">commission_rate,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="loginIp != null">login_ip,</if>
            <if test="loginDate != null">login_date,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="column1 != null">column1,</if>
            <if test="column2 != null">column2,</if>
            <if test="column3 != null">column3,</if>
            <if test="allowCompose != null">allow_compose,</if>
            <if test="bannerImgUrl != null">banner_img_url,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="adminId != null">#{adminId},</if>
            <if test="merchantType != null">#{merchantType},</if>
            <if test="merchantIndex != null">#{merchantIndex},</if>
            <if test="merchantName != null">#{merchantName},</if>
            <if test="merchantDesc != null">#{merchantDesc},</if>
            <if test="merchantPhone != null">#{merchantPhone},</if>
            <if test="serverPhone != null">#{serverPhone},</if>
            <if test="storeAge != null">#{storeAge},</if>
            <if test="provinceId != null">#{provinceId},</if>
            <if test="cityId != null">#{cityId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="detail != null">#{detail},</if>
            <if test="houseNumber != null">#{houseNumber},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="wxServiceId != null">#{wxServiceId},</if>
            <if test="aliServiceId != null">#{aliServiceId},</if>
            <if test="bondMoney != null">#{bondMoney},</if>
            <if test="score != null">#{score},</if>
            <if test="serviceDistance != null">#{serviceDistance},</if>
            <if test="rate != null">#{rate},</if>
            <if test="shopState != null">#{shopState},</if>
            <if test="examineUser != null">#{examineUser},</if>
            <if test="sales != null">#{sales},</if>
            <if test="examineReason != null">#{examineReason},</if>
            <if test="examineTime != null">#{examineTime},</if>
            <if test="closeUser != null">#{closeUser},</if>
            <if test="closeReason != null">#{closeReason},</if>
            <if test="closeTime != null">#{closeTime},</if>
            <if test="promoter != null">#{promoter},</if>
            <if test="businessState != null">#{businessState},</if>
            <if test="commissionRate != null and commissionRate != '' ">#{commissionRate},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="loginIp != null">#{loginIp},</if>
            <if test="loginDate != null">#{loginDate},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="column1 != null">#{column1},</if>
            <if test="column2 != null">#{column2},</if>
            <if test="column3 != null">#{column3},</if>
            <if test="allowCompose != null">#{allowCompose},</if>
            <if test="bannerImgUrl != null">#{bannerImgUrl},</if>
         </trim>
    </insert>

    <update id="updateMerchant" parameterType="Merchant">
        update merchant
        <trim prefix="SET" suffixOverrides=",">
            <if test="adminId != null">admin_id = #{adminId},</if>
            <if test="merchantType != null">merchant_type = #{merchantType},</if>
            <if test="merchantIndex != null">merchant_index = #{merchantIndex},</if>
            <if test="merchantName != null">merchant_name = #{merchantName},</if>
            <if test="merchantDesc != null">merchant_desc = #{merchantDesc},</if>
            <if test="merchantPhone != null">merchant_phone = #{merchantPhone},</if>
            <if test="serverPhone != null">server_phone = #{serverPhone},</if>
            <if test="storeAge != null">store_age = #{storeAge},</if>
            <if test="provinceId != null">province_id = #{provinceId},</if>
            <if test="cityId != null">city_id = #{cityId},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="detail != null">detail = #{detail},</if>
            <if test="houseNumber != null">house_number = #{houseNumber},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="wxServiceId != null">wx_service_id = #{wxServiceId},</if>
            <if test="aliServiceId != null">ali_service_id = #{aliServiceId},</if>
            <if test="bondMoney != null">bond_money = #{bondMoney},</if>
            <if test="score != null">score = #{score},</if>
            <if test="serviceDistance != null">service_distance = #{serviceDistance},</if>
            <if test="orderTotalAmountShow != null">order_total_amount_show = #{orderTotalAmountShow},</if>
            <if test="rate != null">rate = #{rate},</if>
            <if test="shopState != null">shop_state = #{shopState},</if>
            <if test="examineUser != null">examine_user = #{examineUser},</if>
            <if test="sales != null">sales = #{sales},</if>
            <if test="examineReason != null">examine_reason = #{examineReason},</if>
            <if test="examineTime != null">examine_time = #{examineTime},</if>
            <if test="closeUser != null">close_user = #{closeUser},</if>
            <if test="closeReason != null">close_reason = #{closeReason},</if>
            <if test="closeTime != null">close_time = #{closeTime},</if>
            <if test="promoter != null">promoter = #{promoter},</if>
            <if test="businessState != null">business_state = #{businessState},</if>
            <if test="commissionRate != null and commissionRate != ''">commission_rate = #{commissionRate},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="column1 != null">column1 = #{column1},</if>
            <if test="column2 != null">column2 = #{column2},</if>
            <if test="column3 != null">column3 = #{column3},</if>
            <if test="allowCompose != null">allow_compose = #{allowCompose},</if>
            <if test="bannerImgUrl != null and bannerImgUrl != ''">banner_img_url = #{bannerImgUrl},</if>
        </trim>
        where merchant_id = #{merchantId}
    </update>

    <delete id="deleteMerchantByMerchantId" parameterType="Long">
        delete from merchant where merchant_id = #{merchantId}
    </delete>

    <delete id="deleteMerchantByMerchantIds" parameterType="String">
        delete from merchant where merchant_id in
        <foreach item="merchantId" collection="array" open="(" separator="," close=")">
            #{merchantId}
        </foreach>
    </delete>

<!--    根据服务人员信息查询店铺信息-->
    <select id="selectMerchantByServerId" resultMap="MerchantResult" parameterType="Long">
         SELECT <include refid="selectMerchant"/>
            FROM server_user a, merchant b
            WHERE a.merchant_id = b.merchant_id
                AND a.del_flag = 0
                AND b.del_flag = 0
                AND a.status = 0
                AND b.shop_state = '002'
                AND a.server_id = #{serverId}
    </select>
</mapper>
