<?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.UnifiedWarningMapperEx">



	<resultMap id="OrderAmountResultMap" type="cn.superdesk.uniorder.dto.vo.unified.WarningOrderAmountVo">
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="virt_id" property="virtId" jdbcType="BIGINT"/>
		<result column="virt_name" property="virtName" jdbcType="VARCHAR"/>
		<result column="project_id" property="projectId" jdbcType="BIGINT"/>
		<result column="credit" property="credit" jdbcType="DECIMAL"/>
		<result column="cycle_days" property="cycleDays" jdbcType="INTEGER"/>
	</resultMap>

	<select id="getWarningOrderAmount" resultMap="OrderAmountResultMap">
		SELECT
			TA.amount,B.virt_id,B.virt_name,B.credit,B.cycle_days,B.project_id
		FROM
		(
			SELECT
				SUM(amount) AS amount,purchase_virt_id
			from
				unified_order
			WHERE
				pay_state=2
			GROUP BY purchase_virt_id
		) TA LEFT JOIN unified_customer B ON TA.purchase_virt_id=B.virt_id
		WHERE
			TA.amount>B.credit
	</select>

	<resultMap id="OrderInvResultMap" type="cn.superdesk.uniorder.dto.vo.unified.WarningOrderInvoiceVo">
		<result column="invoice_number" property="invoiceNumber" jdbcType="VARCHAR"/>
		<result column="virt_id" property="virtId" jdbcType="BIGINT"/>
		<result column="virt_name" property="virtName" jdbcType="VARCHAR"/>
		<result column="project_id" property="projectId" jdbcType="BIGINT"/>
		<result column="credit" property="credit" jdbcType="DECIMAL"/>
		<result column="cycle_days" property="cycleDays" jdbcType="INTEGER"/>
		<result column="make_invoice_time" property="makeInvoiceTime" jdbcType="TIMESTAMP"/>
	</resultMap>

	<select id="getWarningOrderInv" resultMap="OrderInvResultMap">
		SELECT
			TA.invoice_number,TA.make_invoice_time,TB.virt_id,TB.virt_name,TB.credit,TB.cycle_days,TB.project_id
		from
			(
				SELECT
					inva.invoice_number,
					aucuo.enterprise_id,
					inva.make_invoice_time
				FROM
					invoice_apply_x_goods invo
					LEFT JOIN accounting_unified_customer_order aucuo ON invo.unified_order_id = aucuo.id
					LEFT JOIN invoice_apply inva ON invo.invoice_apply_id = inva.id
				<where>
					and inva.back_account_status != 2
					and inva.make_invoice_status = 1
				</where>
				GROUP BY inva.invoice_number,aucuo.enterprise_id
			) TA LEFT JOIN unified_customer TB ON TA.enterprise_id = TB.virt_id
		WHERE
			DATEDIFF(CURDATE(),TA.make_invoice_time)>TB.cycle_days
	</select>

	<resultMap id="VoResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedWarningVo">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="virt_id" property="virtId" jdbcType="BIGINT"/>
		<result column="warning_type" property="warningType" jdbcType="TINYINT"/>
		<result column="inv_no" property="invNo" jdbcType="VARCHAR"/>
		<result column="credit_pass" property="creditPass" jdbcType="DECIMAL"/>
		<result column="state" property="state" jdbcType="TINYINT"/>
		<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="virt_name" property="virtName" jdbcType="VARCHAR"/>
		<result column="project_name" property="projectName" jdbcType="VARCHAR"/>
		<result column="project_id" property="projectId" jdbcType="BIGINT"/>
	</resultMap>

	<select id="getData" resultMap="VoResultMap">
		SELECT
			a.id,
			a.virt_id,
			a.warning_type,
			a.inv_no,
			a.credit_pass,
			a.state,
			a.create_time,
			a.create_user_id,
			a.update_time,
			a.update_user_id,
			b.virt_name,
			b.project_id,
			c.project_name,
			c.customer_user_id,
			cs.user_name AS customer_user_name
		FROM
			unified_warning AS a
			LEFT JOIN unified_customer AS b ON a.virt_id = b.virt_id
			LEFT JOIN unified_project AS c ON b.project_id = c.project_id
			LEFT JOIN sys_user AS cs ON cs.id = c.customer_user_id
		<where>
		<if test="project != null and project != '' ">
			and (
				b.project_id = #{project} or
				c.project_name like CONCAT('%',#{project},'%')
			)
		</if>
		<if test="cususername != null and cususername != '' ">
			and cs.user_name like CONCAT('%',#{cususername},'%')
		</if>
		<if test="state != null and state != ''">
			and state = #{state,jdbcType=TINYINT}
		</if>
		<if test="startTime != null and startTime != ''">
			and DATE_FORMAT(a.create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(a.create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
		</if>
		<if test="overstarTime != null and overstarTime != ''">
			and DATE_FORMAT(a.update_time,'%Y-%m-%d') &gt;= #{overstarTime,jdbcType=VARCHAR}
		</if>
		<if test="overendTime != null and overendTime != ''">
			and DATE_FORMAT(a.update_time,'%Y-%m-%d') &lt;= #{overendTime,jdbcType=VARCHAR}
		</if>
		</where>
		order by a.create_time desc
	</select>

</mapper>