package com.zt.plat.module.qms.business.other.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.other.controller.vo.*;
import com.zt.plat.module.qms.business.other.dal.dataobject.OtherDictionaryProjectDO;
import com.zt.plat.module.qms.business.other.dal.mapper.OtherDictionaryProjectMapper;

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 OtherDictionaryProjectServiceImpl implements OtherDictionaryProjectService {

    @Resource
    private OtherDictionaryProjectMapper otherDictionaryProjectMapper;

    @Override
    public OtherDictionaryProjectRespVO createOtherDictionaryProject(OtherDictionaryProjectSaveReqVO createReqVO) {
        // 插入
        OtherDictionaryProjectDO otherDictionaryProject = BeanUtils.toBean(createReqVO, OtherDictionaryProjectDO.class);
        otherDictionaryProjectMapper.insert(otherDictionaryProject);
        // 返回
        return BeanUtils.toBean(otherDictionaryProject, OtherDictionaryProjectRespVO.class);
    }

    @Override
    public void updateOtherDictionaryProject(OtherDictionaryProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateOtherDictionaryProjectExists(updateReqVO.getId());
        // 更新
        OtherDictionaryProjectDO updateObj = BeanUtils.toBean(updateReqVO, OtherDictionaryProjectDO.class);
        otherDictionaryProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteOtherDictionaryProject(Long id) {
        // 校验存在
        validateOtherDictionaryProjectExists(id);
        // 删除
        otherDictionaryProjectMapper.deleteById(id);
    }

    @Override
        public void deleteOtherDictionaryProjectListByIds(List<Long> ids) {
        // 校验存在
        validateOtherDictionaryProjectExists(ids);
        // 删除
        otherDictionaryProjectMapper.deleteByIds(ids);
        }

    private void validateOtherDictionaryProjectExists(List<Long> ids) {
        List<OtherDictionaryProjectDO> list = otherDictionaryProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OTHER_DICTIONARY_PROJECT_NOT_EXISTS);
        }
    }

    private void validateOtherDictionaryProjectExists(Long id) {
        if (otherDictionaryProjectMapper.selectById(id) == null) {
            throw exception(OTHER_DICTIONARY_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public OtherDictionaryProjectDO getOtherDictionaryProject(Long id) {
        return otherDictionaryProjectMapper.selectById(id);
    }

    @Override
    public PageResult<OtherDictionaryProjectDO> getOtherDictionaryProjectPage(OtherDictionaryProjectPageReqVO pageReqVO) {
        return otherDictionaryProjectMapper.selectPage(pageReqVO);
    }

}