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.ConfigReportFieldPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigReportFieldRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigReportFieldSaveReqVO;
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.ConfigReportFieldDO;
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.ConfigReportFieldMapper;

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

/**
 * 报表字段配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigReportFieldServiceImpl implements ConfigReportFieldService {

    @Resource
    private ConfigReportFieldMapper configReportFieldMapper;

    @Override
    public ConfigReportFieldRespVO createConfigReportField(ConfigReportFieldSaveReqVO createReqVO) {
        // 插入
        ConfigReportFieldDO configReportField = BeanUtils.toBean(createReqVO, ConfigReportFieldDO.class);
        configReportFieldMapper.insert(configReportField);
        // 返回
        return BeanUtils.toBean(configReportField, ConfigReportFieldRespVO.class);
    }

    @Override
    public void updateConfigReportField(ConfigReportFieldSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigReportFieldExists(updateReqVO.getId());
        // 更新
        ConfigReportFieldDO updateObj = BeanUtils.toBean(updateReqVO, ConfigReportFieldDO.class);
        configReportFieldMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigReportField(Long id) {
        // 校验存在
        validateConfigReportFieldExists(id);
        // 删除
        configReportFieldMapper.deleteById(id);
    }

    @Override
        public void deleteConfigReportFieldListByIds(List<Long> ids) {
        // 校验存在
        validateConfigReportFieldExists(ids);
        // 删除
        configReportFieldMapper.deleteByIds(ids);
        }

    private void validateConfigReportFieldExists(List<Long> ids) {
        List<ConfigReportFieldDO> list = configReportFieldMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_REPORT_FIELD_NOT_EXISTS);
        }
    }

    private void validateConfigReportFieldExists(Long id) {
        if (configReportFieldMapper.selectById(id) == null) {
            throw exception(CONFIG_REPORT_FIELD_NOT_EXISTS);
        }
    }

    @Override
    public ConfigReportFieldDO getConfigReportField(Long id) {
        return configReportFieldMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigReportFieldDO> getConfigReportFieldPage(ConfigReportFieldPageReqVO pageReqVO) {
        return configReportFieldMapper.selectPage(pageReqVO);
    }

}