package cn.iocoder.yudao.module.pms.service.iotdevicecategorytemplate;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplate.vo.IotDeviceCategoryTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicecategorytemplate.vo.IotDeviceCategoryTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicecategorytemplate.IotDeviceCategoryTemplateDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotdevicecategorytemplate.IotDeviceCategoryTemplateMapper;
import cn.iocoder.yudao.module.pms.service.IotProductClassifyService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_DEVICE_CATEGORY_TEMPLATE_NOT_EXISTS;

/**
 * 设备分类属性公用模板 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotDeviceCategoryTemplateServiceImpl implements IotDeviceCategoryTemplateService {

    @Resource
    private IotDeviceCategoryTemplateMapper iotDeviceCategoryTemplateMapper;

    @Resource
    private IotProductClassifyService iotProductClassifyService;

    @Override
    public Long createIotDeviceCategoryTemplate(IotDeviceCategoryTemplateSaveReqVO createReqVO) {
        // 插入
        IotDeviceCategoryTemplateDO iotDeviceCategoryTemplate = BeanUtils.toBean(createReqVO, IotDeviceCategoryTemplateDO.class);
        iotDeviceCategoryTemplateMapper.insert(iotDeviceCategoryTemplate);
        // 返回
        return iotDeviceCategoryTemplate.getId();
    }

    @Override
    public void updateIotDeviceCategoryTemplate(IotDeviceCategoryTemplateSaveReqVO updateReqVO) {
        // 校验存在
        validateIotDeviceCategoryTemplateExists(updateReqVO.getId());
        // 更新
        IotDeviceCategoryTemplateDO updateObj = BeanUtils.toBean(updateReqVO, IotDeviceCategoryTemplateDO.class);
        iotDeviceCategoryTemplateMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotDeviceCategoryTemplate(Long id) {
        // 校验存在
        validateIotDeviceCategoryTemplateExists(id);
        // 删除
        iotDeviceCategoryTemplateMapper.deleteById(id);
    }

    private void validateIotDeviceCategoryTemplateExists(Long id) {
        if (iotDeviceCategoryTemplateMapper.selectById(id) == null) {
            throw exception(IOT_DEVICE_CATEGORY_TEMPLATE_NOT_EXISTS);
        }
    }

    @Override
    public IotDeviceCategoryTemplateDO getIotDeviceCategoryTemplate(Long id) {
        return iotDeviceCategoryTemplateMapper.selectById(id);
    }

    @Override
    public PageResult<IotDeviceCategoryTemplateDO> getIotDeviceCategoryTemplatePage(IotDeviceCategoryTemplatePageReqVO pageReqVO) {
        // 查询当前设备分类及子设备分类的所有BOM
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeviceCategoryId())) {
            ids = iotProductClassifyService.getChildIotProductClassifyList(pageReqVO.getDeviceCategoryId());
            ids.add(pageReqVO.getDeviceCategoryId());
        }
        return iotDeviceCategoryTemplateMapper.selectPage(pageReqVO, ids);
    }

    @Override
    public void updateDeviceCategoryTemplateStatus(Long id, Integer status) {
        // 校验 设备分类属性模板 存在
        validateIotDeviceCategoryTemplateExists(id);
        // 更新状态
        IotDeviceCategoryTemplateDO updateObj = new IotDeviceCategoryTemplateDO();
        updateObj.setId(id);
        updateObj.setStatus(status);
        iotDeviceCategoryTemplateMapper.updateById(updateObj);
    }

}