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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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.processingoperation.vo.*;
import com.zt.plat.module.base.dal.dataobject.processingoperation.ProcessingOperationDO;
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.processingoperation.ProcessingOperationMapper;

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 ProcessingOperationServiceImpl implements ProcessingOperationService {

    @Resource
    private ProcessingOperationMapper processingOperationMapper;

    @Override
    public ProcessingOperationRespVO createProcessingOperation(ProcessingOperationSaveReqVO createReqVO) {
        // 插入
        ProcessingOperationDO processingOperation = BeanUtils.toBean(createReqVO, ProcessingOperationDO.class);
        processingOperationMapper.insert(processingOperation);
        // 返回
        return BeanUtils.toBean(processingOperation, ProcessingOperationRespVO.class);
    }

    @Override
    public void updateProcessingOperation(ProcessingOperationSaveReqVO updateReqVO) {
        // 校验存在
        validateProcessingOperationExists(updateReqVO.getId());
        // 更新
        ProcessingOperationDO updateObj = BeanUtils.toBean(updateReqVO, ProcessingOperationDO.class);
        processingOperationMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessingOperation(Long id) {
        // 校验存在
        validateProcessingOperationExists(id);
        // 删除
        processingOperationMapper.deleteById(id);
    }

    @Override
    public void deleteProcessingOperationListByIds(List<Long> ids) {
        // 校验存在
        validateProcessingOperationExists(ids);
        // 删除
        processingOperationMapper.deleteByIds(ids);
    }

    private void validateProcessingOperationExists(List<Long> ids) {
        List<ProcessingOperationDO> list = processingOperationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PROCESSING_OPERATION_NOT_EXISTS);
        }
    }

    private void validateProcessingOperationExists(Long id) {
        if (processingOperationMapper.selectById(id) == null) {
            throw exception(PROCESSING_OPERATION_NOT_EXISTS);
        }
    }

    @Override
    public ProcessingOperationDO getProcessingOperation(Long id) {
        return processingOperationMapper.selectById(id);
    }

    @Override
    public PageResult<ProcessingOperationDO> getProcessingOperationPage(ProcessingOperationPageReqVO pageReqVO) {
        return processingOperationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProcessingOperationDO> getProcessingOperationList() {
        return processingOperationMapper.selectSimpleList(null);
    }

    @Override
    public List<ProcessingOperationDO> getProcessingOperationListByType(String typeValue) {
        String normalized = StrUtil.isBlank(typeValue) ? null : StrUtil.trim(typeValue);
        return processingOperationMapper.selectSimpleList(normalized);
    }

}