package com.loan.system.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.loan.system.context.BaseContext;
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.repository.ApproveRecordRepository;
import com.loan.system.service.ApproveService;
import org.springframework.stereotype.Service;

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

@Service
public class ApproveServiceImpl implements ApproveService {
    private final ApproveRecordRepository approveRecordRepository;

    public ApproveServiceImpl(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);
    }
}
