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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryParameterPageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryParameterRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryParameterSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryParameterDO;
import com.zt.plat.module.qms.business.dic.dal.mapper.DictionaryParameterMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.DICTIONARY_PARAMETER_NOT_EXISTS;

/**
 * 检测参数字典 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class DictionaryParameterServiceImpl implements DictionaryParameterService {

    @Resource
    private DictionaryParameterMapper dictionaryParameterMapper;

    @Override
    public DictionaryParameterRespVO createDictionaryParameter(DictionaryParameterSaveReqVO createReqVO) {
        // 插入
        DictionaryParameterDO dictionaryParameter = BeanUtils.toBean(createReqVO, DictionaryParameterDO.class);
        dictionaryParameterMapper.insert(dictionaryParameter);
        // 返回
        return BeanUtils.toBean(dictionaryParameter, DictionaryParameterRespVO.class);
    }

    @Override
    public void updateDictionaryParameter(DictionaryParameterSaveReqVO updateReqVO) {
        // 校验存在
        validateDictionaryParameterExists(updateReqVO.getId());
        // 更新
        DictionaryParameterDO updateObj = BeanUtils.toBean(updateReqVO, DictionaryParameterDO.class);
        dictionaryParameterMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictionaryParameter(Long id) {
        // 校验存在
        validateDictionaryParameterExists(id);
        // 删除
        dictionaryParameterMapper.deleteById(id);
    }

    @Override
        public void deleteDictionaryParameterListByIds(List<Long> ids) {
        // 校验存在
        validateDictionaryParameterExists(ids);
        // 删除
        dictionaryParameterMapper.deleteByIds(ids);
        }

    private void validateDictionaryParameterExists(List<Long> ids) {
        List<DictionaryParameterDO> list = dictionaryParameterMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DICTIONARY_PARAMETER_NOT_EXISTS);
        }
    }

    private void validateDictionaryParameterExists(Long id) {
        if (dictionaryParameterMapper.selectById(id) == null) {
            throw exception(DICTIONARY_PARAMETER_NOT_EXISTS);
        }
    }

    @Override
    public DictionaryParameterDO getDictionaryParameter(Long id) {
        return dictionaryParameterMapper.selectById(id);
    }

    @Override
    public PageResult<DictionaryParameterDO> getDictionaryParameterPage(DictionaryParameterPageReqVO pageReqVO) {
        return dictionaryParameterMapper.selectPage(pageReqVO);
    }

}