package com.zt.plat.module.qms.business.config.service;

import cn.hutool.core.collection.CollUtil;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardForecastProjectDO;
import com.zt.plat.module.qms.business.config.dal.mapper.MaterialAssayStandardForecastProjectMapper;

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 MaterialAssayStandardForecastProjectServiceImpl implements MaterialAssayStandardForecastProjectService {

    @Resource
    private MaterialAssayStandardForecastProjectMapper materialAssayStandardForecastProjectMapper;

    @Override
    public MaterialAssayStandardForecastProjectRespVO createMaterialAssayStandardForecastProject(MaterialAssayStandardForecastProjectSaveReqVO createReqVO) {
        // 插入
        MaterialAssayStandardForecastProjectDO materialAssayStandardForecastProject = BeanUtils.toBean(createReqVO, MaterialAssayStandardForecastProjectDO.class);
        materialAssayStandardForecastProjectMapper.insert(materialAssayStandardForecastProject);
        // 返回
        return BeanUtils.toBean(materialAssayStandardForecastProject, MaterialAssayStandardForecastProjectRespVO.class);
    }

    @Override
    public void updateMaterialAssayStandardForecastProject(MaterialAssayStandardForecastProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialAssayStandardForecastProjectExists(updateReqVO.getId());
        // 更新
        MaterialAssayStandardForecastProjectDO updateObj = BeanUtils.toBean(updateReqVO, MaterialAssayStandardForecastProjectDO.class);
        materialAssayStandardForecastProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialAssayStandardForecastProject(Long id) {
        // 校验存在
        validateMaterialAssayStandardForecastProjectExists(id);
        // 删除
        materialAssayStandardForecastProjectMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialAssayStandardForecastProjectListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialAssayStandardForecastProjectExists(ids);
        // 删除
        materialAssayStandardForecastProjectMapper.deleteByIds(ids);
        }

    private void validateMaterialAssayStandardForecastProjectExists(List<Long> ids) {
        List<MaterialAssayStandardForecastProjectDO> list = materialAssayStandardForecastProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_ASSAY_STANDARD_FORECAST_PROJECT_NOT_EXISTS);
        }
    }

    private void validateMaterialAssayStandardForecastProjectExists(Long id) {
        if (materialAssayStandardForecastProjectMapper.selectById(id) == null) {
            throw exception(MATERIAL_ASSAY_STANDARD_FORECAST_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public MaterialAssayStandardForecastProjectDO getMaterialAssayStandardForecastProject(Long id) {
        return materialAssayStandardForecastProjectMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialAssayStandardForecastProjectDO> getMaterialAssayStandardForecastProjectPage(MaterialAssayStandardForecastProjectPageReqVO pageReqVO) {
        return materialAssayStandardForecastProjectMapper.selectPage(pageReqVO);
    }

	@Override
	public List<MaterialAssayStandardForecastProjectExtendRespVO> getMaterialAssayStandardForecastProjectList(
			MaterialAssayStandardForecastProjectReqVO reqVO) {
		return materialAssayStandardForecastProjectMapper.selectList(reqVO);
	}

}