<?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="com.zcloud.mapper.datasource.bus.OrderMapper">
	
	<!--表名 -->
	<sql id="tableName">
		BUS_ORDER
	</sql>
	
	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		f.USER_ID,	
		f.ISDELETE,	
		f.CREATOR,	
		f.CREATTIME,	
		f.OPERATOR,	
		f.OPERATTIME,	
		f.ADDRESS_ID,	
		f.CLASSIFY_ID,
		f.TIMEMANAGE_ID,	
		f.ESTIMATE,	
		f.DESCR,	
		f.STATUS,	
		f.CODE,	
		f.TRANSACTION_NUMBER,
		f.DISTRIBUTION_USER_ID,	
		f.DISTRIBUTION_TIME,	
		f.DISTRIBUTION_DESCR,	
		f.IS_COUPON,
		f.IMG_URLS,	
		f.ORDER_ID,
		cu.NAME AS USER_NAME,
		a.NAME as ADDRESS_NAME,
		a.ADDRESS as ADDRESS,
		a.PHONE as ADDRESS_PHONE,
		u.NAME as DISTRIBUTION_USER_NAME,
		c.NAME as CLASSIFY_NAME,
		f.TIMEQUANTUM,
		f.PRICE,	
		f.PICKUP_IMG_URLS,
		f.RETURN_IMG_URLS,
		f.CHARGEBACK_TIME,
		f.PROCESSING_TIME,
		f.COMPLETION_TIME,
		f.CLASSIFY_CHILD_ID,
		f.CHARGEBACK_DESCR,
		f.CHARGEBACK_NUMBER,
		f.SCHOOL,
		f.GRADE,
		f.CLASS,
		f.PAPER,
		f.STUDENT_NAME
	</sql>
	
	<!-- 字段用于新增 -->
	<sql id="Field2">
		USER_ID,	
		ISDELETE,	
		CREATOR,	
		CREATTIME,	
		OPERATOR,	
		OPERATTIME,	
		ADDRESS_ID,	
		CLASSIFY_ID,	
		TIMEMANAGE_ID,
		ESTIMATE,	
		DESCR,	
		STATUS,	
		CODE,
		TRANSACTION_NUMBER,
		DISTRIBUTION_USER_ID,	
		DISTRIBUTION_TIME,	
		DISTRIBUTION_DESCR,	
		IS_COUPON,
		IMG_URLS,	
		ORDER_ID,
		TIMEQUANTUM,
		PRICE,	
		CLASSIFY_CHILD_ID,
		SCHOOL,
		GRADE,
		CLASS,
		PAPER,
		STUDENT_NAME
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USER_ID},	
		#{ISDELETE},	
		#{CREATOR},	
		#{CREATTIME},	
		#{OPERATOR},	
		#{OPERATTIME},	
		#{ADDRESS_ID},	
		#{CLASSIFY_ID},	
		#{TIMEMANAGE_ID},
		#{ESTIMATE},	
		#{DESCR},	
		#{STATUS},	
		#{CODE},
		#{TRANSACTION_NUMBER},
		#{DISTRIBUTION_USER_ID},	
		#{DISTRIBUTION_TIME},	
		#{DISTRIBUTION_DESCR},	
		#{IS_COUPON},
		#{IMG_URLS},	
		#{ORDER_ID},
		#{TIMEQUANTUM},
		#{PRICE},	
		#{CLASSIFY_CHILD_ID},
		#{SCHOOL},
		#{GRADE},
		#{CLASS},
		#{PAPER},
		#{STUDENT_NAME}
	</sql>
	
	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into 
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>
	
	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			STATUS = #{STATUS}
		where 
			ORDER_ID = #{ORDER_ID}
	</delete>
	
	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set 
			USER_ID = #{USER_ID},
			ADDRESS_ID = #{ADDRESS_ID},
			CLASSIFY_ID = #{CLASSIFY_ID},
			TIMEMANAGE_ID=#{TIMEMANAGE_ID},
			TIMEQUANTUM=#{TIMEQUANTUM},
			ESTIMATE = #{ESTIMATE},
			DESCR = #{DESCR},
			STATUS = #{STATUS},
			DISTRIBUTION_USER_ID = #{DISTRIBUTION_USER_ID},
			DISTRIBUTION_TIME = #{DISTRIBUTION_TIME},
			DISTRIBUTION_DESCR = #{DISTRIBUTION_DESCR},
			IMG_URLS = #{IMG_URLS},
			PICKUP_IMG_URLS = #{PICKUP_IMG_URLS},
			RETURN_IMG_URLS = #{RETURN_IMG_URLS},
			CHARGEBACK_DESCR = #{CHARGEBACK_DESCR},
			CHARGEBACK_TIME = #{CHARGEBACK_TIME},
			PROCESSING_TIME = #{PROCESSING_TIME},
			COMPLETION_TIME = #{COMPLETION_TIME},
			CHARGEBACK_NUMBER=#{CHARGEBACK_NUMBER},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},	
			ORDER_ID = ORDER_ID
		where 
			ORDER_ID = #{ORDER_ID}
	</update>
	
	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select 
		<include refid="Field"></include>,
		xx.NAME SCHOOLNAME,
		nj.NAME GRADENAME,
		bj.NAME CLASSNAME
		from 
		<include refid="tableName"></include> f
		left join BUS_ADDRESS a on a.ADDRESS_ID=f.ADDRESS_ID
		left join SYS_USER u on u.USER_ID=f.DISTRIBUTION_USER_ID
		left join BUS_CLASSIFY c on c.CLASSIFY_ID = f.CLASSIFY_ID
		left join BUS_CUSTOMER cu on cu.APPID=f.USER_ID
		left join SYS_DICTIONARIES xx on xx.YNDEL = 'no' and xx.DICTIONARIES_ID = f.SCHOOL
		left join SYS_DICTIONARIES nj on nj.YNDEL = 'no' and nj.DICTIONARIES_ID = f.GRADE
		left join SYS_DICTIONARIES bj on bj.YNDEL = 'no' and bj.DICTIONARIES_ID = f.CLASS
		where 
			f.ORDER_ID = #{ORDER_ID}
	</select>
	
	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from 
		<include refid="tableName"></include> f
		left join BUS_ADDRESS a on a.ADDRESS_ID=f.ADDRESS_ID
		left join SYS_USER u on u.USER_ID=f.DISTRIBUTION_USER_ID
		left join BUS_CLASSIFY c on c.CLASSIFY_ID = f.CLASSIFY_ID
		left join BUS_CUSTOMER cu on cu.APPID=f.USER_ID
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					cu.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					f.CODE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.USER_ID != null and pd.USER_ID != ''"><!-- 用户-->
			AND 
				f.USER_ID = #{pd.USER_ID}
		</if>
		<if test="pd.isFInish != null and pd.isFInish == 0 "><!-- 未完成-->
			AND 
				(
					f.STATUS &lt; 2 
					or
					f.STATUS = 5
				)
		</if>
		<if test="pd.isFInish != null and pd.isFInish == 1 "><!-- 已完成-->
			AND f.STATUS &gt; 1
			and f.STATUS != 5	
		</if>
		order by f.CREATTIME desc
	</select>
	
	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		xx.NAME SCHOOLNAME,
		nj.NAME GRADENAME,
		bj.NAME CLASSNAME
		from 
		<include refid="tableName"></include> f
		left join BUS_ADDRESS a on a.ADDRESS_ID=f.ADDRESS_ID
		left join SYS_USER u on u.USER_ID=f.DISTRIBUTION_USER_ID
		left join BUS_CLASSIFY c on c.CLASSIFY_ID = f.CLASSIFY_ID
		left join BUS_CUSTOMER cu on cu.APPID=f.USER_ID
		left join SYS_DICTIONARIES xx on xx.YNDEL = 'no' and xx.DICTIONARIES_ID = f.SCHOOL
		left join SYS_DICTIONARIES nj on nj.YNDEL = 'no' and nj.DICTIONARIES_ID = f.GRADE
		left join SYS_DICTIONARIES bj on bj.YNDEL = 'no' and bj.DICTIONARIES_ID = f.CLASS
		where 
				f.TRANSACTION_NUMBER = #{TRANSACTION_NUMBER} and f.TRANSACTION_NUMBER is not null
				AND f.TRANSACTION_NUMBER!=''
	</select>
	
	<!-- 列表(全部) -->
	<select id="listAllUnFinished" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		xx.NAME SCHOOLNAME,
		nj.NAME GRADENAME,
		bj.NAME CLASSNAME
		from 
		<include refid="tableName"></include> f
		left join BUS_ADDRESS a on a.ADDRESS_ID=f.ADDRESS_ID
		left join SYS_USER u on u.USER_ID=f.DISTRIBUTION_USER_ID
		left join BUS_CLASSIFY c on c.CLASSIFY_ID = f.CLASSIFY_ID
		left join BUS_CUSTOMER cu on cu.APPID=f.USER_ID
		left join SYS_DICTIONARIES xx on xx.YNDEL = 'no' and xx.DICTIONARIES_ID = f.SCHOOL
		left join SYS_DICTIONARIES nj on nj.YNDEL = 'no' and nj.DICTIONARIES_ID = f.GRADE
		left join SYS_DICTIONARIES bj on bj.YNDEL = 'no' and bj.DICTIONARIES_ID = f.CLASS
		where 
				f.STATUS = 0
		<if test="PAPER != null and PAPER != ''"><!--纸张类型 -->
			AND
				f.PAPER = #{PAPER}
		</if>
	</select>
	
	<!-- 列表 -->
	<select id="todayCount" resultType="pd">
		select
			count(1) as count
		from 
		<include refid="tableName"></include> f
		where 
		DATE_FORMAT(f.CREATTIME,'%Y-%m-%d')=DATE_FORMAT(NOW(),'%Y-%m-%d')
		<if test="CLASSIFY_ID != null and CLASSIFY_ID != ''"><!-- 关键词检索 -->
			AND
				f.CLASSIFY_ID = #{CLASSIFY_ID}
		</if>
	</select>
	
	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where 
			ORDER_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
	
	<!-- 查询服务人员订单 -->
	<select id="getOrderList" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		c2.NAME as CLASSIFY_CHILD_NAME
		from 
		<include refid="tableName"></include> f
		left join BUS_ADDRESS a on a.ADDRESS_ID=f.ADDRESS_ID
		left join SYS_USER u on u.USER_ID=f.DISTRIBUTION_USER_ID
		left join BUS_CLASSIFY c on c.CLASSIFY_ID = f.CLASSIFY_ID
		left join BUS_CLASSIFY c2 on c2.CLASSIFY_ID = f.CLASSIFY_CHILD_ID
		left join BUS_CUSTOMER cu on cu.APPID=f.USER_ID
		where f.ISDELETE = '0'
			and f.DISTRIBUTION_USER_ID = #{DISTRIBUTION_USER_ID}
		<if test="STATUS != null and STATUS != ''" >
			and f.STATUS in
			<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
	                 #{item}
			</foreach>
		</if>
		<if test="CLASSIFY_ID != null and CLASSIFY_ID != ''" >
			and f.CLASSIFY_ID = #{CLASSIFY_ID}
		</if>
		order by f.CREATTIME desc
	</select>
	
	<!-- 完成订单 -->
	<update id="finishOrder" parameterType="pd">
		update
		<include refid="tableName"></include>
		set 
			STATUS = #{STATUS},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},	
			ORDER_ID = ORDER_ID
		where 
			ORDER_ID = #{ORDER_ID}
	</update>
	<!-- 查看未完成订单 -->
	<select id="findOrderByClassify" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		xx.NAME SCHOOLNAME,
		nj.NAME GRADENAME,
		bj.NAME CLASSNAME
		from 
		<include refid="tableName"></include> f
		left join BUS_ADDRESS a on a.ADDRESS_ID=f.ADDRESS_ID
		left join SYS_USER u on u.USER_ID=f.DISTRIBUTION_USER_ID
		left join BUS_CLASSIFY c on c.CLASSIFY_ID = f.CLASSIFY_ID
		left join BUS_CUSTOMER cu on cu.APPID=f.USER_ID
		left join SYS_DICTIONARIES xx on xx.YNDEL = 'no' and xx.DICTIONARIES_ID = f.SCHOOL
		left join SYS_DICTIONARIES nj on nj.YNDEL = 'no' and nj.DICTIONARIES_ID = f.GRADE
		left join SYS_DICTIONARIES bj on bj.YNDEL = 'no' and bj.DICTIONARIES_ID = f.CLASS
		where 
			f.STATUS = '1'
			and f.ISDELETE = '0'
		<if test="CLASSIFY_ID != null and CLASSIFY_ID != ''" >
			and f.CLASSIFY_ID = #{CLASSIFY_ID}
		</if>
	
	
	</select>
		<!-- 完成订单 -->
	<update id="finishOrderByClassify" parameterType="pd">
		update
		<include refid="tableName"></include>
		set 
			STATUS = #{STATUS},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			COMPLETION_TIME = #{COMPLETION_TIME}
		where 
			STATUS = '1'
			and ISDELETE = '0'
		<if test="CLASSIFY_ID != null and CLASSIFY_ID != ''" >
			and CLASSIFY_ID = #{CLASSIFY_ID}
		</if>
	</update>
	
   	<!-- 订单统计 -->
   	<select id="orderCount" parameterType="pd" resultType="pd">
        SELECT
			f.CLASSIFY_ID,
			c.NAME,
			count( f.CLASSIFY_ID ) NUM 
		FROM
			bus_order f
			LEFT JOIN BUS_CLASSIFY c ON c.CLASSIFY_ID = f.CLASSIFY_ID 
		WHERE
			f.isdelete = '0' 
			AND f.DISTRIBUTION_USER_ID = #{DISTRIBUTION_USER_ID} 
			and f.STATUS = '9'
		GROUP BY
			f.CLASSIFY_ID,
			c.NAME 
		ORDER BY
			f.CLASSIFY_ID
   	</select>

 	<select id="getSpeOrdernumber" parameterType="pd" resultType="pd">
        select _nextval('ordernumber')  as ordernumber
   	</select>
   	<select id="getBackOrdernumber" parameterType="pd" resultType="pd">
        select _nextval('backordernumber')  as backordernumber
   	</select>
</mapper>
