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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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.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.TbCustomerFollowTemplatePart;
import com.gci.boot.modules.basedata.service.ITbCustomerFollowTemplatePartService;
import com.gci.boot.modules.customer.entity.TbCustomerFollow;
import com.gci.boot.modules.customer.entity.TbCustomerFollowPart;
import com.gci.boot.modules.customer.mapper.TbCustomerFollowPartMapper;
import com.gci.boot.modules.customer.service.ITbCustomerFollowPartService;

/**
 * <p>
 * 客户回访详情 服务实现类
 * </p>
 *
 * @author xushuhan
 * @since 2023-10-14
 */
@Service
public class TbCustomerFollowPartServiceImpl extends ServiceImpl<TbCustomerFollowPartMapper, TbCustomerFollowPart> implements ITbCustomerFollowPartService {

	@Autowired
	private ITbCustomerFollowTemplatePartService tbCustomerFollowTemplatePartService;

	@Override
	public void getPartsByFollowId(TbCustomerFollow check) {
		LambdaQueryWrapper<TbCustomerFollowPart> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(TbCustomerFollowPart::getFollowId, check.getId());
		queryWrapper.orderByAsc(TbCustomerFollowPart::getSort);
		List<TbCustomerFollowPart> list = list(queryWrapper);
		check.setPartList(list);
	}

	@Override
	public void saveItem(TbCustomerFollow check) {

		QueryWrapper<TbCustomerFollowPart> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("follow_id", check.getId());
		remove(queryWrapper);
		
		//Map<String, TbCustomerFollowTemplatePart> tempMap = tbCustomerFollowTemplatePartService.getMapByTemplateId(check.getTemplateId());
		
		List<TbCustomerFollowPart> projects = new ArrayList<>();
		int sort = 1;
		for(TbCustomerFollowPart project:check.getPartList()) {
			/*if(tempMap.containsKey(project.getTemplatePartId())) {
				TbCustomerFollowTemplatePart temp = tempMap.get(project.getTemplatePartId());
				BeanUtilsCopy.copyBeanPropFilterId(project, temp);
			}*/
			project.setSort(sort);
			project.setFollowId(check.getId());
			projects.add(project);
			sort++;
		}
		saveBatch(projects);
	}
	
}
