package com.xunwang.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.education.entity.Complaint;
import com.xunwang.education.feign.UserFeign;
import com.xunwang.education.mapper.ComplaintMapper;
import com.xunwang.education.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Yl
 * @Date: 2023/1/3 15:36
 */
@Service
@Transactional
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintService {

    @Autowired
    private UserFeign userFeign;

    @Override
    public HashMap<String,Object> findAllComplaint(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        Page<Complaint> suggestionPage = new Page<Complaint>(Long.parseLong(pageNum), Long.parseLong(pageSize));
        IPage<Complaint> personComplaint = baseMapper.selectPage(suggestionPage,new QueryWrapper<Complaint>().orderByDesc("fist_datetime"));

        List<Complaint> records = personComplaint.getRecords();

        List<Complaint> list = records.stream().map(v->{

            if (v.getProcessorUid()==null || "".equals(v.getProcessorUid())){
            }
            else {
                R r = userFeign.getUserName(v.getProcessorUid());
                v.setName((String) r.getData().get("name"));
            }

            return  v;
        }).collect(Collectors.toList());

        long total =  personComplaint.getTotal();
        HashMap<String,Object> map = new HashMap<>();
        map.put("count",total);
        map.put("data",list);

        return map;
    }

    @Override
    public boolean handleComplaint(Complaint complaint, HttpServletRequest request) {
        String processorUid = JwtUtils.getUIdByJwtToken(request);
        complaint.setProcessorUid(processorUid);
        complaint.setStatus("已处理");
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        complaint.setClosetime(String.valueOf(localDateTime));
        return baseMapper.updateById(complaint) != 0;
    }

    @Override
    public boolean handlingComplaint(Complaint complaint, HttpServletRequest request) {
        String processorUid = JwtUtils.getUIdByJwtToken(request);
        complaint.setProcessorUid(processorUid);
        complaint.setStatus("处理中");
        return baseMapper.updateById(complaint) != 0;
    }

    @Override
    public List<Complaint> findPersonComplaint(HttpServletRequest request) {

        QueryWrapper<Complaint> wrapper = new QueryWrapper<Complaint>().eq("from_uid", JwtUtils.getUIdByJwtToken(request)).orderByDesc("fist_datetime");

        List<Complaint> list =  baseMapper.selectList(wrapper);

        List<Complaint> list1 = list.stream().map(v->{
            if (v.getProcessorUid()==null || "".equals(v.getProcessorUid())){
            }
            else {
                R r = userFeign.getUserName(v.getProcessorUid());
                v.setName((String) r.getData().get("name"));
            }
            return  v;
        }).collect(Collectors.toList());

        return list1;

    }

}
