package com.fizz.business.service.hanle;

import com.fizz.business.anno.OpcMessageHandlerType;
import com.fizz.business.constants.enums.L1OperateMatEnum;
import com.fizz.business.constants.enums.OpcMessageType;
import com.fizz.business.domain.CrmPdiPlan;
import com.fizz.business.domain.msg.EntryMovementMessage;
import com.fizz.business.dto.MatmapDTO;
import com.fizz.business.form.L1OperateMatForm;
import com.fizz.business.service.CrmPdiPlanService;
import com.fizz.business.service.OpcMessageHandler;
import com.fizz.business.service.TrackService;
import com.fizz.business.utils.MatmapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
@OpcMessageHandlerType(OpcMessageType.ENTRY_MOVEMENT)
public class EntryMovementHandler implements OpcMessageHandler<EntryMovementMessage> {


    @Autowired
    private CrmPdiPlanService crmPdiPlanService;

    @Autowired
    private TrackService trackService;

    @Override
    public void handle(EntryMovementMessage message) {
        Integer src = message.getMaterialPlaceSource();
        Integer des = message.getMaterialPlaceDestination();

        String action = resolveAction(src, des);

        log.info("处理入口移动消息: " + message);
        if (action != null) {
            log.info("识别到动作: " + action);

            // 如果是上卷动作
            if (action.contains("上卷")) {
                CrmPdiPlan coilPlan = crmPdiPlanService.getFirstUnProducedCoil();
                if (coilPlan != null) {
                    log.info("获取到未生产的钢卷: " + coilPlan.getCoilid());
                    trackService.l1OperateMat(L1OperateMatForm.builder()
                            .entryMatId(coilPlan.getCoilid())
                            .operation(L1OperateMatEnum.ONLINE)
                            .planId(coilPlan.getPlanid())
                            .porIdx(des)
                            .build());
                } else {
                    log.error("未找到可上卷的钢卷计划！");
                }
            }else if (action.contains("甩尾")){

                MatmapDTO matmap = MatmapUtil.getMatmap(src);

                log.info("获取到甩尾信息: " + matmap.getMatId());
                trackService.l1OperateMat(L1OperateMatForm.builder()
                        .entryMatId(matmap.getMatId())
                        .operation(L1OperateMatEnum.PAY_OVER)
                        .planId(matmap.getPlanId())
                        .porIdx(src)
                        .build());
            }
        } else {
            log.error("未识别的组合: SRC=" + src + ", DES=" + des);
        }
    }

    private String resolveAction(Integer src, Integer des) {
        if (src == null || des == null) {
            return null;
        }

        if (src == 100 && des == 0) {
            return "1#开卷机上卷";
        } else if (src == 100 && des == 1) {
            return "2#开卷机上卷";
        } else if (src == 0 && des == 200) {
            return "1#开卷机甩尾";
        } else if (src == 1 && des == 200) {
            return "2#开卷机甩尾";
        }
        return null;
    }
}