package com.pnkx.service.impl;

import com.pnkx.common.constant.WebsiteAddressConstants;
import com.pnkx.common.core.domain.entity.SysUser;
import com.pnkx.common.exception.ServiceException;
import com.pnkx.common.utils.DateUtils;
import com.pnkx.common.utils.SecurityUtils;
import com.pnkx.common.utils.template.TemplateUtils;
import com.pnkx.domain.po.PxLeaveMessage;
import com.pnkx.mapper.PxMessageMapper;
import com.pnkx.service.IPxMessageService;
import com.pnkx.common.utils.StringUtils;
import com.pnkx.system.domain.SysEmail;
import com.pnkx.system.mapper.SysUserMapper;
import com.pnkx.system.mapper.SysUserPostMapper;
import com.pnkx.system.service.ISysConfigService;
import com.pnkx.system.service.ISysEmailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 文章Service业务层处理
 *
 * @author phy
 * @date 2021-01-26
 */
@Service
public class PxMessageServiceImpl implements IPxMessageService {
    @Resource
    private PxMessageMapper pxMessageMapper;
    @Resource
    private ISysEmailService sysEmailService;
    @Resource
    private ISysConfigService configService;
    @Resource
    private SysUserMapper userMapper;


    /**
     * 留言
     *
     * @param pxLeaveMessage 参数
     * @return 留言结果
     */
    @Override
    public Integer addMessage(PxLeaveMessage pxLeaveMessage) {
        pxLeaveMessage.setCreateBy(SecurityUtils.getUserId().toString());
        pxLeaveMessage.setCreateTime(DateUtils.getNowDate());
        pxLeaveMessage.setAvatar(SecurityUtils.getLoginUser().getUser().getAvatar());
        Integer result = pxMessageMapper.addMessage(pxLeaveMessage);
        SysEmail sysEmail = new SysEmail();
        if (StringUtils.isNull(pxLeaveMessage.getParentId())) {
            // 发送给博客主人有人留言
            String activationTemplate = TemplateUtils.getTemplate("message");
            activationTemplate = activationTemplate.replace("template-nickName", SecurityUtils.getLoginUser().getUser().getNickName());
            activationTemplate = activationTemplate.replace("template-messageContent", pxLeaveMessage.getContent());
            if ("0".equals(pxLeaveMessage.getMessageBoard())) {
                // 文章
                activationTemplate = activationTemplate.replace("template-url", WebsiteAddressConstants.WEB_SITE_ADDRESS + "post/" + pxLeaveMessage.getArticleId());
            } else if ("1".equals(pxLeaveMessage.getMessageBoard())) {
                // 留言板
                activationTemplate = activationTemplate.replace("template-url", WebsiteAddressConstants.WEB_SITE_ADDRESS + "message");
            } else if ("3".equals(pxLeaveMessage.getMessageBoard())) {
                // 友链
                activationTemplate = activationTemplate.replace("template-url", WebsiteAddressConstants.WEB_SITE_ADDRESS + "link");
            }
            String siteEmail = configService.selectConfigByKey("siteEmail");
            String[] emails = siteEmail.split(",");
            sysEmail.setReceiverEmail(emails[0]);
            sysEmail.setCcEmail(emails[1]);
            sysEmail.setSubject("\uD83D\uDC49 叮咚！「Pei你看雪博客」上有人留言了");
            sysEmail.setContent(activationTemplate);
        } else {
            // 发送给被回复者有人回复
            String activationTemplate = TemplateUtils.getTemplate("reply");
            PxLeaveMessage replyMessage = pxMessageMapper.getMessageById(pxLeaveMessage.getReplyId());
            activationTemplate = activationTemplate.replace("template-nickName", replyMessage.getNickName());
            activationTemplate = activationTemplate.replace("template-messageContent", replyMessage.getContent());
            activationTemplate = activationTemplate.replace("template-replyNickName", SecurityUtils.getLoginUser().getUser().getNickName());
            activationTemplate = activationTemplate.replace("template-replyMessageContent", pxLeaveMessage.getContent());
            if ("0".equals(pxLeaveMessage.getMessageBoard())) {
                // 文章
                activationTemplate = activationTemplate.replace("template-url", WebsiteAddressConstants.WEB_SITE_ADDRESS + "post/" + pxLeaveMessage.getArticleId());
            } else if ("1".equals(pxLeaveMessage.getMessageBoard())) {
                // 留言板
                activationTemplate = activationTemplate.replace("template-url", WebsiteAddressConstants.WEB_SITE_ADDRESS + "message");
            } else if ("3".equals(pxLeaveMessage.getMessageBoard())) {
                // 友链
                activationTemplate = activationTemplate.replace("template-url", WebsiteAddressConstants.WEB_SITE_ADDRESS + "link");
            }
            SysUser sysUser = userMapper.selectUserById(pxLeaveMessage.getReplyUserId());
            sysEmail.setReceiverEmail(sysUser.getEmail());
            sysEmail.setSubject("\uD83D\uDC49 叮咚！「Pei你看雪博客」上有人@了您");
            sysEmail.setContent(activationTemplate);
        }
        try {
            sysEmailService.sendMail(sysEmail);
        } catch (Exception e) {
            throw new ServiceException("发送邮件异常");
        }
        return result;
    }

