<?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.base.UnifiedOrderReturnMapperEx">

	<resultMap id="UnifiedOrderReturnVoPageResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderReturnVo">
		<result column="id" property="id" jdbcType="BIGINT"/>
		<result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
		<result column="goods_alias" jdbcType="VARCHAR" property="goodsAlias" />
		<result column="quantity" property="quantity" jdbcType="INTEGER"/>
		<result column="cost_price" property="costPrice" jdbcType="DECIMAL"/>
		<result column="price" property="price" jdbcType="DECIMAL"/>
		<result column="order_return_id" property="orderReturnId" jdbcType="BIGINT"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="return_sn" property="returnSn" jdbcType="VARCHAR"/>
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="transport_amount" property="transportAmount" jdbcType="DECIMAL"/>
		<result column="complet_state" property="completState" jdbcType="TINYINT"/>
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="purchase_virt_name" property="purchaseVirtName" jdbcType="VARCHAR"/>
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>
	</resultMap>

	<resultMap id="PageHResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderCommonHeadSum">
		<result column="total" property="total" jdbcType="INTEGER"/>
		<result column="return_amount" property="returnAmount" jdbcType="DECIMAL"/>
	</resultMap>

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderReturnMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderReturnMapper.Base_Column_List"/>
		from
			unified_order_return
		<where>
		<if test="orderSn != null and orderSn != ''">
			and order_sn = #{orderSn}
		</if>
		</where>
		order by create_time desc
	</select>

	<select id="findPage" resultMap="UnifiedOrderReturnVoPageResultMap">
		SELECT
			uorg.id,
			uorg.goods_name,
			uorg.goods_alias,
			uorg.quantity,
			uorg.cost_price,
			uorg.price,
			uor.id AS order_return_id,
			uor.order_sn,
			uor.return_sn,
			uor.amount,
			uor.transport_amount,
			uor.complet_state,
			uor.create_time,
			uo.purchase_virt_name,
			uo.sale_name
		FROM
			unified_order_return_goods AS uorg
			LEFT JOIN unified_order_return AS uor ON uorg.return_sn = uor.return_sn
			LEFT JOIN unified_order AS uo ON uo.order_sn = uor.order_sn
		<where>

		<if test="serviceName != null and serviceName != ''">
			and uo.service_name = #{serviceName}
		</if>
		<if test="orderSn != null and orderSn != ''">
			and uor.order_sn like CONCAT('%',#{orderSn},'%')
		</if>
		<if test="returnSn != null and returnSn != ''">
			and uor.return_sn like CONCAT('%',#{returnSn},'%')
		</if>
		<if test="saleName != null and saleName != ''">
			and uo.sale_name like CONCAT('%',#{saleName},'%')
		</if>
		<if test="virtName != null and virtName != ''">
			and uo.purchase_virt_name like CONCAT('%',#{virtName},'%')
		</if>
		<if test="startime != null and startime != ''">
			and DATE_FORMAT(uor.create_time,'%Y-%m-%d') &gt;= #{startime,jdbcType=VARCHAR}
		</if>
		<if test="endtime != null and endtime != ''">
			and DATE_FORMAT(uor.create_time,'%Y-%m-%d') &lt;= #{endtime,jdbcType=VARCHAR}
		</if>
		</where>
		order by uor.create_time desc
	</select>

	<select id="getPageH" resultMap="PageHResultMap">
		SELECT
			count(uor.id) as total,IFNULL(sum(uor.amount),0) as return_amount
		FROM
			unified_order_return AS uor
			LEFT JOIN unified_order AS uo ON uo.order_sn = uor.order_sn
		where
			1=1
		<if test="serviceName != null and serviceName != ''">
			and uo.service_name = #{serviceName}
		</if>
		<if test="orderSn != null and orderSn != ''">
			and uor.order_sn like CONCAT('%',#{orderSn},'%')
		</if>
		<if test="returnSn != null and returnSn != ''">
			and uor.return_sn like CONCAT('%',#{returnSn},'%')
		</if>
		<if test="saleName != null and saleName != ''">
			and uo.sale_name like CONCAT('%',#{saleName},'%')
		</if>
		<if test="virtName != null and virtName != ''">
			and uo.purchase_virt_name like CONCAT('%',#{virtName},'%')
		</if>
		<if test="startime != null and startime != ''">
			and DATE_FORMAT(uor.create_time,'%Y-%m-%d') &gt;= #{startime,jdbcType=VARCHAR}
		</if>
		<if test="endtime != null and endtime != ''">
			and DATE_FORMAT(uor.create_time,'%Y-%m-%d') &lt;= #{endtime,jdbcType=VARCHAR}
		</if>
	</select>

	<select id="findByRefundOrderSn"
			resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderReturnMapper.BaseResultMap" parameterType="java.lang.String">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderReturnMapper.Base_Column_List"/>
		from
			unified_order_return
		<where>
			<if test="refundOrderSn != null and refundOrderSn != ''">
			and return_sn = #{refundOrderSn,jdbcType=VARCHAR}
			</if>
		</where>
	</select>

	<select id="findByOrderSn"
			resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderReturnMapper.BaseResultMap" parameterType="java.lang.String">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderReturnMapper.Base_Column_List"/>
		from
			unified_order_return
		<where>
			<if test="orderSn != null and orderSn != ''">
				and order_sn = #{orderSn,jdbcType=VARCHAR}
			</if>
		</where>
	</select>


</mapper>