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

import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.flow.dao.FlowStartMapper;
import com.lz.modules.flow.entity.EvaluationGroup;
import com.lz.modules.flow.entity.FlowStart;
import com.lz.modules.flow.service.EvaluationGroupService;
import com.lz.modules.flow.service.EvaluationStartStaffService;
import com.lz.modules.performance.req.AssessDetailReq;
import com.lz.modules.performance.req.AssessListReq;
import com.lz.modules.performance.res.AssessManagerDetailRes;
import com.lz.modules.performance.res.AssessManagerListRes;
import com.lz.modules.performance.service.AssessManagerService;
import com.lz.modules.sys.dao.app.ResultRecordMapper;
import com.lz.modules.sys.entity.app.ResultRecord;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: djc
 * @Desc:
 * @Date: 2020/10/22 17:28
 */
@Service("assessManagerService")
public class AssessManagerServiceImpl implements AssessManagerService {
    @Autowired
    private FlowStartMapper flowStartMapper;
    @Autowired
    private ResultRecordMapper resultRecordMapper;
    @Autowired
    private EvaluationGroupService evaluationGroupService;
    @Autowired
    private EvaluationStartStaffService evaluationStartStaffService;

    @Override
    public PageUtils assessList(AssessListReq req) {
        List<AssessManagerListRes> data = new ArrayList<>();
        PageUtils pageUtils = PageUtils.startPage(req.getCurrPage(),req.getPageSize()).doSelect(
                page -> flowStartMapper.selectListByTime(page,req.getCycleType(),req.getName())
        );
        List<FlowStart> list = pageUtils.getList();
        if(CollectionUtils.isEmpty(list)){
            return pageUtils;
        }
        list.forEach(flowStart -> {
            AssessManagerListRes res = new AssessManagerListRes();
            String name = flowStart.getName();
            res.setId(flowStart.getId());
            res.setName(name);
            if(name.contains("绩效考核")){
                res.setCycleTime(name.substring(0,name.lastIndexOf("绩效考核")));
            }
            int i = resultRecordMapper.countStartAndGroupNum(flowStart.getId());
            ResultRecord resultRecord = resultRecordMapper.selectOneByStartId(flowStart.getId());
            res.setJoinNum(resultRecord == null? StringUtil.EMPTY : resultRecord.getStaffName() + i + "等人");
            data.add(res);
        });
        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 PageUtils assessDetail(AssessDetailReq req) {
        PageUtils pageUtils = PageUtils.startPage(req.getCurrPage(),req.getPageSize()).doSelect(
                page ->  resultRecordMapper.selectAssessListByStartId(page,req)
        );
        return pageUtils;
    }

    @Override
    public void accessDelete(FlowStart flowStart) {
        flowStart.setIsDelete(1);
        flowStartMapper.updateFlowStartById(flowStart);
        resultRecordMapper.batchDeleteByStartId(flowStart.getId());
        return ;
    }
}