    /**
     * 查询留言列表
     *
     * @param pxLeaveMessage 留言
     * @return 留言
     */
    @Override
    public List<PxLeaveMessage> selectPxLeaveMessageList(PxLeaveMessage pxLeaveMessage) {
        List<PxLeaveMessage> list = pxMessageMapper.selectPxLeaveMessageList(pxLeaveMessage);
        list.forEach(item -> {
            item.setLikeNumber(item.getArticleLikeNumber());
            if (StringUtils.isEmpty(item.getCreateBy())) {
                item.setNickName(item.getAuthorName());
            }
            // 补充回复列表
            PxLeaveMessage replyParams = new PxLeaveMessage();
            if (StringUtils.isNull(pxLeaveMessage.getParentId())) {
                replyParams.setParentId(item.getId());
                List<PxLeaveMessage> replyList = pxMessageMapper.selectPxLeaveMessageList(replyParams);
                // 只要3条
                if (replyList.size() > 2) {
                    replyList = replyList.subList(0, 3);
                }
                replyList.forEach(reply -> {
                    reply.setLikeNumber(reply.getCommentLikeNumber());
                    // 补充回复信息
                    if (StringUtils.isNotNull(reply.getReplyId())) {
                        PxLeaveMessage replyMessage = pxMessageMapper.getMessageById(reply.getReplyId());
                        if (StringUtils.isEmpty(replyMessage.getCreateBy())) {
                            reply.setReplyUserId(null);
                            reply.setReplyNickName(replyMessage.getAuthorName());
                        } else {
                            reply.setReplyUserId(new Long(replyMessage.getCreateBy()));
                            reply.setReplyNickName(replyMessage.getNickName());
                        }
                    }
                });
                item.setReplyList(replyList);
            }
        });
        return list;
    }

    /**
     * 查询留言审核
     *
     * @param pxLeaveMessage 留言
     * @return 留言
     */
    @Override
    public List<PxLeaveMessage> selectPxLeaveMessageExamine(PxLeaveMessage pxLeaveMessage) {
        return pxMessageMapper.selectPxLeaveMessageList(pxLeaveMessage);
    }

    /**
     * 修改留言
     *
     * @param pxLeaveMessage 留言
     * @return 结果
     */
    @Override
    public int updatePxLeaveMessage(PxLeaveMessage pxLeaveMessage) {
        pxLeaveMessage.setUpdateBy(SecurityUtils.getUserId().toString());
        pxLeaveMessage.setUpdateTime(DateUtils.getNowDate());
        return pxMessageMapper.updatePxLeaveMessage(pxLeaveMessage);
    }
}
