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

import java.util.List;
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.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.modules.customer.entity.TbCustomerFollow;
import com.gci.boot.modules.customer.entity.TbCustomerOpinion;
import com.gci.boot.modules.customer.entity.TbCustomerOpinionHandle;
import com.gci.boot.modules.customer.mapper.TbCustomerOpinionMapper;
import com.gci.boot.modules.customer.service.ITbCustomerOpinionHandleService;
import com.gci.boot.modules.customer.service.ITbCustomerOpinionService;

/**
 * <p>
 * 客户反馈意见 服务实现类
 * </p>
 *
 * @author xushuhan
 * @since 2023-10-16
 */
@Service
public class TbCustomerOpinionServiceImpl extends ServiceImpl<TbCustomerOpinionMapper, TbCustomerOpinion> implements ITbCustomerOpinionService {


	@Autowired
	private ITbCustomerOpinionHandleService tbCustomerOpinionHandleService;
	
	@Override
	public void saveItem(TbCustomerFollow item) {
		if(item.getOpinion()!=null&&StringUtils.hasLength(item.getOpinion().getName())) {
			TbCustomerOpinion op = item.getOpinion();
			op.setFollowId(item.getId());
			op.setRegisterTime(item.getReviewTime());
			op.setProjectId(item.getProjectId());
			op.setRegistrant(item.getReviewUserId());
			saveOrUpdate(op);
		}
	}
	
	//满意更新反馈意见状态
	@Override
	public void updateStatus(TbCustomerOpinionHandle item) {
		if("1".equals(item.getPleasedStatus())) {
			UpdateWrapper<TbCustomerOpinion> updateWrapper = new UpdateWrapper<>();
			updateWrapper.eq("id", item.getOpinionId());
			updateWrapper.set("opinion_status", "2");
			this.update(updateWrapper);
		}
	}
	

	@Override
	public void getOpinionByFollowId(TbCustomerFollow check) {
		LambdaQueryWrapper<TbCustomerOpinion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(TbCustomerOpinion::getFollowId, check.getId());
		queryWrapper.orderByAsc(TbCustomerOpinion::getCreateTime);
		List<TbCustomerOpinion> list = list(queryWrapper);
		if(list.size()>0) {
			TbCustomerOpinion op = list.get(0);
			tbCustomerOpinionHandleService.getHandlesByOpinionId(op);
			check.setOpinion(op);
			check.setHasOpinion("1");
		}
	}
	
	@Override
	public void initHasOpinion(List<TbCustomerFollow> list) {
		if(list.size()<1) {
			return;
		}
		List<String> ids = list.stream().map(t->t.getId()).collect(Collectors.toList());
		LambdaQueryWrapper<TbCustomerOpinion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(TbCustomerOpinion::getFollowId, ids);
		queryWrapper.select(TbCustomerOpinion::getFollowId);
		List<TbCustomerOpinion> ops = this.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.setHasOpinion("1");
			}
		}
	}


}
