package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.domain.dto.ApprovalRecordDTO;
import com.loan.system.domain.entity.ApprovalRecord;
import com.loan.system.domain.enums.StepPropertyEnum;
import com.loan.system.domain.vo.ApprovalRecordVO;
import com.loan.system.repository.ApproveRecordRepository;
import com.loan.system.service.ApprovalService;
import org.springframework.stereotype.Service;

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

@Service
public class ApprovalServiceImpl implements ApprovalService {
    private final ApproveRecordRepository approveRecordRepository;

    public ApprovalServiceImpl(ApproveRecordRepository approveRecordRepository) {
        this.approveRecordRepository = approveRecordRepository;
    }
    @Override
    public void addApprovalRecord(ApprovalRecordDTO approvalRecordDTO) {
        ApprovalRecord approvalRecord = BeanUtil.copyProperties(approvalRecordDTO, ApprovalRecord.class);
        approvalRecord.setIsDelete( false);
        approvalRecord.setCreateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        approvalRecord.setUpdateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        approveRecordRepository.save(approvalRecord);
    }

    @Override
    public void addPreApprovalRecord(String label, Long currentId, Long caseId) {
        ApprovalRecord approvalRecord = new ApprovalRecord();
        approvalRecord.setCaseId(caseId);
        approvalRecord.setStepName(StepPropertyEnum.PRE_TRIAL.getLabel());
        approvalRecord.setApproverId(currentId);
        approvalRecord.setDecision("通过");
        approvalRecord.setComments("跳过审批环节");
        approvalRecord.setIsDelete( false);
        approvalRecord.setCreateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        approvalRecord.setUpdateTime(LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        approveRecordRepository.save(approvalRecord);
    }
    @Override
    public List<ApprovalRecordVO> findByCaseIdAndIsDelete(Long id){
        return BeanUtil.copyToList(approveRecordRepository.findByCaseIdAndIsDelete(id, false), ApprovalRecordVO.class);
    }

    public void save(ApprovalRecord approvalRecord){
        approveRecordRepository.save(approvalRecord);
    }

    public void updateDecisionByCaseIdAndStepNameAndIsDelete(String decision, Long caseId, String stepName,String  comments,Long approverId){
        approveRecordRepository.updateDecisionByCaseIdAndStepNameAndIsDeleteAndApproverId(decision, caseId, stepName, false, java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),comments,approverId);
    }

    public ApprovalRecordVO findByCaseIdAndIsDeleteAndStepName(Long caseId, String stepName, Long approverId){
        return BeanUtil.copyProperties(approveRecordRepository.findByCaseIdAndIsDeleteAndStepNameAndApproverId(caseId, false, stepName, approverId), ApprovalRecordVO.class);
    }
}
