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

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

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ai.entity.PreExaminationMatters;
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.PreExaminationMattersMapper;
import org.jeecg.modules.ai.mapper.YsDirectoryMapper;
import org.jeecg.modules.ai.qo.PreExaminationMattersQuery;
import org.jeecg.modules.ai.service.IPreExaminationMattersService;
import org.jeecg.modules.ai.service.IYsDirectoryService;
import org.jeecg.modules.ai.vo.PreExaminationMattersVo;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 PreExaminationMattersServiceImpl extends ServiceImpl<PreExaminationMattersMapper, PreExaminationMatters> implements IPreExaminationMattersService {

	
	@Resource
	private PreExaminationMattersMapper preExaminationMattersMapper;
	@Resource
	private IYsDirectoryService iYsDirectoryService;
	
	
	/**
	 * 根据参数关联查询事项信息
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	@Override
	public IPage<PreExaminationMattersVo> getPreExaminationMattersList(Page page, PreExaminationMattersQuery query){
		return preExaminationMattersMapper.getPreExaminationMattersList(page, query);
	}
	
	/**
	 * 添加预审事项
	 * @param preExaminationMatters
	 * @return
	 */
	@Override
	public boolean saveInfo(String ids) {
		
		LoginUser user= (LoginUser)SecurityUtils.getSubject().getPrincipal();
		List<String> idsList = Arrays.asList(ids.split(","));
		List<YsDirectory> listYsDirectory = iYsDirectoryService.listByIds(idsList);
		
		listYsDirectory.stream().forEach(ysDirectory -> {
			
			//查询是否已添加
			LambdaQueryWrapper<PreExaminationMatters> queryPreExaminationMatters = new LambdaQueryWrapper<>();
			queryPreExaminationMatters.eq(PreExaminationMatters::getEventCode, ysDirectory.getEventCode());
			queryPreExaminationMatters.eq(PreExaminationMatters::getDelFlag, 0);
			PreExaminationMatters preExaminationMattersOld = preExaminationMattersMapper.selectOne(queryPreExaminationMatters);
			if(preExaminationMattersOld==null) {
				PreExaminationMatters preExaminationMatters = new PreExaminationMatters();
				preExaminationMatters.setDisplayName(ysDirectory.getEventName());
				preExaminationMatters.setEventCode(ysDirectory.getEventCode());
				preExaminationMatters.setSysOrgCode(ysDirectory.getSysOrgCode());
				preExaminationMatters.setCreateTime(new Date());
				preExaminationMatters.setCreateBy(user.getUsername());
				preExaminationMatters.setProcessingType(ysDirectory.getEvtentType());
				preExaminationMattersMapper.insert(preExaminationMatters);
			}
		});
		return true;
	}
}
