package cn.iocoder.yudao.module.iot.service.devicecontactmodel;

import cn.iocoder.yudao.module.iot.dal.dataobject.devicecontactmodel.DeviceContactModelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodelattribute.DeviceModelAttributeDO;
import cn.iocoder.yudao.module.iot.dal.mysql.deviceattributetype.DeviceAttributeTypeMapper;
import cn.iocoder.yudao.module.iot.service.device.TDengineService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.iot.controller.admin.devicecontactmodel.vo.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.iot.dal.mysql.devicecontactmodel.DeviceContactModelMapper;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.DEVICE_CONTACT_MODEL_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_MODEL_ATTRIBUTE_POTIN_CODE_EXISTS;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_MODEL_POINT_CODE_EXISTS;

/**
 * 采集设备模型-点位管理 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class DeviceContactModelServiceImpl implements DeviceContactModelService {

    @Resource
    private DeviceContactModelMapper deviceContactModelMapper;
    @Resource
    private DeviceAttributeTypeMapper deviceAttributeTypeMapper;
    @Resource
    private TDengineService tDengineService;

    @Override
    public Long createDeviceContactModel(DeviceContactModelSaveReqVO createReqVO) {


        //判断编码是否唯一
        boolean exists = deviceContactModelMapper.exists(
                Wrappers.<DeviceContactModelDO>lambdaQuery()
                        .eq(DeviceContactModelDO::getDeviceId, createReqVO.getDeviceId())
                        .eq(DeviceContactModelDO::getAttributeCode, createReqVO.getAttributeCode()));

        if (exists) {
            throw exception(DEVICE_MODEL_POINT_CODE_EXISTS);
        }

        // 插入
        DeviceContactModelDO deviceContactModel = BeanUtils.toBean(createReqVO, DeviceContactModelDO.class);
//        deviceContactModel.setTypeName(deviceAttributeTypeMapper.selectById(createReqVO.getAttributeCode()).getName());
        deviceContactModelMapper.insert(deviceContactModel);

        //新增td数据库列
        tDengineService.AddTDDatabaseColumn(createReqVO.getDeviceId(),deviceContactModel);

        // 返回
        return deviceContactModel.getId();
    }

    @Override
    public void updateDeviceContactModel(DeviceContactModelSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceContactModelExists(updateReqVO.getId());
        // 更新
        DeviceContactModelDO updateObj = BeanUtils.toBean(updateReqVO, DeviceContactModelDO.class);
        deviceContactModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceContactModel(List<Long> ids) {
        for (Long id : ids) {
            // 校验存在
            validateDeviceContactModelExists(id);
            // 删除
            deviceContactModelMapper.deleteById(id);
        }
    }

    private void validateDeviceContactModelExists(Long id) {
        if (deviceContactModelMapper.selectById(id) == null) {
            throw exception(DEVICE_CONTACT_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public DeviceContactModelDO getDeviceContactModel(Long id) {
        DeviceContactModelDO deviceContactModelDO = deviceContactModelMapper.selectById(id);
        return deviceContactModelDO;
    }

    @Override
    public PageResult<DeviceContactModelDO> getDeviceContactModelPage(DeviceContactModelPageReqVO pageReqVO) {
        return deviceContactModelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DeviceContactModelDO> getDeviceContactModelList(Long id) {
        return deviceContactModelMapper.selectList(Wrappers.<DeviceContactModelDO>lambdaQuery().eq(DeviceContactModelDO::getDeviceId,id)
                .orderByDesc(DeviceContactModelDO::getCreateTime));
    }

    @Override
    public List<DeviceContactModelDO> selectListByDeviceId(Long id) {
        return deviceContactModelMapper.selectList(Wrappers.<DeviceContactModelDO>lambdaQuery().eq(DeviceContactModelDO::getDeviceId,id));
    }

}