<?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.ym.mec.collectfee.dao.OrderDao">
	
	<resultMap type="com.ym.mec.collectfee.entity.Order" id="Order">
		<result column="id" property="id" />
		<result column="oid" property="oid" />
		<result column="branch_id" property="branchId" />
		<result column="group_id" property="groupId" />
		<result column="order_no" property="orderNo" />
		<result column="user_id" property="userId" />
		<result column="user_name" property="userName" />
		<result column="amount" property="amount" />
		<result column="type" property="type" />
		<result column="pay" property="pay"/>
		<result column="bank" property="bank" />
		<result column="account" property="account" />
		<result column="u_account" property="uAccount" />
		<result column="pay_id" property="payId" />
		<result column="pay_time" property="payTime" />
		<result column="remark" property="remark" />
		<result column="class_id" property="classId" />
		<result column="status" property="status" />
		<result column="create_time" property="createTime" />
		<result column="po_name" property="poName" />
		<result column="voicy_part" property="voicyPart" />
		<result column="balance" property="balance" />
		<result column="batch_num" property="batchNum" />
		<result column="tui_fee" property="tuiFee" />
		<result column="goods_fee" property="goodsFee" />
		<result column="sd_name" property="sdName" />
		<result column="sd_fee" property="sdFee" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="Order" >
		SELECT * FROM `order` WHERE id = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="Order">
		SELECT * FROM `order` ORDER BY id
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.collectfee.entity.Order" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" >
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
		</selectKey>
		-->
		INSERT INTO `order` (id,oid,order_no,branch_id,group_id,user_id,user_name,amount,type,pay,bank,account,u_account,pay_id,pay_time,remark,class_id,status,create_time,po_name,voicy_part,balance,batch_num,tui_fee,goods_fee,sd_name,sd_fee) VALUES(#{id},#{oid},#{orderNo},#{branchId},#{groupId},#{userId},#{userName},#{amount},#{type},#{pay},#{bank},#{account},#{uAccount},#{payId},#{payTime},#{remark},#{classId},#{status},#{createTime},#{poName},#{voicyPart},#{balance},#{batchNum},#{tuiFee},#{goodsFee},#{sdName},#{sdFee})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.collectfee.entity.Order">
		UPDATE `order`
		<set>
			<if test="tuiFee != null">
				tui_fee = #{tuiFee},
			</if>
			<if test="goodsFee != null">
				goods_fee = #{goodsFee},
			</if>
			<if test="sdName != null">
				sd_name = #{sdName},
			</if>
			<if test="sdFee != null">
				sd_fee = #{sdFee},
			</if>
			<if test="amount != null">
				amount = #{amount},
			</if>
			<if test="classId != null">
				class_id = #{classId},
			</if>
			<if test="pay != null">
				pay = #{pay},
			</if>
			<if test="uAccount != null">
				u_account = #{uAccount},
			</if>
			<if test="remark != null">
				remark = #{remark},
			</if>
			<if test="oid != null">
				oid = #{oid},
			</if>
			<if test="batchNum != null">
				batch_num = #{batchNum},
			</if>
			<if test="payTime != null">
				pay_time = #{payTime},
			</if>
			<if test="bank != null">
				bank = #{bank},
			</if>
			<if test="balance != null">
				balance = #{balance},
			</if>
			<if test="userId != null">
				user_id = #{userId},
			</if>
			<if test="voicyPart != null">
				voicy_part = #{voicyPart},
			</if>
			<if test="poName != null">
				po_name = #{poName},
			</if>
			<if test="account != null">
				account = #{account},
			</if>
			<if test="payId != null">
				pay_id = #{payId},
			</if>
			<if test="status != null">
				status = #{status},
			</if>
		</set>
		WHERE id = #{id}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM `order` WHERE id = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="Order" parameterType="map">
		SELECT * FROM `order` ORDER BY id <include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM `order`
	</select>

	<select id="getOrderByUserId" resultMap="Order">
		SELECT * FROM `order` WHERE user_id = #{userId}
	</select>
	
	<select id="getOrderByClassId" resultMap="Order">
		SELECT * FROM `order` WHERE status=2
		<if test="classId != null">
				and class_id = #{classId}
		</if>
		<if test="type != null">
				and type = #{type}
		</if>
	</select>

	<select id="getOrderByBatch" resultMap="Order">
		SELECT * FROM `order` WHERE batch_num = #{batchNum}
	</select>
	<select id="countOrder" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT user_id) num FROM `order` WHERE po_name = #{poName} AND voicy_part = #{voicePort} AND status >= 1
	</select>

	<select id="getOrderByOrderNo" resultMap="Order">
		SELECT * FROM `order` WHERE order_no = #{orderNo} AND status = 1
	</select>
    <select id="findOrderByStatus" resultType="com.ym.mec.collectfee.entity.Order">
		SELECT * FROM `order` WHERE user_id = #{userId} AND status = #{status}
	</select>
	<!-- 获取支付中和成功的订单数-->
	<select id="getPayOrderNums" resultType="java.lang.Integer">
		SELECT COUNT(*) FROM `order` WHERE status >= 1
	</select>
	<!-- 获取支付中的订单-->
	<select id="findPayingOrders" resultMap="Order">
		SELECT * FROM `order` WHERE status = 1
	</select>
</mapper>
