package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.entity.Step;
import com.loan.system.domain.enums.StepEnum;
import com.loan.system.domain.enums.StepPropertyEnum;
import com.loan.system.domain.vo.StepVO;
import com.loan.system.repository.StepRepository;
import com.loan.system.service.StepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class StepServiceImpl implements StepService {
    private final StepRepository stepRepository;

    @Autowired
    public StepServiceImpl(StepRepository stepRepository) {
        this.stepRepository = stepRepository;
    }

    @Override
    public List<StepVO> addStepByCaseId(Long caseId) {
        List<StepVO> list = new  ArrayList<>();//存放当前业务的流程
        List<StepPropertyEnum> stepPropertyEnums = StepPropertyEnum.listAll();//获取所有环节

        //创建初始环节
        Step step = new Step();
        step.setCaseId(caseId);
        step.setStepName(stepPropertyEnums.get(0).getLabel());
        step.setStatus(StepEnum.PROCESS.getMsg());
        step.setBeginTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        step.setParentId(0L);
        step.setIsDelete( false);

        Step step1=stepRepository.save(step);

        StepVO stepVO = BeanUtil.copyProperties(step1, StepVO.class);
        list.add(stepVO);

        Long currentParentId = step1.getId();
        for(int i = 1; i < stepPropertyEnums.size(); i++){
            StepPropertyEnum stepPropertyEnum = stepPropertyEnums.get(i);//获取当前环节

            Step s = new Step();
            s.setCaseId(caseId);
            s.setStepName(stepPropertyEnum.getLabel());
            if(i == 1){
                s.setStatus(StepEnum.PROCESS.getMsg());
                s.setBeginTime(LocalDateTime.now()
                        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            else
                s.setStatus(StepEnum.UNSTART.getMsg());

            if(stepPropertyEnum.isParent())
                s.setParentId(0L);
            else
                s.setParentId(currentParentId);
            s.setIsDelete( false);

            Step step2 = stepRepository.save(s);
            if (stepPropertyEnum.isParent())
                currentParentId = step2.getId();

            stepVO = BeanUtil.copyProperties(step2, StepVO.class);

            list.add(stepVO);
        }

        return list;
    }

    @Override
    public void updateStatusByCaseId(String msg, String stepName, Long caseId) {
        stepRepository.updateStatusByCaseIdAndStepName(msg,stepName,caseId,LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
    
    @Override
    public void cancelPreApprove(Long caseId) {
        //设置预审核阶段已完成，审批阶段开始
        updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.PRE_TRIAL_PARENT.getLabel(),caseId);
        updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.PRE_TRIAL.getLabel(),caseId);

        updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.APPROVAL_PARENT.getLabel(),caseId);
        updateStatusByCaseId(StepEnum.PROCESS.getMsg(),StepPropertyEnum.APPROVAL.getLabel(),caseId);
    }

    @Override
    public StepVO updateUserByCaseId(String stepName, Long approverId, Long caseId) {
        Step step = stepRepository.findByStepNameAndCaseId(stepName, caseId);
        if(step.getUserId1() == null)
            stepRepository.updateUser1ByCaseIdAndStepName(stepName,approverId,caseId);
        else
            if(!step.getUserId1().equals(approverId))
                stepRepository.updateUser2ByCaseIdAndStepName(stepName,approverId,caseId);

        step = stepRepository.findByStepNameAndCaseId(stepName, caseId);

        return BeanUtil.copyProperties(step, StepVO.class);

    }

    @Override
    public List<StepVO> getStepByCaseId(Long caseId) {
        List<Step> steps = stepRepository.findByCaseId(caseId);
        List<StepVO> stepVOS = new ArrayList<>();
        for (Step step : steps) {
            StepVO stepVO = BeanUtil.copyProperties(step, StepVO.class);
            stepVOS.add(stepVO);
        }
        return stepVOS;
    }

    @Override
    public StepVO findByStepNameAndCaseId(String stepName,Long caseId) {
        return BeanUtil.copyProperties(stepRepository.findByStepNameAndCaseId(stepName, caseId), StepVO.class);
    }

    @Override
    public void updateUserId1ByCaseIdAndStepName(String stepName, Long approverId, Long caseId){

        stepRepository.updateUser1ByCaseIdAndStepName(stepName,approverId,caseId,LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    };

    @Override
    public void updateUserId2ByCaseIdAndStepName(String stepName, Long approverId, Long caseId){
        stepRepository.updateUser2ByCaseIdAndStepName(stepName,approverId,caseId,LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    };

    public void logic_delete(Long id){
        stepRepository.logic_delete(id,true,LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}