package com.zcxk.task.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxk.task.dao.PlanManageMapper;
import com.zcxk.task.dto.PlanManageDto;
import com.zcxk.task.dto.TaskChangeTendDto;
import com.zcxk.task.dto.TaskConditionDto;
import com.zcxk.task.entity.PlanManage;
import com.zcxk.task.service.PlanManageService;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PlanManageServiceImpl implements PlanManageService {

    @Autowired
    private PlanManageMapper planManageMapper;

    @Override
    public int insertSelective(PlanManage record) {
        return planManageMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(PlanManage record) {
        return planManageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public IPage<PlanManageDto> selectPage(IPage<PlanManageDto> page, PlanManageDto planManageDto){
        return planManageMapper.findPage(page,planManageDto);
    }

    @Override
    public PlanManage selectByPlanId(String planId) {
        return planManageMapper.selectByPlanId(planId);
    }

    @Override
    public PlanManage selectByPrimaryKey(Integer id) {
        return planManageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(PlanManage record) {
        return planManageMapper.updateByPrimaryKey(record);
    }

    @Override
    public Integer batchDelete(List<Integer> ids) {
        return planManageMapper.batchDelete(ids);
    }

    @Override
    public PlanManageDto selectPlanDetail(Integer id) {
        return planManageMapper.selectPlanDetail(id);
    }

    @Override
    public int selectPendingPower(Map<String, Object> map) {
        return planManageMapper.selectPendingPower(map);
    }

    @Override
    public List<Map<String,Object>> selectTaskName(Map<String, Object> map) {
        return planManageMapper.selectTaskName(map);
    }

    @Override
    public List<PlanManageDto> selectTaskFinished(PlanManageDto planManageDto) {
        List<PlanManageDto>  result= planManageMapper.selectTaskFinished(planManageDto);
        PlanManageDto plan=new PlanManageDto();
        Integer taskCount=0,FinishedCount=0;
        for (PlanManageDto planDto:result) {
            taskCount+=planDto.getTaskCount();
            FinishedCount+=planDto.getFinishedCount();
        }
        plan.setTaskCount(taskCount);
        plan.setFinishedCount(FinishedCount);
        result.add(plan);
        return result;
    }

    @Override
    public List<PlanManageDto> selectTaskFinishedByDate(PlanManageDto planManageDto) {
        return planManageMapper.selectTaskFinishedByDate(planManageDto);
    }

    @Override
    public TaskConditionDto selectTaskCondition(PlanManageDto planManageDto) {
        TaskConditionDto result=new TaskConditionDto();
        try{
            PlanManageDto plan=planManageMapper.selectTaskCondition(planManageDto);
            result.setTaskCount(plan.getTaskCount());
            result.setFinishedCount(plan.getFinishedCount());
            result.setTaskFinishedRate(plan.getTaskCount()!=0?(double)plan.getFinishedCount()/plan.getTaskCount()*100:null);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar c=Calendar.getInstance();

            PlanManageDto planMOnM=new PlanManageDto();
            PlanManageDto planYOnY=new PlanManageDto();
            if(planManageDto.getStatsType()==0){
                Date thisDate=sdf.parse(planManageDto.getStartDate());
                c.setTime(thisDate);
                c.add(Calendar.MONTH,-1);
                planManageDto.setStartDate(sdf.format(c.getTime()));
                planMOnM=planManageMapper.selectTaskCondition(planManageDto);

                c.setTime(thisDate);
                c.add(Calendar.YEAR,-1);
                planManageDto.setStartDate(sdf.format(c.getTime()));
                planYOnY=planManageMapper.selectTaskCondition(planManageDto);
            }else if(planManageDto.getStatsType()==1){
                planManageDto.setStartDate(String.valueOf(Integer.parseInt(planManageDto.getStartDate())-1));
                planMOnM=planManageMapper.selectTaskCondition(planManageDto);
                planYOnY=planMOnM;
            }else if(planManageDto.getStatsType()==2){
                //自定义时间相差多少个月
                int month=Months.monthsBetween(new DateTime(planManageDto.getStartDate()), new DateTime(planManageDto.getEndDate())).getMonths();
                Date startDate=sdf.parse(planManageDto.getStartDate());
                Date endDate=sdf.parse(planManageDto.getEndDate());
                c.setTime(startDate);
                c.add(Calendar.MONTH,-1);
                planManageDto.setEndDate(sdf.format(c.getTime()));
                c.add(Calendar.MONTH,-month);
                planManageDto.setStartDate(sdf.format(c.getTime()));
                planMOnM=planManageMapper.selectTaskCondition(planManageDto);

                c.setTime(startDate);
                c.add(Calendar.YEAR,-1);
                planManageDto.setStartDate(sdf.format(c.getTime()));
                c.setTime(endDate);
                c.add(Calendar.YEAR,-1);
                planManageDto.setEndDate(sdf.format(c.getTime()));
                planYOnY=planManageMapper.selectTaskCondition(planManageDto);
            }
            double taskFinishedRateMOnM=planMOnM.getTaskCount()!=0?(double)planMOnM.getFinishedCount()/planMOnM.getTaskCount()*100:0;
            double taskFinishedRateYOnY=planYOnY.getTaskCount()!=0?(double)planYOnY.getFinishedCount()/planYOnY.getTaskCount()*100:0;

            result.setTaskCountRateMOnM(planMOnM.getTaskCount()!=0?(double)(result.getTaskCount()-planMOnM.getTaskCount())/planMOnM.getTaskCount()*100:null);
            result.setTaskCountRateYOnY(planYOnY.getTaskCount()!=0?(double)(result.getTaskCount()-planYOnY.getTaskCount())/planYOnY.getTaskCount()*100:null);
            result.setFinishedCountRateMOnM(planMOnM.getFinishedCount()!=0?(double)(result.getFinishedCount()-planMOnM.getFinishedCount())/planMOnM.getFinishedCount()*100:null);
            result.setFinishedCountRateYOnY(planYOnY.getFinishedCount()!=0?(double)(result.getFinishedCount()-planYOnY.getFinishedCount())/planYOnY.getFinishedCount()*100:null);
            result.setTaskFinishedRateMOnM(taskFinishedRateMOnM !=0 ?(result.getTaskFinishedRate()-taskFinishedRateMOnM)/taskFinishedRateMOnM*100:null);
            result.setTaskFinishedRateYOnY(taskFinishedRateYOnY !=0 ?(result.getTaskFinishedRate()-taskFinishedRateYOnY)/taskFinishedRateYOnY*100:null);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<TaskChangeTendDto>  selectTaskChangeTrend(PlanManageDto planManageDto) {
        return planManageMapper.selectTaskChangeTrend(planManageDto);
    }

    @Override
    public int[]  selectTaskDuration(PlanManageDto planManageDto) {
        int[] result=new int[6];
        List<PlanManageDto> listPlan=planManageMapper.selectTaskDuration(planManageDto);
        for(PlanManageDto plan : listPlan){
            if(plan.getIntervalCount()!=null){
                if(plan.getIntervalCount()<5){
                    result[plan.getIntervalCount()]=plan.getFinishedCount();
                }else{
                    result[5]+=plan.getFinishedCount();
                }
            }
        }
        return result;
    }

    @Override
    public PlanManageDto selectWorkFlowById(Integer id) {
        return planManageMapper.selectWorkFlowById(id);
    }
}
