package com.huiju.estateagents.property.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.property.entity.TpMessageBoard;
import com.huiju.estateagents.property.entity.TpMessageReply;
import com.huiju.estateagents.property.mapper.TpMessageBoardMapper;
import com.huiju.estateagents.property.mapper.TpMessageReplyMapper;
import com.huiju.estateagents.property.service.IMessageBoardService;
import com.huiju.estateagents.property.vo.MessageBoardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MessageBoardServiceImpl extends ServiceImpl<TpMessageBoardMapper, TpMessageBoard> implements IMessageBoardService {
    @Autowired
    TpMessageBoardMapper tpMessageBoardMapper;

    @Autowired
    TpMessageReplyMapper tpMessageReplyMapper;

    @Override
    public IPage<TpMessageBoard> getPagedBy(IPage<TpMessageBoard> page, Map<String, Object> params) {
        return tpMessageBoardMapper.getPagedBy(page, params);
    }

    @Override
    public IPage<TpMessageBoard> getByMsgId(IPage<TpMessageBoard> page, Integer orgId, Integer id) {
        return tpMessageBoardMapper.getByMsgId(page, orgId, id);
    }

    @Override
    public TpMessageBoard getByPersonId(Integer orgId, String personId) {
        return tpMessageBoardMapper.getByPersonId(orgId, personId);
    }

    @Override
    public IPage<TpMessageReply> getReplyList(IPage<TpMessageReply> page, Integer orgId, Integer msgId) {
        return tpMessageReplyMapper.getReplyList(page, orgId, msgId);
    }

    @Override
    public void saveMessageOrReply(MessageBoardVO params, Integer orgId, Integer userId, String personId) throws Exception {
        if (StringUtils.isEmpty(personId) && null == userId) {
            throw new Exception("找不到留言人员");
        }

        TpMessageBoard messageBoard = null;
        if (!StringUtils.isEmpty(personId)) {
            messageBoard = getByPersonId(orgId, personId);
        }

        // 一个人只能有一个主记录
        if (null != messageBoard && null != params.getMsgId() && !messageBoard.getMsgId().equals(params.getMsgId())) {
            throw new Exception("数据记录错误, 请联系管理员");
        }

        if (null == messageBoard && null != params.getMsgId()) {
            messageBoard = getById(params.getMsgId());
        }

        // 新增留言主表
        if (null == messageBoard) {
            messageBoard = new TpMessageBoard();
            messageBoard.setContent(params.getContent());
            messageBoard.setContentType("text");
            messageBoard.setPersonId(personId);
            messageBoard.setOrgId(orgId);

            if (null != params.getRoomId()) {
                // 如果留言板按照小区划分
                // 暂时没这需求
            }

            save(messageBoard);
            return;
        }

        // 否则新增回复记录
        TpMessageReply reply = new TpMessageReply();
        reply.setContent(params.getContent());
        reply.setContentType("text");
        reply.setPersonId(StringUtils.ifNull(personId, messageBoard.getPersonId()));
        reply.setUserId(userId);
        reply.setMsgId(params.getMsgId());
        reply.setOrgId(orgId);
        tpMessageReplyMapper.insert(reply);
        return;
    }

    @Override
    public IPage<TpMessageBoard> getWxList(IPage<TpMessageBoard> page, Integer orgId, String personId) {
        return tpMessageBoardMapper.getWxList(page, orgId, personId);
    }

}
