package com.fzm.mall.server.front.order.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.asset.model.UserAsset;
import com.fzm.mall.server.front.base.PageVO;
import com.fzm.mall.server.front.goods.model.vo.PageVo;
import com.fzm.mall.server.front.order.model.Complaint;
import com.fzm.mall.server.front.order.mapper.ComplaintMapper;
import com.fzm.mall.server.front.order.model.vo.ComplaintVo;
import com.fzm.mall.server.front.order.service.IComplaintService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.util.OrderUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 订单中心-订单投诉 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-06-01
 */
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements IComplaintService {

    @Override
    public int addComplaint(String uid, ComplaintVo complaintVo) {
        QueryWrapper<Complaint> q = new QueryWrapper<>();
        q.eq("uid", uid);
        q.eq("order_id", complaintVo.getOrderId());
        Complaint one = getOne(q);
        if (one != null) {
            return 0;
        } else {
            one = new Complaint();
        }
        one.setComplaintId(OrderUtil.getDeliveryId());
        one.setUid(uid);
        one.setOrderId(complaintVo.getOrderId());
        one.setComplaintStatus(1);
        one.setOrderType(complaintVo.getOrderType());
        one.setDescription(complaintVo.getDescription());
        if (complaintVo.getAttachments() == null) {
            one.setAttachments(null);
        } else {
            //String res = complaintVo.getAttachments().toJSONString();
            //List<String> strings = JSONArray.parseArray(res, String.class);
            one.setAttachments(complaintVo.getAttachments().toJSONString());
        }
        save(one);
        return 1;
    }

    @Override
    public PageVO<Complaint> complaintList(String uid, PageVo pageVo) {
        QueryWrapper<Complaint> q = new QueryWrapper<>();
        q.eq("uid", uid);
        List<Complaint> list = list(q);
        for (Complaint i : list) {
        i.setAttachmentList(JSONObject.parseArray(i.getAttachments(),String.class));
        }
        List<Complaint> waitComplaints = list.stream().filter(status -> status.getComplaintStatus() == 1).sorted(Comparator.comparing(Complaint::getCreateTime).reversed()).collect(Collectors.toList());
        List<Complaint> doneComplaints = list.stream().filter(status -> status.getComplaintStatus() == 2).sorted(Comparator.comparing(Complaint::getCreateTime).reversed()).collect(Collectors.toList());
        list.clear();
        list.addAll(waitComplaints);
        list.addAll(doneComplaints);
        PageVO<Complaint> pageVO = new PageVO<>();
        pageVO.init(Integer.valueOf(pageVo.getPage()), Integer.valueOf(pageVo.getSize()), list.size(), list);
        return pageVO;
    }

    @Override
    public Complaint complaintById(String uid, String id) {
        QueryWrapper<Complaint> q = new QueryWrapper<>();
        q.eq("uid", uid);
        q.eq("id", id);
        Complaint complaint = getOne(q);
        return complaint;
    }

}
