package com.ecep.contract.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.WebSocketClientTasker;
import com.ecep.contract.vo.InventoryVo;

import lombok.Setter;

public class InventorySyncTask extends Tasker<Object> implements WebSocketClientTasker {
    private static final Logger logger = LoggerFactory.getLogger(InventorySyncTask.class);

    @Setter
    private InventoryVo inventory;

    @Override
    public String getTaskName() {
        return "InventorySyncTask";
    }

    @Override
    public void updateProgress(long current, long total) {
        super.updateProgress(current, total);
    }

    @Override
    protected Object execute(MessageHolder holder) throws Exception {
        try {
            // 设置任务标题
            updateTitle("库存同步任务");

            // 更新任务消息
            holder.info("开始同步库存数据...");

            if (inventory == null) {
                holder.warn("请选择要同步的数据.");
                return null;
            }
            if (!StringUtils.hasText(inventory.getCode())) {
                holder.warn("请填写商品编码.");
                return null;
            }

            // 调用远程WebSocket任务
            return callRemoteTask(holder, getLocale(), inventory.getId());
        } catch (Exception e) {
            logger.error("库存同步任务执行失败", e);
            updateMessage("库存同步任务执行失败：" + e.getMessage());
            throw e;
        }
    }
}
