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

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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 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
public class OrderImportTaskServiceImpl implements OrderImportTaskService {
    @Resource
    private OrderImportTaskMapper orderImportTaskMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void updateServiceExceptionTaskStatus(Long taskId, Long organId, String serviceExceptionMessage) {
        orderImportTaskMapper.update(new LambdaUpdateWrapper<OrderImportTaskDO>().eq(OrderImportTaskDO::getId, taskId)
                .set(OrderImportTaskDO::getStatus, OrderImportTaskStatusEnum.CONSUME_SUCCESS.getStatus())
                .set(OrderImportTaskDO::getImportStatus, OrderImportStatusEnum.IMPORT_FAIL.getStatus())
                .set(OrderImportTaskDO::getOrganId, organId)
                .set(OrderImportTaskDO::getResult, serviceExceptionMessage));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void updateExceptionTaskStatus(Long taskId, Long organId) {
        orderImportTaskMapper.update(new LambdaUpdateWrapper<OrderImportTaskDO>().eq(OrderImportTaskDO::getId, taskId)
                .set(OrderImportTaskDO::getStatus, OrderImportTaskStatusEnum.CONSUME_SUCCESS.getStatus())
                .set(OrderImportTaskDO::getImportStatus, OrderImportStatusEnum.IMPORT_FAIL.getStatus())
                .set(OrderImportTaskDO::getOrganId, organId)
                .set(OrderImportTaskDO::getResult, ORDER_IMPORT_FAIL.getMsg()));
    }
}
