package com.lz.modules.performance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.entity.DepartmentsStaffRelateEntity;
import com.lz.modules.app.service.DepartmentsStaffRelateService;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.flow.dao.FlowStartMapper;
import com.lz.modules.flow.entity.FlowStart;
import com.lz.modules.flow.service.EvaluationGroupService;
import com.lz.modules.flow.service.FlowStartService;
import com.lz.modules.performance.enums.ResultFlowProcessEnum;
import com.lz.modules.performance.req.AssessDetailReq;
import com.lz.modules.performance.req.ChartStartsReq;
import com.lz.modules.performance.res.ChartStartsRes;
import com.lz.modules.performance.res.ChartStatistical;
import com.lz.modules.performance.res.ChartStatisticalRes;
import com.lz.modules.performance.service.ChartResultService;
import com.lz.modules.sys.dao.app.ResultRecordMapper;
import com.lz.modules.sys.entity.app.ResultRecord;
import com.lz.modules.sys.service.app.ResultRecordService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Consumer;

/**
 * @Author: djc
 * @Desc:
 * @Date: 2020/10/14 16:53
 */
@Service("chartResultService")
public class ChartResultServiceImpl implements ChartResultService {

    @Autowired
    private ResultRecordService resultRecordService;
    @Autowired
    private DepartmentsStaffRelateService departmentsStaffRelateService;
    @Autowired
    private EvaluationGroupService evaluationGroupService;
    @Autowired
    private ResultRecordMapper resultRecordMapper;
    @Autowired
    private StaffService staffService;
    @Autowired
    private FlowStartService flowStartService;
    @Autowired
    private FlowStartMapper flowStartMapper;


    @Override
    public List<ChartStatisticalRes> chartReport(Long startId,String defaultTime) {
        List<ChartStatisticalRes> data = new ArrayList<>();
        ChartStatisticalRes res;

        List<ChartStatistical> process = resultRecordService.countNumByFlowProcess(startId);

        res = new ChartStatisticalRes();
        res.setType(0);
        res.setStatisticals(buildProcess(process,startId));
        res.setDefaultTime(defaultTime);
        if(StringUtil.isNotBlank(defaultTime)){
            res.setDefaultId(startId);
        }
        data.add(res);

        List<ChartStatistical> scoreLevel = resultRecordService.countNumByScoreLevel(startId);
        res = new ChartStatisticalRes();
        res.setType(1);
        res.setStatisticals(scoreLevel);
        res.setDefaultTime(defaultTime);
        if(StringUtil.isNotBlank(defaultTime)){
            res.setDefaultId(startId);
        }
        data.add(res);

        List<String> strings = evaluationGroupService.selectAllStaffIdsByGroupId(3L);
        List<ChartStatistical> depstaff = this.countDepartmentAndStaffNum(strings);
        res = new ChartStatisticalRes();
        res.setType(2);
        res.setStatisticals(depstaff);
        res.setDefaultTime(defaultTime);
        if(StringUtil.isNotBlank(defaultTime)){
            res.setDefaultId(startId);
        }
        data.add(res);
        return data;

    }

    @Override
    public List<ChartStatistical> countDepartmentAndStaffNum(List<String> staffIds) {
        if(CollectionUtils.isEmpty(staffIds)){
            return Collections.EMPTY_LIST;
        }

        List<ChartStatistical> data = new ArrayList<>();
        List<DepartmentsStaffRelateEntity> list = departmentsStaffRelateService.list(new QueryWrapper<DepartmentsStaffRelateEntity>()
                .eq("is_delete", 0)
                .in("staff_id", staffIds));

        Map<String,Integer> map = Maps.newHashMap();
        for(DepartmentsStaffRelateEntity entity:list){
            String departmentId = entity.getDepartmentId();
            Integer count = map.get(departmentId);
            if(null != count){
                map.put(departmentId,count + 1);
            }else {
                map.put(departmentId,1);
            }
        }
        for(String s:map.keySet()){
            ChartStatistical statistical = new ChartStatistical();
            statistical.setDesc(s);
            statistical.setNum(map.get(s));
            data.add(statistical);
        }
        return data;
    }

   /* @Override
    public PageUtils resultRankList(ChartResultReq req) {
        PageUtils pageUtils = PageUtils.startPage(req.getCurrPage(), req.getPageSize()).doSelect(
                page -> resultRecordMapper.selectResultRankList(page,req)
        );
        return pageUtils;
    }*/

    @Override
    public PageUtils chartStarts(ChartStartsReq req) {
        List<ChartStartsRes> data = new ArrayList<>();
        PageUtils pageUtils = PageUtils.startPage(req.getCurrPage(), req.getPageSize()).doSelect(
                page -> flowStartMapper.selectListByPage(page,req.getCycleType())
        );
        List<FlowStart> starts = pageUtils.getList();
        starts.forEach(flowStart -> {
            ChartStartsRes start = new ChartStartsRes();
            start.setStartId(flowStart.getId());
            start.setTime(flowStart.getName());
            data.add(start);
        });


        PageUtils pages = new PageUtils();
        pages.setTotalPage(pageUtils.getTotalPage());
        pages.setTotalCount(pageUtils.getTotalCount());
        pages.setCurrPage(req.getCurrPage());
        pages.setPageSize(req.getPageSize());
        pages.setList(data);

        return pages;

    }

    @Override
    public List<ChartStatistical> countAssessNumByFlowProcess(AssessDetailReq req) {
        List<ChartStatistical> process = resultRecordMapper.countAssessNumByFlowProcess(req);
        List<ChartStatistical> data = buildProcess(process, req.getStartId());
        return data;
    }

    //构建流程默认人数
    private List<ChartStatistical> buildProcess(List<ChartStatistical> process,Long startId){
        List<ChartStatistical> data = new ArrayList<>();
        Map map = Maps.newHashMap();
        int count = resultRecordService.count(new QueryWrapper<ResultRecord>()
                .eq("is_delete", 0)
                .eq("start_id", startId));

        ChartStatistical all = new ChartStatistical();
        all.setDesc("参与人数");
        all.setNum(count);
        data.add(all);

        for(ChartStatistical statistical:process){
            map.put(ResultFlowProcessEnum.getDesc(Integer.valueOf(statistical.getDesc())),statistical.getNum());
        }
        for(ResultFlowProcessEnum flowProcessEnum:ResultFlowProcessEnum.values()){
            ChartStatistical statistical = new ChartStatistical();
            statistical.setDesc(flowProcessEnum.getDesc());
            statistical.setNum(0);
            Object o = map.get(flowProcessEnum.getDesc());
            if(o!=null){
                statistical.setNum(Integer.valueOf(o.toString()));
            }
            data.add(statistical);
        }
        return data;
    }
}
