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 org.springframework.transaction.annotation.Transactional;

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.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubParentSampleAssessmentDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubParentSampleAssessmentMapper;

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 BusinessSubParentSampleAssessmentServiceImpl implements BusinessSubParentSampleAssessmentService {

    @Resource
    private BusinessSubParentSampleAssessmentMapper businessSubParentSampleAssessmentMapper;

    @Override
    public BusinessSubParentSampleAssessmentRespVO createBusinessSubParentSampleAssessment(BusinessSubParentSampleAssessmentSaveReqVO createReqVO) {
        // 插入
        BusinessSubParentSampleAssessmentDO businessSubParentSampleAssessment = BeanUtils.toBean(createReqVO, BusinessSubParentSampleAssessmentDO.class);
        businessSubParentSampleAssessmentMapper.insert(businessSubParentSampleAssessment);
        // 返回
        return BeanUtils.toBean(businessSubParentSampleAssessment, BusinessSubParentSampleAssessmentRespVO.class);
    }

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

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

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

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

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

    @Override
    public BusinessSubParentSampleAssessmentDO getBusinessSubParentSampleAssessment(Long id) {
        return businessSubParentSampleAssessmentMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubParentSampleAssessmentDO> getBusinessSubParentSampleAssessmentPage(BusinessSubParentSampleAssessmentPageReqVO pageReqVO) {
        return businessSubParentSampleAssessmentMapper.selectPage(pageReqVO);
    }

}