package com.ruoyi.mill.service.impl;

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MillProductionServiceImpl implements IMillProductionService {

    private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    @Autowired
    private UdpSender udpSender;

    // TODO: 注入 MillProductionResultMapper

    @Override
    public List<MillProductionResult> selectResultList(MillProductionResult query) {
        return new ArrayList<>();
    }

    @Override
    public MillProductionResult selectResultById(Long id) {
        return null;
    }

    @Override
    public int saveAndReport(MillProductionResult r) {
        // TODO: mapper.insert(r)
        // 保存成功后发送 K12F03
        Map<String, Object> data = new HashMap<>();
        data.put("FLAG",                  "I");
        data.put("PLAN_NO",               r.getPlanNo());
        data.put("SEQ_NO",                r.getSeqNo() == null ? 0 : r.getSeqNo());
        data.put("UNIT_CODE",             r.getUnitCode());
        data.put("PROC_SEQ_NO",           r.getProcSeqNo());
        data.put("ANNEAL_FLAG",           r.getAnnealFlag());
        data.put("IN_MAT_NO",             r.getInMatNo());
        data.put("IN_MAT_WT",             r.getInMatWt() == null ? 0 : r.getInMatWt());
        data.put("IN_MAT_THICK",          r.getInMatThick() == null ? 0f : r.getInMatThick().floatValue());
        data.put("IN_MAT_WIDTH",          r.getInMatWidth() == null ? 0f : r.getInMatWidth().floatValue());
        data.put("OUT_MAT_NO",            r.getOutMatNo());
        data.put("CUST_MAT_NO",           r.getCustMatNo());
        data.put("OUT_MAT_ACT_WT",        r.getOutMatActWt() == null ? 0 : r.getOutMatActWt());
        data.put("OUT_MAT_ACT_THICK",     r.getOutMatActThick() == null ? 0f : r.getOutMatActThick().floatValue());
        data.put("OUT_MAT_ACT_WIDTH",     r.getOutMatActWidth() == null ? 0f : r.getOutMatActWidth().floatValue());
        data.put("OUT_MAT_ACT_LEN",       r.getOutMatActLen() == null ? 0f : r.getOutMatActLen().floatValue());
        data.put("OUT_MAT_ACT_INNER_DIA", r.getOutMatActInnerDia() == null ? 0f : r.getOutMatActInnerDia().floatValue());
        data.put("OUT_MAT_ACT_OUTER_DIA", r.getOutMatActOuterDia() == null ? 0f : r.getOutMatActOuterDia().floatValue());
        data.put("PROD_SHIFT_NO",         r.getProdShiftNo());
        data.put("PROD_SHIFT_GROUP",      r.getProdShiftGroup());
        data.put("START_PROD_TIME",       r.getStartProdTime());
        data.put("END_PROD_TIME",         r.getEndProdTime());
        data.put("SOCKET_NO",             r.getSocketNo());
        data.put("REMARK",                r.getRemark());
        udpSender.sendK12F03(data);
        return 1;
    }

    @Override
    public int deleteCoilAndNotify(String planNo, Integer seqNo, String unitCode,
                                   String inMatNo, String causeDesc) {
        // TODO: mapper 软删除
        Map<String, Object> data = new HashMap<>();
        data.put("PLAN_NO",    planNo);
        data.put("SEQ_NO",     seqNo == null ? 0 : seqNo);
        data.put("UNIT_CODE",  unitCode);
        data.put("IN_MAT_NO",  inMatNo);
        data.put("CAUSE_DESC", causeDesc);
        data.put("DO_TIME",    LocalDateTime.now().format(FMT));
        udpSender.sendK12F02(data);
        return 1;
    }
}
