<?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.cyl.manager.oms.mapper.OrderMapper">
    
    <resultMap type="Order" id="OrderResult">
        <result property="id" column="id"/>
        <result property="memberId" column="member_id"/>
        <result property="memberUsername" column="member_username"/>
        <result property="totalAmount" column="total_amount"/>
        <result property="purchasePrice" column="purchase_price"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="freightAmount" column="freight_amount"/>
        <result property="payType" column="pay_type"/>
        <result property="status" column="status"/>
        <result property="aftersaleStatus" column="aftersale_status"/>
        <result property="deliveryCompany" column="delivery_company"/>
        <result property="deliverySn" column="delivery_sn"/>
        <result property="autoConfirmDay" column="auto_confirm_day"/>
        <result property="receiverName" column="receiver_name"/>
        <result property="receiverPhone" column="receiver_phone"/>
        <result property="receiverPostCode" column="receiver_post_code"/>
        <result property="receiverProvince" column="receiver_province"/>
        <result property="receiverCity" column="receiver_city"/>
        <result property="receiverDistrict" column="receiver_district"/>
        <result property="receiverProvinceId" column="receiver_province_id"/>
        <result property="receiverCityId" column="receiver_city_id"/>
        <result property="receiverDistrictId" column="receiver_district_id"/>
        <result property="receiverDetailAddress" column="receiver_detail_address"/>
        <result property="note" column="note"/>
        <result property="confirmStatus" column="confirm_status"/>
        <result property="deleteStatus" column="delete_status"/>
        <result property="paymentTime" column="payment_time"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="receiveTime" column="receive_time"/>
        <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="needPreFamiliarize" column="need_pre_familiarize"/>
        <result property="wechatMemberCouponId" column="wechat_member_coupon_id"/>
        <result property="servicerId" column="servicer_id"/>
        <result property="orderServiceId" column="order_service_id"/>
        <result property="orderMethod" column="order_method"/>


    </resultMap>

    <sql id="selectOrderVo">
        select id, member_id, member_username, total_amount, purchase_price, pay_amount, freight_amount, pay_type, status, aftersale_status, delivery_company, delivery_sn, auto_confirm_day, receiver_name, receiver_phone, receiver_post_code, receiver_province, receiver_city, receiver_district, receiver_province_id, receiver_city_id, receiver_district_id, receiver_detail_address, note, confirm_status, delete_status, payment_time, delivery_time, receive_time, create_by, create_time, update_by, update_time, need_pre_familiarize, wechat_member_coupon_id,servicer_id,order_method   from oms_order
    </sql>
    <update id="cancelBatch">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update oms_order set status=#{item.status}, update_by=#{item.updateBy}, update_time=#{item.updateTime} where id=#{item.id}
        </foreach>
    </update>

    <select id="selectByEntity" parameterType="Order" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        <where>  
            <if test="memberId != null "> and member_id = #{memberId}</if>
            <if test="memberUsername != null  and memberUsername != ''"> and member_username like concat('%', #{memberUsername}, '%')</if>
            <if test="totalAmount != null "> and total_amount = #{totalAmount}</if>
            <if test="purchasePrice != null "> and purchase_price = #{purchasePrice}</if>
            <if test="payAmount != null "> and pay_amount = #{payAmount}</if>
            <if test="freightAmount != null "> and freight_amount = #{freightAmount}</if>
            <if test="payType != null "> and pay_type = #{payType}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="aftersaleStatus != null "> and aftersale_status = #{aftersaleStatus}</if>
            <if test="deliveryCompany != null  and deliveryCompany != ''"> and delivery_company = #{deliveryCompany}</if>
            <if test="deliverySn != null  and deliverySn != ''"> and delivery_sn = #{deliverySn}</if>
            <if test="autoConfirmDay != null "> and auto_confirm_day = #{autoConfirmDay}</if>
            <if test="receiverName != null  and receiverName != ''"> and receiver_name like concat('%', #{receiverName}, '%')</if>
            <if test="receiverPhone != null  and receiverPhone != ''"> and receiver_phone = #{receiverPhone}</if>
            <if test="receiverPostCode != null  and receiverPostCode != ''"> and receiver_post_code = #{receiverPostCode}</if>
            <if test="receiverProvince != null  and receiverProvince != ''"> and receiver_province = #{receiverProvince}</if>
            <if test="receiverCity != null  and receiverCity != ''"> and receiver_city = #{receiverCity}</if>
            <if test="receiverDistrict != null  and receiverDistrict != ''"> and receiver_district = #{receiverDistrict}</if>
            <if test="receiverProvinceId != null "> and receiver_province_id = #{receiverProvinceId}</if>
            <if test="receiverCityId != null "> and receiver_city_id = #{receiverCityId}</if>
            <if test="receiverDistrictId != null "> and receiver_district_id = #{receiverDistrictId}</if>
            <if test="receiverDetailAddress != null  and receiverDetailAddress != ''"> and receiver_detail_address = #{receiverDetailAddress}</if>
            <if test="note != null  and note != ''"> and note = #{note}</if>
            <if test="confirmStatus != null "> and confirm_status = #{confirmStatus}</if>
            <if test="deleteStatus != null "> and delete_status = #{deleteStatus}</if>
            <if test="paymentTime != null "> and payment_time = #{paymentTime}</if>
            <if test="deliveryTime != null "> and delivery_time = #{deliveryTime}</if>
            <if test="receiveTime != null "> and receive_time = #{receiveTime}</if>
            <if test="needPreFamiliarize != null "> and need_pre_familiarize = #{needPreFamiliarize}</if>
            <if test="wechatMemberCouponId != null "> and wechat_member_coupon_id = #{wechatMemberCouponId}</if>
            <if test="servicerId != null "> and servicer_id = #{servicerId}</if>
            <if test="orderMethod != null "> and order_method = #{orderMethod}</if>

            ,
        </where>
    </select>
    <select id="selectManagerOrderPage" resultType="com.cyl.manager.oms.pojo.vo.ManagerOrderVO">
        select
        a.id,
        a.member_id memberId,
        a.order_sn orderSn,
        a.aftersale_status aftersaleStatus,
        a.status,
        a.member_username  userName,
        a.total_amount     totalAmount,
        a.pay_amount       payAmount,
        a.note,
        a.merchant_note merchantNote,
        a.create_time      createTime,
        a.payment_time     payTime,
        a.receive_time     receiveTime,
        a.pay_type         payType,
        a.receiver_name    receiverName,
        a.receiver_phone    receiverPhone,
        a.receiver_province    receiverProvince,
        a.receiver_city    receiverCity,
        a.receiver_district    receiverDistrict,
        a.receiver_detail_address    receiverDetailAddress,
        a.need_pre_familiarize    needPreFamiliarize,
        a.wechat_member_coupon_id    wechatMemberCouponId,
        a.delivery_time deliveryTime,
        a.delivery_sn deliverySn,
--         b.quantity         buyNum,
--         b.pic,
--         b.sp_data          spData,
--         b.product_id       productId,
--         b.product_name     productName,
        c.phone_hidden            userPhone,
        c.nickname         nickName,
        c.mark
        from oms_order a
--         left join oms_order_item b on a.id = b.order_id
        left join ums_member c on a.member_id = c.id
        where a.aftersale_status = 1
        <if test="orderSn != null and orderSn != ''">
            and a.order_sn = #{orderSn}
        </if>
        <!--<if test="productName != null and productName != ''">-->
            <!--and instr(b.product_name, #{productName}) > 0-->
        <!--</if>-->
        <if test="receiverProvinceId != null ">
            and a.receiver_province_id = #{receiverProvinceId}
        </if>
        <if test="receiverCityId != null">
            and a.receiver_city_id = #{receiverCityId}
        </if>
        <if test="receiverDistrictId != null">
            and a.receiver_district_id = #{receiverDistrictId}
        </if>
        <if test="receiverDistrict != null">
            and a.receiver_district = #{receiverDistrict}
        </if>
        <if test="userPhone != null and userPhone != ''">
            and c.phone_encrypted=#{userPhone} > 0
        </if>
        <if test="status != null">
            and a.status = #{status}
        </if>
        <if test="payType != null">
            and a.pay_type=#{payType}
        </if>
        <if test="startTime != null and endTime != null">
            and a.create_time between #{startTime} and #{endTime}
        </if>
        order by a.create_time desc
    </select>

    <select id="orderPage" resultType="com.cyl.h5.pojo.vo.H5OrderVO">
        select
        id orderId,
        pay_id,
        order_sn,
        member_id,
        total_amount,
        pay_amount,
        status,
        aftersale_status,
        note,
        delivery_sn,
        create_time,
        payment_time,
        receiver_name,
        receiver_phone,
        receiver_province,
        receiver_city,
        receiver_district,
        receiver_detail_address
        from oms_order
        where
        delete_status=0
        and member_id=#{memberId}
        <if test="status==-1">
            and status in (1,2,3,4)
        </if>
        <if test="status>-1 and status&lt;3">
            and status=#{status}
            and aftersale_status=1
        </if>
        <if test="status==-2">
            and aftersale_status in (2, 3)
        </if>
        order by create_time desc
    </select>

    <select id="orderPage2" resultType="com.cyl.h5.pojo.vo.H5OrderVO">
        select
        id orderId,
        pay_id,
        order_sn,
        member_id,
        total_amount,
        pay_amount,
        status,
        aftersale_status,
        note,
        delivery_sn,
        create_time,
        payment_time,
        receiver_name,
        receiver_phone,
        receiver_province,
        receiver_city,
        receiver_district,
        receiver_detail_address
        from oms_order
        where
        delete_status=0
        and member_id=#{memberId}
        <if test="status != null and status.size() != 0">
            and status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by create_time desc
    </select>

    <select id="selectOrderDetail" resultType="com.cyl.h5.pojo.vo.H5OrderVO">
        select
        id orderId,
        pay_id,
        order_sn,
        member_id,
        total_amount,
        pay_amount,
        status,
        aftersale_status,
        note,
        delivery_sn,
        create_time,
        payment_time,
        receiver_name,
        receiver_phone,
        receiver_province,
        receiver_city,
        receiver_district,
        receiver_detail_address
        from oms_order
        where
        delete_status=0
        and id=#{orderId}
    </select>
    <select id="countByStatusAndMemberId" resultType="com.cyl.h5.pojo.vo.CountOrderVO">
        select
            IFNULL(sum(case when status=0 and aftersale_status=1 then 1 else 0 end),0) unpaid,
            IFNULL(sum(case when status=1 and aftersale_status=1 then 1 else 0 end),0) nosend,
            IFNULL(sum(case when status=2 and aftersale_status=1 then 1 else 0 end),0) noget,
            IFNULL(sum(case when aftersale_status in (2,3) then 1 else 0 end),0) aftersale
        from oms_order
        where member_id=#{memberId} and delete_status=0
    </select>
    <select id="statOrderCountAndAmount" resultType="com.cyl.manager.ums.pojo.vo.MemberDataStatisticsVO">
        select
            IFNULL(count(id), 0) orderCount,
            IFNULL(sum(pay_amount), 0) orderAmount
        from oms_order
        where status in (1,2,3) and aftersale_status=1 and member_id=#{memberId}
    </select>
    <select id="statWaitDelivered" resultType="java.lang.Integer">
        select IFNULL(count(id), 0) from oms_order where status=1 and aftersale_status=1
    </select>
    <select id="statTodayData" resultType="com.cyl.manager.statistics.pojo.vo.OrderAndAftersaleStatisticsVO">
        select
            IFNULL(sum(case when status in (2,3) then 1 else 0 end), 0) todayHasDeliveredCount,
            IFNULL(count(id), 0) todayOrderCount,
            IFNULL(sum(case when status in (1,2,3) then pay_amount else 0 end), 0) todayTransactionAmount
        from oms_order
        where create_time between #{startTime} and #{endTime}
    </select>
    <select id="statNewAndDeal" resultType="com.cyl.manager.aws.domain.SystemStatistics">
        select
            IFNULL(count(distinct member_id), 0) createOrderMemberCount,
            IFNULL(count(id), 0) orderCount,
            IFNULL(sum(case when status in (1,2,3) then 1 else 0 end), 0) dealCount,
            IFNULL(sum(case when status in (1,2,3) then pay_amount else 0 end), 0) dealAmount
        from oms_order
        where create_time between #{startTime} and #{endTime}
    </select>
    <select id="statDealMember" resultType="java.lang.Integer">
        select
            IFNULL(count(distinct member_id), 0)
        from oms_order
        where status in (1,2,3) and create_time between #{startTime} and #{endTime}
    </select>

    <select id="getAllValidOrder" resultType="com.cyl.manager.oms.domain.Order">
        select * from  oms_order where status in (1,2,3)
    </select>

    <select id="getOrdersByMemberId" resultType="com.cyl.manager.oms.domain.Order">
        select * from  oms_order where status in (1,2,3)
                                   and member_id = #{memberId}
                                   AND payment_time BETWEEN #{startDate} AND #{endDate}
    </select>
</mapper>
