<?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.iformall.mapper.WxMerchantMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxMerchant">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="img_url" jdbcType="VARCHAR" property="imgUrl"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="link_phone" jdbcType="VARCHAR" property="linkPhone"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="car_vendor_type" jdbcType="INTEGER" property="carVendorType"/>
        <result column="car_params" jdbcType="VARCHAR" property="carParams"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="link_person" jdbcType="VARCHAR" property="linkPerson"/>
        <result column="business_id" jdbcType="INTEGER" property="businessId"/>
        <result column="sub_business_id" jdbcType="INTEGER" property="subBusinessId"/>
        <result column="shop_type" jdbcType="INTEGER" property="shopType"/>
        <result column="brand" jdbcType="VARCHAR" property="brand"/>
        <result column="type" jdbcType="SMALLINT" property="type"/>
	    <result column="is_public" jdbcType="SMALLINT" property="isPublic"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="cover_picture" jdbcType="VARCHAR" property="coverPicture"/>
        <result column="is_admin" jdbcType="INTEGER" property="isAdmin"/>
        <result column="bill_setting" jdbcType="INTEGER" property="billSetting"/>
        <result column="qr_code" jdbcType="VARCHAR" property="qrCode"/>
        <result column="introduction" jdbcType="VARCHAR" property="introduction"/>
        <result column="action_desc" jdbcType="VARCHAR" property="actionDesc"/>
        <result column="is_del" jdbcType="INTEGER" property="isDel"/>
        <result column="talk_user_main" jdbcType="VARCHAR" property="talkUserMain"/>
        <result column="talk_user_aux" jdbcType="VARCHAR" property="talkUserAux"/>
        <result column="link_line_phone" property="linkLinePhone"/>
        <result column="rental_start_date" property="rentalStartDate"/>
        <result column="rental_end_date" property="rentalEndDate"/>
        <result column="credit_locked" property="creditLocked"/>

    </resultMap>
    
    <sql id="allColumns">
	    `id`,`tenant_id`,`img_url`,`name`,`link_phone`,`create_date`,`update_date`,`car_vendor_type`,`car_params`,`status`,`link_person`,
	    `business_id`,`sub_business_id`,`shop_type`,`brand`,`type`,`is_public`,email,`title`,`cover_picture`,`is_admin`,`bill_setting`,`qr_code`,
	    `introduction`,`action_desc`,`is_del`,`talk_user_main`,`talk_user_aux`,link_line_phone,credit_locked
	</sql>
    
    <sql id="allColumnsVo">
	    m.`id`,m.`tenant_id`,m.`img_url`,m.`name`,m.`link_phone`,m.`create_date`,m.`update_date`,m.`car_vendor_type`,m.`car_params`,m.`status`,m.`link_person`,
	    m.`business_id`,m.`sub_business_id`,m.`shop_type`,m.`brand`,m.`type`,m.`is_public`,m.email,m.`title`,m.`cover_picture`,m.`is_admin`,m.`bill_setting`,m.`qr_code`,
	    m.`introduction`,m.`action_desc`,m.`is_del`,m.`talk_user_main`,m.`talk_user_aux`,m.link_line_phone,r.rental_start_date,r.rental_end_date
	</sql>
    
    <sql id="dynamicWhereConditions">
        where is_del = 0

        <if test=" null != id ">
            and `id` = #{id}
        </if>

        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>

        <if test=" null != imgUrl ">
            and `img_url` like concat('%', #{imgUrl},'%')
        </if>
    
        <if test=" null != name and ''!=name ">
            and `name` like concat('%', #{name},'%')
        </if>
    
        <if test=" null != linkPhone and ''!=linkPhone ">
            and `link_phone` like concat('%', #{linkPhone},'%')
        </if>

        <if test=" null != createDate ">
            and `create_date` = #{createDate}
        </if>

        <if test=" null != updateDate ">
            and `update_date` = #{updateDate}
        </if>

        <if test=" null != carVendorType ">
            and `car_vendor_type` = #{carVendorType}
        </if>

        <if test=" null != businessId ">
            and `business_id` = #{businessId}
        </if>

        <if test=" null != subBusinessId ">
            and `sub_business_id` = #{subBusinessId}
        </if>

        <if test=" null != carParams ">
            and `car_params` like concat('%', #{carParams},'%')
        </if>
        <if test=" null != status ">
            and `status` = #{status}
        </if>
        <if test=" null != linkPerson ">
            and `link_person` = #{linkPerson}
        </if>
        <if test=" null != type ">
            and `type` = #{type}
        </if>
	    <if test=" null != isPublic ">
		    and `is_public` = #{isPublic}
        </if>
        <if test=" null != email ">
            and `email` = #{email}
        </if>
        <if test=" null != isAdmin ">
            and `is_admin` = #{isAdmin}
        </if>
        <if test=" null != isDel ">
            and `is_del` = #{isDel}
        </if>
        <if test=" null != brand ">
            and `brand` = #{brand}
        </if>
        <if test=" null != creditLocked ">
            and `credit_locked` = #{creditLocked}
        </if>
        <if test=" null != rentShopType ">
	        and id not in(
            select merchant_id from wx_rent_contract where tenant_id=#{tenantId} and rent_shop_type=1 and merchant_id is
	        not null and status in(1,2,3,4)
            )
        </if>
    
        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
	
	    <if test="(null != floorForRule and '' != floorForRule) or (null != businessForRule and '' != businessForRule)">
		    and id in(
		    select ms.merchant_id from (select merchant_id,shop_id from wx_merchant_shop where is_del=0) ms
		    inner join wx_merchant m on ms.merchant_id = m.id
		    inner join wx_shop s on s.id=ms.shop_id
		    where m.tenant_id=#{tenantId} and s.is_del=0
		    <if test=" null != businessForRule and '' != businessForRule ">
			    and m.business_id in (${businessForRule})
		    </if>
		    <if test=" null != floorForRule and '' != floorForRule ">
			    and s.`floor` in (${floorForRule})
		    </if>
		    )
	    </if>
    
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findList" parameterType="com.iformall.domain.po.WxMerchant" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
	    from wx_merchant m
        <include refid="dynamicWhereConditions"/>
    </select>
    
    <select id="findListVo" parameterType="com.iformall.domain.po.WxMerchant" resultMap="BaseResultMap">
        select
        <include refid="allColumnsVo"/>
        from wx_merchant m left join (select merchant_id,rental_start_date,rental_end_date from wx_rent_contract where
        tenant_id=#{tenantId} and status in(2,3,4)) r
        on m.id=r.merchant_id
        <include refid="dynamicWhereConditionsVo"/>
    
    </select>
    
    <sql id="dataRule">
        <if test="(null != floorForRule and '' != floorForRule) or (null != businessForRule and '' != businessForRule)">
            and m.id in(
            select ms.merchant_id from (select merchant_id,shop_id from wx_merchant_shop where is_del=0) ms
            inner join wx_merchant m on ms.merchant_id = m.id
            inner join wx_shop s on s.id=ms.shop_id
            where m.tenant_id=#{tenantId} and s.is_del=0
            <if test=" null != businessForRule and '' != businessForRule ">
                and m.business_id in (${businessForRule})
            </if>
            <if test=" null != floorForRule and '' != floorForRule ">
                and s.`floor` in (${floorForRule})
            </if>
            )
        </if>
    </sql>
    
    <sql id="dynamicWhereConditionsVo">
        where m.is_del = 0
        
        <if test=" null != id ">
            and m.`id` = #{id}
        </if>
        
        <if test=" null != tenantId ">
            and m.`tenant_id` = #{tenantId}
        </if>
        
        <if test=" null != imgUrl ">
            and m.`img_url` like concat('%', #{imgUrl},'%')
        </if>
        
        <if test=" null != name and ''!=name ">
            and m.`name` like concat('%', #{name},'%')
        </if>
        
        <if test=" null != linkPhone and ''!=linkPhone ">
            and m.`link_phone` like concat('%', #{linkPhone},'%')
        </if>
        
        <if test=" null != createDate ">
            and m.`create_date` = #{createDate}
        </if>
        
        <if test=" null != updateDate ">
            and m.`update_date` = #{updateDate}
        </if>
        
        <if test=" null != carVendorType ">
            and m.`car_vendor_type` = #{carVendorType}
        </if>
        
        <if test=" null != businessId ">
            and m.`business_id` = #{businessId}
        </if>
        
        <if test=" null != subBusinessId ">
            and m.`sub_business_id` = #{subBusinessId}
        </if>
        
        <if test=" null != carParams ">
            and m.`car_params` like concat('%', #{carParams},'%')
        </if>
        <if test=" null != status ">
            and m.`status` = #{status}
        </if>
        <if test=" null != linkPerson ">
            and m.`link_person` = #{linkPerson}
        </if>
        <if test=" null != type ">
            and m.`type` = #{type}
        </if>
        <if test=" null != isPublic ">
            and m.`is_public` = #{isPublic}
        </if>
        <if test=" null != email ">
            and m.`email` = #{email}
        </if>
        <if test=" null != isAdmin ">
            and m.`is_admin` = #{isAdmin}
        </if>
        <if test=" null != isDel ">
            and m.`is_del` = #{isDel}
        </if>
        <if test=" null != brand ">
            and m.`brand` = #{brand}
        </if>
        <if test=" null != rentShopType ">
            and m.id not in(
            select merchant_id from wx_rent_contract where tenant_id=#{tenantId} and rent_shop_type=1 and merchant_id is
	        not null and status in(1,2,3,4,8)
            )
        </if>
        <if test=" null != ids ">
            and m.id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <include refid="dataRule"/>
    
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>
    
    
    <sql id="allMerchantVoColumns">
        distinct m.id,m.img_url,m.name,m.link_phone,m.status,m.business_id,
		m.is_public,m.email,m.title,m.cover_picture,m.sub_business_id,
		m.shop_type,m.introduction,m.action_desc,bu.title as business_name,m.link_line_phone
	</sql>
    
    <select id="getMerchantByCode" parameterType="java.util.Map" resultMap="MerchantVoResultMap">
        select
        <include refid="allMerchantVoColumns"/>
        FROM wx_merchant m
	    left join wx_business bu on bu.id = m.business_id
        <where>
            <if test=" null != tenantId ">
                and m.`tenant_id` = #{tenantId}
            </if>
            <if test=" null != status ">
                and m.`status` = #{status}
            </if>
            <if test=" null != merchantCode ">
                and md5(m.`id`) = #{merchantCode}
            </if>
        </where>
    </select>
    
    <resultMap id="MerchantVoResultMap" type="com.iformall.domain.vo.WxMerchantVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="img_url" jdbcType="VARCHAR" property="merchantImgUrl"/>
        <result column="name" jdbcType="VARCHAR" property="merchantName"/>
        <result column="link_phone" jdbcType="VARCHAR" property="merchantLinkPhone"/>
        <result column="status" jdbcType="VARCHAR" property="merchantStatus"/>
        <result column="business_id" jdbcType="INTEGER" property="businessId"/>
        <result column="sub_business_id" jdbcType="INTEGER" property="subBusinessId"/>
        <result column="is_public" jdbcType="INTEGER" property="isPublic"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="cover_picture" jdbcType="VARCHAR" property="coverPicture"/>
        <result column="shop_type" jdbcType="VARCHAR" property="shopType"/>
    
        <result column="brand_name" jdbcType="VARCHAR" property="brandName"/>
        <result column="brand_img" jdbcType="VARCHAR" property="brandImg"/>
        <result column="summary" jdbcType="VARCHAR" property="summary"/>
    
        <result column="business_name" jdbcType="VARCHAR" property="businessName"/>

        <result column="introduction" jdbcType="VARCHAR" property="introduction"/>
        <result column="action_desc" jdbcType="VARCHAR" property="actionDesc"/>
        <result column="link_line_phone" property="linkLinePhone"/>

        <collection column="{merchantId=id}" property="shopVoList"
                    ofType="com.iformall.domain.vo.WxShopVo"
                    javaType="java.util.List"
                    select="com.iformall.mapper.WxShopMapper.findShopVoList">
        </collection>
    
    </resultMap>

    <select id="findListCVo" parameterType="com.iformall.domain.dto.WxMerchantDto" resultMap="MerchantVoResultMap">
        select
        <include refid="allMerchantVoColumns"/>
        FROM wx_merchant m
        <if test=" null != building or null != floor ">
            join wx_merchant_shop ms on ms.merchant_id=m.id
            join wx_shop s on ms.shop_id=s.id
            <if test=" null != building">
                and s.building = #{building}
            </if>
            <if test=" null != floor ">
                and s.floor = #{floor}
            </if>
        </if>
	    left join wx_business bu on bu.id = m.business_id
        where 1=1 and m.is_del=0
        <if test=" null != merchantName and ''!=merchantName ">
            and m.`name` like concat('%', #{merchantName},'%')
        </if>

        <if test=" null != merchantLinkPhone and ''!=merchantLinkPhone ">
            and m.`link_phone` like concat('%', #{merchantLinkPhone},'%')
        </if>

        <if test=" null != businessId ">
            and m.`business_id` = #{businessId}
        </if>

        <if test=" null != subBusinessId ">
            and m.`sub_business_id` = #{subBusinessId}
        </if>

        <if test=" null != merchantStatus ">
            and m.`status` = #{merchantStatus}
        </if>
	
	    <if test=" null != isPublic">
            and m.`is_public` = #{isPublic}
        </if>
    
        <if test=" null != id">
            and m.`id` = #{id}
        </if>
    
        order by m.top_time desc
    </select>
    
    <select id="queryMerchantByMerchantIdOfSharingReceiver" resultType="com.iformall.domain.po.WxMerchant"
            parameterType="com.iformall.domain.po.WxMerchant">
        select m.name,psr.id from wx_merchant m
        left join (select * from wx_profit_sharing_receiver where status=0) psr on m.id=psr.merchant_id
        where m.id=#{id}
    </select>

    <select id="findQrcodeEmptyList" resultMap="BaseResultMap">
        select * from wx_merchant where qr_code is null;
    </select>

    <select id="findBusByFloorId" resultType="hashmap" parameterType="hashmap">
        select count(distinct m.id) count,b.id bid,b.`title` btitle from wx_merchant m
        left join wx_merchant_shop ms on(m.id = ms.merchant_id)
        left join wx_shop s on(ms.shop_id = s.id)
        left join wx_mall_floor f on(s.floor = f.id)
        left join wx_mall_building mb on(f.building_id = mb.id)
        left join wx_business b on(m.business_id = b.id)
        where 1=1
        <if test=" null != id">
            and f.id = #{id}
        </if>
        <if test=" null != buildingId">
            and mb.id = #{buildingId}
        </if>
        and m.tenant_id = #{tenantId}
        and s.is_del = 0
        and ms.is_del = 0
        and m.status = 1
        and m.is_del = 0
        group by m.business_id
    </select>
    
    <select id="hasMerchant" parameterType="com.iformall.domain.po.WxMerchant" resultType="int">
        select count(*) from wx_merchant where `name`=#{name} and status=#{status}
        <if test=" null != id">
            and `id` != #{id}
        </if>
    </select>
    
    <update id="updateCreditLocked" parameterType="com.iformall.domain.po.WxMerchant">
        update wx_merchant set credit_locked = #{creditLocked} where tenant_id = #{tenantId}
    </update>

    <select id="userTradeList" parameterType="com.iformall.domain.po.WxMerchant" resultType="com.iformall.domain.vo.WxMerchantTradeVo">
	
	    SELECT m.id, m.`img_url` cover, m.`name` merchantName, m.`link_person` linkName,
	    ifnull(consume.consumeCount,0) consumeCount,ifnull(consume.consumeCountP,0) as consumeCountP,
	    ifnull(consume.tradeAmt,0) tradeAmt
	    FROM wx_merchant m
	    left join (select count(co.c_user_id) AS consumeCountP,
	    count(DISTINCT co.c_user_id) AS consumeCount,sum(co.coupon_price) AS tradeAmt, m.id as merchant_id from
	    wx_coupon_order co
	    left join (select m.id,mbu.id as b_user_id from wx_merchant m
        left join wx_merchant_b_user mbu on m.id=mbu.merchant_id where mbu.status=0) m
        on co.b_user_id=m.b_user_id
	    WHERE co.coupon_order_status = 1
	    <if test="tenantId != null">
		    and co.tenant_id = #{tenantId}
	    </if>
	    <if test="starttime != null">
		    and co.create_date &gt;= #{starttime}
	    </if>
	    <if test="endtime != null">
		    and co.create_date &lt;= #{endtime}
	    </if>
	    group by m.id
	    ) consume on consume.merchant_id=m.id
	    where m.status=1
        <if test=" null != name and ''!=name ">
            and `name` like concat('%', #{name},'%')
        </if>
        <if test=" null != linkPhone and ''!=linkPhone ">
            and `link_phone` like concat('%', #{linkPhone},'%')
        </if>
        <if test=" null != linkPerson and ''!=linkPerson ">
            and `link_person` like concat('%', #{linkPerson},'%')
        </if>

        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </select>

    <select id="userTradeDetailList" parameterType="com.iformall.domain.po.WxMerchant" resultType="com.iformall.domain.vo.WxMerchantTradeDetailVo">
	    select cb.`name` 'userName',cb.`phone`,co.title couponName,co.`sale_price` couponSale ,co.price couponPrice,
	    co.price tradeAmt,o.create_date tradeDate
        from wx_coupon_order o
        left join wx_coupon co on(o.`coupon_id`=co.id)
	    left join wx_c_user_basic_info cb on o.c_user_id=cb.id
        where o.coupon_order_status=1
        and o.tenant_id=#{tenantId}
        <if test=" null != id ">
	        and o.b_user_id in(
	        select id from wx_merchant_b_user where status=0 and merchant_id=#{id}
	        )
        </if>
	
	    <if test=" null != userName and '' != userName ">
	        and cb.`name` like concat('%', #{userName},'%')
        </if>
	    <if test=" null != phone and '' != phone">
	        and cb.`phone` like concat('%', #{phone},'%')
        </if>
	    <if test=" null != couponName and '' != couponName">
            and co.`title` like concat('%', #{couponName},'%')
        </if>
        <if test="starttime != null">
            and o.create_date &gt;= #{starttime}
        </if>
        <if test="endtime != null">
            and o.create_date &lt;= #{endtime}
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
        <if test=" null == sortColumns">order by o.create_date desc</if>

    </select>

    <select id="findByTrade" parameterType="java.util.Map" resultMap="BaseResultMap">
        select * from wx_merchant where id in(
        select merchant_id from wx_coupon_merchant where product_id=#{id}
        )limit 1
    </select>



</mapper>
