package com.cf.imes.module.plan.service.orderImport;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.util.i18n.core.util.I18nUtils;
import com.cf.imes.module.executor.enums.OrderImportStatusEnum;
import com.cf.imes.module.executor.enums.OrderImportTaskStatusEnum;
import com.cf.imes.module.plan.dal.dataobject.orderImport.OrderImportTaskDO;
import com.cf.imes.module.plan.dal.mysql.orderImport.OrderImportTaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;

import static com.cf.imes.module.plan.enums.ErrorCodeConstants.ORDER_IMPORT_FAIL;

/**
 * @author Gqr
 * @since 2025/5/13 16:41
 */
@Service
@Slf4j
public class OrderImportTaskServiceImpl implements OrderImportTaskService {
    @Resource
    private OrderImportTaskMapper orderImportTaskMapper;

    @Resource
    private I18nUtils i18nUtils;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public OrderImportTaskDO confirmOrderImport(Long taskId) {
        OrderImportTaskDO orderImportTaskDO = orderImportTaskMapper.selectById(taskId);
        if (ObjectUtil.isNotNull(orderImportTaskDO)) {
            // 更新状态为消创建接收成功
            orderImportTaskMapper.update(new LambdaUpdateWrapper<OrderImportTaskDO>().eq(OrderImportTaskDO::getId, taskId).set(OrderImportTaskDO::getStatus, OrderImportTaskStatusEnum.CREATE_RECEIVE.getStatus()));
        } else {
            String taskNotExistNotify = String.format("%s不存在的导入任务，消费中止", taskId);
            // 更新状态为消费成功，导入失败
            orderImportTaskMapper.update(
                    new LambdaUpdateWrapper<OrderImportTaskDO>()
                            .eq(OrderImportTaskDO::getId, taskId)
                            .set(OrderImportTaskDO::getStatus, OrderImportTaskStatusEnum.CREATE_RECEIVE.getStatus())
                            .set(OrderImportTaskDO::getImportStatus, OrderImportStatusEnum.IMPORT_FAIL.getStatus())
                            .set(OrderImportTaskDO::getResult, taskNotExistNotify)
            );
            log.error(String.format("====================【%s】====================", taskNotExistNotify));
        }
        return orderImportTaskDO;
    }

    /**
     * 更新导入任务为失败
     *
     * @param orderImportTaskDO
     * @param ex
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void updateImportTaskFail(OrderImportTaskDO orderImportTaskDO, Exception ex) {
        if (orderImportTaskDO == null) {
            return;
        }
        String msg;
        if (ex instanceof ServiceException) {
            msg = i18nUtils.getMessage(ex.getMessage());
        } else {
            msg = ORDER_IMPORT_FAIL.getMsg();
        }
        orderImportTaskMapper.update(
                new LambdaUpdateWrapper<OrderImportTaskDO>()
                        .eq(OrderImportTaskDO::getId, orderImportTaskDO.getId())
                        .set(OrderImportTaskDO::getStatus, OrderImportTaskStatusEnum.CREATE_RECEIVE.getStatus())
                        .set(OrderImportTaskDO::getImportStatus, OrderImportStatusEnum.IMPORT_FAIL.getStatus())
                        .set(OrderImportTaskDO::getResult, msg)
        );
    }
}
