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.DictionarySampleFlowNodePageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleFlowNodeRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleFlowNodeSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionarySampleFlowNodeDO;
import com.zt.plat.module.qms.business.dic.dal.mapper.DictionarySampleFlowNodeMapper;
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_SAMPLE_FLOW_NODE_NOT_EXISTS;

/**
 * 样品流程节点字典 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class DictionarySampleFlowNodeServiceImpl implements DictionarySampleFlowNodeService {

    @Resource
    private DictionarySampleFlowNodeMapper dictionarySampleFlowNodeMapper;

    @Override
    public DictionarySampleFlowNodeRespVO createDictionarySampleFlowNode(DictionarySampleFlowNodeSaveReqVO createReqVO) {
        // 插入
        DictionarySampleFlowNodeDO dictionarySampleFlowNode = BeanUtils.toBean(createReqVO, DictionarySampleFlowNodeDO.class);
        dictionarySampleFlowNodeMapper.insert(dictionarySampleFlowNode);
        // 返回
        return BeanUtils.toBean(dictionarySampleFlowNode, DictionarySampleFlowNodeRespVO.class);
    }

    @Override
    public void updateDictionarySampleFlowNode(DictionarySampleFlowNodeSaveReqVO updateReqVO) {
        // 校验存在
        validateDictionarySampleFlowNodeExists(updateReqVO.getId());
        // 更新
        DictionarySampleFlowNodeDO updateObj = BeanUtils.toBean(updateReqVO, DictionarySampleFlowNodeDO.class);
        dictionarySampleFlowNodeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictionarySampleFlowNode(Long id) {
        // 校验存在
        validateDictionarySampleFlowNodeExists(id);
        // 删除
        dictionarySampleFlowNodeMapper.deleteById(id);
    }

    @Override
        public void deleteDictionarySampleFlowNodeListByIds(List<Long> ids) {
        // 校验存在
        validateDictionarySampleFlowNodeExists(ids);
        // 删除
        dictionarySampleFlowNodeMapper.deleteByIds(ids);
        }

    private void validateDictionarySampleFlowNodeExists(List<Long> ids) {
        List<DictionarySampleFlowNodeDO> list = dictionarySampleFlowNodeMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DICTIONARY_SAMPLE_FLOW_NODE_NOT_EXISTS);
        }
    }

    private void validateDictionarySampleFlowNodeExists(Long id) {
        if (dictionarySampleFlowNodeMapper.selectById(id) == null) {
            throw exception(DICTIONARY_SAMPLE_FLOW_NODE_NOT_EXISTS);
        }
    }

    @Override
    public DictionarySampleFlowNodeDO getDictionarySampleFlowNode(Long id) {
        return dictionarySampleFlowNodeMapper.selectById(id);
    }

    @Override
    public PageResult<DictionarySampleFlowNodeDO> getDictionarySampleFlowNodePage(DictionarySampleFlowNodePageReqVO pageReqVO) {
        return dictionarySampleFlowNodeMapper.selectPage(pageReqVO);
    }

}