package com.zjty.tynotes.job.basic.service.impl;

import com.google.common.collect.Lists;
import com.zjty.tynotes.job.basic.entity.database.ScoreCoefficient;
import com.zjty.tynotes.job.basic.entity.database.Work;
import com.zjty.tynotes.job.basic.entity.database.WorkTime;
import com.zjty.tynotes.job.basic.entity.response.WorkIsNotCommit;
import com.zjty.tynotes.job.basic.entity.response.WorkMpping;
import com.zjty.tynotes.job.basic.entity.response.WorkVo;
import com.zjty.tynotes.job.basic.entity.response.WorkVoMapping;
import com.zjty.tynotes.job.basic.repository.WorkRepository;
import com.zjty.tynotes.job.basic.service.ConversionService;
import com.zjty.tynotes.job.basic.service.ScoreCoefficientService;
import com.zjty.tynotes.job.basic.service.WorkService;
import com.zjty.tynotes.job.basic.service.WorkTimeService;
import com.zjty.tynotes.job.basic.utils.WorkingTime;
import com.zjty.tynotes.job.common.constant.WorkStatus;
import com.zjty.tynotes.job.common.exception.BadRequestException;
import com.zjty.tynotes.job.common.exception.NotFoundException;
import com.zjty.tynotes.pas.entity.vo.HolidayRequest;
import com.zjty.tynotes.pas.service.ConfigService;
import com.zjty.tynotes.pas.service.IUserService;
import com.zjty.tynotes.pas.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
 * <p>Description : note
 * <p>Date : 2019/4/23 11:32
 * <p>@author : C
 * 接口定义说明见↓
 *
 * @see WorkService
 */
@Slf4j
@Service
public class WorkServiceImpl implements WorkService {

    private final
    WorkRepository workRepository;

    @Autowired
    IUserService iUserService;

    @Autowired
    ConversionService conversionService;

    @Autowired
    ScoreCoefficientService scoreCoefficientService;

    @Autowired
    WorkTimeService workTimeService;

    @Autowired
    public WorkServiceImpl(WorkRepository workRepository) {
        this.workRepository = workRepository;
    }
    @Autowired
    ConfigService configService;

    @Override
    public String add(Work work) {
        //新增数据不可附带id.
        Boolean idMustNull = isNull(work.getId());
        if (idMustNull) {
            work.setCreateTime(new Date());
            work.setUpdateTime(new Date());
            String saveId = workRepository.save(work).getId();
            log.info(String.format("[job]  新增了id为 %s 的数据.", saveId));
            return saveId;
        } else {
            String msg = "[job] %s: 新增数据不可附带id.";
            log.error(msg);
            throw new BadRequestException(msg);
        }
    }

    @Override
    public String modify(Work work) {
        //更新数据必须指定id且指定id的数据存在.
        Boolean idMustExist = (nonNull(work.getId())) && (workRepository.existsById(work.getId()));
        if (idMustExist) {
            String modifyId = workRepository.save(work).getId();
            log.info(String.format("[job] 更新了id为 %s 的数据.", modifyId));
            return modifyId;
        } else {
            String msg = "[job] 更新数据必须指定id且指定id的数据存在.";
            log.error(msg);
            throw new BadRequestException(msg);
        }
    }

    @Override
    public List<Work> findAll() {
        return workRepository.findAll();
    }

    @Override
    public List<Work> findAll(List<String> ids) {
        return Lists.newArrayList(workRepository.findAllById(ids));
    }

    @Override
    public Boolean idExists(String workId) {
        return workRepository.existsById(workId);
    }

    @Override
    public Work findById(String workId) {
        Optional<Work> workOptional = workRepository.findById(workId);
        return workOptional.orElseThrow(() -> new BadRequestException(String.format("[job] id为 %s 的数据不存在.", workId)));
    }

