<?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.qunzhixinxi.hnqz.admin.mapper.WmDaCustomerMapper" >
	<resultMap id="BaseResultMap" type="com.qunzhixinxi.hnqz.admin.entity.WmDaCustomer" >
		<result column="id" property="id" jdbcType="INTEGER" />
		<result column="dept_id" property="deptId" jdbcType="INTEGER" />
		<result column="customer_number" property="customerNumber" jdbcType="VARCHAR" />
		<result column="customer_name" property="customerName" jdbcType="VARCHAR" />
		<result column="customer_type" property="customerType" jdbcType="VARCHAR" />
		<result column="customer_manager" property="customerManager" jdbcType="VARCHAR" />
		<result column="channel_name" property="channelName" jdbcType="VARCHAR" />
		<result column="customer_status" property="customerStatus" jdbcType="VARCHAR" />
		<result column="remark" property="remark" jdbcType="VARCHAR" />
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
		<result column="create_user" property="createUser" jdbcType="INTEGER" />
		<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
		<result column="update_user" property="updateUser" jdbcType="INTEGER" />
		<!-- 合同列表 -->
		<collection property="contractList" javaType="java.util.ArrayList" ofType="com.qunzhixinxi.hnqz.admin.entity.Contract">
			<result column="title" property="title"/>
			<result column="serial_no" property="serialNo"/>
			<result column="sign_time" property="signTime"/>
			<result column="expiration_time" property="expirationTime"/>
			<result column="pic_url" property="picUrl"/>
		</collection>
		<!-- 跟踪信息列表 -->
		<collection property="wmDaTrackList" javaType="java.util.ArrayList" ofType="com.qunzhixinxi.hnqz.admin.entity.WmDaTrack">
			<result column="trace_principal_name" property="tracePrincipalName" jdbcType="VARCHAR" />
			<result column="trace_object_name" property="traceObjectName" jdbcType="VARCHAR" />
			<result column="status" property="status" jdbcType="INTEGER" />
		</collection>
	</resultMap>

	<!-- 查询列表用 -->
	<resultMap id="customerWithGmvMap" type="com.qunzhixinxi.hnqz.admin.entity.WmDaCustomer" >
		<result column="id" property="id" jdbcType="INTEGER" />
		<result column="customer_name" property="customerName" jdbcType="VARCHAR" />
		<result column="customer_type" property="customerType" jdbcType="VARCHAR" />
		<result column="customer_manager" property="customerManager" jdbcType="VARCHAR" />
		<result column="channel_name" property="channelName" jdbcType="VARCHAR" />
		<collection property="deptGmvList" javaType="java.util.ArrayList" ofType="com.qunzhixinxi.hnqz.admin.entity.WmDeptGmv">
			<result property="id" column="gmv_id"/>
			<result property="deptId" column="dept_id"/>
			<result property="year" column="year"/>
			<result property="month" column="month"/>
			<result property="gmv1" column="gmv1"/>
			<result property="gmv1Rate" column="gmv1_rate"/>
			<result property="gmv2" column="gmv2"/>
			<result property="gmv2Rate" column="gmv2_rate"/>
			<result property="renlijiaServiceChargeRate" column="renlijia_service_charge_rate"/>
			<result property="income" column="income"/>
		</collection>
	</resultMap>

	<!-- 查询列表分页用 -->
	<resultMap id="customerWithGmvPageMap" type="com.qunzhixinxi.hnqz.admin.entity.WmDaCustomer" >
		<result column="id" property="id" jdbcType="INTEGER" />
		<result column="customer_name" property="customerName" jdbcType="VARCHAR" />
		<result column="customer_type" property="customerType" jdbcType="VARCHAR" />
		<result column="customer_manager" property="customerManager" jdbcType="VARCHAR" />
		<result column="channel_name" property="channelName" jdbcType="VARCHAR" />
		<collection property="deptGmvList" ofType="com.qunzhixinxi.hnqz.admin.entity.WmDeptGmv"
					select="com.qunzhixinxi.hnqz.admin.mapper.WmDeptGmvMapper.listDeptGmvByDeptId" column="dept_id" />
	</resultMap>

	<!-- 分页查询 -->
	<select id="listCustomerPage" resultMap="BaseResultMap">
		SELECT
			ac.*, wc.title,
			wc.serial_no,
			wc.sign_time,
			wc.expiration_time,
			wc.pic_url,
			dt.trace_principal_name,
			dt.trace_object_name,
			dt.`status`
		FROM
			wm_af_customer ac
				LEFT JOIN wm_contract wc ON wc.customer_id = ac.id
				LEFT JOIN wm_da_track dt ON dt.project_id = ac.id
		<where>
			<if test="query.customerNumber != null and query.customerNumber != ''">
				AND ac.customer_id = #{query.customerNumber}
			</if>
			<if test="query.customerName != null and query.customerName != ''">
				AND ac.customer_name LIKE CONCAT('%', #{query.customerName}, '%')
			</if>
			<if test="query.customerType != null">
				AND ac.customer_type = #{query.customerType}
			</if>
			<if test="query.customerStatus != null">
				AND ac.customer_status = #{query.customerStatus}
			</if>
			<if test="query.customerManager != null and query.customerManager != ''">
				AND ac.customer_manager LIKE CONCAT('%', #{query.customerManager}, '%')
			</if>
		</where>
		ORDER BY ac.create_time DESC
	</select>

	<!-- 查询销售报表展示的客户（停用的，有结算额的要展示） -->
	<select id="listCustomerWithGmvPage" resultMap="customerWithGmvPageMap">
		SELECT
			c.*
		FROM
			wm_af_customer c
		LEFT JOIN
			(SELECT dept_id, sum(gmv1) as gmv1_sum, sum(gmv2) as gmv2_sum, sum(CASE `month` WHEN ${endMonth} THEN income ELSE 0 END)
				AS last_month_income FROM wm_dept_gmv WHERE `year` = #{year} AND CAST(`month` AS SIGNED INT)
				BETWEEN #{startMonth} AND #{endMonth} GROUP BY dept_id) temp
		ON temp.dept_id = c.dept_id
		<where>
			c.dept_id IS NOT NULL
			AND (c.customer_status = 'GOING_ONLINE'
			OR temp.gmv1_sum != 0
			OR temp.gmv2_sum != 0)
			<if test="query.customerName != null and query.customerName != ''">
				AND c.customer_name LIKE CONCAT('%', #{query.customerName}, '%')
			</if>
			<if test="query.customerType != null">
				AND c.customer_type = #{query.customerType}
			</if>
			<if test="query.customerManager != null and query.customerManager != ''">
				AND c.customer_manager LIKE CONCAT('%', #{query.customerManager}, '%')
			</if>
			<if test="query.channelName != null and query.channelName != ''">
				AND c.channel_name LIKE CONCAT('%', #{query.channelName}, '%')
			</if>
		</where>
		ORDER BY temp.last_month_income DESC
	</select>

	<!-- 查询销售报表展示的客户（停用的，有结算额的要展示） -->
	<select id="listCustomerWithGmv" resultMap="customerWithGmvMap">
		SELECT
			c.*,
			g.id AS gmv_id,
			g.YEAR AS year,
			g.MONTH AS month,
			g.gmv1 AS gmv1,
			g.gmv1_rate AS gmv1_rate,
			g.gmv2 AS gmv2,
			g.gmv2_rate AS gmv2_rate,
			g.renlijia_service_charge_rate AS renlijia_service_charge_rate,
			g.income AS income
		FROM
			wm_af_customer c
		LEFT JOIN wm_dept_gmv g ON g.dept_id = c.dept_id
		LEFT JOIN
			(SELECT dept_id, sum(gmv1) as gmv1_sum, sum(gmv2) as gmv2_sum, sum(CASE `month` WHEN ${endMonth} THEN income ELSE 0 END)
			    AS last_month_income FROM wm_dept_gmv WHERE `year` = #{year} AND CAST(`month` AS SIGNED INT)
				BETWEEN #{startMonth} AND #{endMonth} GROUP BY dept_id) temp
		ON temp.dept_id = c.dept_id
		<where>
			c.dept_id IS NOT NULL
			AND (c.customer_status = 'GOING_ONLINE'
				OR temp.gmv1_sum != 0
				OR temp.gmv2_sum != 0)
			AND g.`year` = #{year}
			AND CAST(g.`month` AS SIGNED INT) BETWEEN #{startMonth} AND #{endMonth}
			<if test="query.customerName != null and query.customerName != ''">
				AND c.customer_name LIKE CONCAT('%', #{query.customerName}, '%')
			</if>
			<if test="query.customerType != null">
				AND c.customer_type = #{query.customerType}
			</if>
			<if test="query.customerManager != null and query.customerManager != ''">
				AND c.customer_manager LIKE CONCAT('%', #{query.customerManager}, '%')
			</if>
			<if test="query.channelName != null and query.channelName != ''">
				AND c.channel_name LIKE CONCAT('%', #{query.channelName}, '%')
			</if>
		</where>
		ORDER BY temp.last_month_income DESC
	</select>

</mapper>