package com.ruoyi.mill.service.impl;

import com.ruoyi.mill.domain.MillPlan;
import com.ruoyi.mill.service.IMillPlanService;
import com.ruoyi.mill.udp.UdpSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MillPlanServiceImpl implements IMillPlanService {

    @Autowired
    private UdpSender udpSender;

    // TODO: 注入 MillPlanMapper 并替换以下占位实现

    @Override
    public List<MillPlan> selectMillPlanList(MillPlan query) {
        return new ArrayList<>();
    }

    @Override
    public MillPlan selectMillPlanByPlanNo(String planNo) {
        return null;
    }

    @Override
    public int insertMillPlan(MillPlan plan) {
        // TODO: mapper.insert(plan)
        // 写入成功后回复 K12F01 计划信息应答
        sendK12F01Ack(plan.getPlanNo(), plan.getMatSeqNo(),
                plan.getUnitCode(), plan.getInMatNo(), "1", "OK");
        return 1;
    }

    @Override
    public int updateMillPlan(MillPlan plan) {
        return 0; // TODO
    }

    @Override
    public int cancelMillPlan(String planNo, String matSeqNo) {
        // TODO: mapper 软删除
        return 1;
    }

    private void sendK12F01Ack(String planNo, String seqNo, String unitCode,
                               String inMatNo, String flag, String dealResult) {
        Map<String, Object> data = new HashMap<>();
        data.put("TC_NO",       "K12F01");
        data.put("PLAN_NO",     planNo);
        data.put("SEQ_NO",      0);
        data.put("UNIT_CODE",   unitCode);
        data.put("IN_MAT_NO",   inMatNo);
        data.put("FLAG",        flag);
        data.put("DEAL_RESULT", dealResult);
        udpSender.sendK12F01(data);
    }
}
