<?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.ruoyi.system.mapper.SysOrderMapper">
    
    <resultMap type="SysOrder" id="SysOrderResult">
        <result property="orderId"    column="order_id"    />
        <result property="userId"    column="user_id"    />
        <result property="sourceId"    column="source_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="deleteFlag"    column="delete_flag"    />
        <result property="orderInfoId"    column="order_info_id"    />
        <result property="money"    column="money"    />
        <result property="way"    column="way"    />
        <result property="detail"    column="detail"    />
        <result property="status"    column="status"    />
        <result property="refund" column="refund"/>
    </resultMap>

    <resultMap id="OrderListDtoResult" type="OrderListDto">
        <result property="orderId" column="order_id"/>
        <result property="createTime" column="create_time"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="orderInfoId" column="order_info_id"/>
        <result property="money" column="money"/>
        <result property="refund" column="refund"/>
        <result property="way"    column="way"    />
        <result property="orderDetail" column="detail"/>
        <result property="status" column="status"/>
        <result property="userId" column="user_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="avatar" column="avatar" />

    </resultMap>

    <resultMap id="OrderDetailDtoResult" type="orderDetailDto">
        <result property="orderId" column="order_id"/>
        <result property="createTime" column="create_time"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="orderInfoId" column="order_info_id"/>
        <result property="money" column="money"/>
        <result property="refund" column="refund"/>
        <result property="way"    column="way"    />
        <result property="orderDetail" column="detail"/>
        <result property="status" column="status"/>
        <result property="userId" column="user_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="sourceId" column="source_id"/>
        <result property="workId" column="work_id"/>
        <result property="city" column="city"/>
        <result property="scenic" column="scenic"/>
        <result property="part" column="part" />
        <result property="workDetail" column="detail"/>
        <result property="workName" column="work_name"/>
        <result property="workAddress" column="work_address"/>
        <result property="coordinate" column="coordinate" />
        <result property="refundTime" column="refund_time"/>
