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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAssessmentProjectPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAssessmentProjectRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAssessmentProjectSaveReqVO;
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.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleAssessmentProjectDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleAssessmentProjectMapper;
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 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 BusinessSubSampleAssessmentProjectServiceImpl implements BusinessSubSampleAssessmentProjectService {

    @Resource
    private BusinessSubSampleAssessmentProjectMapper businessSubSampleAssessmentProjectMapper;

    @Override
    public BusinessSubSampleAssessmentProjectRespVO createBusinessSubSampleAssessment(BusinessSubSampleAssessmentProjectSaveReqVO createReqVO) {
        // 插入
        BusinessSubSampleAssessmentProjectDO businessSubSampleAssessment = BeanUtils.toBean(createReqVO, BusinessSubSampleAssessmentProjectDO.class);
        businessSubSampleAssessmentProjectMapper.insert(businessSubSampleAssessment);
        // 返回
        return BeanUtils.toBean(businessSubSampleAssessment, BusinessSubSampleAssessmentProjectRespVO.class);
    }

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

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

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

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

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

    @Override
    public BusinessSubSampleAssessmentProjectDO getBusinessSubSampleAssessment(Long id) {
        return businessSubSampleAssessmentProjectMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubSampleAssessmentProjectDO> getBusinessSubSampleAssessmentPage(BusinessSubSampleAssessmentProjectPageReqVO pageReqVO) {
        return businessSubSampleAssessmentProjectMapper.selectPage(pageReqVO);
    }

}