<?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.fs.live.mapper.LiveAfterSalesMapper">

    <resultMap type="LiveAfterSales" id="LiveAfterSalesResult">
        <result property="id"    column="id"    />
        <result property="liveId"    column="live_id"    />
        <result property="storeId"    column="store_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="refundAmount"    column="refund_amount"    />
        <result property="refundType"    column="refund_type"    />
        <result property="reasons"    column="reasons"    />
        <result property="explains"    column="explains"    />
        <result property="explainImg"    column="explain_img"    />
        <result property="deliveryCode"    column="delivery_code"    />
        <result property="deliverySn"    column="delivery_sn"    />
        <result property="deliveryName"    column="delivery_name"    />
        <result property="status"    column="status"    />
        <result property="salesStatus"    column="sales_status"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="userId"    column="user_id"    />
        <result property="consignee"    column="consignee"    />
        <result property="phoneNumber"    column="phone_number"    />
        <result property="address"    column="address"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="deptId"    column="dept_id"    />
    </resultMap>

    <sql id="selectLiveAfterSalesVo">
        select id, live_id, store_id, order_id, refund_amount, refund_type, reasons, explains, explain_img, delivery_code, delivery_sn, delivery_name, status, sales_status, order_status, create_time, is_del, user_id, consignee, phone_number, address, company_id, company_user_id, dept_id from live_after_sales
    </sql>

    <select id="selectLiveAfterSalesList" parameterType="LiveAfterSales" resultMap="LiveAfterSalesResult">
        <include refid="selectLiveAfterSalesVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="storeId != null "> and store_id = #{storeId}</if>
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="refundAmount != null "> and refund_amount = #{refundAmount}</if>
            <if test="refundType != null "> and refund_type = #{refundType}</if>
            <if test="reasons != null  and reasons != ''"> and reasons = #{reasons}</if>
            <if test="explains != null  and explains != ''"> and explains = #{explains}</if>
            <if test="explainImg != null  and explainImg != ''"> and explain_img = #{explainImg}</if>
            <if test="deliveryCode != null  and deliveryCode != ''"> and delivery_code = #{deliveryCode}</if>
            <if test="deliverySn != null  and deliverySn != ''"> and delivery_sn = #{deliverySn}</if>
            <if test="deliveryName != null  and deliveryName != ''"> and delivery_name like concat('%', #{deliveryName}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="salesStatus != null "> and sales_status = #{salesStatus}</if>
            <if test="orderStatus != null "> and order_status = #{orderStatus}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="consignee != null  and consignee != ''"> and consignee = #{consignee}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and phone_number = #{phoneNumber}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
        </where>
    </select>
    <select id="selectLiveAfterSalesVoList" parameterType="com.fs.live.vo.LiveAfterSalesVo" resultType="com.fs.live.vo.LiveAfterSalesVo">
        select las.id, las.live_id, las.store_id, las.order_id, las.refund_amount,
        las.refund_type, las.reasons, las.explains, las.explain_img, las.delivery_code, las.delivery_sn, las.delivery_name, las.status, las.sales_status,
        las.order_status, las.create_time, las.is_del, las.user_id, las.consignee, las.phone_number, las.address, las.company_id, las.company_user_id, las.dept_id,
        cu.nick_name as company_user_nick_name, c.company_name,lo.order_code,lo.user_phone,las.user_id
        from live_after_sales las
        left join live_order lo on lo.order_id = las.order_id
        left join company_user cu on cu.user_id = las.company_user_id
        left join company c on c.company_id = cu.company_id

        <where>
            <if test="liveId != null and liveId != ''"> and las.live_id = #{liveId}</if>
            <if test="storeId != null and storeId != ''"> and las.store_id = #{storeId}</if>
            <if test="orderCode != null and orderCode != ''"> and lo.order_code = #{orderCode}</if>
            <if test="refundAmount != null "> and las.refund_amount = #{refundAmount}</if>
            <if test="refundType != null "> and las.refund_type = #{refundType}</if>
            <if test="reasons != null  and reasons != ''"> and las.reasons = #{reasons}</if>
            <if test="explains != null  and explains != ''"> and las.explains = #{explains}</if>
            <if test="explainImg != null  and explainImg != ''"> and las.explain_img = #{explainImg}</if>
            <if test="deliveryCode != null  and deliveryCode != ''"> and las.delivery_code = #{deliveryCode}</if>
            <if test="deliverySn != null  and deliverySn != ''"> and las.delivery_sn = #{deliverySn}</if>
            <if test="deliveryName != null  and deliveryName != ''"> and las.delivery_name like concat('%', #{deliveryName}, '%')</if>
            <if test="status != null "> and las.status = #{status}</if>
            <if test="salesStatus != null "> and las.sales_status = #{salesStatus}</if>
            <if test="orderStatus != null "> and las.order_status = #{orderStatus}</if>
            <if test="isDel != null  and isDel != ''"> and las.is_del = #{isDel}</if>
            <if test="userId != null "> and las.user_id = #{userId}</if>
            <if test="consignee != null  and consignee != ''"> and las.consignee = #{consignee}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and las.phone_number = #{phoneNumber}</if>
            <if test="address != null  and address != ''"> and las.address = #{address}</if>
            <if test="companyId != null "> and las.company_id = #{companyId}</if>
            <if test="companyUserId != null "> and las.company_user_id = #{companyUserId}</if>
            <if test="deptId != null "> and cu.dept_id = #{deptId}</if>
        </where>
        order by las.create_time desc
    </select>

    <select id="selectLiveAfterSalesById" parameterType="Long" resultMap="LiveAfterSalesResult">
        <include refid="selectLiveAfterSalesVo"/>
        where id = #{id}
        order by create_time desc
    </select>

    <insert id="insertLiveAfterSales" parameterType="LiveAfterSales" useGeneratedKeys="true" keyProperty="id">
        insert into live_after_sales
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="refundAmount != null">refund_amount,</if>
            <if test="refundType != null">refund_type,</if>
            <if test="reasons != null">reasons,</if>
            <if test="explains != null">explains,</if>
            <if test="explainImg != null">explain_img,</if>
            <if test="deliveryCode != null">delivery_code,</if>
            <if test="deliverySn != null">delivery_sn,</if>
            <if test="deliveryName != null">delivery_name,</if>
            <if test="status != null">status,</if>
            <if test="salesStatus != null">sales_status,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="userId != null">user_id,</if>
            <if test="consignee != null">consignee,</if>
            <if test="phoneNumber != null">phone_number,</if>
            <if test="address != null">address,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="refundAmount != null">#{refundAmount},</if>
            <if test="refundType != null">#{refundType},</if>
            <if test="reasons != null">#{reasons},</if>
            <if test="explains != null">#{explains},</if>
            <if test="explainImg != null">#{explainImg},</if>
            <if test="deliveryCode != null">#{deliveryCode},</if>
            <if test="deliverySn != null">#{deliverySn},</if>
            <if test="deliveryName != null">#{deliveryName},</if>
            <if test="status != null">#{status},</if>
            <if test="salesStatus != null">#{salesStatus},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="userId != null">#{userId},</if>
            <if test="consignee != null">#{consignee},</if>
            <if test="phoneNumber != null">#{phoneNumber},</if>
            <if test="address != null">#{address},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateLiveAfterSales" parameterType="LiveAfterSales">
        update live_after_sales
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="storeId != null">store_id = #{storeId},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="refundAmount != null">refund_amount = #{refundAmount},</if>
            <if test="refundType != null">refund_type = #{refundType},</if>
            <if test="reasons != null">reasons = #{reasons},</if>
            <if test="explains != null">explains = #{explains},</if>
            <if test="explainImg != null">explain_img = #{explainImg},</if>
            <if test="deliveryCode != null">delivery_code = #{deliveryCode},</if>
            <if test="deliverySn != null">delivery_sn = #{deliverySn},</if>
            <if test="deliveryName != null">delivery_name = #{deliveryName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="salesStatus != null">sales_status = #{salesStatus},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="consignee != null">consignee = #{consignee},</if>
            <if test="phoneNumber != null">phone_number = #{phoneNumber},</if>
            <if test="address != null">address = #{address},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLiveAfterSalesById" parameterType="Long">
        delete from live_after_sales where id = #{id}
    </delete>

    <delete id="deleteLiveAfterSalesByIds" parameterType="String">
        delete from live_after_sales where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
