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

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.modules.customer.entity.TbCustomerFollow;
import com.gci.boot.modules.customer.entity.TbInspectionTask;
import com.gci.boot.modules.customer.mapper.TbCustomerFollowMapper;
import com.gci.boot.modules.customer.service.ITbCustomerFollowPartService;
import com.gci.boot.modules.customer.service.ITbCustomerFollowService;
import com.gci.boot.modules.customer.service.ITbCustomerOpinionService;
import com.gci.boot.modules.customer.service.ITbInspectionTaskService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 客户回访台账 服务实现类
 * </p>
 *
 * @author xushuhan
 * @since 2023-10-14
 */
@Slf4j
@Service
public class TbCustomerFollowServiceImpl extends ServiceImpl<TbCustomerFollowMapper, TbCustomerFollow> implements ITbCustomerFollowService {

	@Autowired
	private ITbCustomerFollowPartService tbCustomerFollowPartService;
	@Autowired
	private ITbCustomerOpinionService tbCustomerOpinionService;
	@Autowired
	private ITbInspectionTaskService tbInspectionTaskService;
	
	@Transactional
	@Override
	public void saveItem(TbCustomerFollow item) {
		if(!StringUtils.hasLength(item.getFollowNo())) {
			item.setFollowNo("HF"+DateUtils.formatDate(new Date(), DateUtils.HYPHENS_DATETIME4));
		}
		if(!StringUtils.hasLength(item.getId())) {
			save(item);
			tbCustomerFollowPartService.saveItem(item);
			//保存反馈意见
			tbCustomerOpinionService.saveItem(item);
			
			//保存车辆巡检
			if(item.getInspectionTask()!=null&&StringUtils.hasLength(item.getInspectionTask().getInspectionTemplateId())) {
				TbInspectionTask op = item.getInspectionTask();
				op.setFollowId(item.getId());
				op.setProjectIds(item.getProjectId());
				op.setInspectionType("2");
				op.setFollowId(item.getId());
				op.setInspectionTime(item.getReviewTime());
				op.setSourceType(item.getSourceType());
				op.setInspectionUserId(item.getReviewUserId());
				tbInspectionTaskService.saveItemFromFollow(op);
			}
		}else {
			updateById(item);
			tbCustomerFollowPartService.saveItem(item);
		}
	}
	

	@Override
	public void getInspectionTaskByFollowId(TbCustomerFollow check) {
		LambdaQueryWrapper<TbInspectionTask> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(TbInspectionTask::getFollowId, check.getId());
		queryWrapper.orderByAsc(TbInspectionTask::getCreateTime);
		List<TbInspectionTask> list = tbInspectionTaskService.list(queryWrapper);
		if(list.size()>0) {
			check.setInspectionTask(tbInspectionTaskService.getItemById(list.get(0).getId()));
			check.setHasInspectionTask("1");
		}
	}


	@Override
	public void initHasInspectionTask(List<TbCustomerFollow> list) {
		if(list.size()<1) {
			return;
		}
		List<String> ids = list.stream().map(t->t.getId()).collect(Collectors.toList());
		LambdaQueryWrapper<TbInspectionTask> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(TbInspectionTask::getFollowId, ids);
		queryWrapper.select(TbInspectionTask::getFollowId);
		List<TbInspectionTask> ops = tbInspectionTaskService.list(queryWrapper);
		if(ops.size()<1) {
			return;
		}
		List<String> fids = ops.stream().map(t->t.getFollowId()).collect(Collectors.toList());
		for(TbCustomerFollow fo: list) {
			if(fids.contains(fo.getId())) {
				fo.setHasInspectionTask("1");
			}
		}
	}

}
