package com.zt.plat.module.base.service.processingoperationmaterial;

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.module.base.controller.admin.processingoperationmaterial.vo.*;
import com.zt.plat.module.base.dal.dataobject.processingoperationmaterial.ProcessingOperationMaterialDO;
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.base.dal.dao.processingoperationmaterial.ProcessingOperationMaterialMapper;

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.base.enums.ErrorCodeConstants.*;

/**
 * 工艺工序物料 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class ProcessingOperationMaterialServiceImpl implements ProcessingOperationMaterialService {

    @Resource
    private ProcessingOperationMaterialMapper processingOperationMaterialMapper;

    @Override
    public ProcessingOperationMaterialRespVO createProcessingOperationMaterial(ProcessingOperationMaterialSaveReqVO createReqVO) {
        // 插入
        ProcessingOperationMaterialDO processingOperationMaterial = BeanUtils.toBean(createReqVO, ProcessingOperationMaterialDO.class);
        processingOperationMaterialMapper.insert(processingOperationMaterial);
        // 返回
        return BeanUtils.toBean(processingOperationMaterial, ProcessingOperationMaterialRespVO.class);
    }

    @Override
    public void updateProcessingOperationMaterial(ProcessingOperationMaterialSaveReqVO updateReqVO) {
        // 校验存在
        validateProcessingOperationMaterialExists(updateReqVO.getId());
        // 更新
        ProcessingOperationMaterialDO updateObj = BeanUtils.toBean(updateReqVO, ProcessingOperationMaterialDO.class);
        processingOperationMaterialMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessingOperationMaterial(Long id) {
        // 校验存在
        validateProcessingOperationMaterialExists(id);
        // 删除
        processingOperationMaterialMapper.deleteById(id);
    }

    @Override
        public void deleteProcessingOperationMaterialListByIds(List<Long> ids) {
        // 校验存在
        validateProcessingOperationMaterialExists(ids);
        // 删除
        processingOperationMaterialMapper.deleteByIds(ids);
        }

    private void validateProcessingOperationMaterialExists(List<Long> ids) {
        List<ProcessingOperationMaterialDO> list = processingOperationMaterialMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PROCESSING_OPERATION_MATERIAL_NOT_EXISTS);
        }
    }

    private void validateProcessingOperationMaterialExists(Long id) {
        if (processingOperationMaterialMapper.selectById(id) == null) {
            throw exception(PROCESSING_OPERATION_MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public ProcessingOperationMaterialDO getProcessingOperationMaterial(Long id) {
        return processingOperationMaterialMapper.selectById(id);
    }

    @Override
    public PageResult<ProcessingOperationMaterialDO> getProcessingOperationMaterialPage(ProcessingOperationMaterialPageReqVO pageReqVO) {
        return processingOperationMaterialMapper.selectPage(pageReqVO);
    }

}