package com.aucma.report.service.impl;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.report.mapper.BoxTemperatureHistoryMapper;
import com.aucma.report.domain.BoxTemperatureHistory;
import com.aucma.report.service.IBoxTemperatureHistoryService;

/**
 * 测温记录报表Service业务层处理
 *
 * @author Yinq
 * @date 2024-03-13
 */
@Service
public class BoxTemperatureHistoryServiceImpl implements IBoxTemperatureHistoryService
{
    @Autowired
    private BoxTemperatureHistoryMapper boxTemperatureHistoryMapper;

    /**
     * 查询测温记录报表
     *
     * @param objId 测温记录报表主键
     * @return 测温记录报表
     */
    @Override
    public BoxTemperatureHistory selectBoxTemperatureHistoryByObjId(Long objId)
    {
        return boxTemperatureHistoryMapper.selectBoxTemperatureHistoryByObjId(objId);
    }

    /**
     * 查询测温记录报表列表
     *
     * @param boxTemperatureHistory 测温记录报表
     * @return 测温记录报表
     */
    @Override
    public List<BoxTemperatureHistory> selectBoxTemperatureHistoryList(BoxTemperatureHistory boxTemperatureHistory)
    {
        return boxTemperatureHistoryMapper.selectBoxTemperatureHistoryList(boxTemperatureHistory);
    }

    /**
     * 新增测温记录报表
     *
     * @param boxTemperatureHistory 测温记录报表
     * @return 结果
     */
    @Override
    public int insertBoxTemperatureHistory(BoxTemperatureHistory boxTemperatureHistory)
    {
        return boxTemperatureHistoryMapper.insertBoxTemperatureHistory(boxTemperatureHistory);
    }

    /**
     * 修改测温记录报表
     *
     * @param boxTemperatureHistory 测温记录报表
     * @return 结果
     */
    @Override
    public int updateBoxTemperatureHistory(BoxTemperatureHistory boxTemperatureHistory)
    {
        return boxTemperatureHistoryMapper.updateBoxTemperatureHistory(boxTemperatureHistory);
    }

    /**
     * 批量删除测温记录报表
     *
     * @param objIds 需要删除的测温记录报表主键
     * @return 结果
     */
    @Override
    public int deleteBoxTemperatureHistoryByObjIds(Long[] objIds)
    {
        return boxTemperatureHistoryMapper.deleteBoxTemperatureHistoryByObjIds(objIds);
    }

    /**
     * 删除测温记录报表信息
     *
     * @param objId 测温记录报表主键
     * @return 结果
     */
    @Override
    public int deleteBoxTemperatureHistoryByObjId(Long objId)
    {
        return boxTemperatureHistoryMapper.deleteBoxTemperatureHistoryByObjId(objId);
    }

    @Override
    public HashMap<String, Object>  getTestTemp(BoxTemperatureHistory boxTemperatureHistory) {
        HashMap<String, Object> result = new HashMap<>();
//        BoxTemperatureHistory boxTemperatureHistory = new BoxTemperatureHistory();
//        cpno = "B24051383025017990384";
//        boxTemperatureHistory.setCpno(cpno);
//        boxTemperatureHistory.setCpno("B24051383025017990384");
        BoxTemperatureHistory boxTemp = boxTemperatureHistoryMapper.selectBoxTemperatureHistoryByObjId(boxTemperatureHistory.getObjId());

        //获取数据
        String tempMes = boxTemp.getTempMes();
        String powerMes = boxTemp.getPowerMes();
        Date begindatetime = boxTemp.getBegindatetime();

        //温度数据
        List<String[]> tempList = getStringList(tempMes, begindatetime);
        result.put("tempList", tempList);

        //功率数据
        List<String[]> vList = getStringList(powerMes, begindatetime);
        List<String[]> wList = new ArrayList<>();
        wList.add(vList.get(1));
        result.put("vList", wList);
        return result;
    }


    public List<String[]> getStringList(String MesStream,Date beginTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //通过分号 转成多个数组
        String xaxis[] = MesStream.split(";");
        String yaxis[] = xaxis[0].split(",");
        //将数组转为二维数组，方便存入集合
        String[][] twoArray = getTwoArray(xaxis);
        List<String[]> tempList = new ArrayList<String[]>();
        //根据二维数组每一贴获取所需的数组
        for (int i=0;i<yaxis.length;i++){
            String strings[] = new String[twoArray.length];
            for (int j=0;j<twoArray.length;j++){
                //转换时间
                if (i==0){
                    long l = beginTime.getTime() + (j * 30 * 1000);
                    //获取后八位 不要年月
//                    strings[j] =  "\""+sdf.format(l).substring(sdf.format(l).length()-8,sdf.format(l).length())+"\"";
                    strings[j] = String.valueOf(j);
                }else{
                    strings[j] = twoArray[j][i];
                }
            }
            tempList.add(strings);
        }
//        for (int i=0;i< yaxis.length;i++){
//            System.out.println(Arrays.toString(tempList.get(i)));
//        }
        return tempList;
    }
    //根据分号转为二维数组
    public String[][] getTwoArray(String[] strings){
        String[][] arrTwo = new String[strings.length][12];
        for (int i=0;i<strings.length;i++){
                arrTwo[i] = strings[i].split(",");

        }
        return  arrTwo;
    }
}
