package com.fizz.business.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fizz.business.constants.enums.DeviceEnum;
import com.fizz.business.mapper.PlanDashboardMapper;
import com.fizz.business.mapper.SegmentTotalMapper;
import com.fizz.business.service.DashboardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 首页仪表板统计服务实现
 */
@Service
public class DashboardServiceImpl implements DashboardService {

    @Resource
    private PlanDashboardMapper planDashboardMapper;

    @Resource
    private SegmentTotalMapper segmentTotalMapper;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Map<String, Object> getCurrentProducingPlan() {
        // 查询当前 PRODUCING 的计划
        Map<String, Object> plan = planDashboardMapper.selectCurrentProducingPlan();
        if (plan == null) {
            return Collections.emptyMap();
        }
        return plan;
    }

    @Override
    public Map<String, Object> getCurrentProcessParams() {
        Map<String, Object> result = new HashMap<>();

        // 1. 当前生产计划
        Map<String, Object> plan = planDashboardMapper.selectCurrentProducingPlan();
        if (plan == null || plan.get("coilid") == null) {
            return result;
        }
        String coilId = String.valueOf(plan.get("coilid"));
        result.put("coilId", coilId);

        // 2. 查询该卷最新一段的 total_values_json
        String totalValuesJson = segmentTotalMapper.selectLatestTotalValuesJsonByCoilId(coilId);
        if (totalValuesJson == null || totalValuesJson.isEmpty()) {
            return result;
        }

        try {
            // 3. 解析 JSON -> Map<String, Object>
            Map<String, Object> valuesMap = objectMapper.readValue(
                    totalValuesJson,
                    new TypeReference<Map<String, Object>>() {}
            );

            // 4. 按设备 + 段类型整理数据
            Map<String, Object> entrySection   = new LinkedHashMap<>();
            Map<String, Object> processSection = new LinkedHashMap<>();
            Map<String, Object> exitSection    = new LinkedHashMap<>();

            for (DeviceEnum device : DeviceEnum.values()) {
                List<String> fields = device.getParamFields();
                if (fields == null || fields.isEmpty()) {
                    continue;
                }

                Map<String, Object> devData = new LinkedHashMap<>();
                for (String field : fields) {
                    if (valuesMap.containsKey(field)) {
                        devData.put(field, valuesMap.get(field));
                    }
                }

                if (devData.isEmpty()) {
                    continue;
                }

                // key 用设备英文枚举名，如 POR1/FUR1/TM/TL/COAT 等
                String key = device.name();
                switch (device.getSectionType()) {
                    case ENTRY:
                        entrySection.put(key, devData);
                        break;
                    case PROCESS:
                        processSection.put(key, devData);
                        break;
                    case EXIT:
                        exitSection.put(key, devData);
                        break;
                    default:
                        break;
                }
            }

            result.put("entrySection", entrySection);
            result.put("processSection", processSection);
            result.put("exitSection", exitSection);
        } catch (Exception e) {
            // 解析异常时，可按你项目的日志方案记录
            e.printStackTrace();
        }

        return result;
    }
}