    @Override
    public void delete(String workId) {
        if (workRepository.existsById(workId)) {
            workRepository.deleteById(workId);
        } else {
            throw new NotFoundException(String.format("[job] id为 %s 的工作不存在.", workId));
        }
    }

    @Override
    public void deleteAll() {
    }


    @Override
    public void alterTaskStatus(String taskId, String status, String userId) {
        Work ob = workRepository.findById(taskId).get();
        ob.setStatus(status);
        Date time=new Date();
        if (WorkStatus.ONGOING.equals(status)){
                ob.setAuditTime(time);
                workTimeService.saveWorkEndTime(ob,time);

        }else if (WorkStatus.AUDIT.equals(status)){
            ob.setSubmitTime(time);
            workTimeService.saveWorkStartTime(ob,time);

            Float expect= score(ob.getStateTime(),ob.getCompleteTime());
            Float practical=practical(taskId);
            log.info("预计完成时间："+expect+"--真实完成时间："+practical);
            if (expect<practical){
                float times=  practical-expect;
                ob.setWorkCoefficient(beyond(1,1.0f,times));
                //添加
            }else if (expect.equals(practical)){
                ob.setWorkCoefficient(1f);
            }else {
                float times=  expect-practical;
                ob.setWorkCoefficient(advance(1,1.0f,times));
            }

        }else if (WorkStatus.REVIEW.equals(status)) {
        //
            ob.setAuditTime(time);

        }else if (status.equals(WorkStatus.FINISHED)){
            pWorkload(taskId);
            ob.setEndTime(time);
        }

        workRepository.save(ob);
    }

    @Override
    public void alterTaskStatus1(String taskId, String status, String userId) {
        Work ob = workRepository.findById(taskId).get();
        ob.setStatus(status);
        workRepository.save(ob);
    }

    private Float practical(String workId){
        Float count=0.0f;
        List<WorkTime> workTimes=workTimeService.selectWorkTime(workId);
        for (WorkTime workTime:workTimes){
            Float s=score(workTime.getStartTime(),workTime.getEndTime());
            count=count+s;
        }
        return count;
    }

    private void pWorkload(String workId){
     ScoreCoefficient scoreCoefficient= scoreCoefficientService.findById(workId);
      int workLoad=  findBySuperoirIdCount(workId);
        DecimalFormat df=new  DecimalFormat("#.##");
        double coefficient= (Double.parseDouble(scoreCoefficient.getScore1())+Double.parseDouble(scoreCoefficient.getScore2()))/2.0/100.0;
        double getWorkLoad=workLoad*coefficient;
        scoreCoefficient.setEndWorkLoad(Double.valueOf(df.format(getWorkLoad)));
        scoreCoefficientService.modify(scoreCoefficient);
    }

private float beyond(float count,float coefficient,Float time){
        if (coefficient<0.5f){
            return 0.5f;
        }
        float si=time-4;

        if (si<4){
            return coefficient-0.025f*count;
        }else {
            coefficient=coefficient-0.025f*count;
        }
        count++;
        return beyond(count,coefficient,si);
}

