package com.ho.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.business.entity.DeviceTypeConfig;
import com.ho.business.entity.ModelDevice;
import com.ho.business.entity.ModelType;
import com.ho.business.mapper.ModelTypeMapper;
import com.ho.business.service.ModelDeviceService;
import com.ho.business.service.ModelTypeService;
import com.ho.business.vo.req.modelType.ModelTypeAddReq;
import com.ho.business.vo.req.modelType.ModelTypeQueryReq;
import com.ho.business.vo.resp.StationRespVO;
import com.ho.business.vo.resp.modelType.ModelTypeResp;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author gyan
 * @desc: TODO
 * @DateTime: 2023/3/8 11:34
 */
@Service
@Slf4j
public class ModelTypeServiceImpl implements ModelTypeService {
    @Autowired
    ModelTypeMapper ModelTypeMapper;

    @Autowired
    ModelDeviceService modelDeviceService;

    @Autowired
    DeviceTypeConfigServiceImpl deviceTypeConfigService;

    @Override
    public int insetModelType(ModelType modelType) {
        //新增的时候要看下数据库中有没有相同的model_type，若存在，则不用新增
        List<ModelType> modelTypeValue = ModelTypeMapper.getModelType(modelType.getModelType());
        if(!modelTypeValue.isEmpty()){
            throw new BusinessException(BaseResponseCode.STATION_ALREADY_EXISTS);
        }
        int result = ModelTypeMapper.insertModelType(modelType);
        return result;
    }

    @Override
    public int insetModelDevice(ModelDevice modelDevice) {
        int result = ModelTypeMapper.insertModelDevice(modelDevice);
        return result;
    }

    @Override
    public void updateModelDevice(ModelDevice modelDevice) {
        ModelTypeMapper.updateModelDevice(modelDevice);
    }

    @Override
    public int deleteModelDevice(List<Integer> ids) {
        int count = ModelTypeMapper.deleteModelDevice(ids);
        return count;
    }

    @Override
    public PageResult<ModelTypeResp> selectPage(ModelTypeQueryReq vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        ModelType modelType = new ModelType();
        modelType.setModelType(vo.getModelType());
        modelType.setModelName(vo.getModelName());
        List<ModelType> modelTypeList = ModelTypeMapper.getModelTypeList(modelType);
        return PageUtils.getPageResult(new PageInfo<>(modelTypeList));
    }

    @Override
    public ModelType selectByModelType(String modelType) {
        ModelType byModelType = ModelTypeMapper.selectByModelType(modelType);
        return byModelType;
    }

    @Override
    public List<ModelType> getModelType() {
        List<ModelType> list = ModelTypeMapper.getModelType(null);
        return list;
    }


    @Override
    public List<ModelDevice> getDeviceType(String modelType) {
        List<ModelDevice> list = ModelTypeMapper.getDeviceType(modelType);
        return list;
    }

    @Override
    public List<ModelTypeAddReq> selectDeviceType(ModelTypeAddReq vo) {
        List<ModelTypeAddReq> addReqList = new ArrayList<>();
        List<ModelDevice> list = new ArrayList<>();
        if (vo.getModelType() != null) {
            list = modelDeviceService.selectByModelTypes(vo.getModelType());
        }
        for (ModelDevice modelDevice : list) {
            ModelTypeAddReq modelTypeAddReq = new ModelTypeAddReq();
            //根据deviceType 查询 device_type_config表中的name
            List<DeviceTypeConfig> deviceTypeConfigs = deviceTypeConfigService.selectByType(modelDevice.getDeviceType(), null);
            //将返回结果进行过滤
            List<DeviceTypeConfig> arrays = deviceTypeConfigs.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                    -> new TreeSet<>(Comparator.comparing(DeviceTypeConfig::getDeviceType))), ArrayList::new));
            modelTypeAddReq.setModelType(modelDevice.getModelType());
            if(!arrays.isEmpty()){
                modelTypeAddReq.setName(arrays.get(0).getName());
            }
            modelTypeAddReq.setDeviceType(modelDevice.getDeviceType());
            modelTypeAddReq.setModelName(modelDevice.getDeviceTypeName());
            modelTypeAddReq.setId(modelDevice.getId());
            addReqList.add(modelTypeAddReq);
        }
        return addReqList;
    }

    @Override
    public List<DeviceTypeConfig> selectList(ModelTypeAddReq vo) {
        List<DeviceTypeConfig> deviceTypeConfigs = new ArrayList<>();
        if (vo.getStationId() != null) {
            deviceTypeConfigs = deviceTypeConfigService.selectByType(null, vo.getStationId());
        }
        return deviceTypeConfigs;
    }

    @Override
    public ModelDevice selectByDict(String deviceType, String modelType) {
        ModelDevice modelDevice = ModelTypeMapper.selectByDict(deviceType,modelType);
        return modelDevice;
    }
}
