<?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.store.mapper.FsStoreAfterSalesMapper">

    <resultMap type="FsStoreAfterSales" id="FsStoreAfterSalesResult">
        <result property="id"    column="id"    />
        <result property="orderCode"    column="order_code"    />
        <result property="refundAmount"    column="refund_amount"    />
        <result property="serviceType"    column="service_type"    />
        <result property="reasons"    column="reasons"    />
        <result property="explains"    column="explains"    />
        <result property="explainImg"    column="explain_img"    />
        <result property="shipperCode"    column="shipper_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="isPackage"    column="is_package"    />
        <result property="packageJson"    column="package_json"    />
    </resultMap>

    <sql id="selectFsStoreAfterSalesVo">
        select id, order_code, refund_amount, service_type, reasons, explains, explain_img, shipper_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,is_package,package_json from fs_store_after_sales
    </sql>

    <select id="selectFsStoreAfterSalesList" parameterType="FsStoreAfterSales" resultMap="FsStoreAfterSalesResult">
        <include refid="selectFsStoreAfterSalesVo"/>
        <where>
            <if test="orderCode != null  and orderCode != ''"> and order_code = #{orderCode}</if>
            <if test="refundAmount != null "> and refund_amount = #{refundAmount}</if>
            <if test="serviceType != null "> and service_type = #{serviceType}</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="shipperCode != null  and shipperCode != ''"> and shipper_code = #{shipperCode}</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="isDel != null "> 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>
        </where>
        order by id desc
    </select>

    <select id="selectFsStoreAfterSalesById" parameterType="Long" resultMap="FsStoreAfterSalesResult">
        <include refid="selectFsStoreAfterSalesVo"/>
        where id = #{id}
    </select>
    <select id="selectFsStoreAfterSalesListVO" resultType="com.fs.store.vo.FsStoreAfterSalesVO">
        select s.`id`,
                s.`order_code`,
                s.`refund_amount`,
                s.`service_type`,
                s.`reasons`,
                s.`explains`,
                s.`explain_img`,
                s.`shipper_code`,
                s.`status`,
                s.`sales_status`,
                s.`order_status`,
                s.`create_time`,
                s.`is_del`,
                s.`user_id`,
                s.`consignee`,
                s.`phone_number`,
                s.`address`,
                s.`company_id`,
                s.`company_user_id`,
                s.`is_package`,
                s.`package_json`,
                s.`delivery_sn` as `delivery_sn`,
                s.`delivery_name` as `delivery_name`,
               deliver.status as delivery_status,
               deliver.deliver_id as delivery_id,
               u.phone as user_phone,
               c.company_name ,cu.nick_name as company_user_nick_name ,
               cu.phonenumber as company_usere_phonenumber
                from fs_store_after_sales s
                    INNER join fs_store_order o
                        on o.order_code=s.order_code
                    left join fs_user u
                        on s.user_id=u.user_id
                    left join company c
                        on c.company_id=s.company_id
                    left join company_user cu
                        on cu.user_id=s.company_user_id
                    left join fs_store_delivers deliver
                        on o.order_code=deliver.order_code
        <where>
            <if test = 'maps.status != null'>
                and s.status = #{maps.status}
            </if>
            <if test = 'maps.salesStatus != null'>
            and s.sales_status = #{maps.salesStatus}
            </if>
            <if test = 'maps.orderStatus != null'>
                and s.order_status = #{maps.orderStatus}
            </if>
            <if test = 'maps.orderCode != null and  maps.orderCode != "" '>
                and o.order_code like concat(#{maps.orderCode}, '%')
            </if>
            <if test = 'maps.deliveryStatus != null'>
                and deliver.status = #{maps.deliveryStatus}
            </if>
            <if test = 'maps.serviceType != null'>
                and s.service_type = #{maps.serviceType}
            </if>
            <if test = 'maps.companyId != null'>
                and s.company_id = #{maps.companyId}
            </if>
            <if test = 'maps.companyUserId != null'>
                and s.company_user_id = #{maps.companyUserId}
            </if>
            <if test = 'maps.deliverySn != null and  maps.deliverySn != ""'>
                and ( deliver.deliver_id like concat(#{maps.deliverySn}, '%') or deliver.deliver_sn like concat(#{maps.deliverySn}, '%'))
            </if>
            <if test = 'maps.companyUserNickName != null and  maps.companyUserNickName != ""'>
                and cu.nick_name like concat(#{maps.companyUserNickName}, '%')
            </if>
            <if test = 'maps.beginTime != null and maps.beginTime != ""'>
                AND date_format(s.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d')
            </if>
            <if test = 'maps.consigneePhone != null and  maps.consigneePhone !="" '>
                and o.user_phone like CONCAT(#{maps.consigneePhone},'%')
            </if>
            <if test = 'maps.endTime != null and maps.endTime != ""'>
                AND date_format(s.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')
            </if>
            <if test = 'maps.deptId != null'>
                AND (o.dept_id = #{maps.deptId} OR o.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) ))
            </if>
            ${maps.params.dataScope}
        </where>
        order by s.create_time desc
    </select>

    <insert id="insertFsStoreAfterSales" parameterType="FsStoreAfterSales" useGeneratedKeys="true" keyProperty="id">
        insert into fs_store_after_sales
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderCode != null">order_code,</if>
            <if test="refundAmount != null">refund_amount,</if>
            <if test="serviceType != null">service_type,</if>
            <if test="reasons != null">reasons,</if>
            <if test="explains != null">explains,</if>
            <if test="explainImg != null">explain_img,</if>
            <if test="shipperCode != null">shipper_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="isPackage != null">is_package,</if>
            <if test="packageJson != null">package_json,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderCode != null">#{orderCode},</if>
            <if test="refundAmount != null">#{refundAmount},</if>
            <if test="serviceType != null">#{serviceType},</if>
            <if test="reasons != null">#{reasons},</if>
            <if test="explains != null">#{explains},</if>
            <if test="explainImg != null">#{explainImg},</if>
            <if test="shipperCode != null">#{shipperCode},</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="isPackage != null">#{isPackage},</if>
            <if test="packageJson != null">#{packageJson},</if>
         </trim>
    </insert>

    <update id="updateFsStoreAfterSales" parameterType="FsStoreAfterSales">
        update fs_store_after_sales
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="refundAmount != null">refund_amount = #{refundAmount},</if>
            <if test="serviceType != null">service_type = #{serviceType},</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="shipperCode != null">shipper_code = #{shipperCode},</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="isPackage != null">is_package = #{isPackage},</if>
            <if test="packageJson != null">package_json = #{packageJson},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFsStoreAfterSalesById" parameterType="Long">
        delete from fs_store_after_sales where id = #{id}
    </delete>

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

</mapper>
