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

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

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

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.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 检测方法分析项目判定 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigAssayMethodProjectAssessmentServiceImpl implements ConfigAssayMethodProjectAssessmentService {

    @Resource
    private ConfigAssayMethodProjectAssessmentMapper configAssayMethodProjectAssessmentMapper;

    @Override
    public ConfigAssayMethodProjectAssessmentRespVO createConfigAssayMethodProjectAssessment(ConfigAssayMethodProjectAssessmentSaveReqVO createReqVO) {
        // 插入
        ConfigAssayMethodProjectAssessmentDO configAssayMethodProjectAssessment = BeanUtils.toBean(createReqVO, ConfigAssayMethodProjectAssessmentDO.class);
        configAssayMethodProjectAssessmentMapper.insert(configAssayMethodProjectAssessment);
        // 返回
        return BeanUtils.toBean(configAssayMethodProjectAssessment, ConfigAssayMethodProjectAssessmentRespVO.class);
    }

    @Override
    public void updateConfigAssayMethodProjectAssessment(ConfigAssayMethodProjectAssessmentSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigAssayMethodProjectAssessmentExists(updateReqVO.getId());
        // 更新
        ConfigAssayMethodProjectAssessmentDO updateObj = BeanUtils.toBean(updateReqVO, ConfigAssayMethodProjectAssessmentDO.class);
        configAssayMethodProjectAssessmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigAssayMethodProjectAssessment(Long id) {
        // 校验存在
        validateConfigAssayMethodProjectAssessmentExists(id);
        // 删除
        configAssayMethodProjectAssessmentMapper.deleteById(id);
    }

    @Override
        public void deleteConfigAssayMethodProjectAssessmentListByIds(List<Long> ids) {
        // 校验存在
        validateConfigAssayMethodProjectAssessmentExists(ids);
        // 删除
        configAssayMethodProjectAssessmentMapper.deleteByIds(ids);
        }

    private void validateConfigAssayMethodProjectAssessmentExists(List<Long> ids) {
        List<ConfigAssayMethodProjectAssessmentDO> list = configAssayMethodProjectAssessmentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_ASSAY_METHOD_PROJECT_ASSESSMENT_NOT_EXISTS);
        }
    }

    private void validateConfigAssayMethodProjectAssessmentExists(Long id) {
        if (configAssayMethodProjectAssessmentMapper.selectById(id) == null) {
            throw exception(CONFIG_ASSAY_METHOD_PROJECT_ASSESSMENT_NOT_EXISTS);
        }
    }

    @Override
    public ConfigAssayMethodProjectAssessmentDO getConfigAssayMethodProjectAssessment(Long id) {
        return configAssayMethodProjectAssessmentMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigAssayMethodProjectAssessmentDO> getConfigAssayMethodProjectAssessmentPage(ConfigAssayMethodProjectAssessmentPageReqVO pageReqVO) {
        return configAssayMethodProjectAssessmentMapper.selectPage(pageReqVO);
    }

}