package com.fizz.business.service.hanle;

import com.fizz.business.anno.OpcMessageHandlerType;
import com.fizz.business.constants.enums.OpcMessageType;
import com.fizz.business.domain.msg.ExitMeasureMessage;
import com.fizz.business.service.CrmPdoExcoilService;
import com.fizz.business.service.OpcMessageHandler;
import com.fizz.business.utils.MatmapUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import static com.fizz.business.constants.enums.DeviceEnum.WEIGHT;

@Component
@OpcMessageHandlerType(OpcMessageType.EXIT_MEASURE)
@RequiredArgsConstructor
@Slf4j
public class ExitMeasureHandler implements OpcMessageHandler<ExitMeasureMessage> {

    private final CrmPdoExcoilService crmPdoExcoilService;

    @Override
    public void handle(ExitMeasureMessage message) {

        // 1. 获取卷取机 (TR) 上的成品卷号
        log.info("获取到钢卷称重信息:{}.",message);
        String exitMatId = MatmapUtil.getMatId(WEIGHT.getIdx());

        if (exitMatId == null) {
            log.error("TR device has no coil. Cannot process ExitMeasureMessage.");
            return;
        }

        // 2. 调用服务层更新成品卷的实际重量
        crmPdoExcoilService.updateExitCoilActualWeight(exitMatId, message.getWeight());
    }
}
