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.ConfigXRFProjectDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigXRFProjectMapper;

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 ConfigXRFProjectServiceImpl implements ConfigXRFProjectService {

    @Resource
    private ConfigXRFProjectMapper configXRFProjectMapper;

    @Override
    public ConfigXRFProjectRespVO createConfigXRFProject(ConfigXRFProjectSaveReqVO createReqVO) {
        // 插入
        ConfigXRFProjectDO configXRFProject = BeanUtils.toBean(createReqVO, ConfigXRFProjectDO.class);
        configXRFProjectMapper.insert(configXRFProject);
        // 返回
        return BeanUtils.toBean(configXRFProject, ConfigXRFProjectRespVO.class);
    }

    @Override
    public void updateConfigXRFProject(ConfigXRFProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigXRFProjectExists(updateReqVO.getId());
        // 更新
        ConfigXRFProjectDO updateObj = BeanUtils.toBean(updateReqVO, ConfigXRFProjectDO.class);
        configXRFProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigXRFProject(Long id) {
        // 校验存在
        validateConfigXRFProjectExists(id);
        // 删除
        configXRFProjectMapper.deleteById(id);
    }

    @Override
        public void deleteConfigXRFProjectListByIds(List<Long> ids) {
        // 校验存在
        validateConfigXRFProjectExists(ids);
        // 删除
        configXRFProjectMapper.deleteByIds(ids);
        }

    private void validateConfigXRFProjectExists(List<Long> ids) {
        List<ConfigXRFProjectDO> list = configXRFProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_XRF_PROJECT_NOT_EXISTS);
        }
    }

    private void validateConfigXRFProjectExists(Long id) {
        if (configXRFProjectMapper.selectById(id) == null) {
            throw exception(CONFIG_XRF_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public ConfigXRFProjectDO getConfigXRFProject(Long id) {
        return configXRFProjectMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigXRFProjectDO> getConfigXRFProjectPage(ConfigXRFProjectPageReqVO pageReqVO) {
        return configXRFProjectMapper.selectPage(pageReqVO);
    }

}