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 org.springframework.transaction.annotation.Transactional;

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.BusinessXRFDataDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessXRFDataMapper;

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 BusinessXRFDataServiceImpl implements BusinessXRFDataService {

    @Resource
    private BusinessXRFDataMapper businessXRFDataMapper;

    @Override
    public BusinessXRFDataRespVO createBusinessXRFData(BusinessXRFDataSaveReqVO createReqVO) {
        // 插入
        BusinessXRFDataDO businessXRFData = BeanUtils.toBean(createReqVO, BusinessXRFDataDO.class);
        businessXRFDataMapper.insert(businessXRFData);
        // 返回
        return BeanUtils.toBean(businessXRFData, BusinessXRFDataRespVO.class);
    }

    @Override
    public void updateBusinessXRFData(BusinessXRFDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessXRFDataExists(updateReqVO.getId());
        // 更新
        BusinessXRFDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessXRFDataDO.class);
        businessXRFDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessXRFData(Long id) {
        // 校验存在
        validateBusinessXRFDataExists(id);
        // 删除
        businessXRFDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessXRFDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessXRFDataExists(ids);
        // 删除
        businessXRFDataMapper.deleteByIds(ids);
        }

    private void validateBusinessXRFDataExists(List<Long> ids) {
        List<BusinessXRFDataDO> list = businessXRFDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_XRF_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessXRFDataExists(Long id) {
        if (businessXRFDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_XRF_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessXRFDataDO getBusinessXRFData(Long id) {
        return businessXRFDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessXRFDataDO> getBusinessXRFDataPage(BusinessXRFDataPageReqVO pageReqVO) {
        return businessXRFDataMapper.selectPage(pageReqVO);
    }

}