<?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.accounting.score.customer.AccountingScoreCustomerMapperEx">

	<sql id="getAccountingListWhereIf">

		<if test="ids != null and ids != ''">
			and ascu.id in
			<foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">
				#{id}
			</foreach>
		</if>

		<!--
		//	结算单编号
		private String accountingOrderSn;
		-->
		<if test="accountingOrderSn != null and accountingOrderSn != '' ">
			and ascu.accounting_order_sn like CONCAT('%',#{accountingOrderSn},'%')
		</if>

		<!--
		//	企业名称
		private Long   enterpriseId;
		private String enterpriseName;
		-->

		<if test="enterpriseId != null and enterpriseId != '' ">
			and ascu.enterprise_id = #{enterpriseId}
		</if>
		<if test="enterpriseName != null and enterpriseName != '' ">
			and se.enterprise_name like CONCAT('%',#{enterpriseName},'%')
		</if>

		<!--
		//	出账时间
		private String startTime;
		private String endTime;
		-->

		<!-- 创建时间 so.create_time -->
		<if test="startTime != null and startTime != ''">
			and DATE_FORMAT(ascu.create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(ascu.create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
		</if>


		<!--
		//	结算单状态
		/**
		 * 对账单(结算单)状态:0-未确认;1-已确认;-1-已取消;
		 */
		private Integer accountingStatus;
		-->
		<if test="accountingStatus != null and accountingStatus != '' or accountingStatus == 0">
			and ascu.accounting_status = #{accountingStatus}
		</if>

		<!--
		//	邮件状态
		/**
		 * 是否发邮件:0-不发;1-发;
		 */
		private Integer isSendEmail;
		-->
		<if test="isSendEmail != null and isSendEmail != '' ">
			and ascu.is_send_email = #{isSendEmail}
		</if>

		<!--
		//	发票状态
		/**
		 * 开票状态:0-未开票;1-已开票;2-部分开票;
		 */
		private Integer invoiceStatus;
		-->
		<if test="invoiceStatus != null and invoiceStatus != '' or invoiceStatus ==0">
			and ascu.invoice_status = #{invoiceStatus}
		</if>

		<!--
		//	账单联系人
		private Integer purchaseUserId;
		-->
		<if test="purchaseUserId != null and purchaseUserId != '' ">
			and ascu.purchase_user_id = #{purchaseUserId}
		</if>
		<!--
		private String  purchaseUserName;
		-->
		<if test="purchaseUserName != null and purchaseUserName != '' ">
			and ascu.purchase_user_name like CONCAT('%',#{purchaseUserName},'%')
		</if>
		<!--
		//	账单联系电话
		private String  purchasePhone;
		-->
		<if test="purchasePhone != null and purchasePhone != '' ">
			and ascu.purchase_phone like CONCAT('%',#{purchasePhone},'%')
		</if>


		<if test="batchs != null and batchs != '' ">

			and ascu.id in (
				SELECT
					distinct ascuxc.accounting_order_id
				FROM
					unified_order_credit uoc
					LEFT JOIN accounting_score_customer_x_credit ascuxc ON uoc.id = ascuxc.unified_order_credit_id
				WHERE
					batchs = #{batchs}
			)
		</if>

	</sql>

	<select id="getAccountingOrderList" parameterType="Map" resultType="cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerVo">

		SELECT
			ascu.*,
			'' as accounting_status_name,
			se.enterprise_name
		FROM
			accounting_score_customer ascu
			LEFT JOIN sys_enterprise se ON ascu.enterprise_id = se.enterprise_id
		<where>
			<include refid="getAccountingListWhereIf"></include>
			and ascu.deleted = 0
		</where>
		order by ascu.create_time desc
	</select>

	<select id="getAccountingListHeadSum" parameterType="Map" resultType="cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingScoreCustomerHeadVo">
		SELECT
			ifnull(SUM(socg.total_accounting_order),0) as total_accounting_order,
			ifnull(SUM(socg.quantity_order),0) as total_quantity_order,
			ifnull(SUM(socg.total_balance),0) as total_balance_amount
		FROM (
			SELECT
				COUNT(ascu.accounting_order_sn) as total_accounting_order,
				SUM(ascu.quantity_order) as quantity_order,
				SUM(ascu.accounting_amount) total_balance
			FROM
				accounting_score_customer ascu
				LEFT JOIN sys_enterprise se ON ascu.enterprise_id = se.enterprise_id
			<where>
				<include refid="getAccountingListWhereIf"></include>
				and ascu.deleted = 0
			</where>

			GROUP by
			ascu.accounting_order_sn
		) socg
	</select>

</mapper>