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.servicebase.utils.JwtUtils;
import com.xunwang.servicebase.utils.R;
import com.xunwang.education.entity.MySuggestion;
import com.xunwang.education.feign.UserFeign;
import com.xunwang.education.mapper.MySuggestionMapper;
import com.xunwang.education.service.MySuggestionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
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 11:00
 */
@Service
@Transactional
public class MySuggestionServiceImpl extends ServiceImpl<MySuggestionMapper, MySuggestion> implements MySuggestionService {

    @Resource
    private UserFeign userFeign;

    /**
     * 查询个人的所有建议列表
     * @param request
     * @return
     */
    @Override
    public List<MySuggestion> findPersonAdvise(HttpServletRequest request) {
        QueryWrapper<MySuggestion> wrapper = new QueryWrapper<MySuggestion>().eq("from_uid", JwtUtils.getUIdByJwtToken(request)).orderByDesc("fist_datetime");
        List<MySuggestion> list =  baseMapper.selectList(wrapper);
        list = getListName(list);

        return list;
    }

    /**
     * 分页查询所有建议列表
     * @param request
     * @return
     */
    @Override
    public HashMap<String,Object> findAllAdvise(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        Page<MySuggestion> suggestionPage = new Page<MySuggestion>(Long.parseLong(pageNum), Long.parseLong(pageSize));
        IPage<MySuggestion> personAdvise = baseMapper.selectPage(suggestionPage,new QueryWrapper<MySuggestion>().orderByDesc("fist_datetime"));

        List<MySuggestion> records = personAdvise.getRecords();
        records = getListName(records);

        long total =  personAdvise.getTotal();
        HashMap<String,Object> map = new HashMap<>();
        map.put("count",total);
        map.put("data",records);
        return map;
    }

    private List<MySuggestion> getListName(List<MySuggestion> list) {
        return list.stream().peek(v->{
            if (v.getProcessorUid() != null && !"".equals(v.getProcessorUid())){
                R r = userFeign.getUserName(v.getProcessorUid());
                v.setName((String)r.getData().get("name"));
            }
        }).collect(Collectors.toList());
    }

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

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