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.DictionaryProjectPageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryProjectRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryProjectSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryProjectDO;
import com.zt.plat.module.qms.business.dic.dal.mapper.DictionaryProjectMapper;
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_PROJECT_NOT_EXISTS;

/**
 * 检测项目字典 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class DictionaryProjectServiceImpl implements DictionaryProjectService {

    @Resource
    private DictionaryProjectMapper dictionaryProjectMapper;

    @Override
    public DictionaryProjectRespVO createDictionaryProject(DictionaryProjectSaveReqVO createReqVO) {
        // 插入
        DictionaryProjectDO dictionaryProject = BeanUtils.toBean(createReqVO, DictionaryProjectDO.class);
        dictionaryProjectMapper.insert(dictionaryProject);
        // 返回
        return BeanUtils.toBean(dictionaryProject, DictionaryProjectRespVO.class);
    }

    @Override
    public void updateDictionaryProject(DictionaryProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateDictionaryProjectExists(updateReqVO.getId());
        // 更新
        DictionaryProjectDO updateObj = BeanUtils.toBean(updateReqVO, DictionaryProjectDO.class);
        dictionaryProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictionaryProject(Long id) {
        // 校验存在
        validateDictionaryProjectExists(id);
        // 删除
        dictionaryProjectMapper.deleteById(id);
    }

    @Override
        public void deleteDictionaryProjectListByIds(List<Long> ids) {
        // 校验存在
        validateDictionaryProjectExists(ids);
        // 删除
        dictionaryProjectMapper.deleteByIds(ids);
        }

    private void validateDictionaryProjectExists(List<Long> ids) {
        List<DictionaryProjectDO> list = dictionaryProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DICTIONARY_PROJECT_NOT_EXISTS);
        }
    }

    private void validateDictionaryProjectExists(Long id) {
        if (dictionaryProjectMapper.selectById(id) == null) {
            throw exception(DICTIONARY_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public DictionaryProjectDO getDictionaryProject(Long id) {
        return dictionaryProjectMapper.selectById(id);
    }

    @Override
    public PageResult<DictionaryProjectDO> getDictionaryProjectPage(DictionaryProjectPageReqVO pageReqVO) {
        return dictionaryProjectMapper.selectPage(pageReqVO);
    }

	@Override
	public List<DictionaryProjectDO> getDictionaryProjectList(DictionaryProjectPageReqVO reqVO) {
        return dictionaryProjectMapper.selectList(reqVO);
	}

}