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.ConfigStandardSampleProjectPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigStandardSampleProjectRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigStandardSampleProjectSaveReqVO;
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.ConfigStandardSampleProjectDO;
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.ConfigStandardSampleProjectMapper;

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_STANDARD_SAMPLE_PROJECT_NOT_EXISTS;

/**
 * 标准样检测项目配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigStandardSampleProjectServiceImpl implements ConfigStandardSampleProjectService {

    @Resource
    private ConfigStandardSampleProjectMapper configStandardSampleProjectMapper;

    @Override
    public ConfigStandardSampleProjectRespVO createConfigStandardSampleProject(ConfigStandardSampleProjectSaveReqVO createReqVO) {
        // 插入
        ConfigStandardSampleProjectDO configStandardSampleProject = BeanUtils.toBean(createReqVO, ConfigStandardSampleProjectDO.class);
        configStandardSampleProjectMapper.insert(configStandardSampleProject);
        // 返回
        return BeanUtils.toBean(configStandardSampleProject, ConfigStandardSampleProjectRespVO.class);
    }

    @Override
    public void updateConfigStandardSampleProject(ConfigStandardSampleProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigStandardSampleProjectExists(updateReqVO.getId());
        // 更新
        ConfigStandardSampleProjectDO updateObj = BeanUtils.toBean(updateReqVO, ConfigStandardSampleProjectDO.class);
        configStandardSampleProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigStandardSampleProject(Long id) {
        // 校验存在
        validateConfigStandardSampleProjectExists(id);
        // 删除
        configStandardSampleProjectMapper.deleteById(id);
    }

    @Override
        public void deleteConfigStandardSampleProjectListByIds(List<Long> ids) {
        // 校验存在
        validateConfigStandardSampleProjectExists(ids);
        // 删除
        configStandardSampleProjectMapper.deleteByIds(ids);
        }

    private void validateConfigStandardSampleProjectExists(List<Long> ids) {
        List<ConfigStandardSampleProjectDO> list = configStandardSampleProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_STANDARD_SAMPLE_PROJECT_NOT_EXISTS);
        }
    }

    private void validateConfigStandardSampleProjectExists(Long id) {
        if (configStandardSampleProjectMapper.selectById(id) == null) {
            throw exception(CONFIG_STANDARD_SAMPLE_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public ConfigStandardSampleProjectDO getConfigStandardSampleProject(Long id) {
        return configStandardSampleProjectMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigStandardSampleProjectDO> getConfigStandardSampleProjectPage(ConfigStandardSampleProjectPageReqVO pageReqVO) {
        return configStandardSampleProjectMapper.selectPage(pageReqVO);
    }

}