package com.pnkx.service.impl;

import com.pnkx.common.exception.ServiceException;
import com.pnkx.common.utils.DateUtils;
import com.pnkx.common.utils.SecurityUtils;
import com.pnkx.common.utils.StringUtils;
import com.pnkx.common.utils.template.TemplateUtils;
import com.pnkx.domain.po.PxFriendLink;
import com.pnkx.mapper.PxFriendLinkMapper;
import com.pnkx.service.IPxFriendLinkService;
import com.pnkx.system.domain.SysEmail;
import com.pnkx.system.service.ISysConfigService;
import com.pnkx.system.service.ISysEmailService;
import org.springframework.stereotype.Service;

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

/**
 * @author by PHY
 * @Classname PxFriendLinkServiceImpl
 * @date 2021-04-30 11:44
 */
@Service
public class PxFriendLinkServiceImpl implements IPxFriendLinkService {
    @Resource
    private PxFriendLinkMapper pxFriendLinkMapper;
    @Resource
    private ISysEmailService sysEmailService;

    /**
     * 查询友链
     *
     * @param id 友链ID
     * @return 友链
     */
    @Override
    public PxFriendLink selectPxFriendLinkById(Long id) {
        return pxFriendLinkMapper.selectPxFriendLinkById(id);
    }

    /**
     * 查询友链列表
     *
     * @param pxFriendLink 友链
     * @return 友链
     */
    @Override
    public List<PxFriendLink> selectPxFriendLinkList(PxFriendLink pxFriendLink) {
        return pxFriendLinkMapper.selectPxFriendLinkList(pxFriendLink);
    }

    /**
     * 新增友链
     *
     * @param pxFriendLink 友链
     * @return 结果
     */
    @Override
    public int insertPxFriendLink(PxFriendLink pxFriendLink) {
        pxFriendLink.setCreateBy(SecurityUtils.getUserId().toString());
        pxFriendLink.setCreateTime(DateUtils.getNowDate());
        return pxFriendLinkMapper.insertPxFriendLink(pxFriendLink);
    }

    /**
     * 修改友链
     *
     * @param pxFriendLink 友链
     * @return 结果
     */
    @Override
    public int updatePxFriendLink(PxFriendLink pxFriendLink) {
        pxFriendLink.setUpdateBy(SecurityUtils.getUserName());
        pxFriendLink.setUpdateTime(DateUtils.getNowDate());
        PxFriendLink original = pxFriendLinkMapper.selectPxFriendLinkById(pxFriendLink.getId());
        if ("1".equals(pxFriendLink.getStatus()) && (StringUtils.isNull(original) || "0".equals(original.getStatus()))) {
            // 通过审核的友链
            SysEmail sysEmail = new SysEmail();
            String activationTemplate = TemplateUtils.getTemplate("link");
            activationTemplate = activationTemplate.replace("template-lineTheme", pxFriendLink.getTitle() + "的博主您好，您的友链申请已通过");
            activationTemplate = activationTemplate.replace("template-lineTitle", pxFriendLink.getTitle());
            activationTemplate = activationTemplate.replace("template-lineRemark", pxFriendLink.getRemark());
            activationTemplate = activationTemplate.replace("template-lineUrl", pxFriendLink.getUrl());
            activationTemplate = activationTemplate.replace("template-lineImg", pxFriendLink.getImg());
            activationTemplate = activationTemplate.replace("template-lineEmail", pxFriendLink.getEmail());
            sysEmail.setReceiverEmail(pxFriendLink.getEmail());
            sysEmail.setSubject("\uD83D\uDC49 叮咚！「Pei你看雪博客」上有人申请友链");
            sysEmail.setContent(activationTemplate);
            try {
                sysEmailService.sendMail(sysEmail);
            } catch (Exception e) {
                throw new ServiceException("发送邮件异常");
            }
        }
        if ("2".equals(pxFriendLink.getStatus())) {
            // 改为失效的友链
            SysEmail sysEmail = new SysEmail();
            String activationTemplate = TemplateUtils.getTemplate("link");
            activationTemplate = activationTemplate.replace("template-lineTheme", pxFriendLink.getTitle() + "的博主您好，您的友链地址已失效，请及时将新的链接留在友链评论区");
            activationTemplate = activationTemplate.replace("template-lineTitle", pxFriendLink.getTitle());
            activationTemplate = activationTemplate.replace("template-lineRemark", pxFriendLink.getRemark());
            activationTemplate = activationTemplate.replace("template-lineUrl", pxFriendLink.getUrl());
            activationTemplate = activationTemplate.replace("template-lineImg", pxFriendLink.getImg());
            activationTemplate = activationTemplate.replace("template-lineEmail", pxFriendLink.getEmail());
            sysEmail.setReceiverEmail(pxFriendLink.getEmail());
            sysEmail.setSubject("\uD83D\uDC49 叮咚！「Pei你看雪博客」上有人申请友链");
            sysEmail.setContent(activationTemplate);
            try {
                sysEmailService.sendMail(sysEmail);
            } catch (Exception e) {
                throw new ServiceException("发送邮件异常");
            }
        }
        return pxFriendLinkMapper.updatePxFriendLink(pxFriendLink);
    }

    /**
     * 批量删除友链
     *
     * @param ids 需要删除的友链ID
     * @return 结果
     */
    @Override
    public int deletePxFriendLinkByIds(Long[] ids) {
        return pxFriendLinkMapper.deletePxFriendLinkByIds(ids);
    }

    /**
     * 删除友链信息
     *
     * @param id 友链ID
     * @return 结果
     */
    @Override
    public int deletePxFriendLinkById(Long id) {
        return pxFriendLinkMapper.deletePxFriendLinkById(id);
    }
}
