package com.fjhx.service.tda.impl;

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.TdaApplication;
import com.fjhx.entity.tda.TdaDevice;
import com.fjhx.entity.tda.TdaProduct;
import com.fjhx.mapper.tda.TdaProductMapper;
import com.fjhx.params.tda.TdaProductEx;
import com.fjhx.params.tda.TdaProductVo;
import com.fjhx.service.tda.TdaApplicationService;
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.springblade.core.tool.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-21
 */
@Service
public class TdaProductServiceImpl extends ServiceImpl<TdaProductMapper, TdaProduct> implements TdaProductService {

    @Autowired
    private TdaApplicationService tdaApplicationService;

    @Autowired
    private TdaDeviceService tdaDeviceService;

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

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

        WrapperUtil.init(condition, wrapper)
                .keyword("tp.name", "tp.product_id")
                .eq("tp.tda_application_id", "tdaApplicationId")
                .eq("tp.device_type", "deviceType")
                .eq("tp.manufacturer_name", "manufacturerName")
                .createTimeDesc("tp");

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

    @Override
    public void add(TdaProductVo tdaProductVo) {
        Long tdaApplicationId = tdaProductVo.getTdaApplicationId();
        Assert.notEmpty(tdaApplicationId, "资源空间表id不能为空");

        String name = tdaProductVo.getName();
        Assert.notEmpty(name, "产品名称不能为空");

        String deviceType = tdaProductVo.getDeviceType();
        Assert.notEmpty(deviceType, "设备类型不能为空");

        String manufacturerName = tdaProductVo.getManufacturerName();
        Assert.notEmpty(manufacturerName, "厂商名称不能为空");

        TdaApplication tdaApplication = tdaApplicationService.getById(tdaApplicationId);
        String appId = tdaApplication.getAppId();

        // 华为ioDta添加产品
        String productId = IoTDAUtil.createProduct(appId, name, deviceType, manufacturerName);

        tdaProductVo.setAppId(appId);
        tdaProductVo.setProductId(productId);
        save(tdaProductVo);
    }

    @Override
    public void delete(TdaProductVo tdaProductVo) {
        Long id = tdaProductVo.getId();
        Assert.notEmpty(id, "产品表id不能为空");

        // 查询产品是否已添加设备，已添加设备则不可删除
        Long count = tdaDeviceService.count(TdaDevice::getTdaProductId, id);
        Assert.eqZero(count, "产品已添加设备，无法删除");

        // 华为ioDta删除产品
        TdaProduct tdaProduct = getById(id);
        IoTDAUtil.deleteProduct(tdaProduct.getAppId(), tdaProduct.getProductId());

        removeById(tdaProductVo.getId());
    }

    @Override
    public List<Map<String, Object>> getList(Map<String, Long> condition) {

        Long tdaApplicationId = condition.get("tdaApplicationId");

        return listMaps(Wrappers.<TdaProduct>lambdaQuery()
                .select(BaseEntity::getId, TdaProduct::getTdaApplicationId, TdaProduct::getName)
                .eq(ObjectUtil.isNotEmpty(tdaApplicationId), TdaProduct::getTdaApplicationId, tdaApplicationId)
        );
    }

}
