package com.fjhx.iot.service.tda.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.iot.entity.tda.dto.TdaProductDto;
import com.fjhx.iot.entity.tda.dto.TdaProductSelectDto;
import com.fjhx.iot.entity.tda.po.TdaApplication;
import com.fjhx.iot.entity.tda.po.TdaDevice;
import com.fjhx.iot.entity.tda.po.TdaProduct;
import com.fjhx.iot.entity.tda.vo.TdaProductVo;
import com.fjhx.iot.mapper.tda.TdaProductMapper;
import com.fjhx.iot.service.tda.TdaApplicationService;
import com.fjhx.iot.service.tda.TdaDeviceService;
import com.fjhx.iot.service.tda.TdaProductService;
import com.fjhx.iot.utils.IoTDAUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 产品管理 服务实现类
 * </p>
 *
 * @author zlj
 * @since 2023-03-21
 */
@Service
public class TdaProductServiceImpl extends ServiceImpl<TdaProductMapper, TdaProduct> implements TdaProductService {

    @Autowired
    private TdaApplicationService tdaApplicationService;

    @Autowired
    private TdaDeviceService tdaDeviceService;

    @Override
    public Page<TdaProductVo> getPage(TdaProductSelectDto dto) {
        IWrapper<TdaProduct> wrapper = getWrapper();
        wrapper.eq("tp", TdaProduct::getTdaApplicationId, dto.getTdaApplicationId());
        wrapper.keyword(dto, new SqlField("tp.name"));
        wrapper.orderByDesc("tp", TdaProduct::getId);
        Page<TdaProductVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public TdaProductVo detail(Long id) {
        TdaProduct TdaProduct = this.getById(id);
        TdaProductVo result = BeanUtil.toBean(TdaProduct, TdaProductVo.class);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TdaProductDto tdaProductDto) {

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

        // 华为ioDta添加产品
        String productId = IoTDAUtil.createProduct(
                tdaApplication.getTdaConfigId(),
                appId,
                tdaProductDto.getName(),
                tdaProductDto.getDeviceType(),
                tdaProductDto.getManufacturerName(),
                tdaProductDto.getProtocolType()
        );

        tdaProductDto.setAppId(appId);
        tdaProductDto.setProductId(productId);

        this.save(tdaProductDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long id) {

        // 查询产品是否已添加设备，已添加设备则不可删除
        long count = tdaDeviceService.count(q -> q.eq(TdaDevice::getTdaProductId, id));
        if (count > 0) {
            throw new ServiceException("产品已添加设备，无法删除");
        }

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

        this.removeById(id);
    }

}
