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

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 BusinessQCManagementProjectDataServiceImpl implements BusinessQCManagementProjectDataService {

    @Resource
    private BusinessQCManagementProjectDataMapper businessQCManagementProjectDataMapper;

    @Override
    public BusinessQCManagementProjectDataRespVO createBusinessQCManagementProjectData(BusinessQCManagementProjectDataSaveReqVO createReqVO) {
        // 插入
        BusinessQCManagementProjectDataDO businessQCManagementProjectData = BeanUtils.toBean(createReqVO, BusinessQCManagementProjectDataDO.class);
        businessQCManagementProjectDataMapper.insert(businessQCManagementProjectData);
        // 返回
        return BeanUtils.toBean(businessQCManagementProjectData, BusinessQCManagementProjectDataRespVO.class);
    }

    @Override
    public void updateBusinessQCManagementProjectData(BusinessQCManagementProjectDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessQCManagementProjectDataExists(updateReqVO.getId());
        // 更新
        BusinessQCManagementProjectDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessQCManagementProjectDataDO.class);
        businessQCManagementProjectDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessQCManagementProjectData(Long id) {
        // 校验存在
        validateBusinessQCManagementProjectDataExists(id);
        // 删除
        businessQCManagementProjectDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessQCManagementProjectDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessQCManagementProjectDataExists(ids);
        // 删除
        businessQCManagementProjectDataMapper.deleteByIds(ids);
        }

    private void validateBusinessQCManagementProjectDataExists(List<Long> ids) {
        List<BusinessQCManagementProjectDataDO> list = businessQCManagementProjectDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_QC_MANAGEMENT_PROJECT_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessQCManagementProjectDataExists(Long id) {
        if (businessQCManagementProjectDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_QC_MANAGEMENT_PROJECT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessQCManagementProjectDataDO getBusinessQCManagementProjectData(Long id) {
        return businessQCManagementProjectDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessQCManagementProjectDataDO> getBusinessQCManagementProjectDataPage(BusinessQCManagementProjectDataPageReqVO pageReqVO) {
        return businessQCManagementProjectDataMapper.selectPage(pageReqVO);
    }

}