<?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.UnifiedEncourageMapper">
	<resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.unified.UnifiedEncourage">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="channel_id" property="channelId" jdbcType="BIGINT"/>
		<result column="business_code" property="businessCode" jdbcType="VARCHAR"/>
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="jl_year" property="jlYear" jdbcType="INTEGER"/>
		<result column="jl_month" property="jlMonth" jdbcType="INTEGER"/>
		<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"/>
	</resultMap>
	<resultMap id="PageResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedEncouragePage">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="channel_id" property="channelId" jdbcType="BIGINT"/>
		<result column="channel_name" property="channelName" jdbcType="VARCHAR"/>
		<result column="business_code" property="businessCode" jdbcType="VARCHAR"/>
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="jl_year" property="jlYear" jdbcType="INTEGER"/>
		<result column="jl_month" property="jlMonth" jdbcType="INTEGER"/>
		<result column="remarks" property="remarks" jdbcType="VARCHAR"/>
		<result column="business_code_name" property="businessCodeName" jdbcType="VARCHAR"/>
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>

	</resultMap>
	<select id="getPageData" resultMap="PageResultMap">
		SELECT
			A.amount,A.business_code,A.channel_id,A.create_time,A.id,A.jl_month,A.jl_year,A.remarks,
			B.channel_name,C.group_name as business_code_name
		from unified_encourage A
			LEFT JOIN unified_channel_sync B on A.channel_id = B.channel_id
			LEFT JOIN (SELECT group_name,group_code from unified_service_group GROUP BY group_code) C on C.group_code =
		A.business_code
		where 1=1
		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(A.create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(A.create_time)=#{onlymonth}
		</if>
		<if test="jlYear != null and jlYear != ''">
			and A.jl_year=#{jlYear}
		</if>
		<if test="jlMonth != null and jlMonth != ''">
			and A.jl_month=#{jlMonth}
		</if>
		<if test="channelId != null and channelId != ''">
			and A.channel_id=#{channelId}
		</if>
		<if test="businessCode != null and businessCode != ''">
			and A.business_code = #{businessCode}
		</if>
		<if test="startime != null and startime != ''">
			and DATE_FORMAT(A.create_time,'%Y-%m-%d') &gt;= #{startime,jdbcType=VARCHAR}
		</if>
		<if test="endtime != null and endtime != ''">
			and DATE_FORMAT(A.create_time,'%Y-%m-%d') &lt;= #{endtime,jdbcType=VARCHAR}
		</if>
		order by A.create_time desc
	</select>
	<select id="findAll" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List"/>
		from unified_encourage where 1=1
		<if test="channelId !=null and channelId != ''">
			and channel_id = #{channelId}
		</if>
		<if test="businessCode !=null and businessCode != ''">
			and business_code = #{businessCode}
		</if>
		<if test="jlYear !=null and jlYear != ''">
			and jl_year = #{jlYear}
		</if>
		<if test="jlMonth !=null and jlMonth != ''">
			and jl_month = #{jlMonth}
		</if>
		order by create_time desc
	</select>
	<resultMap id="ServiceGroupResultMap" type="cn.superdesk.uniorder.dao.entity.unified.UnifiedServiceGroup">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="group_name" property="groupName" jdbcType="VARCHAR"/>
		<result column="group_code" property="groupCode" jdbcType="VARCHAR"/>
		<result column="Service_no" property="ServiceNo" jdbcType="VARCHAR"/>

	</resultMap>
	<select id="findServiceGroupByCode" resultMap="ServiceGroupResultMap" parameterType="java.lang.String">
		SELECT
			id,    group_name,    group_code,    service_no
		FROM
			unified_service_group
		where 1=1
			and group_code = #{businessCode}
		LIMIT 1
	</select>

	<!-- 某渠道商 - 累计可分润 -->
	<select id="getAmountProfit" resultType="java.math.BigDecimal">
		SELECT
			sum(profit_amount) profit_amount
		from
			unified_order_profit
		where 1=1
		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(create_time)=#{onlymonth}
		</if>
		<if test="channelId != null and channelId != ''">
			and channel_id=#{channelId}
		</if>
	</select>

	<!-- 某渠道商 - 累计可提现 -->
	<select id="getAmountEncourage" resultType="java.math.BigDecimal">
		SELECT sum(amount) from unified_encourage where 1=1
		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(create_time)=#{onlymonth}
		</if>
		<if test="channelId != null and channelId != ''">
			and channel_id=#{channelId}
		</if>

	</select>
	<select id="getOrderAmount" resultType="java.math.BigDecimal">
		SELECT sum(amount) order_amount from unified_order where complet_state != 3
		<if test="channelId != null and channelId != ''">
			and project_id in
			(
			SELECT organization_id from unified_channel_profit_sync where channel_id=#{channelId}
			)
		</if>
		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(create_time)=#{onlymonth}
		</if>
	</select>
	<select id="getVirtCountByChannelId" resultType="java.lang.Integer">
    SELECT count(id) quantity from sys_enterprise where organization_id in
    (
        SELECT organization_id from unified_channel_profit_sync where channel_id=#{channelId}
    )
  </select>
	<select id="getAmountBack" resultType="java.math.BigDecimal">
		SELECT sum(amount) order_amount from unified_order where complet_state != 3 and pay_state=1
		<if test="channelId != null and channelId != ''">
			and project_id in
			(
			SELECT organization_id from unified_channel_profit_sync where channel_id=#{channelId}
			)
		</if>
		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(create_time)=#{onlymonth}
		</if>
	</select>
	<select id="getAmountGetCash" resultType="java.math.BigDecimal">
		SELECT sum(amount) from unified_drow_money where 1=1
		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(create_time)=#{onlymonth}
		</if>
		<if test="channelId != null and channelId != ''">
			and channel_id=#{channelId}
		</if>
	</select>
	<resultMap id="ChannelItemResultMap" type="cn.superdesk.uniorder.dto.vo.wechat.WxChannelItemList">
		<result column="virt_id" property="virtId" jdbcType="BIGINT"/>
		<result column="org_id" property="orgId" jdbcType="BIGINT"/>
		<result column="virt_name" property="virtName" jdbcType="VARCHAR"/>
		<result column="project_name" property="projectName" jdbcType="VARCHAR"/>
		<result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="amount_back" property="amountBack" jdbcType="DECIMAL"/>
		<result column="amount_profit" property="amountProfit" jdbcType="DECIMAL"/>
	</resultMap>
	<select id="getChannelItemList" resultMap="ChannelItemResultMap">
		SELECT
			ta.enterprise_id as virt_id,
			ta.enterprise_name as virt_name,
			ta.organization_id as org_id,
			ta.organization_name as project_name,

			tb.amount,
			tb.group_name as service_name,

			tc.amount_back,

			td.profit_amount as amount_profit
		from (
				SELECT
					a.enterprise_id,a.enterprise_name,b.organization_name,b.organization_id
				from
					sys_enterprise a
					LEFT JOIN sys_organization b on a.organization_id = b.organization_id
				where a.organization_id in
				(
					SELECT
						organization_id from unified_channel_profit_sync where 1=1
					<if test="channelId != null and channelId != ''">
						and channel_id=#{channelId}
					</if>
				)
				UNION
				SELECT
					null,'游客',null,null
			) ta
			LEFT JOIN
			(
				SELECT
					sum(ia.amount) amount,
					ia.purchase_virt_id,
					ib.group_name
				from unified_order ia
					LEFT JOIN unified_service_group ib on ia.service_no = ib.service_no
				where ia.complet_state != 3
				<if test="onlyyear != null and onlyyear != ''">
					and YEAR(ia.create_time)=#{onlyyear}
				</if>
				<if test="onlymonth != null and onlymonth != ''">
					and MONTH(ia.create_time)=#{onlymonth}
				</if>
					and ia.service_no in ( SELECT business_code from unified_channel_profit_sync where channel_id=#{channelId})
				GROUP BY ia.purchase_virt_id,ib.group_name
			) tb on IFNULL(ta.enterprise_id,0) = IFNULL(tb.purchase_virt_id,0)
			LEFT JOIN
			(
				SELECT
					sum(ia.amount) amount_back,
					ia.purchase_virt_id,
					ib.group_name
				from
					unified_order ia
					LEFT JOIN unified_service_group ib on ia.service_no = ib.service_no
				where
					ia.complet_state != 3
					and pay_state=1
				<if test="onlyyear != null and onlyyear != ''">
					and YEAR(ia.create_time)=#{onlyyear}
				</if>
				<if test="onlymonth != null and onlymonth != ''">
					and MONTH(ia.create_time)=#{onlymonth}
				</if>
					and ia.service_no in ( SELECT business_code from unified_channel_profit_sync where channel_id=#{channelId})
				GROUP BY ia.purchase_virt_id,ib.group_name
			) tc on IFNULL(tb.purchase_virt_id,0) = IFNULL(tc.purchase_virt_id,0) and tb.group_name = tc.group_name
			LEFT JOIN
			(
				SELECT
					sum(ia.profit_amount) profit_amount,
					ib.purchase_virt_id,
					ic.group_name
				from unified_order_profit ia
					LEFT JOIN unified_order ib on ia.order_sn = ib.sn
					LEFT JOIN unified_service_group ic on ib.service_no = ic.service_no
				where 1=1
				<if test="onlyyear != null and onlyyear != ''">
					and YEAR(ia.create_time)=#{onlyyear}
				</if>
				<if test="onlymonth != null and onlymonth != ''">
					and MONTH(ia.create_time)=#{onlymonth}
				</if>
					and ib.service_no in ( SELECT business_code from unified_channel_profit_sync where channel_id=#{channelId})
				GROUP BY ib.purchase_virt_id,ic.group_name
			) td on IFNULL(td.purchase_virt_id,0) = IFNULL(tb.purchase_virt_id,0) and td.group_name = tb.group_name
		ORDER BY td.profit_amount desc
	</select>


	<sql id="Base_Column_List">
    id, channel_id, business_code, amount, jl_year, jl_month, remarks, 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_encourage
		where id = #{id,jdbcType=BIGINT}
	</select>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from unified_encourage
    where id = #{id,jdbcType=BIGINT}
  </delete>
	<insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.unified.UnifiedEncourage">
		<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
		insert into unified_encourage (channel_id, business_code, amount,
		jl_year, jl_month, remarks,
		create_time, create_user_id, update_time,
		update_user_id)
		values (#{channelId,jdbcType=BIGINT}, #{businessCode,jdbcType=VARCHAR}, #{amount,jdbcType=DECIMAL},
		#{jlYear,jdbcType=INTEGER}, #{jlMonth,jdbcType=INTEGER}, #{remarks,jdbcType=VARCHAR},
		#{createTime,jdbcType=TIMESTAMP}, #{createUserId,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP},
		#{updateUserId,jdbcType=BIGINT})
	</insert>
	<insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.unified.UnifiedEncourage">
		<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
		insert into unified_encourage
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="channelId != null">
				channel_id,
			</if>
			<if test="businessCode != null">
				business_code,
			</if>
			<if test="amount != null">
				amount,
			</if>
			<if test="jlYear != null">
				jl_year,
			</if>
			<if test="jlMonth != null">
				jl_month,
			</if>
			<if test="remarks != null">
				remarks,
			</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="channelId != null">
				#{channelId,jdbcType=BIGINT},
			</if>
			<if test="businessCode != null">
				#{businessCode,jdbcType=VARCHAR},
			</if>
			<if test="amount != null">
				#{amount,jdbcType=DECIMAL},
			</if>
			<if test="jlYear != null">
				#{jlYear,jdbcType=INTEGER},
			</if>
			<if test="jlMonth != null">
				#{jlMonth,jdbcType=INTEGER},
			</if>
			<if test="remarks != null">
				#{remarks,jdbcType=VARCHAR},
			</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.UnifiedEncourage">
		update unified_encourage
		<set>
			<if test="channelId != null">
				channel_id = #{channelId,jdbcType=BIGINT},
			</if>
			<if test="businessCode != null">
				business_code = #{businessCode,jdbcType=VARCHAR},
			</if>
			<if test="amount != null">
				amount = #{amount,jdbcType=DECIMAL},
			</if>
			<if test="jlYear != null">
				jl_year = #{jlYear,jdbcType=INTEGER},
			</if>
			<if test="jlMonth != null">
				jl_month = #{jlMonth,jdbcType=INTEGER},
			</if>
			<if test="remarks != null">
				remarks = #{remarks,jdbcType=VARCHAR},
			</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 id = #{id,jdbcType=BIGINT}
	</update>
	<update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.unified.UnifiedEncourage">
    update unified_encourage
    set channel_id = #{channelId,jdbcType=BIGINT},
      business_code = #{businessCode,jdbcType=VARCHAR},
      amount = #{amount,jdbcType=DECIMAL},
      jl_year = #{jlYear,jdbcType=INTEGER},
      jl_month = #{jlMonth,jdbcType=INTEGER},
      remarks = #{remarks,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      create_user_id = #{createUserId,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      update_user_id = #{updateUserId,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>