package com.zt.plat.module.qms.business.bus.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.util.object.BeanUtils;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubParentSampleAssessmentProjectDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubParentSampleAssessmentProjectMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 班组判定检测项目数据业务 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class BusinessSubParentSampleAssessmentProjectServiceImpl implements BusinessSubParentSampleAssessmentProjectService {

    @Resource
    private BusinessSubParentSampleAssessmentProjectMapper businessSubParentSampleAssessmentProjectMapper;

    @Override
    public BusinessSubParentSampleAssessmentProjectRespVO createBusinessSubParentSampleAssessment(BusinessSubParentSampleAssessmentProjectSaveReqVO createReqVO) {
        // 插入
        BusinessSubParentSampleAssessmentProjectDO businessSubParentSampleAssessment = BeanUtils.toBean(createReqVO, BusinessSubParentSampleAssessmentProjectDO.class);
        businessSubParentSampleAssessmentProjectMapper.insert(businessSubParentSampleAssessment);
        // 返回
        return BeanUtils.toBean(businessSubParentSampleAssessment, BusinessSubParentSampleAssessmentProjectRespVO.class);
    }

    @Override
    public void updateBusinessSubParentSampleAssessment(BusinessSubParentSampleAssessmentProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSubParentSampleAssessmentExists(updateReqVO.getId());
        // 更新
        BusinessSubParentSampleAssessmentProjectDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSubParentSampleAssessmentProjectDO.class);
        businessSubParentSampleAssessmentProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSubParentSampleAssessment(Long id) {
        // 校验存在
        validateBusinessSubParentSampleAssessmentExists(id);
        // 删除
        businessSubParentSampleAssessmentProjectMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSubParentSampleAssessmentListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSubParentSampleAssessmentExists(ids);
        // 删除
        businessSubParentSampleAssessmentProjectMapper.deleteByIds(ids);
        }

    private void validateBusinessSubParentSampleAssessmentExists(List<Long> ids) {
        List<BusinessSubParentSampleAssessmentProjectDO> list = businessSubParentSampleAssessmentProjectMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SUB_PARENT_SAMPLE_ASSESSMENT_PROJECT_NOT_EXISTS);
        }
    }

    private void validateBusinessSubParentSampleAssessmentExists(Long id) {
        if (businessSubParentSampleAssessmentProjectMapper.selectById(id) == null) {
            throw exception(BUSINESS_SUB_PARENT_SAMPLE_ASSESSMENT_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSubParentSampleAssessmentProjectDO getBusinessSubParentSampleAssessment(Long id) {
        return businessSubParentSampleAssessmentProjectMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubParentSampleAssessmentProjectDO> getBusinessSubParentSampleAssessmentPage(BusinessSubParentSampleAssessmentProjectPageReqVO pageReqVO) {
        return businessSubParentSampleAssessmentProjectMapper.selectPage(pageReqVO);
    }

}