package com.zt.plat.module.qms.business.config.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationInfomationPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationInfomationRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigWarehouseLocationInfomationSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.zt.plat.module.qms.business.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationInfomationDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.business.config.dal.mapper.ConfigWarehouseLocationInfomationMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.CONFIG_WAREHOUSE_LOCATION_INFOMATION_NOT_EXISTS;

/**
 * 样品库位信息 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigWarehouseLocationInfomationServiceImpl implements ConfigWarehouseLocationInfomationService {

    @Resource
    private ConfigWarehouseLocationInfomationMapper configWarehouseLocationInfomationMapper;

    @Override
    public ConfigWarehouseLocationInfomationRespVO createConfigWarehouseLocationInfomation(ConfigWarehouseLocationInfomationSaveReqVO createReqVO) {
        // 插入
        ConfigWarehouseLocationInfomationDO configWarehouseLocationInfomation = BeanUtils.toBean(createReqVO, ConfigWarehouseLocationInfomationDO.class);
        configWarehouseLocationInfomationMapper.insert(configWarehouseLocationInfomation);
        // 返回
        return BeanUtils.toBean(configWarehouseLocationInfomation, ConfigWarehouseLocationInfomationRespVO.class);
    }

    @Override
    public void updateConfigWarehouseLocationInfomation(ConfigWarehouseLocationInfomationSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigWarehouseLocationInfomationExists(updateReqVO.getId());
        // 更新
        ConfigWarehouseLocationInfomationDO updateObj = BeanUtils.toBean(updateReqVO, ConfigWarehouseLocationInfomationDO.class);
        configWarehouseLocationInfomationMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigWarehouseLocationInfomation(Long id) {
        // 校验存在
        validateConfigWarehouseLocationInfomationExists(id);
        // 删除
        configWarehouseLocationInfomationMapper.deleteById(id);
    }

    @Override
        public void deleteConfigWarehouseLocationInfomationListByIds(List<Long> ids) {
        // 校验存在
        validateConfigWarehouseLocationInfomationExists(ids);
        // 删除
        configWarehouseLocationInfomationMapper.deleteByIds(ids);
        }

    private void validateConfigWarehouseLocationInfomationExists(List<Long> ids) {
        List<ConfigWarehouseLocationInfomationDO> list = configWarehouseLocationInfomationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_WAREHOUSE_LOCATION_INFOMATION_NOT_EXISTS);
        }
    }

    private void validateConfigWarehouseLocationInfomationExists(Long id) {
        if (configWarehouseLocationInfomationMapper.selectById(id) == null) {
            throw exception(CONFIG_WAREHOUSE_LOCATION_INFOMATION_NOT_EXISTS);
        }
    }

    @Override
    public ConfigWarehouseLocationInfomationDO getConfigWarehouseLocationInfomation(Long id) {
        return configWarehouseLocationInfomationMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigWarehouseLocationInfomationDO> getConfigWarehouseLocationInfomationPage(ConfigWarehouseLocationInfomationPageReqVO pageReqVO) {
        return configWarehouseLocationInfomationMapper.selectPage(pageReqVO);
    }

}