package com.ho.business.service.impl;

import com.ho.business.entity.VirtualDeviceCol;
import com.ho.business.service.DeviceService;
import com.ho.business.service.VirtualDeviceColService;
import com.ho.business.service.VirtualDeviceConfigService;
import com.ho.business.vo.VirtuallyModel;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gyan
 * @desc: TODO
 * @DateTime: 2024/3/5 14:56
 */
@Service
public class VirtualDeviceConfigServiceImpl implements VirtualDeviceConfigService {
    @Autowired
    VirtualDeviceColService virtualDeviceColService;
    @Autowired
    DeviceService deviceService;


    @Override
    public void getVirtualList(VirtuallyModel model) {
        String substring = model.getCol().substring(2, model.getCol().length());
        String type;
        if (CommonConstant.ONE.equals(model.getType())) {
            type = "cSingleV";
        } else {
            type = "cSingleT";
        }
        //根据 pid = src_Id  找到组个数
        List<DeviceRespVO> groupList = deviceService.selectByStationIdAndPId(model.getStationId(), model.getSrcId());
        //分组拿到srcid
        List<Integer> collect = groupList.stream().map(DeviceRespVO::getSrcId)
                .collect(Collectors.toList());
        List<VirtualDeviceCol> VirtualDeviceColList = virtualDeviceColService.selectBySrcId(model.getStationId(), collect,type);
        if (!VirtualDeviceColList.isEmpty()) {
            List<Integer> ids = VirtualDeviceColList.stream().map(VirtualDeviceCol::getId)
                    .collect(Collectors.toList());
            virtualDeviceColService.deleteByList(ids);
        }
        String substring1 = model.getColName().substring(CommonConstant.ZERO, model.getColName().length() - 1);
        int i1 = Integer.parseInt(substring) - 1;
        Integer avgNum = model.getAllNum() / groupList.size();
        //查询传过来的src和电站查询组
        DeviceRespVO deviceRespVO = null;
        if (groupList != null || !groupList.isEmpty()) {
            deviceRespVO = groupList.get(CommonConstant.ZERO);
        } else {
            throw new BusinessException(BaseResponseCode.DATA_NOT_EXISTS);
        }
        Integer id = deviceRespVO.getSrcId() + CommonConstant.ONE;
        Integer num = CommonConstant.ZERO;
        Integer colBeginId = model.getColBeginId() - 1;
        for (Integer integer = 1; integer <= groupList.size(); integer++) {
            id = id - 1;
            List<VirtualDeviceCol> list = new ArrayList<>();
            for (int i = 1; i <= avgNum; i++) {
                VirtualDeviceCol virtualDeviceCol = new VirtualDeviceCol();
                virtualDeviceCol.setStationId(model.getStationId());
                virtualDeviceCol.setSrcId(id);
                i1 = i1 + 1;
                num = num + 1;
                String col = "";
                if (i1 < 100) {
                    col = "YC00" + i1;
                } else if (i1 < 1000) {
                    col = "YC0" + i1;
                } else {
                    col = "YC" + i1;
                }
                colBeginId = colBeginId + 1;
                virtualDeviceCol.setStationId(model.getStationId());
                virtualDeviceCol.setCol(col);
                virtualDeviceCol.setDeviceColId(colBeginId);
                virtualDeviceCol.setColName(substring1 + num);
                virtualDeviceCol.setType(type + i);
                list.add(virtualDeviceCol);
            }
            virtualDeviceColService.insertList(list);
        }
    }

}
