package com.njcn.product.terminal.mysqlTerminal.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.common.utils.PubUtils;

import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;

import com.njcn.product.terminal.mysqlTerminal.mapper.LineMapper;
import com.njcn.product.terminal.mysqlTerminal.pojo.dto.DeviceType;
import com.njcn.product.terminal.mysqlTerminal.pojo.param.DeviceInfoParam;
import com.njcn.product.terminal.mysqlTerminal.pojo.po.Line;

import com.njcn.product.terminal.mysqlTerminal.service.TerminalBaseService;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.HttpURLConnection;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * pqs
 *
 * @author cdf
 * @date 2022/1/4
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TerminalBaseServiceImpl extends ServiceImpl<LineMapper, Line> implements TerminalBaseService {

    private final LineMapper lineMapper;






    @Override
    public List<Line> getSubstationById(List<String> list) {
        return this.lambdaQuery().in(Line::getId, list).list();
    }

    @Override
    public List<Line> getLineById(List<String> lineIds) {
        return Collections.emptyList();
    }

    @Override
    public List<Line> getLineByCondition(List<String> ids, DeviceInfoParam deviceInfoParam) {
        return this.baseMapper.getLineByCondition(ids, deviceInfoParam);
    }

    @Override
    public List<Line> getDeviceById(List<String> devIds, DeviceType deviceType) {
        return Collections.emptyList();
    }

    @Override
    public List<Line> getDeviceByCondition(List<String> devIds, DeviceType deviceType, List<SimpleDTO> manufacturer) {
        return this.baseMapper.getDeviceByCondition(devIds, deviceType, manufacturer);
    }

    @Override
    public List<Line> getVoltageByCondition(List<String> voltageIds, List<SimpleDTO> scale) {
        return this.baseMapper.getVoltageByCondition(voltageIds, scale);
    }

    @Override
    public List<Line> getSubByCondition(List<String> subIds, List<SimpleDTO> scale) {
        return this.baseMapper.getSubByCondition(subIds, scale);
    }
/*

    @Override
    public List<String> getVoltageIdByScale(List<String> voltageIds, String scale) {
        return this.baseMapper.getVoltageIdByScale(voltageIds, scale);
    }
*/

    @Override
    public List<String> getSubIdByScale(List<String> subIds, String scale) {
        return this.baseMapper.getSubIdByScale(subIds, scale);
    }

    @Override
    public List<String> getLineIdByLoadType(List<String> lineIds, String loadType) {
        return this.baseMapper.getLineIdByLoadType(lineIds, loadType);
    }

    @Override
    public List<String> getDeviceIdByManufacturer(List<String> deviceIds, String manufacturer) {
        return this.baseMapper.getDeviceIdByManufacturer(deviceIds, manufacturer);
    }

    @Override
    public List<String> getDeviceIdByPowerFlag(List<String> lineIds, Integer manufacturer) {
        return this.baseMapper.getDeviceIdByPowerFlag(lineIds, manufacturer);
    }








    /**
     * 根据条件查询出台账信息
     *
     * @param name  名称
     * @param pid   父id
     * @param level 层级
     * @param state 状态 0 删除 1 正常
     * @return 终端信息
     */
    private Line queryLine(LambdaQueryWrapper<Line> lineLambdaQueryWrapper, String name, String pid, Integer level, Integer state) {
        lineLambdaQueryWrapper.clear();
        lineLambdaQueryWrapper.eq(Line::getName, name)
                .eq(Line::getPid, pid)
                .eq(Line::getLevel, level)
                .eq(Line::getState, state);
        return this.baseMapper.selectOne(lineLambdaQueryWrapper);
    }

    /**
     * 组装台账信息，稍后入库
     *
     * @param name  名称
     * @param level 等级
     * @param pid   父ID
     * @param pids  上层所有ID
     */
    private Line assembleLine(String name, Integer level, String pid, List<String> pids) {
        Line line = new Line();
        line.setName(name);
        line.setLevel(level);
        line.setPid(pid);
        line.setPids(String.join(",", pids));
        line.setSort(0);
        line.setState(DataStateEnum.ENABLE.getCode());
        return line;
    }

    private Line assembleLine(String name, Integer level, String pid, String pids, Integer sort) {
        Line line = new Line();
        line.setName(name);
        line.setLevel(level);
        line.setPid(pid);
        line.setPids(pids);
        line.setSort(sort);
        line.setState(DataStateEnum.ENABLE.getCode());
        return line;
    }



}
