package com.fzm.mall.server.admin.order.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.fzm.mall.server.admin.order.entity.dto.ComplaintDTO;
import com.fzm.mall.server.admin.order.entity.dto.ComplaintHandleLogDTO;
import com.fzm.mall.server.admin.util.CopyUtil;
import com.fzm.mall.server.admin.util.UUIDUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单中心-订单投诉
 * </p>
 *
 * @author wulixian
 * @since 2021-03-16
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("order_complaint")
@ApiModel(value = "Complaint对象", description = "订单中心-订单投诉")
public class Complaint implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 投诉单处理状态 1->处理中;
     */
    public static final Integer COMPLAINT_STATUS_DOING = 1;

    /**
     * 投诉单处理状态 2->已完成;
     */
    public static final Integer COMPLAINT_STATUS_DONE = 2;

    /**
     * 处理记录
     */
    private static final String USER_HANDLE = "用户提交投诉订单";

    /**
     * 处理记录
     */
    private static final String PLAT_HANDLE = "处理投诉订单";

    @ApiModelProperty(value = "主键自增编号")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "投诉单编号")
    private String complaintId;

    @ApiModelProperty(value = "用户编号")
    private String uid;

    @ApiModelProperty(value = "关联订单编号")
    private String orderId;

    @ApiModelProperty(value = "关联订单类型(1->商品订单;2->提货订单)")
    private Integer orderType;

    @ApiModelProperty(value = "投诉描述")
    private String description;

    @ApiModelProperty(value = "投诉单处理状态(1->处理中;2->已完成)")
    private Integer complaintStatus;

    @ApiModelProperty(value = "处理结果")
    private String result;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "处理人编号")
    private String handlerId;

    @ApiModelProperty(value = "附件")
    private String attachments;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Long createTime;

    @ApiModelProperty(value = "更改时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateTime;

    @ApiModelProperty(value = "附件集合")
    @TableField(exist = false)
    private List<String> attachmentList;

    @ApiModelProperty(value = "用户昵称")
    @TableField(exist = false)
    private String nickName;

    @ApiModelProperty(value = "用户联系方式")
    @TableField(exist = false)
    private String userPhone;

    @ApiModelProperty(value = "商家名称")
    @TableField(exist = false)
    private String shopName;

    @ApiModelProperty(value = "商家联系方式")
    @TableField(exist = false)
    private String shopContactPhone;

    public void init() {
        setComplaintId(UUIDUtil.getUUID());
        setComplaintStatus(Complaint.COMPLAINT_STATUS_DOING);
        setAttachments(JSON.toJSONString(getAttachmentList()));
    }

    public ComplaintDTO toDTO() {
        ComplaintDTO complaintDTO = CopyUtil.to(this, ComplaintDTO.class);
        List<ComplaintHandleLogDTO> handleLogList = new ArrayList<>();
        complaintDTO.setAttachmentList(JSONObject.parseArray(this.getAttachments(), String.class));
        /**
         * 处理记录-用户提交投诉
         */
        ComplaintHandleLogDTO createDTO = new ComplaintHandleLogDTO();
        createDTO.setHandle(Complaint.USER_HANDLE);
        createDTO.setRemark(this.getDescription());
        createDTO.setTime(this.getCreateTime());
        handleLogList.add(createDTO);

        if (StringUtils.isNotBlank(this.getHandlerId())) {
            /**
             * 处理记录-平台处理投诉
             */
            ComplaintHandleLogDTO paltDTO = new ComplaintHandleLogDTO();
            paltDTO.setHandle(Complaint.PLAT_HANDLE);
            paltDTO.setRemark(this.getRemark());
            paltDTO.setTime(this.getUpdateTime());
            handleLogList.add(paltDTO);
        }
        complaintDTO.setHandleLogList(handleLogList);
        return complaintDTO;
    }
}
