package com.yfd.platform.modules.auxcontrol.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.modules.auxcontrol.domain.DeviceWorkData;
import com.yfd.platform.modules.auxcontrol.mapper.DeviceSignalMapper;
import com.yfd.platform.modules.auxcontrol.mapper.DeviceWorkDataMapper;
import com.yfd.platform.modules.auxcontrol.service.IDeviceWorkDataService;
import com.yfd.platform.utils.FileUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 变电站-设备-运行数据 服务实现类
 * </p>
 *
 * @author zhengsl
 * @since 2025-04-23
 */
@Service
public class DeviceWorkDataServiceImpl extends ServiceImpl<DeviceWorkDataMapper, DeviceWorkData> implements IDeviceWorkDataService {

    @Resource
    private DeviceSignalMapper deviceSignalMapper;
    @Resource
    private DeviceWorkDataMapper deviceWorkDataMapper;

    /**********************************
     * 用途说明: 导出变电站设备运行数据
     * 参数说明
     * deviceWorkDatas    所需导出的变电站设备运行数据项集合
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回导出成功或者失败
     ***********************************/
    @Override
    public void insertData(String from, String slaveIp, String address, String value, String dateTimeString) {
        DeviceWorkData workdata = new DeviceWorkData();
        if ("IEC61850".equals(from)) {
            slaveIp = null;
        }
        Map<String, Object> map = deviceSignalMapper.selectDeviceSignalMap(slaveIp, "yc", address);
        if (map == null || ObjUtil.isEmpty(map)) {
            return;
        }
        workdata.setId(IdUtil.fastSimpleUUID());
        workdata.setSystemcode(map.get("systemcode").toString());
        workdata.setStationId(map.get("stationId").toString());
        workdata.setDeviceId(map.get("meterDeviceId").toString());
        workdata.setDeviceName(map.get("deviceName").toString());
        workdata.setSignalId(map.get("signalId").toString());
        workdata.setSignalName(map.get("signalName").toString());
        workdata.setUnit(ObjUtil.isNotEmpty(map.get("signalUnit")) ? map.get("signalUnit").toString() : "");
        workdata.setValue(value);
        if (StrUtil.isEmpty(dateTimeString) || "null".equals(dateTimeString)) {
            workdata.setStartTime(LocalDateTime.now());
        } else {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
            LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);
            workdata.setStartTime(localDateTime);
        }
        this.saveOrUpdate(workdata);

    }

    /**********************************
     * 用途说明: 分页查询变电站设备运行记录
     * 参数说明 page 分页参数
     * 参数说明 signalId 信号Id
     * 参数说明 startDate 开始时间
     * 参数说明 endDate 结束时间
     * 返回值说明: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.yfd.platform.modules.auxcontrol.domain.DeviceWorkData>
     ***********************************/
    @Override
    public Page<DeviceWorkData> getDeviceWorkDataPage(Page<DeviceWorkData> page,String signalId, String startDate, String endDate) {
        return deviceWorkDataMapper.getDeviceWorkDataPage(page,signalId,startDate,endDate);
    }

    /**********************************
     * 用途说明: 查询历史曲线
     * 参数说明 signalId 信号id
     * 返回值说明: java.util.List<com.yfd.platform.modules.auxcontrol.domain.DeviceWorkData>
     ***********************************/
    @Override
    public List<DeviceWorkData> getHistoricalCurve(String signalId) {
        return deviceWorkDataMapper.getHistoricalCurve(signalId);
    }

    @Override
    public List<DeviceWorkData> getHistoricalCurveList(List<String> signalIdList) {
        return deviceWorkDataMapper.getHistoricalCurveList(signalIdList);
    }

    @Override
    public List<Map<String, Object>> getDeviceWorkData(String stationId) {
        return deviceWorkDataMapper.getDeviceWorkData(stationId);
    }

    @Override
    public void exportDeviceWorkData(String stationId,String signalId, String startDate, String endDate, HttpServletResponse response) throws IOException {

        LambdaQueryWrapper<DeviceWorkData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceWorkData::getSignalId,signalId).eq(DeviceWorkData::getStationId, stationId).le(DeviceWorkData::getStartTime, endDate).ge(DeviceWorkData::getStartTime, startDate).select(DeviceWorkData::getStartTime, DeviceWorkData::getValue, DeviceWorkData::getUnit).orderByDesc(DeviceWorkData::getStartTime);
        List<DeviceWorkData> list = this.list(queryWrapper);
        List<Map<String, Object>> listMaps = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new LinkedHashMap<>();
            DeviceWorkData deviceWorkData = list.get(i);
            map.put("序号", (i + 1));
            map.put("采集时间", deviceWorkData.getStartTime());
            map.put("监测数值", deviceWorkData.getValue());
            map.put("单位", deviceWorkData.getUnit());
            listMaps.add(map);
        }
        FileUtil.downloadExcel(listMaps, response);
    }

}
