package com.gci.boot.modules.basedata.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.constant.CommonConstant;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.basedata.entity.TbInspectionTemplateProject;
import com.gci.boot.modules.basedata.entity.TbVehicleCheckTemplate;
import com.gci.boot.modules.basedata.entity.TbVehicleCheckTemplateProject;
import com.gci.boot.modules.basedata.mapper.TbVehicleCheckTemplateProjectMapper;
import com.gci.boot.modules.basedata.service.ITbVehicleCheckTemplateProjectService;

/**
 * <p>
 * 车辆验收模板项目 服务实现类
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-07
 */
@Service
public class TbVehicleCheckTemplateProjectServiceImpl extends ServiceImpl<TbVehicleCheckTemplateProjectMapper, TbVehicleCheckTemplateProject> implements ITbVehicleCheckTemplateProjectService {

	@Override
	public void getProjectsByTemplateId(TbVehicleCheckTemplate template) {
		/*LinkedHashMap<String,List<TbVehicleCheckTemplateProject>> projects = new LinkedHashMap<>();*/
		LambdaQueryWrapper<TbVehicleCheckTemplateProject> queryWrapper = new LambdaQueryWrapper<>();
		//queryWrapper.eq(TbVehicleCheckTemplateProject::getDelFlag, CommonConstant.DEL_FLAG_0);
		queryWrapper.eq(TbVehicleCheckTemplateProject::getTemplateId, template.getId());
		queryWrapper.orderByAsc(TbVehicleCheckTemplateProject::getSort);
		List<TbVehicleCheckTemplateProject> list = list(queryWrapper);
		template.setProjectList(list);
		/*for (TbVehicleCheckTemplateProject tbVehicleCheckTemplateProject : list) {
			List<TbVehicleCheckTemplateProject> project = projects.getOrDefault(tbVehicleCheckTemplateProject.getName(), new ArrayList<TbVehicleCheckTemplateProject>());
			project.add(tbVehicleCheckTemplateProject);
			projects.put(tbVehicleCheckTemplateProject.getName(), project);
		}
		template.setProjects(projects);
		String projectNames = String.join(",", projects.keySet());*/
		Set<String> con = new LinkedHashSet<>();
		for (TbVehicleCheckTemplateProject pro : list) {
			con.add(pro.getName());
		}
		String projectNames = String.join(",", con);
		template.setProjectNames(projectNames);
	}
	
	@Override
	public void saveItem(TbVehicleCheckTemplate template) {
		QueryWrapper<TbVehicleCheckTemplateProject> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("template_id", template.getId());
		remove(queryWrapper);
		
		List<TbVehicleCheckTemplateProject> projects = new ArrayList<>();
		int sort = 1;
		for(TbVehicleCheckTemplateProject project:template.getProjectList()) {
			project.setSort(sort);
			project.setTemplateId(template.getId());
			//project.setName(key);
			//project.setDelFlag(CommonConstant.DEL_FLAG_0);
			projects.add(project);
			sort++;
		}
		saveBatch(projects);
	}
	
	@Override
	public void checkTemplate(TbVehicleCheckTemplate template) {
		if(template.getProjectList()!=null&&template.getProjectList().size()>0) {
			Set<String> name = new HashSet<>();
			Set<String> con = new HashSet<>();
			
			for(TbVehicleCheckTemplateProject project:template.getProjectList()) {
				name.add(project.getName());
				con.add(project.getName()+project.getContent());
			}
			/*if(name.size()>0&&name.size()!=template.getProjectList().size()) {
				throw new GciBootException("验收项目不能重复");
			}*/
			if(con.size()>0&&con.size()!=template.getProjectList().size()) {
				throw new GciBootException("验收内容不能重复");
			}
		}else {
			throw new GciBootException("验收项目不能为空");
		}
	}

	@Override
	public Map<String, TbVehicleCheckTemplateProject> getMapByIds(List<String> ids) {
		List<TbVehicleCheckTemplateProject> list = this.listByIds(ids);
		Map<String,TbVehicleCheckTemplateProject> map = list.stream().collect(Collectors.toMap(TbVehicleCheckTemplateProject::getId,Function.identity()));
		return map;
	}
	
	/*@Override
	public void saveItem(TbVehicleCheckTemplate template) {
		if(template.getProjects()!=null&&template.getProjects().size()>0) {
			
			LambdaQueryWrapper<TbVehicleCheckTemplateProject> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(TbVehicleCheckTemplateProject::getTemplateId, template.getId());
			remove(queryWrapper);
			
			List<TbVehicleCheckTemplateProject> projects = new ArrayList<>();
			int sort = 1;
			for(String key:template.getProjects().keySet()) {
				Set<String> con = new HashSet<>();
				for(TbVehicleCheckTemplateProject project:template.getProjects().get(key)) {
					project.setSort(sort);
					project.setName(key);
					project.setTemplateId(template.getId());
					projects.add(project);
					con.add(project.getContent());
					sort++;
				}
				if(con.size()>0&&con.size()!=template.getProjects().get(key).size()) {
					throw new GciBootException("验收内容不能重复");
				}
			}
			saveOrUpdateBatch(projects);
		}else {
			throw new GciBootException("验收项目不能为空");
		}
	}*/
	
}
