package com.zg.project.Inventory.Visualization.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zg.project.Inventory.Visualization.mapper.PlnMapper;
import com.zg.project.Inventory.Visualization.domain.Pln;
import com.zg.project.Inventory.Visualization.service.IPlnService;

/**
 * 计划履约Service业务层处理
 * 
 * @author zg
 * @date 2025-05-09
 */
@Service
public class PlnServiceImpl implements IPlnService 
{
    @Autowired
    private PlnMapper plnMapper;

    /**
     * 查询计划履约
     * 
     * @param id 计划履约主键
     * @return 计划履约
     */
    @Override
    public Pln selectPlnById(Long id)
    {
        return plnMapper.selectPlnById(id);
    }

    /**
     * 查询计划履约列表
     * 
     * @param pln 计划履约
     * @return 计划履约
     */
    @Override
    public List<Pln> selectPlnList(Pln pln)
    {
        return plnMapper.selectPlnList(pln);
    }

    /**
     * 新增计划履约
     * 
     * @param pln 计划履约
     * @return 结果
     */
    @Override
    public int insertPln(Pln pln)
    {
        return plnMapper.insertPln(pln);
    }

    /**
     * 修改计划履约
     * 
     * @param pln 计划履约
     * @return 结果
     */
    @Override
    public int updatePln(Pln pln)
    {
        return plnMapper.updatePln(pln);
    }

    /**
     * 批量删除计划履约
     * 
     * @param ids 需要删除的计划履约主键
     * @return 结果
     */
    @Override
    public int deletePlnByIds(Long[] ids)
    {
        return plnMapper.deletePlnByIds(ids);
    }

    /**
     * 删除计划履约信息
     * 
     * @param id 计划履约主键
     * @return 结果
     */
    @Override
    public int deletePlnById(Long id)
    {
        return plnMapper.deletePlnById(id);
    }

    @Override
    public Map<String, Object> getAll() {
        List<Pln> list = plnMapper.getAll();
        Map<String, Object> result = new HashMap<>();

        // 用来存储每月统计信息的列表
        List<Map<String, Object>> monthStatsList = new ArrayList<>();
        Map<String, Map<String, Object>> monthTempMap = new LinkedHashMap<>();

        for (Pln p : list) {
            String month = p.getIntme() != null && p.getIntme().length() >= 7 ? p.getIntme().substring(0, 7) : "未知";

            Map<String, Object> m = monthTempMap.computeIfAbsent(month, k -> {
                Map<String, Object> map = new HashMap<>();
                map.put("month", k);
                map.put("count", 0);
                map.put("htsl", 0.0);
                map.put("jhjhsl", 0.0);
                map.put("jhjhje", 0.0);
                return map;
            });

            m.put("count", (int) m.get("count") + 1);
            m.put("htsl", (double) m.get("htsl") + toDouble(p.getHtsl()));
            m.put("jhjhsl", (double) m.get("jhjhsl") + toDouble(p.getJhjhsl()));
            m.put("jhjhje", (double) m.get("jhjhje") + toDouble(p.getJhjhje()));
        }

        // 转成 list 结构返回
        monthStatsList.addAll(monthTempMap.values());
        result.put("monthStats", monthStatsList);
        return result;
    }

    private double toDouble(String val) {
        try {
            return val == null ? 0.0 : Double.parseDouble(val.trim());
        } catch (Exception e) {
            return 0.0;
        }
    }

}
