<?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.AftersaleMapper">
    
    <resultMap type="Aftersale" id="AftersaleResult">
        <result property="id" column="id"/>
        <result property="memberId" column="member_id"/>
        <result property="orderId" column="order_id"/>
        <result property="returnAmount" column="return_amount"/>
        <result property="type" column="type"/>
        <result property="status" column="status"/>
        <result property="handleTime" column="handle_time"/>
        <result property="quantity" column="quantity"/>
        <result property="reason" column="reason"/>
        <result property="description" column="description"/>
        <result property="proofPics" column="proof_pics"/>
        <result property="handleNote" column="handle_note"/>
        <result property="handleMan" column="handle_man"/>
        <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="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectAftersaleVo">
        select id, member_id, order_id, return_amount, type, status, handle_time, quantity, reason, description, proof_pics, handle_note, handle_man, create_by, create_time, update_by, update_time   from oms_aftersale
    </sql>

    <select id="selectByEntity" parameterType="Aftersale" resultMap="AftersaleResult">
        <include refid="selectAftersaleVo"/>
        <where>  
            <if test="memberId != null "> and member_id = #{memberId}</if>
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="returnAmount != null "> and return_amount = #{returnAmount}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="handleTime != null "> and handle_time = #{handleTime}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="reason != null  and reason != ''"> and reason = #{reason}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="proofPics != null  and proofPics != ''"> and proof_pics = #{proofPics}</if>
            <if test="handleNote != null  and handleNote != ''"> and handle_note = #{handleNote}</if>
            <if test="handleMan != null  and handleMan != ''"> and handle_man = #{handleMan}</if>
        </where>
    </select>
    <select id="selectManagerRefundOrder" resultType="com.cyl.manager.oms.domain.vo.ManagerRefundOrderVO">
        SELECT a.id            id,
        a.return_amount applyReturnAmount,
        a.quantity      refundNum,
        a.type          applyRefundType,
        a.reason        reason,
        a.description   description,
        a.proof_pics    proof_pics,
        a.order_id      orderId,
        a.create_time   applyRefundTime,
        a.status        aftersaleStatus,
        a.handle_time   handleTime,
        a.handle_note   note,
        a.handle_man,
        c.nickname      nickName,
        c.phone_hidden         phone,
        c.mark,
        a.refund_wp_code   refundWpCode,
        a.refund_waybill_code  refundWaybillCode
        FROM oms_aftersale a
        left join ums_member c on a.member_id = c.id
        <where>
            <if test="id != null">
                and a.id = #{id}
            </if>
            <if test="orderSn != null and orderSn != ''">
                and a.order_id = #{orderSn}
            </if>
            <if test="userPhone != null and userPhone != ''">
                and instr(c.phone, #{userPhone}) > 0
            </if>
            <if test="status != null">
                and a.status = #{status}
            </if>
            <if test="type != null">
                and a.type = #{type}
            </if>
            <if test="startTime != null and endTime != null">
                and a.create_time between #{startTime} and #{endTime}
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="countByMemberId" resultType="java.lang.Integer">
        select
            IFNULL(count(distinct id), 0)
            from oms_aftersale
        where member_id=#{memberId}
    </select>
    <select id="statPendingAndProcessing"
            resultType="com.cyl.manager.statistics.domain.vo.OrderAndAftersaleStatisticsVO">
        select
            IFNULL(sum(case when status=0 then 1 else 0 end), 0) pendingAftersaleCount,
            IFNULL(sum(case when status=1 then 1 else 0 end), 0) processingAftersaleCount
        from oms_aftersale
    </select>

</mapper>
