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

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 后台管理-1
 */
@Service
@Validated
public class BusinessAssayTaskParameterDataServiceImpl implements BusinessAssayTaskParameterDataService {

    @Resource
    private BusinessAssayTaskParameterDataMapper businessAssayTaskParameterDataMapper;

    @Override
    public BusinessAssayTaskParameterDataRespVO createBusinessAssayTaskParameterData(BusinessAssayTaskParameterDataSaveReqVO createReqVO) {
        // 插入
        BusinessAssayTaskParameterDataDO businessAssayTaskParameterData = BeanUtils.toBean(createReqVO, BusinessAssayTaskParameterDataDO.class);
        businessAssayTaskParameterDataMapper.insert(businessAssayTaskParameterData);
        // 返回
        return BeanUtils.toBean(businessAssayTaskParameterData, BusinessAssayTaskParameterDataRespVO.class);
    }

    @Override
    public void updateBusinessAssayTaskParameterData(BusinessAssayTaskParameterDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessAssayTaskParameterDataExists(updateReqVO.getId());
        // 更新
        BusinessAssayTaskParameterDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessAssayTaskParameterDataDO.class);
        businessAssayTaskParameterDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessAssayTaskParameterData(Long id) {
        // 校验存在
        validateBusinessAssayTaskParameterDataExists(id);
        // 删除
        businessAssayTaskParameterDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessAssayTaskParameterDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessAssayTaskParameterDataExists(ids);
        // 删除
        businessAssayTaskParameterDataMapper.deleteByIds(ids);
        }

    private void validateBusinessAssayTaskParameterDataExists(List<Long> ids) {
        List<BusinessAssayTaskParameterDataDO> list = businessAssayTaskParameterDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_ASSAY_TASK_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessAssayTaskParameterDataExists(Long id) {
        if (businessAssayTaskParameterDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_ASSAY_TASK_PARAMETER_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessAssayTaskParameterDataDO getBusinessAssayTaskParameterData(Long id) {
        return businessAssayTaskParameterDataMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessAssayTaskParameterDataDO> getBusinessAssayTaskParameterDataPage(BusinessAssayTaskParameterDataPageReqVO pageReqVO) {
        return businessAssayTaskParameterDataMapper.selectPage(pageReqVO);
    }

}