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

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

import org.springframework.beans.factory.annotation.Autowired;
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.common.util.BeanUtilsCopy;
import com.gci.boot.modules.basedata.entity.TbVehicleCheckTemplateProject;
import com.gci.boot.modules.basedata.service.ITbVehicleCheckTemplateProjectService;
import com.gci.boot.modules.projects.entity.TbVehicleCheck;
import com.gci.boot.modules.projects.entity.TbVehicleCheckProject;
import com.gci.boot.modules.projects.mapper.TbVehicleCheckProjectMapper;
import com.gci.boot.modules.projects.service.ITbVehicleCheckProjectService;

/**
 * <p>
 * 车辆验收项目 服务实现类
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-11
 */
@Service
public class TbVehicleCheckProjectServiceImpl extends ServiceImpl<TbVehicleCheckProjectMapper, TbVehicleCheckProject> implements ITbVehicleCheckProjectService {

	@Autowired
	private ITbVehicleCheckTemplateProjectService tbVehicleCheckTemplateProjectService;
	
	@Override
	public void getProjectsByCheckId(TbVehicleCheck check) {
		LambdaQueryWrapper<TbVehicleCheckProject> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(TbVehicleCheckProject::getCheckId, check.getId());
		queryWrapper.orderByAsc(TbVehicleCheckProject::getSort);
		List<TbVehicleCheckProject> list = list(queryWrapper);
		check.setProjectList(list);
		Set<String> con = new LinkedHashSet<>();
		for (TbVehicleCheckProject pro : list) {
			con.add(pro.getName());
		}
		String projectNames = String.join(",", con);
		check.setProjectNames(projectNames);
	}
	
	@Override
	public void saveItem(TbVehicleCheck check) {

		QueryWrapper<TbVehicleCheckProject> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("check_id", check.getId());
		remove(queryWrapper);
		/*if(!StringUtils.hasLength(check.getTemplateId())) {
			return;
		}*/
		/*List<String> ids = check.getProjectList().stream().map(t->t.getTemplateProjectId()).collect(Collectors.toList());
		Map<String, TbVehicleCheckTemplateProject> tempMap = tbVehicleCheckTemplateProjectService.getMapByIds(ids);
		*/
		List<TbVehicleCheckProject> projects = new ArrayList<>();
		int sort = 1;
		for(TbVehicleCheckProject project:check.getProjectList()) {
			/*if(!StringUtils.hasLength(project.getName())&&tempMap.containsKey(project.getTemplateProjectId())) {
				TbVehicleCheckTemplateProject temp = tempMap.get(project.getTemplateProjectId());
				BeanUtilsCopy.copyBeanPropFilterId(project, temp);
			}*/
			project.setSort(sort);
			project.setCheckId(check.getId());
			projects.add(project);
			sort++;
		}
		saveBatch(projects);
	}
	
}
