<?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.WxShopMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxShop">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="shop_number" jdbcType="VARCHAR" property="shopNumber"/>
        <result column="build_area" jdbcType="VARCHAR" property="buildArea"/>
        <result column="operation_area" jdbcType="VARCHAR" property="operationArea"/>
        <result column="building" jdbcType="BIGINT" property="building"/>
        <result column="floor" jdbcType="BIGINT" property="floor"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="x" jdbcType="DECIMAL" property="x"/>
        <result column="y" jdbcType="DECIMAL" property="y"/>
        <result column="addr" jdbcType="VARCHAR" property="addr"/>
        <result column="baidu_poi" jdbcType="VARCHAR" property="baiduPoi"/>
        <result column="longitude" jdbcType="DECIMAL" property="longitude"/>
        <result column="latitude" jdbcType="DECIMAL" property="latitude"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="img_url" jdbcType="VARCHAR" property="imgUrl"/>
        <result column="manager" jdbcType="VARCHAR" property="manager"/>
        <result column="manager_phone" jdbcType="VARCHAR" property="managerPhone"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="point_type" jdbcType="INTEGER" property="pointType"/>
        <result column="comments" jdbcType="VARCHAR" property="comments"/>
        <result column="is_del" jdbcType="INTEGER" property="isDel"/>
	    <result column="freeDay" property="freeDay"/>
	    <result column="rent" property="rent"/>
	    <result column="rent_unit" property="rentUnit"/>
        <result column="business_id" property="businessId"/>

    </resultMap>

    <sql id="allColumns">
	    `id`,`tenant_id`,`shop_number`,`build_area`,`operation_area`,`building`,`floor`,`status`,`x`,`y`,`addr`,
	    `baidu_poi`,`longitude`,`latitude`,`create_date`,`update_date`,`img_url`,`manager`,`manager_phone`,
	    `type`,`point_type`,`comments`,`is_del`,DATEDIFF(now(),create_date) freeDay,rent,rent_unit,business_id
	</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 != shopNumber and ''!=shopNumber">
            and `shop_number` like concat('%', #{shopNumber},'%')

        </if>

        <if test=" null != buildArea ">
            and `build_area` like concat('%', #{buildArea},'%')

        </if>

        <if test=" null != operationArea ">
            and `operation_area` like concat('%', #{operationArea},'%')

        </if>

        <if test=" null != building ">
            and `building` like concat('%', #{building},'%')

        </if>

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

        </if>

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

        </if>

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

        </if>

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

        </if>

        <if test=" null != addr ">
            and `addr` like concat('%', #{addr},'%')

        </if>

        <if test=" null != baiduPoi ">
            and `baidu_poi` like concat('%', #{baiduPoi},'%')

        </if>

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

        </if>

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

        </if>

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

        </if>

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

        </if>
        <if test=" null != manager ">
            and `manager` = #{manager}
        </if>
        <if test=" null != managerPhone ">
            and `manager_phone` = #{managerPhone}
        </if>
    
        <if test=" null != type ">
            and `type` = #{type}
        </if>
    
        <if test=" null != pointType ">
            and `point_type` = #{pointType}
        </if>

        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findList" parameterType="com.iformall.domain.po.WxShop" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_shop
        <include refid="dynamicWhereConditions"/>
    </select>

    <select id="findListMap" parameterType="com.iformall.domain.po.WxShop" resultType="hashmap">
        select s.id id,s.shop_number shopNumber,CONVERT(s.build_area,DECIMAL(10,2)) buildArea,
        s.img_url imgUrl,CONVERT(s.operation_area,DECIMAL(10,2)) operationArea,
        s.status,b.building_name building,f.floor_name floor,m.`name` merchantName,m.id merchantId,
        m.link_person linkPerson,m.link_phone linkPhone,s.manager,s.manager_phone managerPhone,
        s.type,s.point_type pointType,s.addr,DATEDIFF(now(),s.create_date) freeDay,rent,rent_unit,s.business_id as
        businessId,s.create_date create_date
        from wx_shop s left join wx_merchant_shop ms on ms.shop_id=s.id and ms.is_del=0
        left join wx_merchant m on ms.merchant_id=m.id
        left join wx_mall_building b  on s.building=b.id
        left join wx_mall_floor f on s.floor=f.id
	    where s.is_del=0
	
	    <if test=" null != type ">
		    and s.`type` = #{type}
	    </if>
	
	    <if test=" null != status">
		    and s.`status` = #{status}
	    </if>
	
	    <if test=" null != building ">
		    and s.`building`= #{building}
	    </if>
	
	    <if test=" null != floor ">
		    and s.`floor` = #{floor}
	    </if>
	
	    <if test=" null != id ">
            and s.`id` = #{id}
        </if>

        <if test=" null != shopNumber and ''!=shopNumber">
            and s.`shop_number` like concat('%', #{shopNumber},'%')

        </if>

        <if test=" null != buildArea ">
            and s.`build_area` like concat('%', #{buildArea},'%')

        </if>

        <if test=" null != operationArea ">
            and s.`operation_area` like concat('%', #{operationArea},'%')

        </if>

        <if test=" null != x ">
            and s.`x` = #{x}

        </if>

        <if test=" null != y ">
            and s.`y` = #{y}

        </if>

        <if test=" null != addr ">
            and s.`addr` like concat('%', #{addr},'%')

        </if>

        <if test=" null != baiduPoi ">
            and s.`baidu_poi` like concat('%', #{baiduPoi},'%')

        </if>

        <if test=" null != longitude ">
            and s.`longitude` = #{longitude}

        </if>

        <if test=" null != latitude ">
            and s.`latitude` = #{latitude}

        </if>

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

        </if>

        <if test=" null != updateDate ">
            and s.`update_date` = #{updateDate}
        </if>
    
        <if test=" null != manager ">
            and s.`manager` = #{manager}
        </if>
        <if test=" null != managerPhone ">
            and s.`manager_phone` = #{managerPhone}
        </if>
	
	    <if test=" null != pointType ">
            and s.`point_type` = #{pointType}
        </if>
        <if test=" null != ids ">
            and s.id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>

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

    </select>

    <select id="notRentListMapAsPage" parameterType="com.iformall.domain.po.WxShop" resultType="hashmap">
	    select s.id as id,s.shop_number shopNumber,CONVERT(s.build_area,SIGNED)buildArea,
        s.img_url imgUrl,CONVERT(s.operation_area,SIGNED) operationArea,
        s.status,b.building_name building,f.floor_name floor,s.manager,s.manager_phone managerPhone,
	    s.type,s.point_type pointType,s.addr,DATEDIFF(now(),s.create_date) freeDay,s.create_date as create_date
        from wx_shop s
        left join wx_mall_building b  on s.building=b.id
        left join wx_mall_floor f on s.floor=f.id
        where s.is_del=0

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

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

        <if test=" null != shopNumber and ''!=shopNumber">
            and s.`shop_number` like concat('%', #{shopNumber},'%')
        </if>
        <if test=" null != status">
            and s.`status` = #{status}
        </if>
        <if test=" null != type ">
            and s.`type` = #{type}
        </if>
        <if test=" null != pointType ">
            and s.`point_type` = #{pointType}
        </if>
        <if test="null != sortColumn">
            order by ${sortColumn} ${sortOrder}
        </if>
        <if test="null == sortColumn">
            <if test=" null != sortColumns">order by ${sortColumns}</if>
        </if>
    </select>


    <resultMap id="VoResultMap" type="com.iformall.domain.vo.WxShopVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="shop_number" jdbcType="VARCHAR" property="shopNumber"/>
        <result column="build_area" jdbcType="VARCHAR" property="buildArea"/>
        <result column="operation_area" jdbcType="VARCHAR" property="operationArea"/>
        <result column="building" jdbcType="BIGINT" property="building"/>
        <result column="floor" jdbcType="BIGINT" property="floor"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="x" jdbcType="DECIMAL" property="x"/>
        <result column="y" jdbcType="DECIMAL" property="y"/>
        <result column="addr" jdbcType="VARCHAR" property="addr"/>
        <result column="baidu_poi" jdbcType="VARCHAR" property="baiduPoi"/>
        <result column="longitude" jdbcType="DECIMAL" property="longitude"/>
        <result column="latitude" jdbcType="DECIMAL" property="latitude"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="img_url" jdbcType="VARCHAR" property="imgUrl"/>
        <result column="manager" jdbcType="VARCHAR" property="manager"/>
        <result column="manager_phone" jdbcType="VARCHAR" property="managerPhone"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="point_type" jdbcType="INTEGER" property="pointType"/>
        <result column="comments" jdbcType="VARCHAR" property="comments"/>
        <result column="building_name" jdbcType="VARCHAR" property="buildingName"/>
        <result column="floor_name" jdbcType="VARCHAR" property="floorName"/>


        <result column="merchantId" jdbcType="VARCHAR" property="merchantId"/>
        <result column="merchantName" jdbcType="VARCHAR" property="merchantName"/>
        <result column="link_phone" jdbcType="VARCHAR" property="linkPhone"/>
        <result column="link_person" jdbcType="VARCHAR" property="linkPerson"/>

        <result column="business" jdbcType="VARCHAR" property="business"/>

        <result column="brandName" jdbcType="VARCHAR" property="brandName"/>

    </resultMap>


    <select id="findListWithMerchantMap" parameterType="com.iformall.domain.vo.WxShopVo" resultMap="VoResultMap">
        select
	    s.id as
	    id,s.shop_number,s.build_area,s.img_url,s.operation_area,s.status,s.manager,s.manager_phone,s.addr,s.type,s.point_type,
        b.building_name,f.floor_name,
        m.id merchantId,m.name merchantName,m.link_person,m.link_phone,
        brand.name brandName,
	    bus.title business,s.create_date as create_date
        from wx_shop s left join wx_merchant_shop ms on ms.shop_id=s.id and ms.is_del=0
        left join wx_merchant m on ms.merchant_id=m.id
        left join wx_mall_building b  on s.building=b.id
        left join wx_mall_floor f on s.floor=f.id
        left join wx_business bus on m.business_id = bus.id
        left join wx_brand brand on m.brand = brand.id
        where s.is_del=0

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

        </if>

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

        </if>

        <if test=" null != shopNumber and ''!=shopNumber">
            and s.`shop_number` like concat('%', #{shopNumber},'%')

        </if>

        <if test=" null != buildArea ">
            and s.`build_area` like concat('%', #{buildArea},'%')

        </if>

        <if test=" null != operationArea ">
            and s.`operation_area` like concat('%', #{operationArea},'%')

        </if>

        <if test=" null != building ">
            and s.`building`= #{building}

        </if>

        <if test=" null != floor ">
            and s.`floor` = #{floor}

        </if>

        <if test=" null != status">
            and s.`status` = #{status}

        </if>

        <if test=" null != x ">
            and s.`x` = #{x}

        </if>

        <if test=" null != y ">
            and s.`y` = #{y}

        </if>

        <if test=" null != addr ">
            and s.`addr` like concat('%', #{addr},'%')

        </if>

        <if test=" null != baiduPoi ">
            and s.`baidu_poi` like concat('%', #{baiduPoi},'%')

        </if>

        <if test=" null != longitude ">
            and s.`longitude` = #{longitude}

        </if>

        <if test=" null != latitude ">
            and s.`latitude` = #{latitude}

        </if>

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

        </if>

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

        <if test=" null != manager ">
            and s.`manager` = #{manager}
        </if>
        <if test=" null != managerPhone ">
            and s.`manager_phone` = #{managerPhone}
        </if>
        <if test=" null != type ">
            and s.`type` = #{type}
        </if>
        <if test=" null != pointType ">
            and s.`point_type` = #{pointType}
        </if>
    
        <if test="null != businessId">
            and s.business_id = #{businessId}
        </if>
    
        <if test=" null != ids ">
            and s.id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>



    </select>

    <select id="getbshoplist" resultType="hashmap" parameterType="hashmap">
        select id,shop_number shopNumber from wx_shop
        where tenant_id=#{tenantId} and shop_number = #{shopNumber} and is_del=0
    </select>
    
    <select id="getMerchantShopByShopId" resultType="hashmap" parameterType="hashmap">
        select m.`name` merchantName,s.build_area buildArea,s.operation_area operationArea,
        c.price,c.receive_period receivePeriod,s.shop_number shopNumber,m.link_person linkPerson,
        m.link_phone linkPhone
        from wx_merchant_shop ms inner join wx_merchant m on ms.merchant_id=m.id
        inner join wx_shop s on ms.shop_id=s.id
        inner join wx_rent_contract c on ms.merchant_id=c.merchant_id
        where ms.tenant_id=#{tenantId} and ms.shop_id=#{shopId} and ms.is_del=0 and s.is_del=0
    </select>
    
	<select id="queryShopLeftInfo" resultType="hashmap" parameterType="hashmap">
        select count(id) shopcount,IFNULL(sum(build_area),0) area from wx_shop
        where tenant_id=#{tenantId} and status=#{status} and is_del=0
	</select>

    <select id="listShopFromContract" parameterType="com.iformall.domain.po.WxShop" resultType="hashmap">
        select s.id id,s.shop_number shopNumber,s.build_area buildArea,
        s.img_url imgUrl,s.operation_area operationArea,
        s.status,b.building_name building,f.floor_name floor,m.`name` merchantName,m.id merchantId,
        m.link_person linkPerson,m.link_phone linkPhone,s.manager,s.manager_phone managerPhone,
        m.brand,m.business_id businessId,m.shop_type shopType,s.create_date create_date
        from wx_shop s left join wx_merchant_shop ms on ms.shop_id=s.id
        left join wx_merchant m on ms.merchant_id=m.id
        left join wx_mall_building b  on s.building=b.id
        left join wx_mall_floor f on s.floor=f.id
        where s.is_del=0 and ms.is_del=0
        and s.tenant_id=#{tenantId}
        <if test=" null != status">
            and s.`status` = #{status}
        </if>
	    <if test="null!=status and status==1 and type!=null and type==2">
            and s.id not in(
            SELECT shop_id FROM wx_rent_contract WHERE tenant_id = #{tenantId} and status in(1,2,3,4)
            and rent_shop_type=2
            )
        </if>
        <if test=" null != type ">
            and s.`type` = #{type}
        </if>
    
        <if test=" null != pointType ">
            and s.`point_type` = #{pointType}
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </select>
    
    
    <select id="hasShopNumber" parameterType="com.iformall.domain.po.WxShop" resultType="Integer">
        select count(*) from wx_shop where tenant_id=#{tenantId} and shop_number=#{shopNumber} and `type`=#{type} and
        is_del=0
        <if test=" null != id">
            and id != #{id}
        </if>
    </select>
    
    <resultMap id="ShopVoResultMap" type="com.iformall.domain.vo.WxShopVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="addr" jdbcType="VARCHAR" property="addr"/>
        <result column="shop_number" jdbcType="VARCHAR" property="shopNumber"/>
        <result column="building_name" jdbcType="VARCHAR" property="buildingName"/>
        <result column="floor_name" jdbcType="VARCHAR" property="floorName"/>
        <result column="baidu_poi" jdbcType="VARCHAR" property="baiduPoi"/>
        <result column="manager_phone" jdbcType="VARCHAR" property="linkPhone"/>
        <result column="manager" jdbcType="VARCHAR" property="linkPerson"/>
    </resultMap>


    <resultMap id="rentRate" type="com.iformall.domain.vo.WxShopRentRateVo">
        <result column="xTime" property="xTime"/>
        <result column="rate" property="rate"/>
    </resultMap>
    
    <select id="findShopVoList" parameterType="java.util.Map" resultMap="ShopVoResultMap">
		select s.id,s.shop_number,b.building_name,f.floor_name,s.addr,s.baidu_poi,s.manager_phone,s.manager
        from wx_shop s left join wx_merchant_shop ms on ms.shop_id=s.id
        left join wx_merchant m on ms.merchant_id=m.id
        left join wx_mall_building b  on s.building=b.id
        left join wx_mall_floor f on s.floor=f.id
        where s.is_del=0 and ms.is_del=0
        and m.id=#{merchantId}
	</select>

    <select id="getCountByWxShop" parameterType="com.iformall.domain.po.WxShop" resultType="Long">
        select count(*) from wx_shop w where w.is_del=0
        <if test="null != type">
            and `type`=#{type}
        </if>
        <if test="null != status">
            and status = #{status}
        </if>
        <if test="null != building">
            and building = #{building}
        </if>
        <if test="null != floor">
            and floor = #{floor}
        </if>
        <if test="null != startdate">
            and `create_date` >= #{startdate}
        </if>
        <if test="null != enddate">
            and `create_date` &lt;= #{enddate}
        </if>
        <if test="null != businessId and businessId!=0">
            and business_id = #{businessId}
        </if>
    </select>

    <select id="rentRateHistory" resultType="hashmap" parameterType="hashmap">
        select
        <if test="1 == dateType">
            DATE_FORMAT(w.create_date,'%Y/%m/%d') xTime
        </if>
        <if test="2 == dateType">
            DATE_FORMAT(w.create_date,'%Y/%m') xTime
        </if>
        ,count(distinct w.shop_id) xCount
        from wx_merchant_shop w left join wx_shop s on w.shop_id=s.id
        where w.is_del=0
        and w.tenant_id = #{tenantId}
        <if test="building!=null">
            and s.building=#{building}
        </if>
        <if test="floor!=null">
            and s.floor=#{floor}
        </if>
        <if test="businessId!=null and businessId!=0">
            and s.business_id=#{businessId}
        </if>
        group by xTime
        order by xTime
    </select>
</mapper>
