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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardDetailSaveReqVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardDetailExtendDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardDetailDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.business.config.dal.mapper.MaterialAssayStandardDetailMapper;

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.module.qms.enums.ErrorCodeConstants.MATERIAL_ASSAY_STANDARD_DETAIL_NOT_EXISTS;

/**
 * 物料检测标准检测项目 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class MaterialAssayStandardDetailServiceImpl implements MaterialAssayStandardDetailService {

    @Resource
    private MaterialAssayStandardDetailMapper materialAssayStandardDetailMapper;

    @Override
    public List<MaterialAssayStandardDetailRespVO> selectListWithExtend(MaterialAssayStandardDetailPageReqVO params) {
        List<MaterialAssayStandardDetailExtendDO> list = materialAssayStandardDetailMapper.selectListWithExtend(params);
        return BeanUtils.toBean(list, MaterialAssayStandardDetailRespVO.class);
    }

    @Override
    public List<MaterialAssayStandardDetailRespVO> getByStandardId(Long standardId) {
        MaterialAssayStandardDetailPageReqVO params = new MaterialAssayStandardDetailPageReqVO();
        params.setMaterialAssayStandardId(standardId);
        return selectListWithExtend( params);
    }

    @Override
    public List<MaterialAssayStandardDetailRespVO> getByStandardIdList(List<Long> standardIdList) {
        MaterialAssayStandardDetailPageReqVO params = new MaterialAssayStandardDetailPageReqVO();
        params.setStandardIdList(standardIdList);
        return selectListWithExtend( params);
    }

    @Override
    public MaterialAssayStandardDetailRespVO createMaterialAssayStandardDetail(MaterialAssayStandardDetailSaveReqVO createReqVO) {
        // 插入
        MaterialAssayStandardDetailDO materialAssayStandardDetail = BeanUtils.toBean(createReqVO, MaterialAssayStandardDetailDO.class);
        materialAssayStandardDetailMapper.insert(materialAssayStandardDetail);
        // 返回
        return BeanUtils.toBean(materialAssayStandardDetail, MaterialAssayStandardDetailRespVO.class);
    }

    @Override
    public void updateMaterialAssayStandardDetail(MaterialAssayStandardDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialAssayStandardDetailExists(updateReqVO.getId());
        // 更新
        MaterialAssayStandardDetailDO updateObj = BeanUtils.toBean(updateReqVO, MaterialAssayStandardDetailDO.class);
        materialAssayStandardDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterialAssayStandardDetail(Long id) {
        // 校验存在
        validateMaterialAssayStandardDetailExists(id);
        // 删除
        materialAssayStandardDetailMapper.deleteById(id);
    }

    @Override
        public void deleteMaterialAssayStandardDetailListByIds(List<Long> ids) {
        // 校验存在
        validateMaterialAssayStandardDetailExists(ids);
        // 删除
        materialAssayStandardDetailMapper.deleteByIds(ids);
        }

    private void validateMaterialAssayStandardDetailExists(List<Long> ids) {
        List<MaterialAssayStandardDetailDO> list = materialAssayStandardDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MATERIAL_ASSAY_STANDARD_DETAIL_NOT_EXISTS);
        }
    }

    private void validateMaterialAssayStandardDetailExists(Long id) {
        if (materialAssayStandardDetailMapper.selectById(id) == null) {
            throw exception(MATERIAL_ASSAY_STANDARD_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public MaterialAssayStandardDetailDO getMaterialAssayStandardDetail(Long id) {
        return materialAssayStandardDetailMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialAssayStandardDetailDO> getMaterialAssayStandardDetailPage(MaterialAssayStandardDetailPageReqVO pageReqVO) {
        return materialAssayStandardDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MaterialAssayStandardDetailRespVO> getListWithExtendByIds(List<Long> ids) {
        return materialAssayStandardDetailMapper.selectExtendByIds(ids);
    }

}