package cn.iocoder.yudao.module.configure.service.deviceenvtype;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.configure.controller.admin.deviceenvtype.vo.DeviceEnvTypeCreateReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.deviceenvtype.vo.DeviceEnvTypeUpdateReqVO;
import cn.iocoder.yudao.module.configure.convert.deviceenvtype.DeviceEnvTypeConvert;
import cn.iocoder.yudao.module.configure.dal.dataobject.deviceenvtype.DeviceEnvTypeDO;
import cn.iocoder.yudao.module.configure.dal.mysql.deviceenvtype.DeviceEnvTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.configure.enums.ErrorCodeConstants.DEVICE_ENV_TYPE_NOT_EXISTS;

/**
 * 小程序设备环境类型配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DeviceEnvTypeServiceImpl implements DeviceEnvTypeService {

    @Resource
    private DeviceEnvTypeMapper deviceEnvTypeMapper;

    @Override
    public Long createDeviceEnvType(DeviceEnvTypeCreateReqVO createReqVO) {
        // 插入
        DeviceEnvTypeDO deviceEnvType = DeviceEnvTypeConvert.INSTANCE.convert(createReqVO);
        deviceEnvTypeMapper.insert(deviceEnvType);
        // 返回
        return deviceEnvType.getId();
    }

    @Override
    public void updateDeviceEnvType(DeviceEnvTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validateDeviceEnvTypeExists(updateReqVO.getId());
        // 更新
        DeviceEnvTypeDO updateObj = DeviceEnvTypeConvert.INSTANCE.convert(updateReqVO);
        deviceEnvTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceEnvType(Long id) {
        // 校验存在
        validateDeviceEnvTypeExists(id);
        // 删除
        deviceEnvTypeMapper.deleteById(id);
    }

    private void validateDeviceEnvTypeExists(Long id) {
        if (deviceEnvTypeMapper.selectById(id) == null) {
            throw exception(DEVICE_ENV_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public DeviceEnvTypeDO getDeviceEnvType(Long id) {
        return deviceEnvTypeMapper.selectById(id);
    }

    @Override
    public List<Long> createDeviceEnvTypeBatch(List<DeviceEnvTypeCreateReqVO> createReqVOS) {
        List<DeviceEnvTypeDO> deviceEnvTypes = BeanUtil.copyToList(createReqVOS, DeviceEnvTypeDO.class);
        deviceEnvTypeMapper.insertBatch(deviceEnvTypes);
        return deviceEnvTypes.stream().map(DeviceEnvTypeDO::getId).collect(Collectors.toList());
    }

    @Override
    public void updateDeviceEnvTypeBatch(List<DeviceEnvTypeUpdateReqVO> updateReqVOS) {
        List<Long> willUpdateIds = updateReqVOS.stream().map(DeviceEnvTypeUpdateReqVO::getId).collect(Collectors.toList());
        if(deviceEnvTypeMapper.selectBatchIds(willUpdateIds).size() != updateReqVOS.size()) {
            throw exception(DEVICE_ENV_TYPE_NOT_EXISTS);
        }
        List<DeviceEnvTypeDO> deviceEnvTypes = BeanUtil.copyToList(updateReqVOS, DeviceEnvTypeDO.class);
        deviceEnvTypeMapper.updateBatch(deviceEnvTypes, Integer.MAX_VALUE);
    }

    @Override
    public void deleteDeviceEnvTypeBatch(List<Long> ids) {
        if(deviceEnvTypeMapper.selectBatchIds(ids).size() != ids.size()) {
            throw exception(DEVICE_ENV_TYPE_NOT_EXISTS);
        }

        deviceEnvTypeMapper.deleteBatchIds(ids);
    }

    @Override
    public List<DeviceEnvTypeDO> getByLandId(String landId) {
        return deviceEnvTypeMapper.selectList(new LambdaQueryWrapperX<DeviceEnvTypeDO>().eq(DeviceEnvTypeDO::getLandId, landId));
    }

}
