<?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.channel.UnifiedChannelProfitSyncMapperEx">

	<resultMap id="PageResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedChannelProfitSyncPage">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="sync_id" property="syncId" jdbcType="BIGINT"/>
		<result column="channel_id" property="channelId" jdbcType="BIGINT"/>
		<result column="channel_name" property="channelName" jdbcType="VARCHAR"/>
		<result column="organization_name" property="organizationName" jdbcType="VARCHAR"/>
		<result column="organization_id" property="organizationId" jdbcType="BIGINT"/>
		<result column="business_code" property="businessCode" jdbcType="VARCHAR"/>
		<result column="business_code_name" property="businessCodeName" jdbcType="VARCHAR"/>
		<result column="sync_time" property="syncTime" jdbcType="TIMESTAMP"/>
		<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="begin_date" property="beginDate" jdbcType="TIMESTAMP"/>
		<result column="end_date" property="endDate" jdbcType="TIMESTAMP"/>
		<result column="proportion" property="proportion" jdbcType="DECIMAL"/>

		<result column="is_bind_virt" property="isBindVirt" jdbcType="TINYINT"/>
		<result column="virt_ids" property="virtIds" jdbcType="VARCHAR"/>

	</resultMap>

	<select id="getPageData" resultMap="PageResultMap">
		SELECT
		A.id,
		A.sync_id,
		A.channel_id,
		A.organization_id,
		A.business_code,
		A.sync_time,
		A.update_time,
		A.begin_date,
		A.end_date,
		A.proportion,
		B.channel_name,
		C.organization_name,
		D.group_name AS business_code_name
		FROM
		unified_channel_profit_sync AS A
		LEFT JOIN unified_channel_sync AS B ON A.channel_id = B.channel_id
		LEFT JOIN sys_organization AS C ON A.organization_id = C.organization_id
		LEFT JOIN unified_service_group AS D ON A.business_code = D.group_code
		where 1=1
		<if test="channelName != null and channelName != '' ">
			and B.channel_name like CONCAT('%',#{channelName},'%')
		</if>
		<if test="projectName != null and projectName != '' ">
			and C.organization_name like CONCAT('%',#{projectName},'%')
		</if>
		order by A.update_time DESC
	</select>

	<select id="findChannelProfitBySyncId" resultMap="cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelProfitSyncMapper.BaseResultMap" parameterType="java.lang.Long">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelProfitSyncMapper.Base_Column_List"/>
		from
			unified_channel_profit_sync
		where
			sync_id = #{id,jdbcType=BIGINT}
	</select>

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelProfitSyncMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.channel.UnifiedChannelProfitSyncMapper.Base_Column_List"/>
		from
			unified_channel_profit_sync
		where 1=1
		<if test="channelId !=null and channelId != ''">
			and channel_id = #{channelId}
		</if>
		<if test="virtId != null and virtId != ''">
			and organization_id =
			(
			SELECT organization_id from sys_enterprise where enterprise_id=#{virtId}
			)
		</if>
		<if test="projectId != null and projectId != ''">
			and organization_id = #{projectId}
		</if>
		<if test="serviceNo != null and serviceNo != ''">
			and business_code = (
			SELECT group_code from unified_service_group where service_no = #{serviceNo}
			)
		</if>
		order by create_time desc
	</select>


</mapper>