<?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.unified.UnifiedCustomerMapper">
	<resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer">
		<id column="virt_id" property="virtId" jdbcType="BIGINT"/>
		<result column="virt_name" property="virtName" jdbcType="VARCHAR"/>
		<result column="project_id" property="projectId" jdbcType="BIGINT"/>
		<result column="credit" property="credit" jdbcType="DECIMAL"/>
		<result column="cycle_days" property="cycleDays" jdbcType="INTEGER"/>
		<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"/>
	</resultMap>
	<resultMap id="DataResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerVo">
		<id column="virt_id" property="virtId" jdbcType="BIGINT"/>
		<result column="virt_name" property="virtName" jdbcType="VARCHAR"/>
		<result column="project_id" property="projectId" jdbcType="BIGINT"/>
		<result column="project_name" property="projectName" jdbcType="VARCHAR"/>
		<result column="area_name" property="areaName" jdbcType="VARCHAR"/>
		<result column="city_name" property="areaItemName" jdbcType="VARCHAR"/>
		<result column="area_id" property="areaId" jdbcType="BIGINT"/>
		<result column="area_item_id" property="areaItemId" jdbcType="BIGINT"/>
		<result column="customer_users" property="customerUsers" jdbcType="VARCHAR"/>
		<result column="purchase_users" property="purchaseUsers" jdbcType="VARCHAR"/>

		<result column="customer_users_name" property="customerUserName" jdbcType="VARCHAR"/>
		<result column="purchase_users_name" property="purchaseUserName" jdbcType="VARCHAR"/>
		<result column="credit" property="credit" jdbcType="DECIMAL"/>
		<result column="cycle_days" property="cycleDays" jdbcType="INTEGER"/>
		<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"/>
	</resultMap>
	<select id="getData" resultMap="DataResultMap">
		SELECT
		A.virt_id,
		A.virt_name,
		A.project_id,
		A.credit,
		A.cycle_days,
		A.create_time,
		A.create_user_id,
		A.update_time,
		A.update_user_id,
		B.project_name,

		C.customer_users,
		C.purchase_users,
		(SELECT GROUP_CONCAT(fullname) from sys_user where FIND_IN_SET(id,C.customer_users)) as customer_users_name,
		(SELECT GROUP_CONCAT(fullname) from sys_user where FIND_IN_SET(id,C.purchase_users)) as purchase_users_name,
		C.area_full_names AS area_name,

		C.id as area_id,
		E.area_city_id AS area_item_id
		FROM
		unified_customer AS A
		LEFT JOIN unified_project AS B ON A.project_id = B.project_id
		LEFT JOIN unified_area_city_virt E ON E.virt_id = A.virt_id
		LEFT JOIN unified_area_city AS C ON C.id = E.area_city_id

		WHERE
		1 = 1
		<if test="project != null and project != '' ">
			and (A.project_id = #{project} or
			B.project_name like CONCAT('%',#{project},'%')
			)
		</if>
		<if test="virt != null and virt != '' ">
			and (A.virt_id = #{virt} or
			A.virt_name like CONCAT('%',#{virt},'%')
			)
		</if>
		<if test="areaId != null and areaId != '' ">
			and C.id = #{areaId}
		</if>
		<if test="areaItemId != null and areaItemId != '' ">
			and E.area_city_id = #{areaItemId}
		</if>
		<if test="cususerid != null and cususerid != '' ">
			and FIND_IN_SET(#{cususerid},C.customer_users)
		</if>
		<if test="puruserid != null and puruserid != '' ">
			and FIND_IN_SET(#{puruserid},C.purchase_users)
		</if>
		order by A.create_time desc
	</select>

	<sql id="Base_Column_List">
    virt_id, virt_name, project_id, credit, cycle_days, create_time, create_user_id, 
    update_time, update_user_id
  </sql>
	<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
		select
		<include refid="Base_Column_List"/>
		from unified_customer
		where virt_id = #{virtId,jdbcType=BIGINT}
	</select>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from unified_customer
    where virt_id = #{virtId,jdbcType=BIGINT}
  </delete>
	<insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer">
    insert into unified_customer (virt_id, virt_name, project_id, 
      credit, cycle_days, create_time, 
      create_user_id, update_time, update_user_id
      )
    values (#{virtId,jdbcType=BIGINT}, #{virtName,jdbcType=VARCHAR}, #{projectId,jdbcType=BIGINT}, 
      #{credit,jdbcType=DECIMAL}, #{cycleDays,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, 
      #{createUserId,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP}, #{updateUserId,jdbcType=BIGINT}
      )
  </insert>
	<insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer">
		insert into unified_customer
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="virtId != null">
				virt_id,
			</if>
			<if test="virtName != null">
				virt_name,
			</if>
			<if test="projectId != null">
				project_id,
			</if>
			<if test="credit != null">
				credit,
			</if>
			<if test="cycleDays != null">
				cycle_days,
			</if>
			<if test="createTime != null">
				create_time,
			</if>
			<if test="createUserId != null">
				create_user_id,
			</if>
			<if test="updateTime != null">
				update_time,
			</if>
			<if test="updateUserId != null">
				update_user_id,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="virtId != null">
				#{virtId,jdbcType=BIGINT},
			</if>
			<if test="virtName != null">
				#{virtName,jdbcType=VARCHAR},
			</if>
			<if test="projectId != null">
				#{projectId,jdbcType=BIGINT},
			</if>
			<if test="credit != null">
				#{credit,jdbcType=DECIMAL},
			</if>
			<if test="cycleDays != null">
				#{cycleDays,jdbcType=INTEGER},
			</if>
			<if test="createTime != null">
				#{createTime,jdbcType=TIMESTAMP},
			</if>
			<if test="createUserId != null">
				#{createUserId,jdbcType=BIGINT},
			</if>
			<if test="updateTime != null">
				#{updateTime,jdbcType=TIMESTAMP},
			</if>
			<if test="updateUserId != null">
				#{updateUserId,jdbcType=BIGINT},
			</if>
		</trim>
	</insert>
	<update id="updateByPrimaryKeySelective" parameterType="cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer">
		update unified_customer
		<set>
			<if test="virtName != null">
				virt_name = #{virtName,jdbcType=VARCHAR},
			</if>
			<if test="projectId != null">
				project_id = #{projectId,jdbcType=BIGINT},
			</if>
			<if test="credit != null">
				credit = #{credit,jdbcType=DECIMAL},
			</if>
			<if test="cycleDays != null">
				cycle_days = #{cycleDays,jdbcType=INTEGER},
			</if>
			<if test="createTime != null">
				create_time = #{createTime,jdbcType=TIMESTAMP},
			</if>
			<if test="createUserId != null">
				create_user_id = #{createUserId,jdbcType=BIGINT},
			</if>
			<if test="updateTime != null">
				update_time = #{updateTime,jdbcType=TIMESTAMP},
			</if>
			<if test="updateUserId != null">
				update_user_id = #{updateUserId,jdbcType=BIGINT},
			</if>
		</set>
		where virt_id = #{virtId,jdbcType=BIGINT}
	</update>
	<update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer">
    update unified_customer
    set virt_name = #{virtName,jdbcType=VARCHAR},
      project_id = #{projectId,jdbcType=BIGINT},
      credit = #{credit,jdbcType=DECIMAL},
      cycle_days = #{cycleDays,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      create_user_id = #{createUserId,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      update_user_id = #{updateUserId,jdbcType=BIGINT}
    where virt_id = #{virtId,jdbcType=BIGINT}
  </update>
</mapper>