package com.softwarebr.nunu.service.go.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.brframework.commonweb.exception.HandleException;
import com.softwarebr.nunu.dao.go.PolicyApplyDao;
import com.softwarebr.nunu.entity.go.Policy;
import com.softwarebr.nunu.entity.go.PolicyApply;
import com.softwarebr.nunu.entity.user.Message;
import com.softwarebr.nunu.globals.MQTopics;
import com.softwarebr.nunu.service.go.PolicyApplyService;
import com.softwarebr.nunu.service.go.PolicyService;
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 static com.softwarebr.nunu.entity.go.PolicyApply.STATUS_WAIT_ACCEPTANCE;

/**
 * @date 2020-02-21 10:49:24
 * @author lilin
 */

@Slf4j
@Service
@MQConfig
public class PolicyApplyServiceImpl extends AbstractEntityService<PolicyApply, Long, Object> implements PolicyApplyService {

	@Autowired
	private PolicyApplyDao policyApplyDao;

	@Autowired
	private MQClient mqClient;

	@Autowired
	private MessageService messageService;

	@Autowired
	private PolicyService policyService;

	@Override
	public CommonRepository<Long, PolicyApply> getRepository() {
		return policyApplyDao;
	}

	@Override
	public PolicyApply saveApply(Long policyId, Long memberId, String content, String contact, String contactPhone, String email) {
		PolicyApply policyApply = save(PolicyApply.builder()
				.memberId(memberId)
				.policyId(policyId)
				.content(content)
				.contact(contact)
				.contactPhone(contactPhone)
				.email(email)
				.status(STATUS_WAIT_ACCEPTANCE)
				.createDate(LocalDateTime.now())
				.verifyDate(LocalDateTime.now())
				.build());
		return policyApply;
	}

	@Override
	public PolicyApply findByPolicyId(Long policyId) {
		return policyApplyDao.findTopByPolicyId(policyId);
	}

	@Override
	public Long countApplyTotal(Long policyId){
		Long total = policyApplyDao.countByPolicyId(policyId);
		return total;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void policyApplyVerifyAwait(Long id, String remark) {
		PolicyApply policyApply = accessObject(id);
		Policy policy = policyService.accessObject(policyApply.getPolicyId());
		policyApply.setRemark(remark);
		policyApply.setVerifyDate(LocalDateTime.now());
		policyApply.setStatus(PolicyApply.STATUS_IN_ACCEPTANCE);
		save(policyApply);
		String description = String.format("您的政策申报 %s 平台已受理，点击查看详情跳转相应审核结果页", policy.getTitle());
		mqClient.publish(MQTopics.MEMBER_SEND_MESSAGE, messageService.sendMessageJSON(policyApply.getMemberId(), Message.MESSAGE_TYPE_VERIFY,
				"政策申报进度提醒", description, Message.MESSAGE_TARGET_TYPE_ONE_POLICY, policyApply.getId().toString()));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void policyApplyVerifySuccess(Long id, String remark) {
		PolicyApply policyApply = accessObject(id);
		Policy policy = policyService.accessObject(policyApply.getPolicyId());
		policyApply.setRemark(remark);
		policyApply.setVerifyDate(LocalDateTime.now());
		policyApply.setStatus(PolicyApply.STATUS_SERVICE_SUCCESS);
		save(policyApply);
		String description = String.format("您的政策申报 %s 平台已受理成功，点击查看详情跳转相应审核结果页", policy.getTitle());
		mqClient.publish(MQTopics.MEMBER_SEND_MESSAGE, messageService.sendMessageJSON(policyApply.getMemberId(), Message.MESSAGE_TYPE_VERIFY,
				"政策申报进度提醒", description, Message.MESSAGE_TARGET_TYPE_ONE_POLICY, policyApply.getId().toString()));
	}

}