package com.fjhx.service.tda.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.BaseEntity;
import com.fjhx.entity.tda.TdaDevice;
import com.fjhx.entity.tda.TdaProduct;
import com.fjhx.mapper.tda.TdaDeviceMapper;
import com.fjhx.params.tda.TdaDeviceEx;
import com.fjhx.params.tda.TdaDeviceVo;
import com.fjhx.service.tda.TdaDeviceService;
import com.fjhx.service.tda.TdaProductService;
import com.fjhx.utils.Assert;
import com.fjhx.utils.IoTDAUtil;
import com.fjhx.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-21
 */
@Service
public class TdaDeviceServiceImpl extends ServiceImpl<TdaDeviceMapper, TdaDevice> implements TdaDeviceService {

    @Lazy
    @Autowired
    private TdaProductService tdaProductService;

    @Override
    public Page<TdaDeviceEx> getPage(Map<String, String> condition) {

        QueryWrapper<TdaDevice> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .keyword("td.node_id", "td.device_name")
                .eq("td.tda_product_id", "tdaProductId")
                .eq("tp.tda_application_id", "tdaApplicationId")
                .createTimeDesc("td");

        return baseMapper.getPage(createPage(condition), wrapper);
    }

    @Override
    public void add(TdaDeviceVo tdaDeviceVo) {
        String nodeId = tdaDeviceVo.getNodeId();
        Assert.notEmpty(nodeId, "设备标识不能为空");

        String deviceName = tdaDeviceVo.getDeviceName();
        Assert.notEmpty(deviceName, "设备名称不能为空");

        Long tdaProductId = tdaDeviceVo.getTdaProductId();
        Assert.notEmpty(tdaProductId, "产品表id不能为空");

        // 查询资源所在空间和产品id
        TdaProduct tdaProduct = tdaProductService.getById(tdaProductId);
        String productId = tdaProduct.getProductId();

        // 华为ioDta添加设备
        IoTDAUtil.DeviceKey deviceKey = IoTDAUtil.addDevice(nodeId, deviceName, tdaProduct.getAppId(), productId);

        tdaDeviceVo.setProductId(productId);
        tdaDeviceVo.setDeviceId(deviceKey.getDeviceId());
        tdaDeviceVo.setSecret(deviceKey.getSecret());

        save(tdaDeviceVo);
    }

    @Override
    public void delete(TdaDeviceVo tdaDeviceVo) {
        Long id = tdaDeviceVo.getId();
        Assert.notEmpty(id, "设备表id不能为空");

        TdaDevice tdaDevice = getById(id);

        // 华为ioDta删除设备
        IoTDAUtil.deleteDevice(tdaDevice.getDeviceId());

        removeById(id);
    }

    @Override
    public List<Map<String, Object>> getList(Map<String, Long> condition) {
        Long tdaProductId = condition.get("tdaProductId");
        return listMaps(Wrappers.<TdaDevice>lambdaQuery()
                .select(BaseEntity::getId, TdaDevice::getTdaProductId, TdaDevice::getDeviceName)
                .eq(ObjectUtil.isNotEmpty(tdaProductId), TdaDevice::getTdaProductId, tdaProductId)
        );
    }

}
