<?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.fzm.mall.server.admin.order.mapper.ComplaintMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.fzm.mall.server.admin.order.model.Complaint">
        <id column="id" property="id" />
        <result column="complaint_id" property="complaintId" />
        <result column="uid" property="uid" />
        <result column="order_id" property="orderId" />
        <result column="order_type" property="orderType" />
        <result column="description" property="description" />
        <result column="complaint_status" property="complaintStatus" />
        <result column="result" property="result" />
        <result column="handler_id" property="handlerId" />
        <result column="attachments" property="attachments" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <select id="page" resultType="com.fzm.mall.server.admin.order.entity.dto.ComplaintDTO">
        select c.id, c.complaint_id, c.uid, c.order_id, c.order_type, c.description, c.complaint_status, c.result, c.handler_id,
        c.attachments, c.create_time, c.update_time, c.remark, u.nick_name as nickName, u.phone as userPhone,
        s.shop_name as shopName, s.contact_phone as shopContactPhone
        from order_complaint c
        left join `user` u on c.uid = u.uid
        left join `order_info` o on o.oid = c.order_id
        left join `order_delivery` od on od.order_delivery_id = c.order_id
        left join shop s on o.merchant_id = s.merchant_id or od.merchant_id = s.merchant_id
        <where>
            s.status = '1'
            <if test="uid != null and uid != ''">
                and c.uid like CONCAT('%', #{uid},'%')
            </if>
            <if test="complaintId != null and complaintId != ''">
                and c.complaint_id like CONCAT('%', #{complaintId},'%')
            </if>
            <if test="complaintStatus != null">
                and c.complaint_status = #{complaintStatus}
            </if>
        </where>
        order by c.create_time desc
    </select>

    <select id="read" resultMap="BaseResultMap">
        select c.*, u.nick_name as nickName, u.phone as userPhone, s.shop_name as shopName, s.contact_phone as shopContactPhone
        from (select id, complaint_id, uid, order_id, order_type, description, complaint_status, result, handler_id,
               attachments, create_time, update_time, remark
            from order_complaint where complaint_id = #{complaintId}) c
        left join `user` u on c.uid = u.uid
        left join `order_info` o on o.oid = c.order_id
        left join `order_delivery` od on od.order_delivery_id = c.order_id
        left join shop s on o.merchant_id = s.merchant_id or od.merchant_id = s.merchant_id
        where s.status = '1'
    </select>
</mapper>
