<?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.UnifiedAreaMapperEx">

	<resultMap id="VoResultMap" type="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaVo">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="area_name" property="areaName" 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="city_name" property="cityName" jdbcType="VARCHAR"/>
	</resultMap>

	<resultMap id="CitySelectResultMap" type="cn.superdesk.uniorder.dto.vo._function.select.SelectOptionCity">
		<result column="other_field2" property="cityName" jdbcType="VARCHAR"/>
		<result column="other_field1" property="cityCode" jdbcType="VARCHAR"/>
	</resultMap>

	<resultMap id="PrentResultMap" type="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaWeb">
		<result column="id" property="id" jdbcType="BIGINT"/>
		<result column="area_name" property="areaName" jdbcType="VARCHAR"/>
		<result column="remarks" property="remarks" jdbcType="VARCHAR"/>
		<result column="customer_users" property="customerUsers" jdbcType="VARCHAR"/>
		<result column="purchase_users" property="purchaseUsers" jdbcType="VARCHAR"/>

		<collection property="children" ofType="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaWebChildren"
					javaType="java.util.List" column="id" select="getWebChildren">
		</collection>
	</resultMap>

	<resultMap id="ChildResultMap" type="cn.superdesk.uniorder.dto.vo.marketing.department.UnifiedAreaWebChildren">
		<result column="area_item_id" property="id" jdbcType="BIGINT"/>
		<result column="area_id" property="areaId" jdbcType="BIGINT"/>
		<result column="area_item_name" property="areaName" jdbcType="VARCHAR"/>
		<result column="customer_users" property="customerUsers" jdbcType="VARCHAR"/>
		<result column="purchase_users" property="purchaseUsers" jdbcType="VARCHAR"/>

		<result column="kpi_target" property="kpiTarget" jdbcType="DECIMAL"/>
		<result column="okr_target" property="okrTarget" jdbcType="DECIMAL"/>
		<result column="yearly" property="yearly" jdbcType="INTEGER"/>
		<result column="monthly" property="monthly" jdbcType="INTEGER"/>
	</resultMap>

	<select id="getWebChildren" resultMap="ChildResultMap">
		SELECT
			A.id AS area_item_id,
			A.area_id,
			A.city_name AS area_item_name,
			A.customer_users,
			A.purchase_users,

			D.kpi_target,
			D.okr_target,
			D.yearly,
			D.monthly
		FROM
			unified_area_city AS 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())
		<where>
			and A.area_id = #{_parameter,jdbcType=BIGINT}
		</where>
	</select>

	<select id="getWebData" resultMap="PrentResultMap">
  	SELECT
		id,
		area_name,
		remarks,
		customer_users,
		purchase_users
	FROM
		unified_area
	<where>

	</where>
	
  </select>

	<select id="getCitySelect" resultMap="CitySelectResultMap">
		SELECT
			other_field1,other_field2
		from
			sys_organization
		<where>
			and other_field1 is not null
		<if test="notuse != null and notuse == 1">
			and other_field1 not in ( SELECT city_code from unified_area_city )
		</if>
		</where>
		GROUP BY other_field1
	</select>

	<select id="getData" resultMap="VoResultMap">
		SELECT
			a.area_name,
			a.create_time,
			a.create_user_id,
			a.id,
			a.remarks,
			a.update_time,
			a.update_user_id,
			tb.city_name
		FROM
			unified_area a
			LEFT JOIN ( select group_concat(city_name) city_name,area_id from unified_area_city group by area_id ) tb on tb.area_id = a.id
		<where>
		<if test="areaName != null and areaName != '' ">
			and a.area_name like CONCAT('%',#{areaName},'%')
		</if>
		<if test="virtName != null and virtName !='' ">
			and id in (
				SELECT
					area_id
				from
					unified_area_city
				<where>
					and city_code in (
						SELECT
							city_code
						from
							unified_project
						<where>
							and project_id in (
								SELECT
									project_id
								from
									unified_customer
								where
									virt_name like CONCAT('%',#{virtName},'%')
							)
						</where>
					)
				</where>
			)
		</if>
		</where>
		order by a.create_time desc
	</select>

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


</mapper>