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 java.util.*;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayProjectDataDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayProjectDataMapper;
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 BusinessAssayProjectDataServiceImpl implements BusinessAssayProjectDataService {

    @Resource
    private BusinessAssayProjectDataMapper businessAssayProjectDataMapper;

    @Override
    public BusinessAssayProjectDataRespVO createBusinessAssayProjectData(BusinessAssayProjectDataSaveReqVO createReqVO) {
        // 插入
        BusinessAssayProjectDataDO businessAssayProjectData = BeanUtils.toBean(createReqVO, BusinessAssayProjectDataDO.class);
        businessAssayProjectDataMapper.insert(businessAssayProjectData);
        // 返回
        return BeanUtils.toBean(businessAssayProjectData, BusinessAssayProjectDataRespVO.class);
    }

    @Override
    public void updateBusinessAssayProjectData(BusinessAssayProjectDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessAssayProjectDataExists(updateReqVO.getId());
        // 更新
        BusinessAssayProjectDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessAssayProjectDataDO.class);
        businessAssayProjectDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessAssayProjectData(Long id) {
        // 校验存在
        validateBusinessAssayProjectDataExists(id);
        // 删除
        businessAssayProjectDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessAssayProjectDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessAssayProjectDataExists(ids);
        // 删除
        businessAssayProjectDataMapper.deleteByIds(ids);
        }

    private void validateBusinessAssayProjectDataExists(List<Long> ids) {
        List<BusinessAssayProjectDataDO> list = businessAssayProjectDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_ASSAY_PROJECT_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessAssayProjectDataExists(Long id) {
        if (businessAssayProjectDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_ASSAY_PROJECT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessAssayProjectDataDO getBusinessAssayProjectData(Long id) {
        return businessAssayProjectDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessAssayProjectDataDO> getBusinessAssayProjectDataPage(BusinessAssayProjectDataPageReqVO pageReqVO) {
        return businessAssayProjectDataMapper.selectPage(pageReqVO);
    }

	@Override
	public List<BusinessAssayProjectDataExtendRespVO> getBusinessAssayProjectDataList(BusinessAssayProjectDataReqVO reqVO) {
		return businessAssayProjectDataMapper.selectList(reqVO);
	}

}