    private float advance(float count1,float coefficient1,Float time){
        if (coefficient1>1.2f){
            return 1.2f;
        }
        float remainingTime=time-6;

        if (remainingTime<6){
            return coefficient1+0.025f*count1;
        }else {
            coefficient1=coefficient1 +0.025f*count1;
        }
        count1++;
        return advance(count1,coefficient1,remainingTime);
    }

private Float score(Date startTime,Date entTime){
   List<Date> dates= configService.findHolidays(new HolidayRequest(startTime,entTime));
    try {
       return WorkingTime.findOperHours(dates,startTime,entTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
return 0.0f;
}

    @Override
    public void deleteWork(String taskId, int personalWorkload, int workloadCount) {
        Work ob = workRepository.findById(taskId).get();
        ob.setPWorkload(personalWorkload);
        ob.setWorkloadCount(workloadCount);
        ob.setStatus(WorkStatus.DELETE);
        workRepository.save(ob);
    }

    @Override
    public void updateWorkCrew(String taskId, List<String> crews,Integer workload) {
        Work ob = workRepository.findById(taskId).get();
        ob.setCrewList(crews);
        ob.setWorkload(workload);
        workRepository.save(ob);
    }

    @Override
    public void updateWorkload(String taskId, int workload, float workCoefficient) {
        Work ob = workRepository.findById(taskId).get();
        ob.setWorkload(workload);
        ob.setWorkCoefficient(workCoefficient);
        workRepository.save(ob);
    }

    @Override
    public WorkIsNotCommit findBySuperoirId(String SuperoirId) {
        if (!workRepository.existsById(SuperoirId)){
            throw new NotFoundException(String.format("[job] id为 %s 的任务不存在.", SuperoirId));
        }
       List<Work> works= workRepository.findBySuperiorId(SuperoirId);
       List<String> list=new ArrayList<>();
       String isNotCommit="1";
       if (works!=null && works.size()!=0){
           for (Work work:works){
               if (!WorkStatus.FINISHED.equals(work.getStatus())){
                   isNotCommit="0";
                   list.add(work.getId());
               }
           }
           return new WorkIsNotCommit(isNotCommit,list);
       }else {
           return new WorkIsNotCommit(isNotCommit,list);
       }
    }

    @Override
    public int findBySuperoirIdCount(String SuperoirId) {
        if (!workRepository.existsById(SuperoirId)){
            throw new NotFoundException(String.format("[job] id为 %s 的任务不存在.", SuperoirId));
        }
        List<Work> works= workRepository.findBySuperiorId(SuperoirId);
        int count= workRepository.findById(SuperoirId).get().getWorkload();
        if (works!=null && works.size()!=0){
            for (Work work:works){
                if(work.getWorkloadCount()==1) {
                    count = count - work.getWorkload();
                }
            }
            return count;
        }else {
            return count;
        }

    }

    @Override
    public WorkVoMapping selectUserIdsWork(String userId) {
       List<Work> works= workRepository.findAllByExecutorIn(iUserService.findUsers(userId));
       List<WorkVo> workVos=new ArrayList<>();
        List<WorkMpping> workMppings=new ArrayList<>();
        for (Work work:works){
             if (work.getSuperiorId()==null || work.getSuperiorId().equals("")){
                 workVos.add(conversionService.workToVo(work));
                 workMppings.add(new WorkMpping(work.getId(),work.getTitle(),work.getStatus(),workMppingList(work,works)));
             }else {
                 boolean falg=true;
                 for (Work work1:works) {
                     if (work.getSuperiorId().equals(work1.getId())){
                         falg=false;
                     }
                 }
                 if (falg){
                     workVos.add(conversionService.workToVo(work));
                     workMppings.add(new WorkMpping(work.getId(),work.getTitle(),work.getStatus(),workMppingList(work,works)));
                 }
             }
        }
        return  new WorkVoMapping(workVos,workMppings);
    }


    private List<WorkMpping> workMppingList(Work work,List<Work> workList){
        List<WorkMpping> workMppings=new ArrayList<>();
       List<Work> works=new ArrayList<>();
       for (Work wo:workList){
           if (wo.getSuperiorId()!=null && wo.getSuperiorId().equals(work.getId())){
               works.add(wo);
           }
       }
       if ( works.size()==0){
           return workMppings;
       }
       for (Work work1:works){
            workMppings.add(new WorkMpping(work1.getId(),work1.getTitle(),work1.getStatus(),workMppingList(work1,workList)));
       }
       return workMppings;
    }
//    @Override
//    public List<Work> selectTimeWork() {
////        LocalDateTime minTime = LocalDateTime.now().with(LocalTime.MIN).atZone(ZoneOffset.ofHours(8)).ge;
//        workRepository.findByCompleteTimeGreaterThan(new Date());
//        return null;
//    }
}
