package com.zt.plat.module.qms.business.bus.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDataMapper;
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 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 BusinessAssayTaskDataServiceImpl implements BusinessAssayTaskDataService {

    @Resource
    private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;

    @Override
    public BusinessAssayTaskDataRespVO createBusinessAssayTaskData(BusinessAssayTaskDataSaveReqVO createReqVO) {
        // 插入
        BusinessAssayTaskDataDO businessAssayTaskData = BeanUtils.toBean(createReqVO, BusinessAssayTaskDataDO.class);
        businessAssayTaskDataMapper.insert(businessAssayTaskData);
        // 返回
        return BeanUtils.toBean(businessAssayTaskData, BusinessAssayTaskDataRespVO.class);
    }

    @Override
    public void updateBusinessAssayTaskData(BusinessAssayTaskDataSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessAssayTaskDataExists(updateReqVO.getId());
        // 更新
        BusinessAssayTaskDataDO updateObj = BeanUtils.toBean(updateReqVO, BusinessAssayTaskDataDO.class);
        businessAssayTaskDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessAssayTaskData(Long id) {
        // 校验存在
        validateBusinessAssayTaskDataExists(id);
        // 删除
        businessAssayTaskDataMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessAssayTaskDataListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessAssayTaskDataExists(ids);
        // 删除
        businessAssayTaskDataMapper.deleteByIds(ids);
        }

    private void validateBusinessAssayTaskDataExists(List<Long> ids) {
        List<BusinessAssayTaskDataDO> list = businessAssayTaskDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_ASSAY_TASK_DATA_NOT_EXISTS);
        }
    }

    private void validateBusinessAssayTaskDataExists(Long id) {
        if (businessAssayTaskDataMapper.selectById(id) == null) {
            throw exception(BUSINESS_ASSAY_TASK_DATA_NOT_EXISTS);
        }
    }

    @Override
    public BusinessAssayTaskDataDO getBusinessAssayTaskData(Long id) {
        return businessAssayTaskDataMapper.selectById(id);
    }

	@Override
	public List<BusinessAssayTaskDataGroupRespVO> getUnAssayTaskGroupList(BusinessAssayTaskDataReqVO reqVO) {
		return businessAssayTaskDataMapper.selectUnAssayTaskGroupList(reqVO);
	}

	@Override
	public List<BusinessAssayTaskDataGroupRespVO> getUnAuditTaskGroupList(BusinessAssayTaskDataReqVO reqVO) {
		return businessAssayTaskDataMapper.selectUnAuditTaskGroupList(reqVO);
	}

	@Override
	public List<BusinessAssayTaskDataExtendRespVO> getBusinessAssayTaskDataList(BusinessAssayTaskDataReqVO reqVO) {
		return businessAssayTaskDataMapper.selectList(reqVO);
	}

    @Override
    public PageResult<BusinessAssayTaskDataExtendRespVO> getBusinessAssayTaskDataPage(BusinessAssayTaskDataPageReqVO pageReqVO) {
        return businessAssayTaskDataMapper.selectPage(pageReqVO);
    }

}