package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.entity.CorpFeishuUser;
import cn.com.ctop.common.module.entity.MailLog;
import cn.com.ctop.common.module.mapper.MailLogMapper;
import cn.com.ctop.common.module.service.ICorpFeishuUserService;
import cn.com.ctop.common.module.service.IMailLogService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.CorpWexinUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 邮箱记录表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-12-08
 */
@Service
public class MailLogServiceImpl extends ServiceImpl<MailLogMapper, MailLog> implements IMailLogService {
    @Resource
    private MailLogMapper mailLogMapper;
    @Autowired
    private CorpWexinUtils corpWexinUtils;


    @Override
    public void sendWeChat(String responsibleId, Long accountId, String projectName, String transferor, String assignee) {
        JSONObject weChatId = mailLogMapper.getWChatIdByUserId(responsibleId);
        if (!Check.isNull(weChatId)) {
            String transferorName = mailLogMapper.getUserNameByUserId(transferor); // 转让人姓名
            String assigneeName = mailLogMapper.getUserNameByUserId(assignee); // 转让人姓名
            StringBuilder text = new StringBuilder();
            text.append("转户申请").append("<br/>").
                    append("您的项目：").append(projectName + "，").append("<br/>")
                    .append("账号：" + accountId).append(",申请由：" + transferorName + "转户于：" + assigneeName).append("<br/>")
                    .append("等待您的审核，请您及时处理！");

            corpWexinUtils.sendMessageByWeChatId(weChatId, text.toString());
        }
    }

    @Autowired
    private ICorpFeishuUserService corpFeishuUserService;

    /**
     * 转户通过或失败发送通知
     *
     * @param projectName
     * @param status
     * @param userId
     */
    @Override
    public void sendWeChatByStatus(String projectName, Integer status, String userId, Long accountId) {

        JSONObject weChatId = mailLogMapper.getWChatIdByUserId(userId);
        if (!Check.isNull(weChatId)) {
            String statusName = "";

            if (status == 1) {
                statusName = "通过";
            } else if (status == 2) {
                statusName = "拒绝";
            }

            StringBuilder text = new StringBuilder();
            text.append("转户申请审核通知").append("<br/>").
                    append("您的项目：").append(projectName + "，").append("<br/>")
                    .append("账号：" + accountId).append("<br/>")
                    .append("申请的转户操作已：" + statusName).append("<br/>")
                    .append("请您前往验证，谢谢！");

            corpWexinUtils.sendMessageByWeChatId(weChatId, text.toString());
        }
    }

    @Override
    public void sendLeaderWeChat(String projectName, Integer status, String leaderId, Long accountId, String transferor, String assignee, BigDecimal cost) {
        JSONObject weChatId = mailLogMapper.getWChatIdByUserId(leaderId);
        if (!Check.isNull(weChatId)) {
            String transferorName = mailLogMapper.getUserNameByUserId(transferor); // 转让人姓名
            String assigneeName = mailLogMapper.getUserNameByUserId(assignee); // 转让人姓名
            StringBuilder text = new StringBuilder();
            text.append("转户审核通过通知").append("<br/>").
                    append("项目：").append(projectName).append("<br/>")
                    .append("账号：" + accountId).append("<br/>")
                    .append("申请由：" + transferorName + "转户于：" + assigneeName).append("<br/>")
                    .append("本月消耗：" + cost).append("<br/>")
                    .append("审核结果为：通过，请知晓～");

            corpWexinUtils.sendMessageByWeChatId(weChatId, text.toString());
        }
    }
}
