<?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.OrderMasterMapper">

    <resultMap id="OrderMasterResult" type="com.ghy.order.domain.OrderMaster">
        <id property="id"                    column="id"/>
        <result property="deptId"            column="dept_id"/>
        <result property="code"              column="code"/>
        <result property="customerId"        column="customer_id"/>
        <result property="addressId"         column="address_id"/>
        <result property="orderType"         column="order_type"/>
        <result property="orderMode"         column="order_mode"/>
        <result property="payMode"         column="pay_mode"/>
        <result property="consultMode"  column="consult_mode"/>
        <result property="insuranceId"  column="insurance_id"/>
        <result property="isCharge"         column="is_charge"/>
        <result property="isContact"         column="is_contact"/>
        <result property="orderStatus"       column="order_status"/>
        <result property="payType"           column="pay_type"/>
        <result property="payStatus"         column="pay_status"/>
        <result property="workerId"          column="worker_id"/>
        <result property="payTime"           column="pay_time"/>
        <result property="revTime"           column="rev_time"/>
        <result property="expectTimeStart"   column="expect_time_start"/>
        <result property="expectTimeEnd"     column="expect_time_end"/>
        <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="goodsId"           column="goods_id"/>
        <result property="goodsBrand"           column="goods_brand"/>
        <result property="goodsSpecification"           column="goods_specification"/>
        <result property="allSelfAssigned"   column="all_self_assigned"/>
        <result property="hasDispatchedAll"  column="has_dispatched_all"/>
        <result property="timeout"           column="timeout_"/>
        <result property="timeoutFineTimes"  column="timeout_fine_times"/>
        <result property="isCall" column="is_call" />
        <result property="serverMoney" column="server_money" />
    </resultMap>

    <sql id="selectOrderMaster">
        SELECT id,
               dept_id,
               code,
               customer_id,
               address_id,
               order_type,
               order_mode,
               pay_mode,
               consult_mode,
               insurance_id,
               is_charge,
               is_contact,
               order_status,
               pay_type,
               pay_status,
               worker_id,
               pay_time,
               rev_time,
               expect_time_start,
               expect_time_end,
               create_by,
               create_time,
               update_time,
               remark,
               all_self_assigned,
               goods_id,
               timeout_,
               timeout_fine_times,
               is_call,
               order_mode,
               goods_brand,
               goods_specification
        FROM order_master
    </sql>
    <sql id="selectOrderMasterMoreInfo">
        SELECT om.id,
               om.dept_id,
               om.code,
               om.customer_id,
               om.address_id,
               om.order_type,
               om.order_mode,
               om.pay_mode,
               om.is_charge,
               om.is_contact,
               om.order_status,
               om.pay_type,
               om.pay_status,
               om.worker_id,
               om.pay_time,
               om.rev_time,
               om.expect_time_start,
               om.expect_time_end,
               om.create_by,
               om.create_time,
               om.update_time,
               om.remark,
               om.all_self_assigned,
               om.goods_id,
               om.timeout_,
               om.timeout_fine_times,
               om.is_call,
               fm.server_money,
               om.goods_brand,
               om.goods_specification,
               om.consult_mode,
               om.insurance_id
        FROM order_master om
        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 financial_master fm ON om.id = fm.order_master_id
        left join worker w on om.worker_id = w.worker_id 
    </sql>

    <select id="selectOrderMasterList" parameterType="com.ghy.order.domain.OrderMaster" resultMap="OrderMasterResult">
        <include refid="selectOrderMasterMoreInfo"></include>
        <where>
            <if test="keywords != null and keywords != ''">
                AND
                (
                om.code LIKE concat('%', #{keywords}, '%')
                    or ca.name LIKE concat('%', #{keywords}, '%')
                    or ca.phone LIKE concat('%', #{keywords}, '%')
                    or ca.address like  concat('%', #{keywords}, '%')
                    or g.goods_name like concat('%', #{keywords}, '%')
                )
            </if>
            <if test="workerName != null and workerName != ''">
                AND w.name LIKE concat('%', #{workerName}, '%')
            </if>
            <if test="workerPhone != null and workerPhone != ''">
                AND w.phone LIKE concat('%', #{workerPhone}, '%')
            </if>
            <if test="province != null and province != ''">
                AND ca.province_id = #{province}
            </if>
            <if test="city != null and city != ''">
                AND ca.city_id = #{city}
            </if>
            <if test="district != null and district != ''">
                AND ca.country_id = #{district}
            </if>
            <if test="isMonitoredOrder">
                AND (om.all_self_assigned = 0 or om.all_self_assigned is null) AND om.order_status in (0,1,2,3,4) and om.worker_id is not null
            </if>
            <if test="allSelfAssigned != null">
                AND all_self_assigned = #{allSelfAssigned}
            </if>
            <if test="deptId != null and deptId != 0">
                AND om.dept_id = #{deptId}
            </if>
            <if test="code != null and code != ''">
                AND om.code LIKE concat('%', #{code}, '%')
            </if>
            <if test="customerId != null and customerId != 0">
                AND om.customer_id = #{customerId}
            </if>
            <if test="customerIds != null and customerIds.size > 0">
                AND om.customer_id in
                <foreach item="customerId" collection="customerIds" open="(" separator="," close=")">
                    #{customerId}
                </foreach>
            </if>
            <if test="orderType != null">
                AND om.order_type = #{orderType}
            </if>
            <if test="orderStatus != null">
                AND om.order_status = #{orderStatus}
            </if>
            <if test="payType != null">
                AND om.pay_type = #{payType}
            </if>
            <if test="payStatus != null">
                AND om.pay_status = #{payStatus}
            </if>
            <if test="payStatusList != null">
                AND om.pay_status in ( #{payStatusList} )
            </if>
            <if test="workerId != null and workerId > 0">
                AND om.worker_id = #{workerId}
            </if>
            <if test="workerId == -1">
                AND om.worker_id IS NULL
                AND (om.pay_mode = '01' OR (om.pay_mode = '02' and om.pay_status = '1'))
            </if>
            <if test="workerId == -2">
                and om.worker_id IS not NULL
            </if>
            <if test="payMode != null">
                AND om.pay_mode = #{payMode}
            </if>
            <if test="sysPayStatus != null">
                AND fm.pay_status = #{sysPayStatus}
            </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="exceptOrderStatus != null">
                AND om.order_status != #{exceptOrderStatus}
            </if>
            <if test="createTimeStart != null">
                AND om.create_time &gt;= #{createTimeStart}
            </if>
            <if test="createTimeEnd != null">
                AND om.create_time &lt;= #{createTimeEnd}
            </if>
            <if test="updateTimeStart != null">
                AND om.update_time &gt;= #{updateTimeStart}
            </if>
            <if test="updateTimeEnd != null">
                AND om.update_time &lt;= #{updateTimeEnd}
            </if>
            <if test="orderMasterIds != null and orderMasterIds != ''">
                AND om.id in (${orderMasterIds})
            </if>
            <if test="orderMasterIdList != null and orderMasterIdList.size > 0">
                AND om.id in
                <foreach collection="orderMasterIdList" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="orderStatuses != null and orderStatuses != ''">
                AND om.order_status in (${orderStatuses})
            </if>
            <if test="hasDispatchedAll != null">
                AND om.has_dispatched_all = #{hasDispatchedAll}
            </if>
            <if test="exceptOrderMasterIds != null and exceptOrderMasterIds.size > 0">
                AND om.id not in
                <foreach collection="exceptOrderMasterIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="timeout != null">
                AND om.timeout_ = #{timeout}
            </if>
            <if test="isCall != null">
                AND om.is_call = #{isCall}
            </if>
            <if test="expectTimeStart != null">
                AND om.expect_time_start &gt;= #{expectTimeStart}
            </if>
            <if test="expectTimeEnd != null">
                AND om.expect_time_end &lt;= #{expectTimeEnd}
            </if>
            <if test="createBy != null and createBy != ''">
                AND om.create_by = #{createBy}
            </if>
            <if test="addressIds != null and addressIds.size > 0">
                AND om.address_id IN
                <foreach collection="addressIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by om.create_time
        <trim suffixOverrides=",">
            <choose>
                <when test="params.createTimeSort != null and params.createTimeSort != ''">
                    ${params.createTimeSort}
                </when>
                <otherwise>
                    desc
                </otherwise>
            </choose>
        </trim>
    </select>

    <select id="countOrderMasterList" parameterType="com.ghy.order.domain.OrderMaster" resultType="Long">
        SELECT COUNT(*)
        FROM order_master om
        LEFT JOIN financial_master fm ON om.id = fm.order_master_id
        <where>
            <if test="isMonitoredOrder">
                AND (om.all_self_assigned = 0 or om.all_self_assigned is null) AND om.order_status in (1,2,3,4)
            </if>
            <if test="allSelfAssigned != null">
                AND all_self_assigned = #{allSelfAssigned}
            </if>
            <if test="deptId != null and deptId != 0">
                AND om.dept_id = #{deptId}
            </if>
            <if test="code != null and code != ''">
                AND om.code LIKE concat('%', #{code}, '%')
            </if>
            <if test="customerId != null and customerId != 0">
                AND om.customer_id = #{customerId}
            </if>
            <if test="orderType != null">
                AND om.order_type = #{orderType}
            </if>
            <if test="orderStatus != null">
                AND om.order_status = #{orderStatus}
            </if>
            <if test="orderStatuses != null and orderStatuses != ''">
                AND om.order_status in (${orderStatuses})
            </if>
            <if test="payType != null">
                AND om.pay_type = #{payType}
            </if>
            <if test="payStatus != null">
                AND om.pay_status = #{payStatus}
            </if>
            <if test="payStatusList != null">
                AND om.pay_status in ( #{payStatusList} )
            </if>
            <if test="workerId != null and workerId > 0">
                AND om.worker_id = #{workerId}
            </if>
            <if test="workerId == -1">
                AND om.worker_id IS NULL
                AND (om.pay_mode = '01' OR (om.pay_mode = '02' and om.pay_status = '1'))
            </if>
            <if test="workerId == -2">
                AND om.worker_id IS not NULL
            </if>
            <if test="payMode != null">
                AND om.pay_mode = #{payMode}
            </if>
            <if test="sysPayStatus != null">
                AND fm.pay_status = #{sysPayStatus}
            </if>
            <if test="orderMode != null and orderMode != ''">
                AND om.order_mode = #{orderMode}
            </if>
            <if test="exceptOrderStatus != null">
                AND om.order_status != #{exceptOrderStatus}
            </if>
            <if test="hasDispatchedAll != null">
                AND om.has_dispatched_all = #{hasDispatchedAll}
            </if>
            <if test="orderMasterIds != null and orderMasterIds != ''">
                AND om.id in (${orderMasterIds})
            </if>
            <if test="isCall != null">
                AND om.is_call = #{isCall}
            </if>
            <if test="expectTimeStart != null">
                AND om.expect_time_start &gt;= #{expectTimeStart}
            </if>
            <if test="expectTimeEnd != null">
                AND om.expect_time_end &lt;= #{expectTimeEnd}
            </if>
            <if test="createBy != null and createBy != ''">
                AND om.create_by = #{createBy}
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        <where>
            <if test="orderMasterId != null and orderMasterId != 0">
                AND id = #{orderMasterId}
            </if>
        </where>
    </select>

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

    <update id="updateOrderMaster" parameterType="com.ghy.order.domain.OrderMaster">
        UPDATE order_master
        <set>
            <if test="code != null and code != ''">code = #{code},</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="payType != null">pay_type = #{payType},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="workerId != null and workerId != 0">worker_id = #{workerId},</if>
            <if test="workerId == -1">
                worker_id = NULL,
            </if>
            <if test="payTime != null">pay_time = #{payTime},</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="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="allSelfAssigned != null">all_self_assigned = #{allSelfAssigned},</if>
            <if test="resetAllSelfAssigned">all_self_assigned = null,</if>
            <if test="hasDispatchedAll != null">has_dispatched_all = #{hasDispatchedAll},</if>
            <if test="isCall != null">is_call = #{isCall},</if>
            <if test="isCharge != null">is_charge = #{isCharge},</if>
            <if test="isContact != null">is_contact = #{isContact},</if>
            <if test="timeout != null">timeout_ = #{timeout},</if>
            <if test="insuranceId != null">insurance_id = #{insuranceId},</if>
            update_time = SYSDATE()
        </set>
        WHERE id = #{id}
    </update>

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

    <update id="updatePayStatus">
        UPDATE order_master
        SET pay_status = #{payStatus},
            update_time = SYSDATE()
        WHERE id = #{orderMasterId}
    </update>

    <insert id="insertOrderMaster" parameterType="com.ghy.order.domain.OrderMaster" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO order_master(
        <if test="deptId != null and deptId != 0">dept_id,</if>
        <if test="code != null and code != ''">code,</if>
        <if test="customerId != null and customerId != 0">customer_id,</if>
        <if test="addressId != null and addressId != 0">address_id,</if>
        <if test="goodsId != null and goodsId != 0">goods_id,</if>
        <if test="orderType != null">order_type,</if>
        <if test="orderMode != null">order_mode,</if>
        <if test="payMode != null">pay_mode,</if>
        <if test="consultMode != null">consult_mode,</if>
        <if test="insuranceId != null">insurance_id,</if>
        <if test="orderStatus != null">order_status,</if>
        <if test="payType != null">pay_type,</if>
        <if test="payStatus != null">pay_status,</if>
        <if test="workerId != null and workerId != 0">worker_id,</if>
        <if test="payTime != null">pay_time,</if>
        <if test="revTime != null">rev_time,</if>
        <if test="remark != null">remark,</if>
        <if test="goodsBrand != null and goodsBrand != ''">goods_brand,</if>
        <if test="goodsSpecification != null and goodsSpecification != ''">goods_specification,</if>
        <if test="expectTimeStart != null">expect_time_start,</if>
        <if test="expectTimeEnd != null">expect_time_end,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )VALUES(
        <if test="deptId != null and deptId != 0">#{deptId},</if>
        <if test="code != null">#{code},</if>
        <if test="customerId != null and customerId != 0">#{customerId},</if>
        <if test="addressId != null and addressId != 0">#{addressId},</if>
        <if test="goodsId != null and goodsId != 0">#{goodsId},</if>
        <if test="orderType != null">#{orderType},</if>
        <if test="orderMode != null">#{orderMode},</if>
        <if test="payMode != null">#{payMode},</if>
        <if test="consultMode != null">#{consultMode},</if>
        <if test="insuranceId != null">#{insuranceId},</if>
        <if test="orderStatus != null">#{orderStatus},</if>
        <if test="payType != null">#{payType},</if>
        <if test="payStatus != null">#{payStatus},</if>
        <if test="workerId != null and workerId != 0">#{workerId},</if>
        <if test="payTime != null">#{payTime},</if>
        <if test="revTime != null">#{revTime},</if>
        <if test="remark != null">#{remark},</if>
        <if test="goodsBrand != null and goodsBrand != ''">#{goodsBrand},</if>
        <if test="goodsSpecification != null and goodsSpecification != ''">#{goodsSpecification},</if>
        <if test="expectTimeStart != null">#{expectTimeStart},</if>
        <if test="expectTimeEnd != null">#{expectTimeEnd},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        SYSDATE()
        )
    </insert>

    <select id="checkOrderMasterCodeUnique" parameterType="String" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        WHERE `code` = #{orderMasterCode} LIMIT 1
    </select>

    <select id="selectByCode" parameterType="String" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        WHERE `code` = #{orderMasterCode}
    </select>

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

    <select id="selectUnfinished" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        WHERE `order_status` &lt; 5
    </select>


    <select id="selectByIds" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster"/>
        <where>
            AND id IN
            <foreach item="item" index="orderMasterId" collection="orderMasterId" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

    <update id="updateTimeout">
        UPDATE order_master
        SET timeout_ = #{timeout}
        <if test="timeoutFineTimes != null">,timeout_fine_times = #{timeoutFineTimes}</if>
        WHERE id = #{id}
    </update>

    <update id="updateCreateTime">
        UPDATE order_master
        SET create_time = SYSDATE()
        WHERE id = #{id}
    </update>

    <update id="removeWorker">
        UPDATE order_master
        SET worker_id = NULL,
        update_time = SYSDATE()
        WHERE id = #{id}
    </update>

    <select id="differentStatusOrderCount" resultType="com.ghy.order.domain.OrderMasterCount" parameterType="com.ghy.order.domain.OrderMaster">
        select
            count(case when om.order_status = 0 and om.worker_id is not null then 1 else null end) as newOrderNum,
            count(case when om.order_status = 1 then 1 else null end) as acceptedOrderNum,
            count(case when om.order_status = 1 and om.is_call = '01' then 1 else null end) as notAppointedOrderNum,
            count(case when om.order_status = 1 and om.is_call = '02' then 1 else null end) as notArrangedOrderNum,
            count(case when om.order_status = 2 then 1 else null end) as waitForDoorOrderNum,
            count(case when om.order_status = 3 then 1 else null end) as servingOrderNum,
            count(case when om.order_status = 4 then 1 else null end) as confirmingOrderNum,
            count(case when om.order_status = 5 then 1 else null end) as finishedOrderNum,
            count(case when om.order_status = 6 then 1 else null end) as canceledOrderNum
        from order_master om
        where 1 = 1
        <if test="timeout != null">
            AND om.timeout_ = #{timeout}
        </if>
        <if test="isCall != null">
            AND om.is_call = #{isCall}
        </if>
        <if test="createBy != null and createBy != ''">
            AND om.create_by = #{createBy}
        </if>
    </select>

    <select id="searchByOrderStartTime" parameterType="com.ghy.order.domain.OrderMaster" resultMap="OrderMasterResult">
        <include refid="selectOrderMaster" />
        <where>
            <if test="startTime != null">
                and create_time <![CDATA[ >= ]]> #{startTime}
            </if>
            <if test="endTime != null">
                and create_time <![CDATA[ <= ]]> #{endTime}
            </if>
        </where>
    </select>

</mapper>
