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.BusinessSubSampleAnalysisGroupPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAnalysisGroupRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAnalysisGroupSaveReqVO;
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.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.BusinessSubSampleAnalysisGroupDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleAnalysisGroupMapper;

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 BusinessSubSampleAnalysisGroupServiceImpl implements BusinessSubSampleAnalysisGroupService {

    @Resource
    private BusinessSubSampleAnalysisGroupMapper businessSubSampleAnalysisGroupMapper;

    @Override
    public BusinessSubSampleAnalysisGroupRespVO createBusinessSubSampleAnalysisGroup(BusinessSubSampleAnalysisGroupSaveReqVO createReqVO) {
        // 插入
        BusinessSubSampleAnalysisGroupDO businessSubSampleAnalysisGroup = BeanUtils.toBean(createReqVO, BusinessSubSampleAnalysisGroupDO.class);
        businessSubSampleAnalysisGroupMapper.insert(businessSubSampleAnalysisGroup);
        // 返回
        return BeanUtils.toBean(businessSubSampleAnalysisGroup, BusinessSubSampleAnalysisGroupRespVO.class);
    }

    @Override
    public void updateBusinessSubSampleAnalysisGroup(BusinessSubSampleAnalysisGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSubSampleAnalysisGroupExists(updateReqVO.getId());
        // 更新
        BusinessSubSampleAnalysisGroupDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSubSampleAnalysisGroupDO.class);
        businessSubSampleAnalysisGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSubSampleAnalysisGroup(Long id) {
        // 校验存在
        validateBusinessSubSampleAnalysisGroupExists(id);
        // 删除
        businessSubSampleAnalysisGroupMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSubSampleAnalysisGroupListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSubSampleAnalysisGroupExists(ids);
        // 删除
        businessSubSampleAnalysisGroupMapper.deleteByIds(ids);
        }

    private void validateBusinessSubSampleAnalysisGroupExists(List<Long> ids) {
        List<BusinessSubSampleAnalysisGroupDO> list = businessSubSampleAnalysisGroupMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SUB_SAMPLE_ANALYSIS_GROUP_NOT_EXISTS);
        }
    }

    private void validateBusinessSubSampleAnalysisGroupExists(Long id) {
        if (businessSubSampleAnalysisGroupMapper.selectById(id) == null) {
            throw exception(BUSINESS_SUB_SAMPLE_ANALYSIS_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSubSampleAnalysisGroupDO getBusinessSubSampleAnalysisGroup(Long id) {
        return businessSubSampleAnalysisGroupMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSubSampleAnalysisGroupDO> getBusinessSubSampleAnalysisGroupPage(BusinessSubSampleAnalysisGroupPageReqVO pageReqVO) {
        return businessSubSampleAnalysisGroupMapper.selectPage(pageReqVO);
    }

}