<?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.roadm.mapper.BillInfoMapper">

	<resultMap type="BillInfo" id="BillInfoResultMap">
		<id column="id" property="id" />
		<result column="month" property="month" />
		<result column="sp_id" property="sp_id" />
		<result column="supplier" property="supplier" />
		<result column="supplier_name" property="supplier_name" />
		<result column="carrieroperator" property="carrieroperator" />
		<result column="arrive_num" property="arrive_num" />
		<result column="unitprice" property="unitprice" />
	</resultMap>

	<resultMap type="SmsSpProvinceSummary" id="SmsSpProvinceSummaryResultMap">
		<id column="id" property="id" />
		<result column="adjust_date" property="adjust_date" />
		<result column="month" property="month" />
		<result column="spid" property="spId" />
		<result column="sp_name" property="spName" />
		<result column="submit_succ" property="submit_succ" />
		<result column="arrive_succ" property="arrive_succ" />
		<result column="arrive_fail" property="arrive_fail" />
		<result column="submit_fail" property="submit_fail" />
		<result column="sp_price" property="sp_price" />
		<result column="channel_price" property="channel_price" />
		<result column="channelid" property="channelid" />
		<result column="channel_name" property="channelName" />
		<result column="is_api_billing" property="is_api_billing" />
		<result column="supplier_name" property="supplierName" />
		<result column="cooperate_company" property="cooperateCompany" />
		<result column="full_name" property="companyName" />
		<result column="income" property="income"/>
		<result column="writeAmt" property="writeAmt"/>
		<result column="writeOffStatus" property="writeOffStatus"/>
		<result column="clientid" property="clientId"/>
		<result column="sp_saler" property="spSaler"/>
		<result column="loginname" property="loginname"/>
	</resultMap>


	<select id="listCustomerBill" parameterType="BillInfo"
		resultMap="BillInfoResultMap">
		SELECT c.id,c.MONTH ,c.sp_id,s.sp_name,
		c.supplier,c.supplier_name,c.carrieroperator,c.arrive_num,c.unitprice,sum(c.arrive_num*c.unitprice)
		total,c.allnumber,sum(c.allnumber*c.unitprice) total1
		FROM
		customer_bill c,sms_sp_info s
		where c.sp_id = s.id

		<if test="month!=null and month!=''">
			and c.month=#{month}
		</if>

		<if test="sp_id!=null and sp_id!=''">
			and c.sp_id = #{sp_id}
		</if>

		<if test="sp_name!=null and sp_name!=''">
			and s.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		GROUP BY c.sp_id,c.supplier,c.carrieroperator
		order by c.sp_id
	</select>

	<select id="exportCustomerBillList" parameterType="BillInfo"
		resultMap="BillInfoResultMap">
		SELECT ss.id,
		DATE_FORMAT(ss.createtime, '%Y-%m') month,
		ss.sp_id,
		sp.sp_name,
		ss.costprice,
		sa.saleprice
		unitprice,
		ss.arrive_num,
		ss.saleprice saleprice,ss.status,ss.remark,ss.payment
		FROM
		sms_sp_day_fee ss,
		sms_api_setting sa,
		sms_sp_info sp
		WHERE sp.id =
		sa.sp_id
		AND sa.sp_id = ss.sp_id

		<if test="month!=null and month!=''">
			and DATE_FORMAT(ss.createtime, '%Y-%m')=#{month}
		</if>

		<if test="sp_id!=null and sp_id!=''">
			and ss.sp_id = #{sp_id}
		</if>

		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
		<!-- GROUP BY c.sp_id,c.supplier,c.carrieroperator
		order by c.sp_id -->
	</select>


	<select id="listCustomerFee" parameterType="BillInfo" resultMap="BillInfoResultMap">
		SELECT ss.id,
		DATE_FORMAT(ss.createtime, '%Y-%m') month,
		ss.sp_id,
		sp.sp_name,
		ss.costprice,
		sa.saleprice
		unitprice,
		ss.arrive_num,
		ss.saleprice saleprice,ss.status,ss.remark,ss.payment
		FROM
		sms_sp_day_fee ss,
		sms_api_setting sa,
		sms_sp_info sp
		WHERE sp.id =
		sa.sp_id
		AND sa.sp_id = ss.sp_id

		<if test="month!=null and month!=''">
			and DATE_FORMAT(ss.createtime, '%Y-%m')=#{month}
		</if>

		<if test="sp_id!=null and sp_id!=''">
			and ss.sp_id = #{sp_id}
		</if>

		<if test="sp_name!=null and sp_name!=''">
			and sp.sp_name like CONCAT('%',#{sp_name},'%')
		</if>
	</select>

	<update id="updateBillStatus" parameterType="BillInfo">
		update
		sms_sp_day_fee set
		status=#{status}
		where
		id=#{id}
	</update>

	<select id="getBillInfoById" parameterType="int" resultMap="BillInfoResultMap">
		SELECT ss.id,ss.sp_id,sp.sp_name,ss.saleprice,ss.payment,ss.remark
		FROM sms_sp_day_fee ss,sms_sp_info sp WHERE sp.id = ss.sp_id
		and
		ss.id=#{id}
	</select>

	<update id="updateBillFeeById" parameterType="BillInfo">
		update
		sms_sp_day_fee set
		payment=#{payment},
		remark=#{remark}
		where
		id=#{id}
	</update>

	<select id="listBillFee" parameterType="BillInfo" resultMap="BillInfoResultMap">
		SELECT ss.id,
		DATE_FORMAT(ss.createtime, '%Y-%m') month,
		ss.sp_id,
		sp.sp_name,
		ss.costprice,
		sa.saleprice
		unitprice,
		ss.arrive_num,
		ss.saleprice saleprice,ss.status,ss.remark,ss.payment
		FROM
		sms_sp_day_fee ss,
		sms_api_setting sa,
		sms_sp_info sp
		WHERE sp.id =
		sa.sp_id
		AND sa.sp_id = ss.sp_id and ss.status = 1
		<if test="month!=null and month!=''">
			and DATE_FORMAT(ss.createtime, '%Y-%m')=#{month}
			and ss.sp_id NOT IN(SELECT t.sp_id FROM ticket_open t WHERE t.month=#{month})
		</if>

	</select>

	<select id="listPageChannelBillList" parameterType="SmsSpProvinceSummary" resultMap="SmsSpProvinceSummaryResultMap">
		SELECT a.*,c.loginname FROM (

		SELECT scb.channelid,scb.channel_name,scb.month,
		DATE_FORMAT(scb.adjust_date,'%Y-%m-%d') adjust_date,
		cs.supplier_name,
		SUM(IFNULL(scb.submit_succ, 0 )) submit_succ,
		SUM(IFNULL(scb.arrive_succ, 0 )) arrive_succ,
		SUM(IFNULL(scb.arrive_fail, 0 )) arrive_fail,
		SUM(IFNULL(scb.submit_fail, 0 )) submit_fail,
		scb.channel_price
		FROM
		sms_channel_bill_info scb LEFT JOIN sms_send_channel_info si ON scb.channelid = si.id LEFT JOIN channel_supplier cs
		ON si.supplier_id = cs.id
		<where>
			<if test="month!=null and month!=''">
				and scb.`month` =  #{month}
			</if>
			<if test="channelid!=null and channelid!=''">
				AND scb.channelid = #{channelid}
			</if>
			<if test="supplierName!=null and supplierName!=''">
				AND cs.supplier_name like CONCAT('%',#{supplierName},'%')
			</if>
		</where>
		GROUP BY scb.channelid,scb.channel_price,scb.channel_name,scb.month,cs.supplier_name,adjust_date
		ORDER BY arrive_succ DESC ,scb.channelid DESC,scb.adjust_date DESC
		)a

		LEFT JOIN

		(SELECT channelid,loginname FROM
		(SELECT id AS channelid,loginname FROM sms_carrier_channel_info
		UNION ALL
		SELECT channelid,
		CASE
		WHEN channeltemplatevar->'$.appId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appId')
		WHEN channeltemplatevar->'$.appid' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appid')
		WHEN channeltemplatevar->'$.LoginName' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.LoginName')
		WHEN channeltemplatevar->'$.uname' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.uname')
		WHEN channeltemplatevar->'$.accessKeyId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKeyId')
		WHEN channeltemplatevar->'$.accessKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKey')
		WHEN channeltemplatevar->'$.username' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.username')
		WHEN channeltemplatevar->'$.AccountId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AccountId')
		WHEN channeltemplatevar->'$.Account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.Account')
		WHEN channeltemplatevar->'$.account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.account')
		WHEN channeltemplatevar->'$.AppKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AppKey')
		WHEN channeltemplatevar->'$.api_key' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.api_key')
		WHEN channeltemplatevar->'$.apiKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.apiKey')
		WHEN channeltemplatevar->'$.X_APP_ID' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.X_APP_ID')
		END  AS loginname FROM http_channel_template_config b
		UNION ALL
		SELECT channelid,
		CASE
		WHEN channeltemplatevar->'$.appId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appId')
		WHEN channeltemplatevar->'$.appid' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appid')
		WHEN channeltemplatevar->'$.LoginName' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.LoginName')
		WHEN channeltemplatevar->'$.uname' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.uname')
		WHEN channeltemplatevar->'$.accessKeyId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKeyId')
		WHEN channeltemplatevar->'$.accessKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKey')
		WHEN channeltemplatevar->'$.username' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.username')
		WHEN channeltemplatevar->'$.AccountId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AccountId')
		WHEN channeltemplatevar->'$.Account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.Account')
		WHEN channeltemplatevar->'$.account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.account')
		WHEN channeltemplatevar->'$.AppKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AppKey')
		WHEN channeltemplatevar->'$.api_key' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.api_key')
		WHEN channeltemplatevar->'$.apiKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.apiKey')
		WHEN channeltemplatevar->'$.X_APP_ID' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.X_APP_ID')
		END  AS loginname FROM mms_http_channel_template_config where channelid != 0) c
		) c ON a.channelid = c.channelid
	</select>

	<select id="getChannelBillList" parameterType="SmsSpProvinceSummary" resultMap="SmsSpProvinceSummaryResultMap">
		SELECT a.*,c.loginname FROM (

		SELECT scb.channelid,scb.channel_name,scb.month,
		DATE_FORMAT(scb.adjust_date,'%Y-%m-%d') adjust_date,
		case
		when cs.supplier_name is null
		then (select cs1.supplier_name from channel_supplier cs1  where sc.supplier_id = cs1.id)
		else cs.supplier_name
		end
		as supplier_name,

		case
		when si.cooperate_company is null
		then sc.cooperate_company
		else si.cooperate_company
		end
		as cooperate_company,
		SUM(IFNULL(scb.submit_succ, 0 )) submit_succ,
		SUM(IFNULL(scb.arrive_succ, 0 )) arrive_succ,
		SUM(IFNULL(scb.arrive_fail, 0 )) arrive_fail,
		SUM(IFNULL(scb.submit_fail, 0 )) submit_fail,
		scb.channel_price
		FROM
		sms_channel_bill_info scb LEFT JOIN sms_send_channel_info si ON scb.channelid = si.id LEFT JOIN channel_supplier cs
		ON si.supplier_id = cs.id LEFT JOIN mms_send_channel_info sc ON scb.channelid = sc.id
		<where>
			<if test="month!=null and month!=''">
				and scb.`month` =  #{month}
			</if>
			<if test="channelid!=null and channelid!=''">
				AND scb.channelid = #{channelid}
			</if>
			<if test="supplierName!=null and supplierName!=''">
				AND cs.supplier_name like CONCAT('%',#{supplierName},'%')
			</if>
		</where>
		GROUP BY scb.channelid,scb.channel_price,scb.channel_name,scb.month,supplier_name,cooperate_company,adjust_date
		ORDER BY arrive_succ DESC ,scb.channelid DESC,scb.adjust_date DESC
		)a

		LEFT JOIN

		(SELECT channelid,loginname FROM
		(SELECT id AS channelid,loginname FROM sms_carrier_channel_info
		UNION ALL
		SELECT channelid,
		CASE
		WHEN channeltemplatevar->'$.appId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appId')
		WHEN channeltemplatevar->'$.appid' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appid')
		WHEN channeltemplatevar->'$.LoginName' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.LoginName')
		WHEN channeltemplatevar->'$.uname' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.uname')
		WHEN channeltemplatevar->'$.accessKeyId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKeyId')
		WHEN channeltemplatevar->'$.accessKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKey')
		WHEN channeltemplatevar->'$.username' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.username')
		WHEN channeltemplatevar->'$.AccountId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AccountId')
		WHEN channeltemplatevar->'$.Account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.Account')
		WHEN channeltemplatevar->'$.account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.account')
		WHEN channeltemplatevar->'$.AppKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AppKey')
		WHEN channeltemplatevar->'$.api_key' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.api_key')
		WHEN channeltemplatevar->'$.apiKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.apiKey')
		WHEN channeltemplatevar->'$.X_APP_ID' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.X_APP_ID')
		END  AS loginname FROM http_channel_template_config b
		UNION ALL
		SELECT channelid,
		CASE
		WHEN channeltemplatevar->'$.appId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appId')
		WHEN channeltemplatevar->'$.appid' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.appid')
		WHEN channeltemplatevar->'$.LoginName' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.LoginName')
		WHEN channeltemplatevar->'$.uname' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.uname')
		WHEN channeltemplatevar->'$.accessKeyId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKeyId')
		WHEN channeltemplatevar->'$.accessKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.accessKey')
		WHEN channeltemplatevar->'$.username' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.username')
		WHEN channeltemplatevar->'$.AccountId' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AccountId')
		WHEN channeltemplatevar->'$.Account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.Account')
		WHEN channeltemplatevar->'$.account' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.account')
		WHEN channeltemplatevar->'$.AppKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.AppKey')
		WHEN channeltemplatevar->'$.api_key' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.api_key')
		WHEN channeltemplatevar->'$.apiKey' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.apiKey')
		WHEN channeltemplatevar->'$.X_APP_ID' IS NOT NULL THEN JSON_UNQUOTE(channeltemplatevar->'$.X_APP_ID')
		END  AS loginname FROM mms_http_channel_template_config where channelid != 0) c
		) c ON a.channelid = c.channelid

	</select>

	<select id="listPageClientBillList" parameterType="SmsSpProvinceSummary" resultMap="SmsSpProvinceSummaryResultMap">
		select mm.* from (
		SELECT
			scb.spid,
			scb.sp_name,
			scb.month,
			sp.sp_saler,
			sss.clientid,
			DATE_FORMAT(scb.adjust_date,'%Y-%m-%d') adjust_date,
			scb.is_api_billing,
		    se.full_name,
			SUM(ifnull(scb.submit_succ, 0 )) submit_succ,
			SUM(ifnull(scb.arrive_succ, 0 )) arrive_succ,
			SUM(ifnull(scb.arrive_fail, 0 )) arrive_fail,
			SUM(ifnull(scb.submit_fail, 0 )) submit_fail,
			SUM(scb.income),
			SUM(ifnull(sso.writeAmt, 0 )) writeAmt,
			ifnull( sso.writeOffStatus, 3 ) writeOffStatus,
			scb.sp_price
		FROM
			sms_client_bill_info scb  left join
			sms_sp_info sp on scb.spid = sp.id left join
			sms_api_socket_sp sss on scb.spid = sss.spid left join
			sms_enterprise se on se.id = sp.eid left join
			( SELECT sp_id, SUM( amount ) writeAmt, sp_price,( CASE WHEN SUM( amount ) >= MAX(income) THEN 1 ELSE 2 END ) AS writeOffStatus
			from sms_sp_write_off
			<where>
				status = 0
				<if test="month!=null and month!=''">
					and `month` like CONCAT(#{month},'%')
				</if>
			</where>
			GROUP BY sp_id,sp_price ) sso on sso.sp_id = scb.spid and sso.sp_price = scb.sp_price
		<where>
			<if test="month!=null and month!=''">
				and scb.`month` =  #{month}
			</if>
			<if test="spId!=null">
				AND scb.spid = #{spId}
			</if>
			<if test="spName!=null and spName!=''">
				AND scb.sp_name like CONCAT('%',#{spName},'%')
			</if>
			<if test="companyName!=null and companyName!=''">
				AND se.full_name like CONCAT('%',#{companyName},'%')
			</if>
			<if test="sp_saler!=null and sp_saler!=''">
				and sp.sp_saler like CONCAT('%', #{sp_saler}, '%')
			</if>
			<if test="managerId != null and managerId != ''">
				and sp.sp_saler in (select ifnull(seller,'') from sale_manager sm where managerId=#{managerId}) and
				sp.sp_saler != ''
			</if>
		</where>
		GROUP BY scb.spid,
		scb.sp_price,
		scb.MONTH,
		scb.sp_name,
		sp.sp_saler,
		sss.clientid,
		adjust_date,
		scb.is_api_billing,
		se.full_name,
		writeOffStatus
		ORDER BY submit_succ desc,scb.spid desc,scb.adjust_date desc
		) mm
		<where>
			<if test="writeOffStatus!=null and writeOffStatus!=''">
				and mm.writeOffStatus = #{writeOffStatus}
			</if>
		</where>
		order by
		<choose>
			<when test="sort != null and sort != '' and sort == 'up'">
				mm.adjust_date asc
			</when>
			<when test="sort != null and sort != '' and sort == 'down'">
				mm.adjust_date desc
			</when>
			<otherwise>
				mm.arrive_succ desc,mm.spid desc,mm.adjust_date desc
			</otherwise>
		</choose>

	</select>

	<select id="getClientBillList" parameterType="SmsSpProvinceSummary" resultMap="SmsSpProvinceSummaryResultMap">
		select mm.* from (
		SELECT
			scb.spid,
			scb.sp_name,
			scb.month,
			sp.sp_saler,
			sss.clientid,
			DATE_FORMAT(scb.adjust_date,'%Y-%m-%d') adjust_date,
			scb.is_api_billing,
			se.full_name,
			SUM(ifnull(scb.submit_succ, 0 )) submit_succ,
			SUM(ifnull(scb.arrive_succ, 0 )) arrive_succ,
			SUM(ifnull(scb.arrive_fail, 0 )) arrive_fail,
			SUM(ifnull(scb.submit_fail, 0 )) submit_fail,
			SUM(scb.income),
			SUM(ifnull(sso.writeAmt, 0 )) writeAmt,
			ifnull( sso.writeOffStatus, 3 ) writeOffStatus,
			scb.sp_price
		FROM
		sms_client_bill_info scb  left join
		sms_sp_info sp on scb.spid = sp.id left join
		sms_api_socket_sp sss on scb.spid = sss.spid left join
		sms_enterprise se on se.id = sp.eid left join
		( SELECT sp_id, SUM( amount ) writeAmt, sp_price,( CASE WHEN SUM( amount ) >= MAX(income) THEN 1 ELSE 2 END ) AS writeOffStatus
		from sms_sp_write_off
		<where>
			status = 0
			<if test="month!=null and month!=''">
				and `month` like CONCAT(#{month},'%')
			</if>
		</where>
		GROUP BY sp_id,sp_price ) sso on sso.sp_id = scb.spid and sso.sp_price = scb.sp_price
		<where>
			<if test="month!=null and month!=''">
				and scb.`month` =  #{month}
			</if>
			<if test="spId!=null">
				AND scb.spid = #{spId}
			</if>
			<if test="spName!=null and spName!=''">
				AND scb.sp_name like CONCAT('%',#{spName},'%')
			</if>
			<if test="companyName!=null and companyName!=''">
				AND se.full_name like CONCAT('%',#{companyName},'%')
			</if>
			<if test="sp_saler!=null and sp_saler!=''">
				and sp.sp_saler like CONCAT('%', #{sp_saler}, '%')
			</if>
			<if test="managerId != null and managerId != ''">
				and sp.sp_saler in (select ifnull(seller,'') from sale_manager sm where managerId=#{managerId}) and
				sp.sp_saler != ''
			</if>
		</where>
-- 		GROUP BY IFNULL(se.full_name,scb.spid),scb.sp_price
		GROUP BY scb.spid,
		scb.sp_price,
		scb.MONTH,
		scb.sp_name,
		sp.sp_saler,
		sss.clientid,
		adjust_date,
		scb.is_api_billing,
		se.full_name,
		writeOffStatus
		ORDER BY arrive_succ desc,scb.spid desc,scb.adjust_date desc
		) mm
		<where>
			<if test="writeOffStatus!=null and writeOffStatus!=''">
				and mm.writeOffStatus = #{writeOffStatus}
			</if>
		</where>
		ORDER BY mm.arrive_succ desc,mm.spid desc,mm.adjust_date desc
	</select>

</mapper>
