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

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 BusinessQCProjectDataServiceImpl implements BusinessQCProjectDataService {

    @Resource
    private BusinessQCProjectDataMapper businessQCProjectDataMapper;

    @Override
    public BusinessQCProjectDataRespVO createBusinessQCProjectData(BusinessQCProjectDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCProjectDataDO businessQCProjectData = BeanUtils.toBean(createReqVO, BusinessQCProjectDataDO.class);
        businessQCProjectDataMapper.insert(businessQCProjectData);
        // 返回
        return BeanUtils.toBean(businessQCProjectData, BusinessQCProjectDataRespVO.class);
    }

    @Override
    public void updateBusinessQCProjectData(BusinessQCProjectDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCProjectDataExists(updateReqVO.getId());
        // 更新
        BusinessQCProjectDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCProjectDataDO.class);
        businessQCProjectDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCProjectData(Long id) {
        // 校验存在
        validateBusinessQCProjectDataExists(id);
        // 删除
        businessQCProjectDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCProjectDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCProjectDataExists(ids);
        // 删除
        businessQCProjectDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCProjectDataExists(List<Long> ids) {
        List<BusinessQCProjectDataDO> list = businessQCProjectDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_PROJECT_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCProjectDataExists(Long id) {
        if (businessQCProjectDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_PROJECT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCProjectDataDO getBusinessQCProjectData(Long id) {
        return businessQCProjectDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCProjectDataDO> getBusinessQCProjectDataPage(BusinessQCProjectDataPageReqVO pageReqVO) {
        return businessQCProjectDataMapper.selectPage(pageReqVO);
    }

}