<?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.superdesk.uniorder.dao.mapper.marketing.department.UnifiedAreaCityMapperEx">


	<resultMap id="WebResultMap" type="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaWeb">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="parent_id" property="parentId" jdbcType="BIGINT"/>
		<result column="parent_name" property="parentName" jdbcType="VARCHAR"/>

		<result column="lev" property="lev" jdbcType="INTEGER"/>

		<result column="area_name" property="areaName" jdbcType="VARCHAR"/>
		<result column="area_code" property="areaCode" jdbcType="VARCHAR"/>

		<result column="customer_users" property="customerUsers" jdbcType="VARCHAR"/>
		<result column="purchase_users" property="purchaseUsers" jdbcType="VARCHAR"/>

		<result column="customer_users_name" property="customerUsersName" jdbcType="VARCHAR"/>
		<result column="purchase_users_name" property="purchaseUsersName" jdbcType="VARCHAR"/>

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

		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
		<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
		<result column="update_user_id" property="updateUserId" jdbcType="BIGINT"/>

		<result column="kpi_target" property="kpiTarget" jdbcType="DECIMAL"/>
		<result column="okr_target" property="okrTarget" jdbcType="DECIMAL"/>
		<result column="profit_kpi_target" property="profitKpiTarget" jdbcType="DECIMAL"/>

		<result column="yearly" property="yearly" jdbcType="INTEGER"/>
		<result column="monthly" property="monthly" jdbcType="INTEGER"/>

	</resultMap>

	<select id="getweb" resultMap="WebResultMap">
		SELECT
			(SELECT GROUP_CONCAT(fullname) from sys_user where FIND_IN_SET(id,uac.customer_users)) as customer_users_name,
			(SELECT GROUP_CONCAT(fullname) from sys_user where FIND_IN_SET(id,uac.purchase_users)) as purchase_users_name,
			uac_p.area_name as parent_name,

			uac.*,
			uact.kpi_target,
			uact.okr_target,
			uact.profit_kpi_target,

			uact.yearly,
			uact.monthly
		from
			unified_area_city uac
			LEFT JOIN unified_area_city_target AS uact ON uac.id = uact.area_city_id AND uact.yearly = YEAR(CURDATE()) AND uact.monthly = MONTH(CURDATE())
			LEFT JOIN unified_area_city uac_p on uac.parent_id = uac_p.id
		<where>
		</where>

    </select>

	<select id="getWebChildren" resultMap="WebResultMap">
		SELECT
			(SELECT GROUP_CONCAT(fullname)  from sys_user where FIND_IN_SET(id,a.customer_users)) as customer_users_name,
			(SELECT GROUP_CONCAT(fullname)  from sys_user where FIND_IN_SET(id,a.purchase_users)) as purchase_users_name,
			b.area_name as parent_name,
			a.*,
			D.kpi_target,
			D.okr_target,
			D.yearly,
			D.monthly
		from
			unified_area_city a
			LEFT JOIN unified_area_city_target AS D ON a.id = D.area_city_id AND D.yearly = YEAR(CURDATE()) AND D.monthly = MONTH(CURDATE())
			LEFT JOIN unified_area_city b on a.parent_id = b.id
		<where>
			and a.parent_id = #{_parameter}
		</where>

    </select>

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.marketing.department.UnifiedAreaCityMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.marketing.department.UnifiedAreaCityMapper.Base_Column_List"/>
		from
			unified_area_city
		<where>
		<if test="areaName != null and areaName != '' ">
			and area_name like CONCAT('%',#{areaName},'%')
		</if>
		<if test="parentId != null and parentId != ''">
			and parent_id = #{parentId}
		</if>
		<if test="areaNameFull != null and areaNameFull != ''">
			and area_name = #{areaNameFull}
		</if>
		<if test="lev != null and lev != ''">
			and lev = #{lev}
		</if>
		</where>
	</select>

	<resultMap id="DetailResultMap" type="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaCityDetail">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="area_id" property="areaId" jdbcType="BIGINT"/>
		<result column="area_name" property="areaName" jdbcType="VARCHAR"/>
		<result column="city_name" property="cityName" jdbcType="VARCHAR"/>
		<result column="city_code" property="cityCode" jdbcType="VARCHAR"/>
	</resultMap>
	<resultMap id="ItemSelectResultMap" type="cn.superdesk.uniorder.dto.vo._function.select.SelectOptionAreaItem">
		<id column="id" property="areaItemId" jdbcType="BIGINT"/>
		<result column="area_id" property="areaId" jdbcType="BIGINT"/>
		<result column="city_name" property="areaItemName" jdbcType="VARCHAR"/>
	</resultMap>
	<select id="findAreaItemSelect" resultMap="ItemSelectResultMap">
		select
			id, area_id, city_name
		from
			unified_area_city
		<where>
		<if test="areaId != null and areaId != ''">
			and area_id = #{areaId,jdbcType=BIGINT}
		</if>
		</where>
	</select>

	<resultMap id="TJResultMap" type="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaTJ">

		<result column="id" property="id" jdbcType="BIGINT"/>
		<result column="parent_id" property="parentId" jdbcType="BIGINT"/>

		<result column="area_full_ids" property="areaFullIds" jdbcType="VARCHAR"/>
		<result column="area_name" property="areaName" jdbcType="VARCHAR"/>

		<result column="lev" property="lev" jdbcType="INTEGER"/>

		<result column="kpi_target" property="kpiTarget" jdbcType="DECIMAL"/>
		<result column="okr_target" property="okrTarget" jdbcType="DECIMAL"/>
		<result column="profit_kpi_target" property="profitKpiTarget" jdbcType="DECIMAL"/>

		<result column="customer_users_name" property="customerUsernames" jdbcType="VARCHAR"/>
		<result column="purchase_users_name" property="purchaseUsernames" jdbcType="VARCHAR"/>

		<result column="yearly" property="yearly" jdbcType="INTEGER"/>
		<result column="monthly" property="monthly" jdbcType="INTEGER"/>

		<result column="virt_count" property="virtCount" jdbcType="INTEGER"/>

	</resultMap>

	<select id="getareatj" resultMap="TJResultMap">

		SELECT
			a.id,
			a.parent_id,

			a.area_full_ids,
			a.area_name,

			a.lev,

			ifnull(B.kpi_target,0) as kpi_target,
			ifnull(B.okr_target,0) as okr_target,
			ifnull(B.profit_kpi_target,0) as profit_kpi_target,

			B.yearly,
			B.monthly,

			C.virt_count,
			(SELECT GROUP_CONCAT(fullname) from sys_user where FIND_IN_SET(id,a.customer_users)) as customer_users_name,
			(SELECT GROUP_CONCAT(fullname) from sys_user where FIND_IN_SET(id,a.purchase_users)) as purchase_users_name
		from
			unified_area_city a
			LEFT JOIN unified_area_city_target B on a.id=B.area_city_id and B.yearly=#{yearly} and B.monthly=#{monthly}
			LEFT JOIN (SELECT count(virt_id) AS virt_count,area_city_id from unified_area_city_virt GROUP BY area_city_id) C ON C.area_city_id = a.id
		<where>
		<if test="areaId != null and areaId != ''">
			and a.id =#{areaId}
		</if>
		<if test="customerId != null and customerId != ''">
			AND FIND_IN_SET(#{customerId},a.customer_users )
		</if>
		<if test="purchaseId != null and purchaseId != ''">
			AND FIND_IN_SET(#{purchaseId},a.purchase_users)
		</if>
		</where>
	</select>

	<resultMap id="AreaCityVirtResultMap" type="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaCityWithVirt">
		<result column="id" property="id" jdbcType="BIGINT"/>
		<result column="area_full_names" property="areaFullNames" jdbcType="VARCHAR"/>
		<result column="area_name" property="areaName" jdbcType="VARCHAR"/>
		<result column="customer_users_name" property="customerUsersName" jdbcType="VARCHAR"/>
		<result column="purchase_users_name" property="purchaseUsersName" jdbcType="VARCHAR"/>
	</resultMap>

	<select id="getAreaDetailByVirtId" resultMap="AreaCityVirtResultMap">
		SELECT
			B.area_name,
			B.area_full_names,
			B.id,
			(SELECT GROUP_CONCAT(fullname)  from sys_user where FIND_IN_SET(id,B.customer_users)) as customer_users_name,
			(SELECT GROUP_CONCAT(fullname)  from sys_user where FIND_IN_SET(id,B.purchase_users)) as purchase_users_name
		from
			unified_area_city_virt a
			LEFT JOIN unified_area_city B on a.area_city_id = B.id
		<where>
			and a.virt_id = #{virtId}
		</where>
  </select>

</mapper>