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

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 BusinessAssayTaskDetailServiceImpl implements BusinessAssayTaskDetailService {

    @Resource
    private BusinessAssayTaskDetailMapper businessAssayTaskDetailMapper;

    @Override
    public BusinessAssayTaskDetailRespVO createBusinessAssayTaskDetail(BusinessAssayTaskDetailSaveReqVO createReqVO) {
        // 插入
        BusinessAssayTaskDetailDO businessAssayTaskDetail = BeanUtils.toBean(createReqVO, BusinessAssayTaskDetailDO.class);
        businessAssayTaskDetailMapper.insert(businessAssayTaskDetail);
        // 返回
        return BeanUtils.toBean(businessAssayTaskDetail, BusinessAssayTaskDetailRespVO.class);
    }

    @Override
    public void updateBusinessAssayTaskDetail(BusinessAssayTaskDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessAssayTaskDetailExists(updateReqVO.getId());
        // 更新
        BusinessAssayTaskDetailDO updateObj = BeanUtils.toBean(updateReqVO, BusinessAssayTaskDetailDO.class);
        businessAssayTaskDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessAssayTaskDetail(Long id) {
        // 校验存在
        validateBusinessAssayTaskDetailExists(id);
        // 删除
        businessAssayTaskDetailMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessAssayTaskDetailListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessAssayTaskDetailExists(ids);
        // 删除
        businessAssayTaskDetailMapper.deleteByIds(ids);
        }

    private void validateBusinessAssayTaskDetailExists(List<Long> ids) {
        List<BusinessAssayTaskDetailDO> list = businessAssayTaskDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_ASSAY_TASK_DETAIL_NOT_EXISTS);
        }
    }

    private void validateBusinessAssayTaskDetailExists(Long id) {
        if (businessAssayTaskDetailMapper.selectById(id) == null) {
            throw exception(BUSINESS_ASSAY_TASK_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public BusinessAssayTaskDetailDO getBusinessAssayTaskDetail(Long id) {
        return businessAssayTaskDetailMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessAssayTaskDetailExtendRespVO> getBusinessAssayTaskDetailPage(BusinessAssayTaskDetailPageReqVO pageReqVO) {
        return businessAssayTaskDetailMapper.selectPage(pageReqVO);
    }

	@Override
	public List<BusinessAssayTaskDetailExtendRespVO> getBusinessAssayTaskDetailList(BusinessAssayTaskDetailReqVO reqVO) {
		return businessAssayTaskDetailMapper.selectList(reqVO);
	}

}