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

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 ConfigXRFLineServiceImpl implements ConfigXRFLineService {

    @Resource
    private ConfigXRFLineMapper configXRFLineMapper;

    @Override
    public ConfigXRFLineRespVO createConfigXRFLine(ConfigXRFLineSaveReqVO createReqVO) {
        // 插入
        ConfigXRFLineDO configXRFLine = BeanUtils.toBean(createReqVO, ConfigXRFLineDO.class);
        configXRFLineMapper.insert(configXRFLine);
        // 返回
        return BeanUtils.toBean(configXRFLine, ConfigXRFLineRespVO.class);
    }

    @Override
    public void updateConfigXRFLine(ConfigXRFLineSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigXRFLineExists(updateReqVO.getId());
        // 更新
        ConfigXRFLineDO updateObj = BeanUtils.toBean(updateReqVO, ConfigXRFLineDO.class);
        configXRFLineMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigXRFLine(Long id) {
        // 校验存在
        validateConfigXRFLineExists(id);
        // 删除
        configXRFLineMapper.deleteById(id);
    }

    @Override
        public void deleteConfigXRFLineListByIds(List<Long> ids) {
        // 校验存在
        validateConfigXRFLineExists(ids);
        // 删除
        configXRFLineMapper.deleteByIds(ids);
        }

    private void validateConfigXRFLineExists(List<Long> ids) {
        List<ConfigXRFLineDO> list = configXRFLineMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_XRF_LINE_NOT_EXISTS);
        }
    }

    private void validateConfigXRFLineExists(Long id) {
        if (configXRFLineMapper.selectById(id) == null) {
            throw exception(CONFIG_XRF_LINE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigXRFLineDO getConfigXRFLine(Long id) {
        return configXRFLineMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigXRFLineDO> getConfigXRFLinePage(ConfigXRFLinePageReqVO pageReqVO) {
        return configXRFLineMapper.selectPage(pageReqVO);
    }

}