package com.njcn.serviceimpl.point;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.njcn.mapper.configuration.DeviceDetailMapper;
import com.njcn.mapper.point.PointBaseInfoMapper;
import com.njcn.pojo.commons.device.DeviceDetail;
import com.njcn.pojo.pointInfo.LineLedger;
import com.njcn.pojo.pointInfo.LineLedgerVO;
import com.njcn.pojo.pointInfo.PointInfo;
import com.njcn.pojo.terminal.DeviceUnitPO;
import com.njcn.pojo.terminal.DeviceUnitVO;
import com.njcn.pojo.user.Depts;
import com.njcn.service.point.PointBaseInfoService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Sunwei 【sunW2016@163.com】
 * @Description:
 * @Date: Create in 11:29 2018/5/28
 * @Modified By:
 */
@Service
public class PointBaseInfoServiceImpl implements PointBaseInfoService {
    @Autowired
    private PointBaseInfoMapper pointBaseInfoMapper;
    @Autowired
    private UserUtil userUtil;

    @Autowired
    private DeviceDetailMapper deviceDetailMapper;



    @Override
    public PointInfo getPointInfo(Integer pointIndex) {
        PointInfo result = new PointInfo();
        result = pointBaseInfoMapper.getPointInfo(pointIndex);
        if (result.getDevType().lastIndexOf("_CLD") > 0) {
            PointInfo tmp = pointBaseInfoMapper.getFlowInfo(pointIndex);
            if (tmp == null) {
                result.setBaseFlow(0);
                result.setExtendFlow(0);
                result.setUseFlow(0);
            } else {
                result.setBaseFlow(tmp.getBaseFlow());
                result.setExtendFlow(tmp.getExtendFlow());
                float used = 100 * tmp.getUseFlow();
                result.setUseFlow((float) (Math.round(used * 100)) / 100);
            }
        }
        return result;
    }

    @Override
    public PointInfo getInfo(Integer lineId) {
        return pointBaseInfoMapper.getInfo(lineId);
    }

    @Override
    public Map<String, Object> getOverLimit(Integer lineId) {
        return pointBaseInfoMapper.getOverLimit(lineId);
    }

    @Override
    public List<LineLedger> getLineLedger(LineLedgerVO lineLedgerVO) {
        List<LineLedger> lineLedgerList = new ArrayList<>();
        if (CollectionUtil.isEmpty(lineLedgerVO.getAreaList())) {
            Depts depts = userUtil.getDeptsByUser(TokenManager.getToken());
            List<Integer> lineIndexes = userUtil.listLineDepts(depts.getDeptsIndex());

            if (CollectionUtil.isNotEmpty(lineIndexes)) {

                int length = lineIndexes.size();
                if (length >= 1000) {

                    int count = length / 1000 + 1;
                    for (int i = 0; i < count; i++) {
                        List<Integer> list;
                        if (length >= 1000) {
                            list = lineIndexes.subList(0, 1000);
                        } else {
                            list = lineIndexes;
                        }
                        List<LineLedger> rvoList = pointBaseInfoMapper.getLineLedger(list, lineLedgerVO.getLoadType(), lineLedgerVO.getVoltage(), lineLedgerVO.getStatus(), lineLedgerVO.getAppear(),lineLedgerVO.getPowerId());
                        if (CollectionUtil.isNotEmpty(rvoList)) {
                            lineLedgerList.addAll(rvoList);
                        }
                        list.clear();
                    }
                } else {
                    List<LineLedger> rvoList = pointBaseInfoMapper.getLineLedger(lineIndexes, lineLedgerVO.getLoadType(), lineLedgerVO.getVoltage(), lineLedgerVO.getStatus(), lineLedgerVO.getAppear(),lineLedgerVO.getPowerId());
                    lineLedgerList.addAll(rvoList);
                }


            }
        } else {

            List<Integer> lineList = new ArrayList<>();
            List<String> deptList = lineLedgerVO.getAreaList();
            for (String dept : deptList) {
                List<Integer> lineIndexes = userUtil.listLineDepts(dept);
                if (CollectionUtil.isNotEmpty(lineIndexes)) {
                    lineList.addAll(lineIndexes);
                }
            }
            if (CollectionUtil.isNotEmpty(lineList)) {
                lineList = lineList.stream().distinct().collect(Collectors.toList());
                if (CollectionUtil.isEmpty(lineLedgerVO.getLoadType())) {
                    lineLedgerVO.setLoadType(null);
                }
                if (CollectionUtil.isEmpty(lineLedgerVO.getVoltage())) {
                    lineLedgerVO.setVoltage(null);
                }
                List<LineLedger> rvoList = pointBaseInfoMapper.getLineLedger(lineList, lineLedgerVO.getLoadType(), lineLedgerVO.getVoltage(), lineLedgerVO.getStatus(), lineLedgerVO.getAppear(),lineLedgerVO.getPowerId());
                lineLedgerList.addAll(rvoList);
            }
        }

        //冀北增加内容，获取出终端的一些信息，此处偷懒，采用很慢的方式去读数据，因单体项目后期不用，故不想改动原来的sql去实现
        for (LineLedger lineLedger : lineLedgerList) {
            DeviceDetail deviceDetails = deviceDetailMapper.queryDeviceDetailByLineId(Long.parseLong(lineLedger.getLineIndex()));
            lineLedger.setDevflag(deviceDetails.getDevflag());
            lineLedger.setDevStatus(deviceDetails.getStatus());
            lineLedger.setIp(deviceDetails.getIP());
            lineLedger.setTime(deviceDetails.getTime());
            lineLedger.setDevType(deviceDetails.getDevType());
            lineLedger.setManufacturer(deviceDetails.getManufacturer());
            lineLedger.setDevLocation(deviceDetails.getDevLocation());
            lineLedger.setDevNo(deviceDetails.getDevNo());
            lineLedger.setLineStatus(deviceDetails.getLineStatus());
        }
        return lineLedgerList;
    }

    @Override
    public DeviceUnitVO lineUnitDetail(Integer lineId) {
        DeviceUnitVO deviceUnitVO = pointBaseInfoMapper.lineUnitDetail(lineId);
        if(Objects.isNull(deviceUnitVO)){
            return new DeviceUnitVO();
        }
        return deviceUnitVO;
    }

    @Override
    public DeviceUnitPO devUnitDetail(Integer devId) {
        return null;
    }
}