package com.ebo.workOrder.serviceBtnEnhance;

import cn.hutool.core.date.DateTime;
import com.ebo.entity.WorkOrderEntity;
import com.ebo.framework.common.exception.ServerException;
import com.ebo.mapper.WorkOrderMapper;
import com.ebo.module.fast.common.BtnServerEnhanceJavaInter;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.util.Map;
@Service
@Slf4j
public class SuspendBtnEnhance implements BtnServerEnhanceJavaInter {
    @Resource
    private WorkOrderMapper workOrderMapper;
    @Override
    public void executeBefore(String tableName, String buttonCode, Map data) throws ServerException {

    }

    @Override
    public void executeAfter(String tableName, String buttonCode, Map data) throws ServerException, MalformedURLException {
        log.info("-----工单挂起业务增强，记录次数和时间-----");
        String idValue = (String) data.get("id");
        WorkOrderEntity workOrderEntity = workOrderMapper.selectById(idValue);
        if(workOrderEntity.getStatus().equals("3")){
            workOrderEntity.setStatus("6");
            log.info("值为{}",workOrderEntity.getStatus());
        }else {
            throw new RuntimeException("出现异常，订单已经挂起");
        }
        if(workOrderEntity.getSuspensionCount() == null){
            workOrderEntity.setSuspensionCount(1);
        }else {
            workOrderEntity.setSuspensionCount(workOrderEntity.getSuspensionCount()+1);
        }
        workOrderEntity.setSuspensionTime(DateTime.now());
        workOrderMapper.updateById(workOrderEntity);
    }
}
