package cn.iocoder.yudao.module.mes.service.energytype;

import cn.iocoder.yudao.module.iot.dal.dataobject.deviceattributetype.DeviceAttributeTypeDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.mes.controller.admin.energytype.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.energytype.EnergyTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mes.dal.mysql.energytype.EnergyTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;

/**
 * 能耗类型 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class EnergyTypeServiceImpl implements EnergyTypeService {

    @Resource
    private EnergyTypeMapper energyTypeMapper;

    @Override
    public Long createEnergyType(EnergyTypeSaveReqVO createReqVO) {

        // 校验编码是否重复
        validateCodeUnique(createReqVO);

        // 插入
        EnergyTypeDO energyType = BeanUtils.toBean(createReqVO, EnergyTypeDO.class);
        energyTypeMapper.insert(energyType);
        // 返回
        return energyType.getId();
    }

    private void validateCodeUnique(EnergyTypeSaveReqVO createReqVO) {

        Long count = energyTypeMapper.selectCount(Wrappers.<EnergyTypeDO>lambdaQuery()
                .eq(EnergyTypeDO::getCode, createReqVO.getCode())
                .ne(createReqVO.getId() != null, EnergyTypeDO::getId, createReqVO.getId() ));

        if (count != null && count > 0) {
            throw exception(ENERGY_DEVICE_CHECK_CODE_EXISTS);
        }
    }

    @Override
    public void updateEnergyType(EnergyTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateEnergyTypeExists(updateReqVO.getId());
        // 更新
        EnergyTypeDO updateObj = BeanUtils.toBean(updateReqVO, EnergyTypeDO.class);
        energyTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnergyType(Long id) {
        // 校验存在
        validateEnergyTypeExists(id);
        // 删除
        energyTypeMapper.deleteById(id);
    }

    private void validateEnergyTypeExists(Long id) {
        if (energyTypeMapper.selectById(id) == null) {
            throw exception(ENERGY_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public EnergyTypeDO getEnergyType(Long id) {
        return energyTypeMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyTypeDO> getEnergyTypePage(EnergyTypePageReqVO pageReqVO) {
        return energyTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EnergyTypeDO> getEnergyTypeList() {
        return energyTypeMapper.selectList();
    }
}