<?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.ghy.order.mapper.OrderDetailMapper">

    <resultMap id="OrderDetailResult" type="com.ghy.order.domain.OrderDetail">
        <id property="id"                       column="id"/>
        <result property="code"                 column="code"/>
        <result property="orderMasterId"        column="order_master_id"/>
        <result property="orderMasterCode"      column="order_master_code"/>
        <result property="customerId"           column="customer_id"/>
        <result property="orderType"            column="order_type"/>
        <result property="orderStatus"          column="order_status"/>
        <result property="workerId"             column="worker_id"/>
        <result property="revTime"              column="rev_time"/>
        <result property="expectTimeStart"      column="expect_time_start"/>
        <result property="expectTimeEnd"        column="expect_time_end"/>
        <result property="workBeginTime"        column="work_begin_time"/>
        <result property="workFinishTime"       column="work_finish_time"/>
        <result property="drawCashId"           column="draw_cash_id"/>
        <result property="drawCashTime"         column="draw_cash_time"/>
        <result property="arrivalTime"          column="arrival_time"/>
        <result property="drawCashStatus"       column="draw_cash_status"/>
        <result property="clockInLocation"      column="clock_in_location"/>
        <result property="shelveStatus"         column="shelve_status"/>
        <result property="ledgerAccountStatus"  column="ledger_account_status"/>
        <result property="createBy"             column="create_by"/>
        <result property="createTime"           column="create_time"/>
        <result property="updateBy"             column="update_by"/>
        <result property="updateTime"           column="update_time"/>
        <result property="remark"               column="remark"/>
        <result property="timeout"              column="timeout_"/>
        <result property="timeoutFineTimes"     column="timeout_fine_times"/>
    </resultMap>

    <sql id="selectOrderDetail">
        SELECT id,
               code,
               order_master_id,
               order_master_code,
               customer_id,
               order_type,
               order_status,
               worker_id,
               rev_time,
               expect_time_start,
               expect_time_end,
               work_begin_time,
               work_finish_time,
               draw_cash_id,
               draw_cash_time,
               arrival_time,
               draw_cash_status,
               clock_in_location,
               ledger_account_status,
               shelve_status,
               create_by,
               create_time,
               update_time,
               remark,
               timeout_,
               timeout_fine_times
        FROM order_detail
    </sql>

    <sql id="selectOrderDetailMoreInfo">
        SELECT od.id,
               od.code,
               od.order_master_id,
               od.order_master_code,
               od.customer_id,
               od.order_type,
               od.order_status,
               od.worker_id,
               od.rev_time,
               od.expect_time_start,
               od.expect_time_end,
               od.work_begin_time,
               od.work_finish_time,
               od.draw_cash_id,
               od.draw_cash_time,
               od.arrival_time,
               od.draw_cash_status,
               od.clock_in_location,
               od.ledger_account_status,
               od.shelve_status,
               od.create_by,
               od.create_time,
               od.update_time,
               od.remark,
               od.draw_cash_status,
               od.timeout_,
               od.timeout_fine_times
        FROM order_detail od
        LEFT JOIN order_master om ON om.id = od.order_master_id
        LEFT JOIN customer_address ca ON ca.customer_address_id = om.address_id
        LEFT JOIN goods g ON g.goods_id = om.goods_id
        left join worker w on od.worker_id = w.worker_id
    </sql>

    <select id="selectOrderDetailList" parameterType="com.ghy.order.domain.OrderDetail" resultMap="OrderDetailResult">
        <include refid="selectOrderDetailMoreInfo"/>
        <where>
            <if test="deptId != null and deptId != ''">
                AND w.dept_id = #{deptId}
            </if>
            <if test="code != null and code != ''">
                AND od.code LIKE concat('%', #{code}, '%')
            </if>
            <if test="customerId != null and customerId != 0">
                AND od.customer_id = #{customerId}
            </if>
            <if test="orderType != null">
                AND od.order_type = #{orderType}
            </if>
            <if test="orderStatus != null">
                AND od.order_status = #{orderStatus}
            </if>
            <if test="orderStatusList != null and orderStatusList.size() > 0">
                AND od.order_status in
                <foreach collection="orderStatusList" item="orderStatus" open="(" separator="," close=")">
                    #{orderStatus}
                </foreach>
            </if>
            <if test="orderMasterCode != null and orderMasterCode != ''">
                AND od.order_master_code = #{orderMasterCode}
            </if>
            <if test="workerId != null and workerId != 0">
                AND od.worker_id = #{workerId}
            </if>
            <if test="goodsCategoryId != null">
                AND g.dept_goods_category_id = #{goodsCategoryId}
            </if>
            <if test="goodsName != null and goodsName != ''">
                AND g.goods_name like concat('%', #{goodsName}, '%')
            </if>
            <if test="countryId != null">
                AND ca.country_id = #{countryId}
            </if>
            <if test="expectTimeStart != null">
                AND od.expect_time_start &gt;= #{expectTimeStart}
            </if>
            <if test="expectTimeEnd != null">
                AND od.expect_time_end &lt;= #{expectTimeEnd}
            </if>
            <if test="orderDetailIds != null">
                AND od.id in ( ${orderDetailIds} )
            </if>
            <if test="shelveStatus != null">
                AND od.shelve_status = ${shelveStatus}
            </if>
            <if test="shelveStatus == null">
                AND od.shelve_status = 0
            </if>
            <if test="drawCashStatus != null">
                AND od.draw_cash_status = ${drawCashStatus}
            </if>
            <if test="drawCashStatusList != null and drawCashStatusList.size() > 0">
                AND od.draw_cash_status in
                <foreach collection="drawCashStatusList" item="drawCashStatus" open="(" separator="," close=")">
                    #{drawCashStatus}
                </foreach>
            </if>
            <if test="workFinishTimeExisted">
                AND od.work_finish_time is not null
            </if>
            <if test="timeout != null">
                AND od.timeout_ = #{timeout}
            </if>
        </where>
        order by od.create_time
        <trim suffixOverrides=",">
            <choose>
                <when test="params.createTimeSort != null and params.createTimeSort != ''">
                    ${params.createTimeSort}
                </when>
                <otherwise>
                    desc
                </otherwise>
            </choose>
        </trim>
    </select>

    <select id="countOrderDetailList" parameterType="com.ghy.order.domain.OrderDetail" resultType="Long">
        SELECT COUNT(*) FROM order_detail od
        <where>
            <if test="code != null and code != ''">
                AND od.code LIKE concat('%', #{code}, '%')
            </if>
            <if test="customerId != null and customerId != 0">
                AND od.customer_id = #{customerId}
            </if>
            <if test="orderType != null">
                AND od.order_type = #{orderType}
            </if>
            <if test="orderStatus != null">
                AND od.order_status = #{orderStatus}
            </if>
            <if test="orderStatusList != null and orderStatusList.size() > 0">
                AND od.order_status in
                <foreach collection="orderStatusList" item="orderStatus" open="(" separator="," close=")">
                    #{orderStatus}
                </foreach>
            </if>
            <if test="orderMasterCode != null and orderMasterCode != 0">
                AND od.order_detail_code = #{orderMasterCode}
            </if>
            <if test="workerId != null and workerId != 0">
                AND od.worker_id = #{workerId}
            </if>
            <if test="goodsCategoryId != null">
                AND g.dept_goods_category_id = #{goodsCategoryId}
            </if>
            <if test="goodsName != null and goodsName != ''">
                AND g.goods_name like concat('%', #{goodsName}, '%')
            </if>
            <if test="countryId != null">
                AND ca.country_id = #{countryId}
            </if>
            <if test="expectTimeStart != null">
                AND od.expect_time_start &gt;= #{expectTimeStart}
            </if>
            <if test="expectTimeEnd != null">
                AND od.expect_time_end &lt;= #{expectTimeEnd}
            </if>
            <if test="shelveStatus != null">
                AND od.shelve_status = ${shelveStatus}
            </if>
            <if test="workFinishTimeExisted">
                AND od.work_finish_time is not null
            </if>
            <if test="drawCashStatusList != null and drawCashStatusList.size() > 0">
                AND od.draw_cash_status in
                <foreach collection="drawCashStatusList" item="drawCashStatus" open="(" separator="," close=")">
                    #{drawCashStatus}
                </foreach>
            </if>
        </where>
        order by od.create_time
        <trim suffixOverrides=",">
            <choose>
                <when test="params.createTimeSort != null and params.createTimeSort != ''">
                    ${params.createTimeSort}
                </when>
                <otherwise>
                    desc
                </otherwise>
            </choose>
        </trim>
    </select>

    <select id="selectById" parameterType="long" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/> WHERE id = #{orderDetailId}
    </select>


    <select id="selectByOrderMasterId" parameterType="long" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/> WHERE order_master_id = #{orderMasterId}
    </select>

    <delete id="deleteOrderDetailByIds" parameterType="Long">
        DELETE FROM order_detail WHERE id IN
        <foreach collection="array" item="orderDetailId" open="(" separator="," close=")">
            #{orderDetailId}
        </foreach>
    </delete>

    <delete id="deleteByMaster">
        DELETE FROM order_detail WHERE order_master_id = #{masterId}
    </delete>

    <update id="updateOrderDetail" parameterType="com.ghy.order.domain.OrderDetail">
        UPDATE order_detail
        <set>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="orderMasterId != null and orderMasterId != 0">order_master_id = #{orderMasterId},</if>
            <if test="orderMasterCode != null and orderMasterCode != ''">order_master_code = #{orderMasterCode},</if>
            <if test="customerId != null and customerId != 0">customer_id = #{customerId},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="workerId != null and workerId != 0">worker_id = #{workerId},</if>
            <if test="revTime != null">rev_time = #{revTime},</if>
            <if test="expectTimeStart != null">expect_time_start = #{expectTimeStart},</if>
            <if test="expectTimeEnd != null">expect_time_end = #{expectTimeEnd},</if>
            <if test="useTimeNotRange">expect_time_end = null,</if>
            <if test="workBeginTime != null">work_begin_time = #{workBeginTime},</if>
            <if test="workFinishTime != null">work_finish_time = #{workFinishTime},</if>
            <if test="drawCashId != null">draw_cash_id = #{drawCashId},</if>
            <if test="drawCashTime != null">draw_cash_time = #{drawCashTime},</if>
            <if test="arrivalTime != null">arrival_time = #{arrivalTime},</if>
            <if test="drawCashStatus != null">draw_cash_status = #{drawCashStatus},</if>
            <if test="clockInLocation != null and clockInLocation != ''">clock_in_location = #{clockInLocation},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="ledgerAccountStatus != null">ledger_account_status = #{ledgerAccountStatus},</if>
            update_time = SYSDATE()
        </set>
        WHERE id = #{id}
    </update>

    <update id="updateStatus">
        UPDATE order_detail
        SET order_status = #{orderStatus},
            update_time = SYSDATE()
        WHERE id = #{id}
    </update>

    <update id="updateTimeout">
        UPDATE order_detail
        SET timeout_ = #{timeout},
            timeout_fine_times = #{timeoutFineTimes}
        WHERE id = #{id}
    </update>

    <update id="updateByOrderMasterId">
        UPDATE order_detail
        <set>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            update_time = SYSDATE()
        </set>
        WHERE order_master_id = #{orderMasterId}
    </update>

    <select id="statusCount" resultType="com.ghy.order.domain.OrderStatusCount">
        SELECT
            `order_status` AS `orderStatus`,
            COUNT( * ) AS `count`
        FROM
            `order_detail`
        GROUP BY
            order_status
    </select>

    <insert id="insertOrderDetail" parameterType="com.ghy.order.domain.OrderDetail" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_detail(
        <if test="code != null and code != ''">code,</if>
        <if test="orderMasterId != null and orderMasterId != 0">order_master_id,</if>
        <if test="orderMasterCode != null and orderMasterCode != ''">order_master_code,</if>
        <if test="customerId != null and customerId != 0">customer_id,</if>
        <if test="orderType != null">order_type,</if>
        <if test="orderStatus != null">order_status,</if>
        <if test="workerId != null and workerId != 0">worker_id,</if>
        <if test="revTime != null">rev_time,</if>
        <if test="remark != null">remark,</if>
        <if test="expectTimeStart != null">expect_time_start,</if>
        <if test="expectTimeEnd != null">expect_time_end,</if>
        <if test="workBeginTime != null">work_begin_time,</if>
        <if test="workFinishTime != null">work_finish_time,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )VALUES(
        <if test="code != null and code != ''">#{code},</if>
        <if test="orderMasterId != null and orderMasterId != 0">#{orderMasterId},</if>
        <if test="orderMasterCode != null and orderMasterCode != ''">#{orderMasterCode},</if>
        <if test="customerId != null and customerId != 0">#{customerId},</if>
        <if test="orderType != null">#{orderType},</if>
        <if test="orderStatus != null">#{orderStatus},</if>
        <if test="workerId != null and workerId != 0">#{workerId},</if>
        <if test="revTime != null">#{revTime},</if>
        <if test="remark != null">#{remark},</if>
        <if test="expectTimeStart != null">#{expectTimeStart},</if>
        <if test="expectTimeEnd != null">#{expectTimeEnd},</if>
        <if test="workBeginTime != null">#{workBeginTime},</if>
        <if test="workFinishTime != null">#{workFinishTime},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        SYSDATE()
        )
    </insert>

    <select id="checkOrderDetailCodeUnique" parameterType="String" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/>
        WHERE `code` =#{orderDetailCode} LIMIT 1
    </select>

    <select id="getByOrderIdList" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail" />
        <where>
            and order_master_id in
            <foreach item="item" index="orderIdList" collection="orderIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

    <select id="selectByStatus" resultMap="OrderDetailResult">
        <include refid="selectOrderDetail"/>
        WHERE `order_status` IN
        <foreach collection="list" item="orderStatus" open="(" separator="," close=")">
            #{orderStatus}
        </foreach>
    </select>

    <update id="updateDrawCashStatus">
        UPDATE order_detail SET
        <if test="drawCashId != null">draw_cash_id = #{drawCashId},</if>
        <if test="drawCashTime != null">draw_cash_time = #{drawCashTime},</if>
        <if test="arrivalTime != null">arrival_time = #{arrivalTime},</if>
        <if test="drawCashStatus != null">draw_cash_status = #{drawCashStatus},</if>
        update_time = NOW()
        WHERE draw_cash_id = #{drawCashId}
    </update>
</mapper>
