package com.ho.business.service.impl;


import com.ho.business.entity.DeviceTypeCol;
import com.ho.business.entity.ModelDevice;
import com.ho.business.entity.ModelDeviceCol;
import com.ho.business.entity.ModelDeviceColComp;
import com.ho.business.mapper.DeviceTypeColMapper;
import com.ho.business.mapper.ModelDeviceColMapper;
import com.ho.business.service.ModelDeviceService;
import com.ho.business.vo.req.DeviceTypeColReqVO;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author gyan
 * @description 针对 数据模板 的数据库操作Service实现
 * @createDate 2023-02-09 18:04:50
 */
@Service
@Slf4j
public class ModelDeviceServiceImpl implements ModelDeviceService {

    @Autowired
    ModelDeviceColMapper modelDeviceColMapper;

    @Autowired
    DeviceTypeColMapper deviceTypeColMapper;

    @Autowired
    RedisService redisService;

    /**
     * 根据deviceType 查 设备字段定义的List
     *
     * @param deviceType
     * @return
     */
    @Override
    public List<DeviceTypeCol> selectDeviceColsByDeviceType(String deviceType) {
        List<DeviceTypeCol> deviceTypeCols = new ArrayList<>();
        List<Integer> deviceColIds = new ArrayList<>();
        List<ModelDeviceCol> modelDeviceCols = modelDeviceColMapper.selectByDeviceType(deviceType);
        for (ModelDeviceCol modelDeviceCol : modelDeviceCols) {
            deviceColIds.add(modelDeviceCol.getDeviceColId());
        }
        if (!deviceColIds.isEmpty()) {
            deviceTypeCols = selectDeviceColsByIds(deviceColIds);
        }
        return deviceTypeCols;
    }


    /**
     * 根据deviceType 查 设备模型字段定义的List
     *
     * @param deviceType
     * @return
     */
    @Override
    public List<ModelDeviceCol> selectModelColsByDeviceType(String deviceType) {
        List<ModelDeviceCol> modelCols = new ArrayList<>();
        List<Integer> modelColIds = new ArrayList<>();
        List<ModelDeviceCol> modelDeviceCols = modelDeviceColMapper.selectByDeviceType(deviceType);
        for (ModelDeviceCol modelDeviceCol : modelDeviceCols) {
            modelColIds.add(modelDeviceCol.getModelColId());
        }
        if (!modelColIds.isEmpty()) {
            modelCols = selectModelByIds(modelColIds);
        }
        return modelCols;
    }

    //在model_device_col表中根据deviceType查询
    @Override
    public List<ModelDeviceCol> selectByModelType(String deviceType) {
        List<ModelDeviceCol> modelDeviceCols = modelDeviceColMapper.selectByDeviceType(deviceType);
        return modelDeviceCols;
    }


    public List<ModelDeviceCol> selectModelByIds(List<Integer> modelColIds) {
        List<ModelDeviceCol> modelDeviceCols = modelDeviceColMapper.selectByIds(modelColIds);
        if (modelDeviceCols.isEmpty() || modelDeviceCols == null) {
            return new ArrayList<>();
        }
        return modelDeviceCols;
    }


    //在device_type_col表中根据deviceType查询
    @Override
    public List<DeviceTypeCol> selectByDeviceType(String deviceType) {
        String key = RedisKeyConstant.DEVICE_TYPE_COL + deviceType;
        List<DeviceTypeCol> deviceTypeCols =null;
        if(redisService.hasKey(key)){
            deviceTypeCols =(List<DeviceTypeCol>) redisService.get(key);
        }else{
            deviceTypeCols = deviceTypeColMapper.selectByDeviceType(deviceType,null);
            redisService.set(key,deviceTypeCols);
            redisService.expire(key,1, TimeUnit.HOURS);
        }
        return deviceTypeCols;
    }

    @Override
    public List<DeviceTypeCol> selectByDeviceTypeList(DeviceTypeColReqVO deviceTypeColReqVO) {
        List<DeviceTypeCol> deviceTypeCols = deviceTypeColMapper.selectByDeviceTypeList(deviceTypeColReqVO);
        return deviceTypeCols;
    }

    @Override
    public List<DeviceTypeCol> selectByDeviceTypeAndIsShow(String deviceType, Integer isShow) {
        List<DeviceTypeCol> deviceTypeCols = deviceTypeColMapper.selectByDeviceTypeAndIsShow(deviceType, isShow);
        return deviceTypeCols;
    }

    //根据deviceColIds查询设备列表
    @Override
    public List<DeviceTypeCol> selectDeviceColsByIds(List<Integer> deviceColIds) {
        List<DeviceTypeCol> deviceTypeCols = deviceTypeColMapper.selectByIds(deviceColIds);
        if (deviceTypeCols.isEmpty() || deviceTypeCols == null) {
            return new ArrayList<>();
        }
        return deviceTypeCols;
    }

    /**
     * 根据设备类型查询复合体列表
     *
     * @param type     device 设备 model 模型
     * @param typeName typeName: 设备或模型的具体类型 , 例如　inverter_huawei  或者 inverter
     * @param modelCol 模型 col
     * @return
     */
    @Override
//    @Cacheable(cacheManager = "fiveMinuteCacheManager", value = "modelConfigList", sync = true)
    public List<ModelDeviceColComp> getCompListByType(String type, String typeName, String modelCol) {
        //因为几个表数据量不大, 做个left级联查询
        List<ModelDeviceColComp> compListByType = deviceTypeColMapper.getCompListByType(type, typeName, modelCol);
        return compListByType;
    }

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