MerchantCustomerInfoMapper.xml 14.9 KB
<?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="cn.com.glsx.dj.customer.mapper.MerchantCustomerInfoMapper">
  <resultMap id="BaseResultMap" type="cn.com.glsx.dj.customer.model.MerchantCustomerInfo">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="MERCHANT_ID" jdbcType="INTEGER" property="merchantId" />
    <result column="ACCOUNT_ID" jdbcType="INTEGER" property="accountId" />
    <result column="PHONE" jdbcType="VARCHAR" property="phone" />
    <result column="USER_NAME" jdbcType="VARCHAR" property="userName" />
    <result column="GENDER" jdbcType="CHAR" property="gender" />
    <result column="BIZ_OPEN_ID" jdbcType="VARCHAR" property="bizOpenId" />
    <result column="WEAPP_OPEN_ID" jdbcType="VARCHAR" property="weappOpenId" />
    <result column="UNION_ID" jdbcType="VARCHAR" property="unionId" />
    <result column="BIND_DEVICE" jdbcType="CHAR" property="bindDevice" />
    <result column="OUTER_IMPORT" jdbcType="CHAR" property="outerImport" />
    <result column="STAND_NO" jdbcType="VARCHAR" property="standNo" />
    <result column="SOURCES_DETAIL" jdbcType="VARCHAR" property="sourcesDetail" />
    <result column="LAST_LOGIN_TIME" jdbcType="TIMESTAMP" property="lastLoginTime" />
    <result column="CREATED_BY" jdbcType="VARCHAR" property="createdBy" />
    <result column="CREATED_DATE" jdbcType="TIMESTAMP" property="createdDate" />
    <result column="UPDATED_BY" jdbcType="VARCHAR" property="updatedBy" />
    <result column="UPDATED_DATE" jdbcType="TIMESTAMP" property="updatedDate" />
    <result column="DELETED_FLAG" jdbcType="CHAR" property="deletedFlag" />
  </resultMap>
  
  <resultMap id="BaseResultVOMap" type="cn.com.glsx.dj.customer.vo.MerchantCustomerRefVO">
	<result column="haveDeviceCount" jdbcType="INTEGER" property="haveDeviceCount" />
	<result column="noDeviceCount" jdbcType="INTEGER" property="noDeviceCount" />
	<result column="shopCount" jdbcType="INTEGER" property="shopCount" />
	<result column="outerImportCount" jdbcType="INTEGER" property="outerImportCount" />
	<result column="webAppOpenIdCount" jdbcType="INTEGER" property="webAppOpenIdCount" />
	<result column="merchantCustomerCount" jdbcType="INTEGER" property="merchantCustomerCount" />
	<result column="haveDevicePercentage" jdbcType="DOUBLE" property="haveDevicePercentage" />
	<result column="noDevicePercentage" jdbcType="DOUBLE" property="noDevicePercentage" />
  </resultMap>
  
  <resultMap type="cn.com.glsx.dj.customer.dto.MerchantCustomerDTO" id="DTOMap" extends="BaseResultMap">
  	<result column="RECEIVED_COUPON_NUM" jdbcType="INTEGER" property="receivedCouponNum" />
    <result column="VERIFIED_COUPON_NUM" jdbcType="INTEGER" property="verifiedCouponNum" />
    <result column="SUCCESSED_ORDER_NUM" jdbcType="INTEGER" property="successedOrderNum" />
    <result column="SUCCESSED_ORDER_PAID" jdbcType="DOUBLE" property="successedOrderPaid" />
  	<result column="has_binded_eapp" jdbcType="CHAR" property="hasBindedWeapp"/>
  	<result column="has_orderd" jdbcType="CHAR" property="hasOrderd"/>
  	<result column="merchant_name" jdbcType="VARCHAR" property="merchantName"/>
	<result column="sn" jdbcType="VARCHAR" property="sn"/>
  </resultMap>
  
  <sql id="Base_Column_List">
    ID, MERCHANT_ID, ACCOUNT_ID, PHONE, USER_NAME, GENDER, BIZ_OPEN_ID, WEAPP_OPEN_ID, 
    UNION_ID, BIND_DEVICE, OUTER_IMPORT, STAND_NO, SOURCES_DETAIL, LAST_LOGIN_TIME, CREATED_BY, 
    CREATED_DATE, UPDATED_BY, UPDATED_DATE, DELETED_FLAG
  </sql>
  
  <sql id="Alias_Column_List">
  	c.ID, c.MERCHANT_ID, c.ACCOUNT_ID, c.PHONE, c.USER_NAME, c.BIZ_OPEN_ID, c.WEAPP_OPEN_ID, c.UNION_ID, 
  	c.BIND_DEVICE, c.OUTER_IMPORT, c.STAND_NO, c.GENDER, c.SOURCES_DETAIL, c.LAST_LOGIN_TIME, 
  	c.CREATED_BY, c.CREATED_DATE, c.UPDATED_BY, c.UPDATED_DATE, c.DELETED_FLAG
  </sql>
  
  <sql id="Tables_Column_List">
  	ID, MERCHANT_ID, ACCOUNT_ID, PHONE, USER_NAME, GENDER, BIZ_OPEN_ID, WEAPP_OPEN_ID, 
    UNION_ID, BIND_DEVICE, OUTER_IMPORT, STAND_NO, SOURCES_DETAIL, LAST_LOGIN_TIME, CREATED_BY, 
    CREATED_DATE, UPDATED_BY, UPDATED_DATE, DELETED_FLAG, RECEIVED_COUPON_NUM, VERIFIED_COUPON_NUM,
    SUCCESSED_ORDER_NUM,SUCCESSED_ORDER_PAID, has_binded_eapp,has_orderd,merchant_name
  </sql>
  <sql id="batchMergeSql">
  	INSERT INTO dj_merchant_customer_info (
  		MERCHANT_ID, 
  		PHONE, 
  		USER_NAME,
  		BIND_DEVICE, 
  		OUTER_IMPORT, 
  		STAND_NO,
  		GENDER,
  		SOURCES_DETAIL,
  		CREATED_BY, 
  		CREATED_DATE, 
    	UPDATED_BY, 
    	UPDATED_DATE, 
    	DELETED_FLAG
  	)
  	VALUES
  	<foreach collection="list" item="info" index="index" separator=",">
  	(
  		#{info.merchantId},
  		#{info.phone},
  		#{info.userName},
  		#{info.bindDevice},
  		#{info.outerImport},
  		#{info.standNo},
  		#{info.gender},
  		#{info.sourcesDetail},
  		#{info.phone},
  		<if test="info.createdDate != null">
  			#{info.createdDate},
  		</if>
  		<if test="info.createdDate == null">
  			NOW(),
  		</if>
  		#{info.phone},
  		NOW(),
  		'N'
  	)
  	</foreach>
  	ON DUPLICATE KEY
  	UPDATE
  		UPDATED_BY = VALUES(PHONE),
  		UPDATED_DATE = NOW(),
  </sql>
  
  <insert id="batchMergeForImportUser" parameterType="java.util.List">
  	<include refid="batchMergeSql"></include>
  	OUTER_IMPORT = 'Y',
  	USER_NAME = CASE BIND_DEVICE WHEN 'Y' THEN IFNULL(USER_NAME,VALUES(USER_NAME)) ELSE IFNULL(VALUES(USER_NAME), USER_NAME) END
  </insert>
  
  <insert id="batchMergeForHardwareUser" parameterType="java.util.List">
  	<include refid="batchMergeSql"></include>
  	STAND_NO = VALUES(STAND_NO),
  	GENDER = VALUES(GENDER),
  	USER_NAME = VALUES(USER_NAME),
  	BIND_DEVICE = IFNULL(VALUES(BIND_DEVICE), BIND_DEVICE)
  </insert>
  
  <select id="queryByMerchantId" parameterType="java.lang.Integer" resultMap="BaseResultVOMap">
		  select
		  sum(case when BIND_DEVICE ='Y' then 1 ELSE 0 end) AS haveDeviceCount,
		  sum(case when BIND_DEVICE ='N' or BIND_DEVICE is null then 1 ELSE 0 end) AS noDeviceCount,
		  sum(case when BIND_DEVICE is not null and (OUTER_IMPORT = 'N' or OUTER_IMPORT is null) and WEAPP_OPEN_ID is null then 1 ELSE 0 end) AS shopCount,
		  sum(case when OUTER_IMPORT = 'Y' AND WEAPP_OPEN_ID is NULL then 1 ELSE 0 end) AS outerImportCount,
		  sum(case when WEAPP_OPEN_ID is NOT NULL then 1 ELSE 0 end) AS webAppOpenIdCount,

		  sum(case when BIND_DEVICE is not null and (OUTER_IMPORT != 'Y' or OUTER_IMPORT is null) and WEAPP_OPEN_ID is null then 1 ELSE 0 end)+
		  SUM(case when OUTER_IMPORT = 'Y' AND WEAPP_OPEN_ID is NULL then 1 ELSE 0 end)+
		  SUM(case when WEAPP_OPEN_ID is NOT NULL then 1 ELSE 0 end) AS merchantCustomerCount,

		  cast(sum(case when BIND_DEVICE ='Y' then 1 ELSE 0 end)/count(id)*100 as decimal(18,2)) AS haveDevicePercentage,
		  cast(100-sum(case when BIND_DEVICE ='Y' then 1 ELSE 0 end)/count(id)*100 as decimal(18,2)) AS noDevicePercentage
		  from dj_merchant_customer_info
		  WHERE DELETED_FLAG = 'N'
		<if test="merchantId != null">
			and MERCHANT_ID = #{merchantId, jdbcType=INTEGER}
		</if>
	</select>
  
  <select id="pageMerchanrCustomer" parameterType="cn.com.glsx.dj.customer.dto.MerchantCustomerDTO"
  	resultMap="DTOMap">
  	select <include refid="Tables_Column_List"/>
  	from (
  		select <include refid="Alias_Column_List" /> ,(if(c.WEAPP_OPEN_ID is null,'N','Y')) has_binded_eapp
  	,(if(c.BIND_DEVICE='Y', 'Y','N')) has_orderd, s.merchant_name,
  	IF(m.RECEIVED_COUPON_NUM is null,0,m.RECEIVED_COUPON_NUM) as RECEIVED_COUPON_NUM,
  	IF(m.VERIFIED_COUPON_NUM is null,0,m.VERIFIED_COUPON_NUM) as VERIFIED_COUPON_NUM,
  	IF(m.SUCCESSED_ORDER_NUM is null,0,m.SUCCESSED_ORDER_NUM) as SUCCESSED_ORDER_NUM,
  	IF(m.SUCCESSED_ORDER_PAID is null,0.0,m.SUCCESSED_ORDER_PAID) as SUCCESSED_ORDER_PAID
  	from dj_merchant_customer_info c 
  	left join dj_system_merchant s on c.merchant_id = s.merchant_id
  	left join dj_merchant_customer_marketing m on c.merchant_id = m.merchant_id and c.PHONE = m.PHONE
  	where c.DELETED_FLAG = 'N'
  	AND (c.WEAPP_OPEN_ID is NOT NULL OR c.BIND_DEVICE is NOT NULL OR c.OUTER_IMPORT = 'Y')
  	<if test="merchantId != null">
  		and c.MERCHANT_ID = #{merchantId, jdbcType=INTEGER}
  	</if>
    <if test="merchantIds != null and merchantIds.size() > 0">
	    and c.MERCHANT_ID in
	    <foreach collection="merchantIds" index="index" item="item"
			   open="(" separator="," close=")">
		    #{item}
	    </foreach>
    </if>
  	<if test='hasOrderd != null and hasOrderd == "N"'>
		and (c.OUTER_IMPORT = 'Y' or c.WEAPP_OPEN_ID is not null)
  	</if>
  	<if test='hasOrderd != null and hasOrderd == "Y"'>
  		and c.BIND_DEVICE is not null and (c.OUTER_IMPORT != 'Y' or c.OUTER_IMPORT is null) and c.WEAPP_OPEN_ID is null
  	</if>
  	<if test='hasBindedWeapp != null and hasBindedWeapp == "Y"'>
  		and c.WEAPP_OPEN_ID is not null
  	</if>
  	<if test='hasBindedWeapp != null and hasBindedWeapp == "N"'>
  		and c.WEAPP_OPEN_ID is null
  	</if>
  	<if test="selectCondition != null and selectCondition != '' ">
  		and (
  		c.STAND_NO LIKE CONCAT('%', #{selectCondition, jdbcType=VARCHAR}, '%')
  		or
  		c.PHONE LIKE CONCAT('%', #{selectCondition, jdbcType=VARCHAR}, '%')
  		or
  		c.USER_NAME LIKE CONCAT('%', #{selectCondition, jdbcType=VARCHAR}, '%')
  		)
  	</if>
  	) as t
  	where 1=1 
  	<if test="receivedCouponNumMin != null">
  		<![CDATA[and RECEIVED_COUPON_NUM >= #{receivedCouponNumMin, jdbcType=INTEGER}]]>
  	</if>
  	<if test="receivedCouponNumMax != null">
  		<![CDATA[and RECEIVED_COUPON_NUM <= #{receivedCouponNumMax, jdbcType=INTEGER}]]>
  	</if>
  	<if test="successedOrderNumMin != null">
  		<![CDATA[and SUCCESSED_ORDER_NUM >= #{successedOrderNumMin, jdbcType=INTEGER}]]>
  	</if>
  	<if test="successedOrderNumMax != null">
  		<![CDATA[and SUCCESSED_ORDER_NUM <= #{successedOrderNumMax, jdbcType=INTEGER}]]>
  	</if>
  	<if test="verifiedCouponNumMin != null">
  		<![CDATA[and VERIFIED_COUPON_NUM >= #{verifiedCouponNumMin, jdbcType=INTEGER}]]>
  	</if>
  	<if test="verifiedCouponNumMax != null">
  		<![CDATA[and VERIFIED_COUPON_NUM <= #{verifiedCouponNumMax, jdbcType=INTEGER}]]>
  	</if>
  	<if test="successedOrderPaidMin != null">
  		<![CDATA[and SUCCESSED_ORDER_PAID >= #{successedOrderPaidMin, jdbcType=DOUBLE}]]>
  	</if>
  	<if test="successedOrderPaidMax != null">
  		<![CDATA[and SUCCESSED_ORDER_PAID <= #{successedOrderPaidMax, jdbcType=DOUBLE}]]>
  	</if>
  	ORDER BY CREATED_DATE DESC
  </select>
  
  <select id="listMerchanrCustomer" parameterType="cn.com.glsx.dj.customer.dto.MerchantCustomerDTO"
  	resultMap="DTOMap">
  	select <include refid="Tables_Column_List"/>
  	from (
  		select <include refid="Alias_Column_List" /> ,(if(c.WEAPP_OPEN_ID is null,'N','Y')) has_binded_eapp
  	,(if(c.BIND_DEVICE='Y', 'Y','N')) has_orderd, s.merchant_name,
  	IF(m.RECEIVED_COUPON_NUM is null,0,m.RECEIVED_COUPON_NUM) as RECEIVED_COUPON_NUM,
  	IF(m.VERIFIED_COUPON_NUM is null,0,m.VERIFIED_COUPON_NUM) as VERIFIED_COUPON_NUM,
  	IF(m.SUCCESSED_ORDER_NUM is null,0,m.SUCCESSED_ORDER_NUM) as SUCCESSED_ORDER_NUM,
  	IF(m.SUCCESSED_ORDER_PAID is null,0.0,m.SUCCESSED_ORDER_PAID) as SUCCESSED_ORDER_PAID
  	from dj_merchant_customer_info c 
  	left join dj_system_merchant s on c.merchant_id = s.merchant_id
  	left join dj_merchant_customer_marketing m on c.merchant_id = m.merchant_id and c.PHONE = m.PHONE
  	where c.DELETED_FLAG = 'N'
  	AND (c.WEAPP_OPEN_ID is NOT NULL OR c.BIND_DEVICE is NOT NULL OR c.OUTER_IMPORT = 'Y')
  	<if test="merchantId != null">
  		and c.MERCHANT_ID = #{merchantId, jdbcType=INTEGER}
  	</if>
	<if test="merchantIds != null and merchantIds.size() > 0">
		and c.MERCHANT_ID in
		<foreach collection="merchantIds" index="index" item="item"
			   open="(" separator="," close=")">
			#{item}
		</foreach>
	</if>
  	<if test='hasOrderd != null and hasOrderd == "N"'>
		and (c.OUTER_IMPORT = 'Y' or c.WEAPP_OPEN_ID is not null)
  	</if>
  	<if test='hasOrderd != null and hasOrderd == "Y"'>
		and c.BIND_DEVICE is not null and (c.OUTER_IMPORT != 'Y' or c.OUTER_IMPORT is null) and c.WEAPP_OPEN_ID is null
  	</if>
  	<if test='hasBindedWeapp != null and hasBindedWeapp == "Y"'>
  		and c.WEAPP_OPEN_ID is not null
  	</if>
  	<if test='hasBindedWeapp != null and hasBindedWeapp == "N"'>
  		and c.WEAPP_OPEN_ID is null
  	</if>
  	<if test="selectCondition != null and selectCondition != '' ">
  		and (
  		c.STAND_NO LIKE CONCAT('%', #{selectCondition, jdbcType=VARCHAR}, '%')
  		or
  		c.PHONE LIKE CONCAT('%', #{selectCondition, jdbcType=VARCHAR}, '%')
  		or
  		c.USER_NAME LIKE CONCAT('%', #{selectCondition, jdbcType=VARCHAR}, '%')
  		)
  	</if>
  	) as t
  	where 1=1 
  	<if test="receivedCouponNumMin != null">
  		<![CDATA[and RECEIVED_COUPON_NUM >= #{receivedCouponNumMin, jdbcType=INTEGER}]]>
  	</if>
  	<if test="receivedCouponNumMax != null">
  		<![CDATA[and RECEIVED_COUPON_NUM <= #{receivedCouponNumMax, jdbcType=INTEGER}]]>
  	</if>
  	<if test="successedOrderNumMin != null">
  		<![CDATA[and SUCCESSED_ORDER_NUM >= #{successedOrderNumMin, jdbcType=INTEGER}]]>
  	</if>
  	<if test="successedOrderNumMax != null">
  		<![CDATA[and SUCCESSED_ORDER_NUM <= #{successedOrderNumMax, jdbcType=INTEGER}]]>
  	</if>
  	<if test="verifiedCouponNumMin != null">
  		<![CDATA[and VERIFIED_COUPON_NUM >= #{verifiedCouponNumMin, jdbcType=INTEGER}]]>
  	</if>
  	<if test="verifiedCouponNumMax != null">
  		<![CDATA[and VERIFIED_COUPON_NUM <= #{verifiedCouponNumMax, jdbcType=INTEGER}]]>
  	</if>
  	<if test="successedOrderPaidMin != null">
  		<![CDATA[and SUCCESSED_ORDER_PAID >= #{successedOrderPaidMin, jdbcType=DOUBLE}]]>
  	</if>
  	<if test="successedOrderPaidMax != null">
  		<![CDATA[and SUCCESSED_ORDER_PAID <= #{successedOrderPaidMax, jdbcType=DOUBLE}]]>
  	</if>
  	ORDER BY CREATED_DATE DESC
  </select>
  
  <select id="countAll" resultType="java.lang.Integer">
  	select count(*) from dj_merchant_customer_info 
  	where DELETED_FLAG = 'N'
  	AND (WEAPP_OPEN_ID is NOT NULL OR BIND_DEVICE is not null OR OUTER_IMPORT = 'Y')
  </select>
  
  <select id="countMerchantIds" parameterType="java.util.List" resultType="java.lang.Integer">
  	select count(*) from dj_merchant_customer_info
  	where DELETED_FLAG = 'N'
  	AND (WEAPP_OPEN_ID is NOT NULL OR BIND_DEVICE is not null OR OUTER_IMPORT = 'Y')
  	AND MERCHANT_ID IN
  	<foreach collection="list" index="index" item="item"
		open="(" separator="," close=")">
		#{item}
	</foreach>
  </select>
  
  <select id="selectDeviceByMerchantId" parameterType="java.lang.Integer" resultMap="DTOMap">
	SELECT o.sub_device_sn as sn 
	from dj_merchant_customer_info info left JOIN dj_insure_order o ON info.STAND_NO=o.stand_no
	WHERE o.insurance_status=1 AND info.BIND_DEVICE = 'Y' AND info.DELETED_FLAG='N' AND o.deleted_flag='N' AND o.sub_device_sn is NOT NULL
	<if test="merchantId != null">
		and info.MERCHANT_ID = #{merchantId, jdbcType=INTEGER}
	</if>
  </select>
  
  <select id="queryCustomerMerchantRef" resultType="cn.com.glsx.dj.customer.dto.CustomerMerchantRefDTO" parameterType="java.lang.Long">
  	SELECT
		ID id,
		PHONE mobile,
		MERCHANT_ID merchantId
	FROM
		dj_merchant_customer_info 
	WHERE
		ID > #{lastId} 
		AND PHONE IS NOT NULL 
	ORDER BY
		ID 
  </select>
</mapper>