<?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.soss.system.mapper.OrderMapper">
    
    <resultMap type="Order" id="OrderResult">
        <result property="id"    column="id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="orderNum"    column="order_num"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="userPhone"    column="user_phone"    />
        <result property="payTime"    column="pay_time"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="amount"    column="amount"    />
        <result property="goodsNum"    column="goods_num"    />
        <result property="pickCode"    column="pick_code"    />
        <result property="shopId"    column="shop_id"    />
        <result property="machineId"    column="machine_id"    />
        <result property="source"    column="source"    />
        <result property="state"    column="state"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
    </resultMap>

    <sql id="selectOrderVo">
        select id, order_no, order_num, user_id, user_name, user_phone, pay_time, finish_time, amount, goods_num, pick_code, shop_id, machine_id, source, state, created_at, updated_at from `order`
    </sql>

    <select id="selectOrderList" parameterType="Order" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        <where>
            <if test="orderNo != null  and orderNo != ''"> and order_no = #{orderNo}</if>
            <if test="orderNum != null  and orderNum != ''"> and order_num = #{orderNum}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="userPhone != null  and userPhone != ''"> and user_phone = #{userPhone}</if>
            <if test="payTime != null "> and pay_time = #{payTime}</if>
            <if test="finishTime != null "> and finish_time = #{finishTime}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="goodsNum != null  and goodsNum != ''"> and goods_num = #{goodsNum}</if>
            <if test="pickCode != null  and pickCode != ''"> and pick_code = #{pickCode}</if>
            <if test="shopId != null  and shopId != ''"> and shop_id = #{shopId}</if>
            <if test="machineId != null  and machineId != ''"> and machine_id = #{machineId}</if>
            <if test="source != null  and source != ''"> and source = #{source}</if>
            <if test="state != null  and state != ''"> and state = #{state}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="updatedAt != null "> and updated_at = #{updatedAt}</if>

        </where>
    </select>
    
    <select id="selectOrderById" parameterType="String" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertOrder" parameterType="Order" useGeneratedKeys="true" keyProperty="id">
        insert into `order`
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null and orderNo != ''">order_no,</if>
            <if test="orderNum != null and orderNum != ''">order_num,</if>
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="userPhone != null">user_phone,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="amount != null">amount,</if>
            <if test="goodsNum != null and goodsNum != ''">goods_num,</if>
            <if test="pickCode != null and pickCode != ''">pick_code,</if>
            <if test="shopId != null and shopId != ''">shop_id,</if>
            <if test="machineId != null and machineId != ''">machine_id,</if>
            <if test="source != null and source != ''">source,</if>
            <if test="state != null and state != ''">state,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null and orderNo != ''">#{orderNo},</if>
            <if test="orderNum != null and orderNum != ''">#{orderNum},</if>
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="userPhone != null">#{userPhone},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="amount != null">#{amount},</if>
            <if test="goodsNum != null and goodsNum != ''">#{goodsNum},</if>
            <if test="pickCode != null and pickCode != ''">#{pickCode},</if>
            <if test="shopId != null and shopId != ''">#{shopId},</if>
            <if test="machineId != null and machineId != ''">#{machineId},</if>
            <if test="source != null and source != ''">#{source},</if>
            <if test="state != null and state != ''">#{state},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
         </trim>
    </insert>

    <update id="updateOrder" parameterType="Order">
        update `order`
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null and orderNo != ''">order_no = #{orderNo},</if>
            <if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
            <if test="userId != null and userId != ''">user_id = #{userId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="userPhone != null">user_phone = #{userPhone},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="goodsNum != null and goodsNum != ''">goods_num = #{goodsNum},</if>
            <if test="pickCode != null and pickCode != ''">pick_code = #{pickCode},</if>
            <if test="shopId != null and shopId != ''">shop_id = #{shopId},</if>
            <if test="machineId != null and machineId != ''">machine_id = #{machineId},</if>
            <if test="source != null and source != ''">source = #{source},</if>
            <if test="state != null and state != ''">state = #{state},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderById" parameterType="String">
        delete from order where id = #{id}
    </delete>

    <delete id="deleteOrderByIds" parameterType="String">
        delete from order where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectSalesVolume" resultType="integer">

        select count(*) from (
        select
        DISTINCT o.id
        from
        `order` o,
        order_refund or2,
        order_detail od
        where
        o.id = or2.order_id
        and or2.state != '2'
        and od.order_id = o.id
        <if test="goodsId !=null">
            and od.goods_id =#{goodsId}
        </if>
        <if test="shopId !=null">
            and o.shop_id=#{shopId}
        </if>
        ) a
    </select>
    <select id="selectSalesAmount" resultType="java.math.BigDecimal">
        select SUM(o.amount)   from `order` o,order_refund or2
        where o.id = or2.order_id
        and or2.state !='2'
        and shop_id =#{shopId}
    </select>
    <select id="selectByUserId" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        where
        user_id =#{user_id} order by created_at desc

    </select>
    <select id="selectOrderByQuery" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        <where>
            <if test="orderNo!=null ">
                and order_no =#{orderNo}
            </if>
            <if test="orderNum!=null ">
                and order_num =#{orderNum}
            </if>
            <if test="userName!=null">
                and user_name like concat('%', #{userName}, '%')
            </if>
            <if test="shopId!=null ">
                and shop_id =#{shopId}
            </if>
            <if test="state!=null ">
                and shop_id =#{shopId}
            </if>
            <if test="createAtStart!=null ">

                <![CDATA[ and create_at >= to_date(#{createAtStart,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss')]]>
            </if>
            <if test="createAtEnd!=null ">
                <![CDATA[ and create_at <= to_date(#{createAtEnd,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss')]]>
            </if>
            <if test="payAtStart!=null ">

                <![CDATA[ and pay_time >= to_date(#{payAtStart,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss')]]>
            </if>
            <if test="payAtEnd!=null ">

                <![CDATA[ and pay_time <= to_date(#{payAtEnd,jdbcType=DATE},'yyyy-MM-dd hh24:mi:ss')]]>
            </if>
        </where>
    </select>
    <select id="selectOrder" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        <where>
            state in
            <foreach item="item" index="index" collection="status"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
            and machine_id = #{machineId}
        </where>
    </select>
</mapper>