package com.community.huiju.service.impl;

import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.TpBillInvoiceMapper;
import com.community.huiju.dao.TpBillMapper;
import com.community.huiju.dao.TpMessageMapper;
import com.community.huiju.dao.TpTicketMapper;
import com.community.huiju.model.TpBill;
import com.community.huiju.model.TpMessage;
import com.community.huiju.model.TpTicket;
import com.community.huiju.service.MessageServiceI;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2018-10-22
 */
@Service("messageService")
public class MessageServiceImpl implements MessageServiceI {
	
	@Autowired
	private TpMessageMapper tpMessageMapper;
	
	@Autowired
	private TpBillMapper tpBillMapper;

	@Autowired
	private TpTicketMapper tpTicketMapper;
	
	/**
	 * 获取消息数量
	 * @param communityId
	 * @return
	 */
	@Override
	public Map<String, Object> getMessageTotal(Integer communityId, Integer verifyId) {
		return tpMessageMapper.getMessageTotal(verifyId,communityId);
	}
	
	/**
	 * 分页获取消息或待办
	 * @param communityId
	 * @param pageNum
	 * @param pageSize
	 * @param modelType
	 * @return
	 */
	@Override
	public Map<String,Object> getMessages(Integer communityId, Integer pageNum, Integer pageSize,Integer modelType, Integer verifyId) {
		//使用分页插件
		Page page = PageHelper.startPage(pageNum, pageSize);
		// 获取数据
		List<TpMessage> messageList = tpMessageMapper.getMessages(verifyId,communityId, modelType);
		messageList.stream().forEach(tpMessage -> {
			//缴费相关信息
			//if (tpMessage.getMessageType().equals(Constant.MESSAGE_TYPE_BILL)){
			//	TpBill tpBill = tpBillMapper.selectById(tpMessage.getBillId());
			//	if (null != tpBill){
			//		tpMessage.setContent(tpBill.getBillExplain());
			//	}
			//}
			
			if(tpMessage.getMessageType().equals(Constant.MESSAGE_TYPE_VISITOR) || tpMessage.getMessageType().equals(Constant.MESSAGE_TYPE_ANNOUNCEMENT)
					|| tpMessage.getMessageType().equals(Constant.MESSAGE_TYPE_SYS) || tpMessage.getMessageType().equals(Constant.MESSAGE_TYPE_BILL)
					|| tpMessage.getMessageType().equals(Constant.MESSAGE_TYPE_ACTIVITY)){
				tpMessage.setContent(tpMessage.getMessageContent());
				tpMessage.setMessageContent("");
			}
			// 查询工单是否待评价，如果为待评价把Content改为工单的标题
			TpTicket tpTicket= tpTicketMapper.selectByPrimaryKey(tpMessage.getTicketId());
			if (null != tpTicket) {
				if (!"".equals(tpMessage.getTicketId())) {
					tpMessage.setContent(tpTicket.getTicketTitle());
				}
			}
			if (!StringUtils.isEmpty(tpMessage.getMessageDetailUrl())){
				Integer id = getMessageIdByType(tpMessage);
				tpMessage.setMessageDetailUrl(tpMessage.getMessageDetailUrl() + "&id=" + id);
			}
		});
		Map<String,Object> map = new HashMap<>();
		map.put("list",messageList);
		map.put("total",page.getTotal());
		return map;
	}
	
	/**
	 * 根据不同的类型取不同的id
	 * @param tpMessage
	 */
	private Integer getMessageIdByType(TpMessage tpMessage) {
		Integer id = tpMessage.getTicketId();
		switch (tpMessage.getMessageType()){
			case Constant.MESSAGE_TYPE_VISITOR:
				break;
			case Constant.MESSAGE_TYPE_ANNOUNCEMENT:
				id = tpMessage.getAnnouncementId();
				break;
			case Constant.MESSAGE_TYPE_SYS:
				break;
			case Constant.MESSAGE_TYPE_BILL:
				id = tpMessage.getBillId();
				break;
			case Constant.MESSAGE_TYPE_ACTIVITY:
				id = tpMessage.getActivityId();
				break;
		}
		
		return id;
	}
	
	/**
	 * 消息全部已读
	 */
	@Override
	public void updateMessageReadStatus(Integer communityId, Integer userId) {
		tpMessageMapper.updateReadStatus(communityId, userId);
	}

	@Override
	public ResponseBean updateMessageByIdReadStatus(UserElement userElement, Integer messageId) {
		ResponseBean responseBean = new ResponseBean();

		TpMessage tpMessage = tpMessageMapper.selectByPrimaryKey(messageId);
		if (null == tpMessage) {
			responseBean.addError("消息不存在!");
			return responseBean;
		}
		tpMessage.setReadStatus("1");
		tpMessage.setUpdateDate(new Date());
		tpMessage.setUpdateUser(userElement.getUserVerifyId());
		int row = tpMessageMapper.updateByPrimaryKeySelective(tpMessage);
		if (row > 0) {
			responseBean.addSuccess("操作成功!");
			return responseBean;
		}
		responseBean.addError("操作失败!");
		return responseBean;
	}
}
