package org.jeecg.modules.ai.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.jeecg.modules.ai.entity.PreReviewMaterial;
import org.jeecg.modules.ai.entity.YsDirectory;
import org.jeecg.modules.ai.entity.YsDirectoryMaterial;
import org.jeecg.modules.ai.mapper.PreReviewMaterialMapper;
import org.jeecg.modules.ai.mapper.YsDirectoryMapper;
import org.jeecg.modules.ai.mapper.YsDirectoryMaterialMapper;
import org.jeecg.modules.ai.qo.PreExaminationMattersQuery;
import org.jeecg.modules.ai.qo.PreReviewMaterialQuery;
import org.jeecg.modules.ai.service.IPreReviewMaterialService;
import org.jeecg.modules.ai.service.IYsDirectoryMaterialService;
import org.jeecg.modules.ai.vo.PreExaminationMattersVo;
import org.jeecg.modules.ai.vo.PreReviewMaterialVo;
import org.jeecg.modules.system.entity.MaterialBag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 通用预审材料
 * @Author: jeecg-boot
 * @Date:   2023-11-25
 * @Version: V1.0
 */
@Service
public class PreReviewMaterialServiceImpl extends ServiceImpl<PreReviewMaterialMapper, PreReviewMaterial> implements IPreReviewMaterialService {

	@Resource
	private PreReviewMaterialMapper preReviewMaterialMapper;
	@Resource
	private YsDirectoryMaterialMapper ysDirectoryMaterialMapper;
	@Resource
	private YsDirectoryMapper ysDirectoryMapper;
	@Autowired
	private IYsDirectoryMaterialService iYsDirectoryMaterialService;
	
	/**
	 * 根据参数关联查询事项信息(分页)
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	@Override
	public IPage<PreReviewMaterialVo> getPreReviewMaterialPage(Page page, PreReviewMaterialQuery query){
		return preReviewMaterialMapper.getPreReviewMaterialList(page, query);
	}
	
	/**
	 * 根据参数关联查询事项信息
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	@Override
	public List<PreReviewMaterialVo> getPreReviewMaterialList(String eventCode){
		PreReviewMaterialQuery query = new PreReviewMaterialQuery();
    	query.setEventCode(eventCode);
    	query.setValidStatus(1);
		return preReviewMaterialMapper.getPreReviewMaterialList(query);
	}
	
	/**
	 * 保存通用预审资料
	 * @param preReviewMaterial
	 */
	@Override
	public boolean saveInfo(String ids) {
		
		List<String> idsList = Arrays.asList(ids.split(","));
		List<YsDirectoryMaterial> listYsDirectoryMaterial = iYsDirectoryMaterialService.listByIds(idsList);
		
		listYsDirectoryMaterial.stream().forEach(ysDirectoryMaterial -> {
			YsDirectory ysDirectory = ysDirectoryMapper.selectById(ysDirectoryMaterial.getDirId());
			
			//查询是否已经存在
			LambdaQueryWrapper<PreReviewMaterial> queryPreReviewMaterial = new LambdaQueryWrapper<>();
			queryPreReviewMaterial.eq(PreReviewMaterial::getMaterialCode, ysDirectoryMaterial.getMaterialCode());
			PreReviewMaterial preReviewMaterialOld = preReviewMaterialMapper.selectOne(queryPreReviewMaterial);
			if(preReviewMaterialOld==null) {
				PreReviewMaterial preReviewMaterial = new PreReviewMaterial();
				preReviewMaterial.setMaterialCode(ysDirectoryMaterial.getMaterialCode());
				preReviewMaterial.setSysOrgCode(ysDirectoryMaterial.getSysOrgCode());
				preReviewMaterial.setEventCode(ysDirectory.getEventCode());
				preReviewMaterialMapper.insert(preReviewMaterial);
			}
			
        });
		return true;
	}
	
}
