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 jakarta.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.zt.plat.framework.common.pojo.PageResult;
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.dataobject.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskMapper;
import com.zt.plat.module.qms.business.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodProjectMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigQCSampleMethodMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 检测任务分配业务 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class BusinessAssayTaskServiceImpl implements BusinessAssayTaskService {

    @Resource
    private BusinessAssayTaskMapper businessAssayTaskMapper;
    
    @Resource
    private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
    
    @Resource
    private ConfigAssayMethodMapper configAssayMethodMapper;
    
    @Resource
    private ConfigAssayMethodProjectMapper configAssayMethodProjectMapper;
    
    @Resource
    private ConfigQCSampleMethodMapper configQCSampleMethodMapper;

    @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 BusinessAssayTaskExtendRespVO getBusinessAssayTask(Long id) {
    	BusinessAssayTaskExtendRespVO businessAssayTaskExtendRespVO = businessAssayTaskMapper.selectBusinessAssayTaskById(id);
    	List<BusinessAssayTaskDataDO> businessAssayTaskDataList = businessAssayTaskDataMapper.selectByBusinessAssayTaskId(id);
    	List<Long> configAssayMethodIdList = businessAssayTaskDataList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
    	List<ConfigAssayMethodDO> configAssayMethodList = configAssayMethodMapper.selectByIds(configAssayMethodIdList);
    	String configAssayMethodNames = configAssayMethodList.stream().map(m -> m.getName()).collect(Collectors.joining("、"));
    	businessAssayTaskExtendRespVO.setConfigAssayMethodNames(configAssayMethodNames);
//    	List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectByConfigAssayMethodId(businessAssayTaskExtendRespVO.getConfigAssayMethodId());
//    	String assayProject = configAssayMethodProjectList.stream().map(m -> m.getShowName()).collect(Collectors.joining(","));
//    	businessAssayTaskExtendRespVO.setConfigAssayMethodProjectShowNames(assayProject);
    	List<ConfigQCSampleMethodExtendRespVO> configQCSampleMethodList = configQCSampleMethodMapper.selectByConfigAssayMethodId(configAssayMethodIdList.get(0));
//    	List<ConfigQCSampleMethodExtendRespVO> configQCSampleMethodList = configQCSampleMethodMapper.selectByConfigAssayMethodIdList(configAssayMethodIdList);
    	businessAssayTaskExtendRespVO.setConfigQCSampleMethodList(configQCSampleMethodList);
        return businessAssayTaskExtendRespVO;
    }

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

	@Override
	public List<BusinessAssayTaskExtendRespVO> getBusinessAssayTaskList(BusinessAssayTaskPageReqVO reqVO) {
		return businessAssayTaskMapper.selectList(reqVO);
	}

}