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 cn.com.feishu.provider.MessageProvider;
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;

/**
 * 邮箱记录表
 *
 * @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());
        }

        CorpFeishuUser user = corpFeishuUserService.getByUserId(responsibleId);
        if(null!=user){
            JSONArray openIds = new JSONArray();
            openIds.add(user.getOpenId());
            try {
                String transferorName = mailLogMapper.getUserNameByUserId(transferor); // 转让人姓名
                String assigneeName = mailLogMapper.getUserNameByUserId(assignee); // 转让人姓名
                StringBuilder text = new StringBuilder();
                text.append("转户申请").append("\n\r").
                        append("您的项目：").append(projectName + "，").append("\n\r")
                        .append("账号：" + accountId).append(",申请由：" + transferorName + "转户于：" + assigneeName).append("\n\r")
                        .append("等待您的审核，请您及时处理！");
                MessageProvider.batchSend(null,openIds,null,text.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @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());
        }

        CorpFeishuUser user = corpFeishuUserService.getByUserId(userId);
        if(null!=user){
            JSONArray openIds = new JSONArray();
            openIds.add(user.getOpenId());
            try {
                String statusName = "";
                if (status == 1) {
                    statusName = "通过";
                } else if (status == 2) {
                    statusName = "拒绝";
                }
                StringBuilder text = new StringBuilder();
                text.append("转户申请审核通知").append("\n\r").
                        append("您的项目：").append(projectName + "，").append("\n\r")
                        .append("账号：" + accountId).append("\n\r")
                        .append("申请的转户操作已：" + statusName).append("\n\r")
                        .append("请您前往验证，谢谢！");
                MessageProvider.batchSend(null,openIds,null,text.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
