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.BusinessSampleAssayResultPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleAssayResultRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleAssayResultSaveReqVO;
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.BusinessSampleAssayResultDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleAssayResultMapper;

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 BusinessSampleAssayResultServiceImpl implements BusinessSampleAssayResultService {

    @Resource
    private BusinessSampleAssayResultMapper businessSampleAssayResultMapper;

    @Override
    public BusinessSampleAssayResultRespVO createBusinessSampleAssayResult(BusinessSampleAssayResultSaveReqVO createReqVO) {
        // 插入
        BusinessSampleAssayResultDO businessSampleAssayResult = BeanUtils.toBean(createReqVO, BusinessSampleAssayResultDO.class);
        businessSampleAssayResultMapper.insert(businessSampleAssayResult);
        // 返回
        return BeanUtils.toBean(businessSampleAssayResult, BusinessSampleAssayResultRespVO.class);
    }

    @Override
    public void updateBusinessSampleAssayResult(BusinessSampleAssayResultSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessSampleAssayResultExists(updateReqVO.getId());
        // 更新
        BusinessSampleAssayResultDO updateObj = BeanUtils.toBean(updateReqVO, BusinessSampleAssayResultDO.class);
        businessSampleAssayResultMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessSampleAssayResult(Long id) {
        // 校验存在
        validateBusinessSampleAssayResultExists(id);
        // 删除
        businessSampleAssayResultMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessSampleAssayResultListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessSampleAssayResultExists(ids);
        // 删除
        businessSampleAssayResultMapper.deleteByIds(ids);
        }

    private void validateBusinessSampleAssayResultExists(List<Long> ids) {
        List<BusinessSampleAssayResultDO> list = businessSampleAssayResultMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_SAMPLE_ASSAY_RESULT_NOT_EXISTS);
        }
    }

    private void validateBusinessSampleAssayResultExists(Long id) {
        if (businessSampleAssayResultMapper.selectById(id) == null) {
            throw exception(BUSINESS_SAMPLE_ASSAY_RESULT_NOT_EXISTS);
        }
    }

    @Override
    public BusinessSampleAssayResultDO getBusinessSampleAssayResult(Long id) {
        return businessSampleAssayResultMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessSampleAssayResultDO> getBusinessSampleAssayResultPage(BusinessSampleAssayResultPageReqVO pageReqVO) {
        return businessSampleAssayResultMapper.selectPage(pageReqVO);
    }

}