package com.softwarebr.nunu.service.hall.impl;

import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.brframework.commonmq.annotation.MQConfig;
import com.brframework.commonmq.core.MQClient;
import com.softwarebr.nunu.dao.hall.InvestApplyDao;
import com.softwarebr.nunu.entity.hall.Invest;
import com.softwarebr.nunu.entity.hall.InvestApply;
import com.softwarebr.nunu.entity.user.Message;
import com.softwarebr.nunu.globals.MQTopics;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.hall.InvestApplyService;
import com.softwarebr.nunu.service.hall.InvestService;
import com.softwarebr.nunu.service.user.MemberService;
import com.softwarebr.nunu.service.user.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @date 2020-02-25 15:27:45
 * @author lilin
 */

@Slf4j
@Service
@MQConfig
public class InvestApplyServiceImpl extends AbstractEntityService<InvestApply, Long, Object> implements InvestApplyService {

	@Autowired
	private InvestApplyDao investApplyDao;

	@Autowired
	private InvestService investService;

	@Autowired
	private MQClient mqClient;

	@Autowired
	private MessageService messageService;

	@Autowired
	private MemberService memberService;

	@Override
	public CommonRepository<Long, InvestApply> getRepository() {
		return investApplyDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void openInvestApply(List<Long> ids, Integer verify, Integer auditor) {
		String title;
		String nickname;
		for (Long id : ids) {
			InvestApply apply = accessObject(id);
			Invest invest = investService.accessObject(apply.getInvestId());
			if (auditor.equals(SystemConst.AUDITOR_PUBLISHER)) {
				nickname = memberService.nicknameHandle(apply.getPublisher());
			} else {
				nickname = "平台";
			}
			if (verify.equals(SystemConst.VERIFY_ADOPT)) {
				title = String.format("%s已同意您查看 %s 的联系方式和项目附件", nickname, invest.getName());
			} else {
				title = String.format("%s已拒绝您查看 %s 的联系方式和项目附件", nickname, invest.getName());
			}
			apply.setVerify(verify);
			save(apply);
			mqClient.publish(MQTopics.MEMBER_SEND_MESSAGE, messageService.sendMessageJSON(apply.getProposer(), Message.MESSAGE_TYPE_CONTACT_VERIFY,
					title, apply.getReason(), Message.MESSAGE_TARGET_TYPE_TWO_INVEST, invest.getId().toString()));
		}
	}


	@Override
	public Long countApplyNumber(Long investId) {
		return investApplyDao.countByInvestId(investId);
	}

	@Override
	public boolean checkApplyVerify(Long memberId, Long investId){
		InvestApply investApply = investApplyDao.findTopByProposerAndInvestIdAndVerify(memberId, investId, SystemConst.VERIFY_ADOPT);
		if(null != investApply){
			return true;
		}else{
			return false;
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void investContactApply(Long memberId, Long investId, String reason) {
		InvestApply investApply = new InvestApply();
		investApply.setProposer(memberId);
		investApply.setCreateDate(LocalDateTime.now());
		investApply.setVerify(SystemConst.VERIFY_AWAIT);
		investApply.setInvestId(investId);
		investApply.setReason(reason);
		Invest invest = investService.accessObject(investApply.getInvestId());
		investApply.setPublisher(invest.getPublisher());
		save(investApply);
		if (invest.getPublisher() > 0) {
			String title = String.format("%s申请查看您的 %s 项目的联系方式和项目附件", memberService.nicknameHandle(memberId), invest.getName());
			mqClient.publish(MQTopics.MEMBER_SEND_MESSAGE, messageService.sendMessageJSON(invest.getPublisher(), Message.MESSAGE_TYPE_APPLY,
					title, investApply.getReason(), Message.MESSAGE_TARGET_TYPE_TWO_INVEST, invest.getId().toString()));
		}

	}

	@Override
	public Long countInvestHandleTotal(Long investId){

		return investApplyDao.countByInvestIdAndVerify(investId, SystemConst.VERIFY_AWAIT);
	}

	@Override
	public Long[] getAwaitVerifyInvestIds(){

		List<InvestApply> investApplies = investApplyDao.findByVerify(SystemConst.VERIFY_AWAIT);
		if(investApplies.size()>0){
			List<Long> investIds = investApplies.stream().map(InvestApply::getInvestId).collect(Collectors.toSet()).stream().collect(Collectors.toList());
			return investIds.toArray(new Long[investIds.size()]);
		}
		return new Long[]{0L};
	}
}