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

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.processinginfomation.vo.*;
import com.zt.plat.module.base.dal.dataobject.processinginfomation.ProcessingInfomationDO;
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.processinginfomation.ProcessingInfomationMapper;

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 ProcessingInfomationServiceImpl implements ProcessingInfomationService {

    @Resource
    private ProcessingInfomationMapper processingInfomationMapper;

    @Override
    public ProcessingInfomationRespVO createProcessingInfomation(ProcessingInfomationSaveReqVO createReqVO) {
        // 插入
        ProcessingInfomationDO processingInfomation = BeanUtils.toBean(createReqVO, ProcessingInfomationDO.class);
        processingInfomationMapper.insert(processingInfomation);
        // 返回
        return BeanUtils.toBean(processingInfomation, ProcessingInfomationRespVO.class);
    }

    @Override
    public void updateProcessingInfomation(ProcessingInfomationSaveReqVO updateReqVO) {
        // 校验存在
        validateProcessingInfomationExists(updateReqVO.getId());
        // 更新
        ProcessingInfomationDO updateObj = BeanUtils.toBean(updateReqVO, ProcessingInfomationDO.class);
        processingInfomationMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessingInfomation(Long id) {
        // 校验存在
        validateProcessingInfomationExists(id);
        // 删除
        processingInfomationMapper.deleteById(id);
    }

    @Override
        public void deleteProcessingInfomationListByIds(List<Long> ids) {
        // 校验存在
        validateProcessingInfomationExists(ids);
        // 删除
        processingInfomationMapper.deleteByIds(ids);
        }

    private void validateProcessingInfomationExists(List<Long> ids) {
        List<ProcessingInfomationDO> list = processingInfomationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PROCESSING_INFOMATION_NOT_EXISTS);
        }
    }

    private void validateProcessingInfomationExists(Long id) {
        if (processingInfomationMapper.selectById(id) == null) {
            throw exception(PROCESSING_INFOMATION_NOT_EXISTS);
        }
    }

    @Override
    public ProcessingInfomationDO getProcessingInfomation(Long id) {
        return processingInfomationMapper.selectById(id);
    }

    @Override
    public PageResult<ProcessingInfomationDO> getProcessingInfomationPage(ProcessingInfomationPageReqVO pageReqVO) {
        return processingInfomationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProcessingInfomationDO> getProcessingInfomationList() {
        return processingInfomationMapper.selectList();
    }

}