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.BusinessSubSampleAssessmentDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleAssessmentMapper;

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 BusinessSubSampleAssessmentServiceImpl implements BusinessSubSampleAssessmentService {

    @Resource
    private BusinessSubSampleAssessmentMapper businessSubSampleAssessmentMapper;

    @Override
    public BusinessSubSampleAssessmentRespVO createBusinessSubSampleAssessment(BusinessSubSampleAssessmentSaveReqVO createReqVO) {
        // 插入
        BusinessSubSampleAssessmentDO businessSubSampleAssessment = BeanUtils.toBean(createReqVO, BusinessSubSampleAssessmentDO.class);
        businessSubSampleAssessmentMapper.insert(businessSubSampleAssessment);
        // 返回
        return BeanUtils.toBean(businessSubSampleAssessment, BusinessSubSampleAssessmentRespVO.class);
    }

    @Override
    public void updateBusinessSubSampleAssessment(BusinessSubSampleAssessmentSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSubSampleAssessmentExists(updateReqVO.getId());
        // 更新
        BusinessSubSampleAssessmentDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSubSampleAssessmentDO.class);
        businessSubSampleAssessmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSubSampleAssessment(Long id) {
        // 校验存在
        validateBusinessSubSampleAssessmentExists(id);
        // 删除
        businessSubSampleAssessmentMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSubSampleAssessmentListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSubSampleAssessmentExists(ids);
        // 删除
        businessSubSampleAssessmentMapper.deleteByIds(ids);
        }

    private void validateBusinessSubSampleAssessmentExists(List<Long> ids) {
        List<BusinessSubSampleAssessmentDO> list = businessSubSampleAssessmentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SUB_SAMPLE_ASSESSMENT_NOT_EXISTS);
        }
    }

    private void validateBusinessSubSampleAssessmentExists(Long id) {
        if (businessSubSampleAssessmentMapper.selectById(id) == null) {
            throw exception(BUSINESS_SUB_SAMPLE_ASSESSMENT_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSubSampleAssessmentDO getBusinessSubSampleAssessment(Long id) {
        return businessSubSampleAssessmentMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubSampleAssessmentDO> getBusinessSubSampleAssessmentPage(BusinessSubSampleAssessmentPageReqVO pageReqVO) {
        return businessSubSampleAssessmentMapper.selectPage(pageReqVO);
    }

}