<!--        <result property="workS"-->
        <result property="id" column="id"/>

    </resultMap>

    <resultMap id="SysOrderPayBackDtoResult" type="SysOrderPayBackDto">
        <result property="orderId" column="order_id"/>
        <result property="userId" column="user_id"/>
        <result property="createTime" column="create_time"/>
        <result property="endTime" column="end_time"/>
        <result property="orderInfoId" column="order_info_id"/>
        <result property="money" column="money"/>
        <result property="part" column="part"/>
        <result property="c_x" column="c_x"/>
        <result property="c_y" column="c_y"/>
    </resultMap>

    <sql id="selectSysOrderVo">
        select order_id, user_id, source_id, create_time, start_time, end_time, delete_flag from sys_order
    </sql>

    <select id="selectSysOrderList" parameterType="SysOrder" resultMap="SysOrderResult">
        <include refid="selectSysOrderVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="sourceId != null "> and source_id = #{sourceId}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="deleteFlag != null  and deleteFlag != ''"> and delete_flag = #{deleteFlag}</if>
            <if test="orderInfoId != null "> and order_info_id = #{orderInfoId}</if>
            <if test="money != null "> and money = #{money}</if>
            <if test="way != null "> and way = #{way}</if>
            <if test="detail != null  and detail != ''"> and detail like concat('%', #{detail}, '%')</if>
             <if test="status != null "> and status = #{status}</if>
              <if test="refund != null "> and refund = #{refund}</if>
        </where>
    </select>
    
    <select id="selectSysOrderByOrderId" parameterType="Long" resultMap="SysOrderResult">
        <include refid="selectSysOrderVo"/>
        where order_id = #{orderId}
    </select>
    <select id="isTimeLegal" resultType="java.lang.Integer">
<!--        select count(*) from sys_order where (start_time < #{startTime} and end_time > #{endTime}) and source_id = #{sourceId}-->
        select count(*) from sys_order where  source_id = #{sourceId} and #{endTime} >= sys_order.start_time and sys_order.end_time >= #{startTime}
    </select>
    <select id="getOrderListDto" resultMap="OrderListDtoResult">
           select o.order_id, o.user_id, o.source_id, o.create_time, o.start_time, o.end_time, o.order_info_id, o.money, o.way, o.detail, o.status , o.refund, u.nick_name, u.phonenumber, u.avatar
           from  sys_order o
           left join  sys_user u
           on o.user_id = u.user_id
          <where>
            <if test="status != null "> and o.status = #{status}</if>
              <if test="userId != null "> and o.user_id = #{userId}</if>

          </where>
            order by o.create_time desc

    </select>
    <select id="getOrderDetailDto" resultMap="OrderDetailDtoResult">
        select o.order_id, o.user_id, o.source_id, o.create_time, o.start_time, o.end_time, o.order_info_id, o.money, o.way, o.detail order_detail, o.status, o.refund, u.nick_name, u.phonenumber, w.work_name, w.detail, s.city, s.scenic, s.part, s.coordinate, w.work_address, w.user_id, i.id, w.work_id
        from  sys_order o
        left join  sys_user u on o.user_id = u.user_id
        left join order_info i on o.order_info_id = i.order_info_id
        left join sys_source s on i.source_id = s.source_id
        left join user_work w on i.work_id = w.work_id
        where o.order_id = #{orderId} and i.id = #{id}
    </select>
    <select id="getMoneybyDateAndStatus" resultType="java.lang.Long">
        select sum(money)
        from sys_order
        where status != '1' and create_time = #{date} and delete_flag = '0'
    </select>
    <select id="getRefund" resultType="java.lang.Long">
        select sum(refund)
        from sys_order
        where status = '1' and create_time = #{date} and delete_flag = '0'
    </select>
    <select id="getSysOrderPayBackDto" resultMap="SysOrderPayBackDtoResult">
        select sys_order.order_id, sys_order.user_id ,create_time, sys_order.end_time, order_info_id, money, part, c_x, c_y
        from sys_order
        left join sys_source
        on sys_order.source_id = sys_source.source_id
        where sys_order.order_id = #{orderId}
    </select>



    <insert id="insertSysOrder" parameterType="SysOrder" useGeneratedKeys="true" keyProperty="orderId">
        insert into sys_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="sourceId != null">source_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="deleteFlag != null">delete_flag,</if>
            <if test="orderInfoId != null">order_info_id,</if>
            <if test="money != null">money,</if>
            <if test="way != null">way,</if>
            <if test="detail != null">detail,</if>
            <if test="status != null">status,</if>
            <if test="refund != null">refund,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="sourceId != null">#{sourceId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="deleteFlag != null">#{deleteFlag},</if>
            <if test="orderInfoId != null">#{orderInfoId},</if>
            <if test="money != null">#{money},</if>
            <if test="way != null">#{way},</if>
            <if test="detail != null">#{detail},</if>
            <if test="status != null">#{status},</if>
            <if test="refund != null">#{refund},</if>
         </trim>
    </insert>

    <update id="updateSysOrder" parameterType="SysOrder">
        update sys_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="sourceId != null">source_id = #{sourceId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
            <if test="orderInfoId != null">order_info_id = #{orderInfoId},</if>
            <if test="money != null">money = #{money},</if>
            <if test="way != null">way = #{way},</if>
            <if test="detail != null">detail = #{detail},</if>
            <if test="status != null">status = #{status},</if>
            <if test="refund != null">refund = #{refund},</if>
        </trim>
        where order_id = #{orderId}
    </update>

    <delete id="deleteSysOrderByOrderId" parameterType="Long">
        delete from sys_order where order_id = #{orderId}
    </delete>

    <delete id="deleteSysOrderByOrderIds" parameterType="String">
        delete from sys_order where order_id in 
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>
</mapper>