<?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.yifu.cloud.plus.v1.yifu.order.mapper.TOrderMapper">

    <resultMap id="BaseResultMap" type="com.yifu.cloud.plus.v1.yifu.order.entity.TOrder">
            <id property="id" column="ID" jdbcType="VARCHAR"/>
            <result property="orderNo" column="ORDER_NO" jdbcType="VARCHAR"/>
            <result property="customerName" column="CUSTOMER_NAME" jdbcType="VARCHAR"/>
            <result property="deptName" column="DEPT_NAME" jdbcType="VARCHAR"/>
            <result property="deptNo" column="DEPT_NO" jdbcType="VARCHAR"/>
            <result property="orderStatus" column="ORDER_STATUS" jdbcType="TINYINT"/>
            <result property="orderContent" column="ORDER_CONTENT" jdbcType="VARCHAR"/>
            <result property="createName" column="CREATE_NAME" jdbcType="VARCHAR"/>
            <result property="createTime" column="CREATE_TIME" jdbcType="TIMESTAMP"/>
            <result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/>
            <result property="updateName" column="UPDATE_NAME" jdbcType="VARCHAR"/>
			<result property="level" column="LEVEL" jdbcType="VARCHAR"/>
            <result property="updateTime" column="UPDATE_TIME" jdbcType="TIMESTAMP"/>
            <result property="deleteFlag" column="DELETE_FLAG" jdbcType="TINYINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        ID,ORDER_NO,CUSTOMER_NAME,LEVEL,
        DEPT_NAME,DEPT_NO,ORDER_STATUS,
        ORDER_CONTENT,CREATE_NAME,CREATE_TIME,
        UPDATE_BY,UPDATE_NAME,UPDATE_TIME,
        DELETE_FLAG
    </sql>

	<select id="getOrderPageList" resultType="com.yifu.cloud.plus.v1.yifu.order.vo.OrderListVO">
		select a.id as id,
		a.ORDER_NO as orderNo,
		a.CREATE_TIME as createTime,
		a.CREATE_NAME as createName,
		a.CUSTOMER_NAME as customerName,
		a.DEPT_NAME as deptName,
		a.DEPT_NO as deptNo,
		a.LEVEL as level,
		a.ORDER_STATUS as orderStatus
		from t_order a
		left join t_order_handler h on h.ORDER_NO = a.ORDER_NO
		where a.DELETE_FLAG = 0
		<if test="param.orderNo != null and param.orderNo.trim() != ''">
			and a.ORDER_NO = #{param.orderNo}
		</if>
		<if test="param.startDate != null and param.startDate.trim() != '' and param.endDate != null and param.endDate.trim() != ''">
			and a.CREATE_TIME <![CDATA[ >= ]]>  concat(#{param.startDate}, ' 00:00:00') and a.CREATE_TIME <![CDATA[ <= ]]> concat(#{param.endDate}, ' 23:59:59')
		</if>
		<if test="param.createName != null and param.createName.trim() != ''">
			and a.CREATE_NAME like concat('%',replace(replace(#{param.createName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.customerName != null and param.customerName.trim() != ''">
			and a.CUSTOMER_NAME = #{param.customerName}
		</if>
		<if test="param.deptName != null and param.deptName.trim() != ''">
			and a.DEPT_NAME = #{param.deptName}
		</if>
		<if test="param.deptNo != null and param.deptNo.trim() != ''">
			and a.DEPT_NO like concat('%',replace(replace(#{param.deptNo},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.orderStatus != null">
			and a.ORDER_STATUS = #{param.orderStatus}
		</if>
		<if test="param.level != null and param.level.trim() != ''">
			and a.LEVEL = #{param.level}
		</if>
		<if test="param.authSql != null and param.authSql.trim() != ''">
			${param.authSql}
		</if>
		GROUP BY a.ID
		ORDER BY a.CREATE_TIME DESC
	</select>

	<select id="getOrderListBySelect" resultType="com.yifu.cloud.plus.v1.yifu.order.vo.OrderListVO">
		select t.id as id,
		t.ORDER_NO as orderNo,
		t.CREATE_TIME as createTime,
		t.CREATE_NAME as createName,
		t.CUSTOMER_NAME as customerName,
		t.DEPT_NAME as deptName,
		t.DEPT_NO as deptNo,
		t.LEVEL as level,
		t.ORDER_STATUS as orderStatus
		from t_order t
		where t.DELETE_FLAG = 0 and t.id in
		<foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
		ORDER BY t.CREATE_TIME DESC
	</select>

	<select id="getOrderList" resultType="com.yifu.cloud.plus.v1.yifu.order.vo.OrderListVO">
		select a.id as id,
		a.ORDER_NO as orderNo,
		a.CREATE_TIME as createTime,
		a.CREATE_NAME as createName,
		a.CUSTOMER_NAME as customerName,
		a.DEPT_NAME as deptName,
		a.DEPT_NO as deptNo,
		a.LEVEL as level,
		a.ORDER_STATUS as orderStatus
		from t_order a
		left join t_order_handler h on h.ORDER_NO = a.ORDER_NO
		where a.DELETE_FLAG = 0
		<if test="param.orderNo != null and param.orderNo.trim() != ''">
			and a.ORDER_NO = #{param.orderNo}
		</if>
		<if test="param.startDate != null and param.startDate.trim() != '' and param.endDate != null and param.endDate.trim() != ''">
			and a.CREATE_TIME <![CDATA[ >= ]]>  concat(#{param.startDate}, ' 00:00:00') and a.CREATE_TIME <![CDATA[ <= ]]> concat(#{param.endDate}, ' 23:59:59')
		</if>
		<if test="param.createName != null and param.createName.trim() != ''">
			and a.CREATE_NAME like concat('%',replace(replace(#{param.createName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.customerName != null and param.customerName.trim() != ''">
			and a.CUSTOMER_NAME = #{param.customerName}
		</if>
		<if test="param.deptName != null and param.deptName.trim() != ''">
			and a.DEPT_NAME = #{param.deptName}
		</if>
		<if test="param.deptNo != null and param.deptNo.trim() != ''">
			and a.DEPT_NO like concat('%',replace(replace(#{param.deptNo},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.orderStatus != null">
			and a.ORDER_STATUS = #{param.orderStatus}
		</if>
		<if test="param.level != null and param.level.trim() != ''">
			and a.LEVEL = #{param.level}
		</if>
		<if test="param.authSql != null and param.authSql.trim() != ''">
			${param.authSql}
		</if>
		GROUP BY a.ID
		ORDER BY a.CREATE_TIME DESC
	</select>

	<select id="getOrderDetailById" resultType="com.yifu.cloud.plus.v1.yifu.order.vo.OrderDetailVO">
		select
			   t.CREATE_NAME as createName,
			   t.CREATE_TIME as createTime,
			   t.DEPT_NO as deptNo,
			   t.DEPT_NAME as deptName,
			   t.ORDER_NO as orderNo,
			   t.CUSTOMER_NAME as customerName,
			   t.ORDER_STATUS as orderStatus,
			   t.LEVEL as level,
		       t.ORDER_CONTENT as orderContent
		from t_order t
		where t.ID = #{id}
	</select>

	<select id="getOrderReplyPageList" resultType="com.yifu.cloud.plus.v1.yifu.order.vo.OrderReplyListVO">
		select
		    t.ID as id,
			t.ORDER_NO as orderNo,
			t.CREATE_NAME as createName,
			t.CREATE_TIME as createTime,
			t.REPLY_CONTENT as replyContent
		from t_order_reply t
		where t.DELETE_FLAG = 0
		and t.ORDER_NO = #{orderNo}
		ORDER BY t.CREATE_TIME DESC
	</select>

</mapper>