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.BusinessAssayTaskExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskSaveReqVO;
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.BusinessAssayTaskDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskMapper;

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 BusinessAssayTaskServiceImpl implements BusinessAssayTaskService {

    @Resource
    private BusinessAssayTaskMapper businessAssayTaskMapper;

    @Override
    public BusinessAssayTaskRespVO createBusinessAssayTask(BusinessAssayTaskSaveReqVO createReqVO) {
        // 插入
        BusinessAssayTaskDO businessAssayTask = BeanUtils.toBean(createReqVO, BusinessAssayTaskDO.class);
        businessAssayTaskMapper.insert(businessAssayTask);
        // 返回
        return BeanUtils.toBean(businessAssayTask, BusinessAssayTaskRespVO.class);
    }

    @Override
    public void updateBusinessAssayTask(BusinessAssayTaskSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessAssayTaskExists(updateReqVO.getId());
        // 更新
        BusinessAssayTaskDO updateObj = BeanUtils.toBean(updateReqVO, BusinessAssayTaskDO.class);
        businessAssayTaskMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusinessAssayTask(Long id) {
        // 校验存在
        validateBusinessAssayTaskExists(id);
        // 删除
        businessAssayTaskMapper.deleteById(id);
    }

    @Override
        public void deleteBusinessAssayTaskListByIds(List<Long> ids) {
        // 校验存在
        validateBusinessAssayTaskExists(ids);
        // 删除
        businessAssayTaskMapper.deleteByIds(ids);
        }

    private void validateBusinessAssayTaskExists(List<Long> ids) {
        List<BusinessAssayTaskDO> list = businessAssayTaskMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BUSINESS_ASSAY_TASK_NOT_EXISTS);
        }
    }

    private void validateBusinessAssayTaskExists(Long id) {
        if (businessAssayTaskMapper.selectById(id) == null) {
            throw exception(BUSINESS_ASSAY_TASK_NOT_EXISTS);
        }
    }

    @Override
    public BusinessAssayTaskDO getBusinessAssayTask(Long id) {
        return businessAssayTaskMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessAssayTaskExtendRespVO> getBusinessAssayTaskPage(BusinessAssayTaskPageReqVO pageReqVO) {
        return businessAssayTaskMapper.selectPage(pageReqVO);
    }

}