<?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="getAccountingOrderListWhereIf">

		<if test="accountingOrderIds != null and accountingOrderIds != ''">
			and ascu.id in
			<foreach collection="accountingOrderIds" 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-发;  and isSendEmail != ''
		 */
		private Integer isSendEmail;
		-->
		<if test="isSendEmail != null">
			and ascu.is_send_email = #{isSendEmail}
		</if>

		<!--
		//	发票状态
		/**
		 * 开票状态:0-未开票;1-已开票;2-部分开票;  and invoiceStatus != '' or invoiceStatus ==0
		 */
		private Integer invoiceStatus;
		-->
		<if test="invoiceStatus != null">
			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>

		<if test="needReviseMark != null">
			and ascu.need_revise_mark = #{needReviseMark}
		</if>

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

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

	</sql>



	<resultMap id="AccountingOrderListResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerListItemVo">
		<id column="id" jdbcType="BIGINT" property="id" />
		<result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn" />

		<result column="service_no" jdbcType="VARCHAR" property="serviceNo" />
		<result column="source_from" jdbcType="VARCHAR" property="sourceFrom" />

		<result column="credit_type" jdbcType="VARCHAR" property="creditType" />
		<result column="credit_name" jdbcType="VARCHAR" property="creditName" />
		<result column="batchs" jdbcType="VARCHAR" property="batchs" />

		<result column="quantity_order" jdbcType="INTEGER" property="quantityOrder" />
		<result column="quantity_order_goods" jdbcType="INTEGER" property="quantityOrderGoods" />
		<result column="quantity_order_credit" jdbcType="INTEGER" property="quantityOrderCredit" />

		<result column="accounting_amount" jdbcType="DECIMAL" property="accountingAmount" />
		<result column="statistics_total_amount" jdbcType="DECIMAL" property="statisticsTotalAmount" />
		<result column="un_accounting_amount" jdbcType="DECIMAL" property="unAccountingAmount" />
		<result column="accounting_status" jdbcType="TINYINT" property="accountingStatus" />
		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType" />
		<result column="invoice_status" jdbcType="TINYINT" property="invoiceStatus" />
		<result column="invoice_company_name" jdbcType="VARCHAR" property="invoiceCompanyName" />
		<result column="invoice_taxpayers_i_d_code" jdbcType="VARCHAR" property="invoiceTaxpayersIDCode" />

		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId" />

		<result column="purchase_user_id" jdbcType="BIGINT" property="purchaseUserId" />
		<result column="purchase_user_name" jdbcType="VARCHAR" property="purchaseUserName" />
		<result column="purchase_phone" jdbcType="VARCHAR" property="purchasePhone" />
		<result column="purchase_address" jdbcType="VARCHAR" property="purchaseAddress" />

		<result column="is_send_email" jdbcType="TINYINT" property="isSendEmail" />
		<result column="send_email_time" jdbcType="TIMESTAMP" property="sendEmailTime" />

		<result column="date_type" jdbcType="TINYINT" property="dateType" />
		<result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
		<result column="end_time" jdbcType="TIMESTAMP" property="endTime" />

		<result column="back_account_status" jdbcType="TINYINT" property="backAccountStatus" />
		<result column="bank_info_id" jdbcType="BIGINT" property="bankInfoId" />

		<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
		<result column="create_user_id" jdbcType="BIGINT" property="createUserId" />
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
		<result column="update_user_id" jdbcType="BIGINT" property="updateUserId" />

<!--		<result column="pay_type" jdbcType="TINYINT" property="payType" />-->
<!--		<result column="pay_status" jdbcType="TINYINT" property="payStatus" />-->
<!--		<result column="wx_qrcode_img_url" jdbcType="VARCHAR" property="wxQrcodeImgUrl" />-->
<!--		<result column="deleted" jdbcType="TINYINT" property="deleted" />-->

		<result column="remark" jdbcType="VARCHAR" property="remark" />

		<result column="need_revise_mark" jdbcType="INTEGER" property="needReviseMark" />

		<result column="express_company_id" jdbcType="BIGINT" property="expressCompanyId" />
		<result column="express_no" jdbcType="VARCHAR" property="expressNo" />
		<result column="attachment_upload_files_uids" jdbcType="VARCHAR" property="attachmentUploadFilesUids" />
	</resultMap>
	<select id="getAccountingOrderList" resultMap="AccountingOrderListResultMap">

		SELECT

			ascu.id,
			ascu.accounting_order_sn,

			ascu.credit_type,
			ascu.credit_name,
			ascu.batchs,
			ascu.quantity_order,
			ascu.quantity_order_goods,
			ascu.quantity_order_credit,
			ascu.accounting_amount,
			ascu.statistics_total_amount,
			ascu.un_accounting_amount,
			ascu.accounting_status,
			ascu.invoice_type,
			ascu.invoice_status,
			ascu.invoice_company_name,
			ascu.invoice_taxpayers_i_d_code,
			ascu.enterprise_id,
			ascu.purchase_user_id,
			ascu.purchase_user_name,
			ascu.purchase_phone,
			ascu.purchase_address,

			ascu.is_send_email,
			ascu.send_email_time,
			ascu.date_type,
			ascu.start_time,
			ascu.end_time,

			ascu.back_account_status,
			ascu.bank_info_id,

			ascu.create_time,
			ascu.create_user_id,
			ascu.update_time,
			ascu.update_user_id,

			ascu.service_no,
			ascu.source_from,

			ascu.pay_type,
			ascu.pay_status,
			ascu.wx_qrcode_img_url,
			ascu.deleted,
			ascu.remark,
			ascu.need_revise_mark,

			ascu.express_company_id,
			ascu.express_no,
			ascu.attachment_upload_files_uids,
			'' 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="getAccountingOrderListWhereIf"></include>
			and ascu.deleted = 0
		</where>
		order by ascu.create_time desc
	</select>

	<select id="getAccountingOrderListHeadSum" resultType="cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerHeadVo">
		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.quantity_order_goods),0) as total_quantity_order_goods,
			ifnull(SUM(socg.quantity_order_credit),0) as total_quantity_order_credit,

			ifnull(SUM(socg.total_statistics_total_amount),0) as total_statistics_total_amount,
			ifnull(SUM(socg.total_accounting_amount),0) as total_accounting_amount,
			ifnull(SUM(socg.total_un_accounting_amount),0) as total_un_accounting_amount
		FROM (
			SELECT
				COUNT(ascu.accounting_order_sn) as total_accounting_order,
				SUM(ascu.quantity_order) as quantity_order,
				SUM(ascu.quantity_order_goods) as quantity_order_goods,
				SUM(ascu.quantity_order_credit) as quantity_order_credit,

				SUM(ascu.statistics_total_amount) total_statistics_total_amount,
				SUM(ascu.accounting_amount) total_accounting_amount,
				SUM(ascu.un_accounting_amount) total_un_accounting_amount
			FROM
				accounting_score_customer ascu
				LEFT JOIN sys_enterprise se ON ascu.enterprise_id = se.enterprise_id
			<where>
				<include refid="getAccountingOrderListWhereIf"></include>
				and ascu.deleted = 0
			</where>

			GROUP by
			ascu.accounting_order_sn
		) socg
	</select>

</mapper>
