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.BusinessAssayParameterDataPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayParameterDataRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayParameterDataSaveReqVO;
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.BusinessAssayParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayParameterDataMapper;
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 BusinessAssayParameterDataServiceImpl implements BusinessAssayParameterDataService {

    @Resource
    private BusinessAssayParameterDataMapper businessAssayParameterDataMapper;

    @Override
    public BusinessAssayParameterDataRespVO createBusinessAssayParameterData(BusinessAssayParameterDataSaveReqVO createReqVO) {
        // 插入
        BusinessAssayParameterDataDO businessAssayParameterData = BeanUtils.toBean(createReqVO, BusinessAssayParameterDataDO.class);
        businessAssayParameterDataMapper.insert(businessAssayParameterData);
        // 返回
        return BeanUtils.toBean(businessAssayParameterData, BusinessAssayParameterDataRespVO.class);
    }

    @Override
    public void updateBusinessAssayParameterData(BusinessAssayParameterDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessAssayParameterDataExists(updateReqVO.getId());
        // 更新
        BusinessAssayParameterDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessAssayParameterDataDO.class);
        businessAssayParameterDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessAssayParameterData(Long id) {
        // 校验存在
        validateBusinessAssayParameterDataExists(id);
        // 删除
        businessAssayParameterDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessAssayParameterDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessAssayParameterDataExists(ids);
        // 删除
        businessAssayParameterDataMapper.deleteByIds(ids);
        }

    private void validateBusinessAssayParameterDataExists(List<Long> ids) {
        List<BusinessAssayParameterDataDO> list = businessAssayParameterDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_ASSAY_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessAssayParameterDataExists(Long id) {
        if (businessAssayParameterDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_ASSAY_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessAssayParameterDataDO getBusinessAssayParameterData(Long id) {
        return businessAssayParameterDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessAssayParameterDataDO> getBusinessAssayParameterDataPage(BusinessAssayParameterDataPageReqVO pageReqVO) {
        return businessAssayParameterDataMapper.selectPage(pageReqVO);
    }

}