package com.usky.oa.service.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.exception.BusinessException;
import com.usky.oa.domain.OaFormDefinition;
import com.usky.oa.mapper.OaFormDefinitionMapper;
import com.usky.oa.mapper.SysUserMapper;
import com.usky.oa.service.enums.OaBuiltInDocument;
import com.usky.system.RemoteMceService;
import com.usky.system.domain.MceRequestVO;
import com.usky.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @author fyc
 * @email yuchuan.fu@chinausky.com
 * @date 2025/1/9
 */
@Slf4j
@Component
public class OaSendMessageCenter {

    @Autowired
    private RemoteMceService remoteMceService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OaFormDefinitionMapper oaFormDefinitionMapper;

    private static final String INFO_TITLE = "审批提醒";
    private static final String INFO_CONTENT = "待审批";
    private static final String INFO_APPROVE_FAIL = "审核不通过";
    private static final String INFO_APPROVE_SUCCESS = "审核通过";
    private static final int INFO_TYPE = 3;

    /**
     * 异步发送消息
     *
     * @param username 提交人账号名
     * @param approvalUid 审批人userId
     * @param id 数据id
     * @param receivers 接收人userId集合
     * @param isPass 是否通过（0：不通过，1：通过）
     * @param sign 申请单类型
     */
    @Async
    public void sendAsyncMessage(Integer tenantId, String username, Long approvalUid, Integer id, List<Long> receivers, Integer isPass, String sign) {

        log.info(username + "的申请开始发送消息中心-----------------------------------");

        LambdaQueryWrapper<SysUser> nickNameQuery = Wrappers.lambdaQuery();
        nickNameQuery.select(SysUser::getNickName)
                .eq(SysUser::getUserId, approvalUid);
        SysUser user = sysUserMapper.selectOne(nickNameQuery);

        // JSONObject jsonObject = new JSONObject();
        // jsonObject.put("infoTitle", INFO_TITLE);

        MceRequestVO mceRequestVO = new MceRequestVO();
        mceRequestVO.setInfoTitle(INFO_TITLE);

        switch (isPass) {
            case 0:
                // jsonObject.put("infoContent", nickName.getNickName() + INFO_APPROVE_FAIL);
                mceRequestVO.setInfoContent(user.getNickName() + INFO_APPROVE_FAIL);
                break;
            case 1:
                // jsonObject.put("infoContent", nickName.getNickName() + INFO_APPROVE_SUCCESS);
                mceRequestVO.setInfoContent(user.getNickName() + INFO_APPROVE_SUCCESS);
                break;
            default:
                OaBuiltInDocument oaBuiltInDocument = OaBuiltInDocument.valueOf(sign);
                String name = oaBuiltInDocument.getName();
                // jsonObject.put("infoContent", username + "的" + name + INFO_CONTENT);
                mceRequestVO.setInfoContent(username + "的" + name + INFO_CONTENT);
        }

        // jsonObject.put("infoType", INFO_TYPE);
        // jsonObject.put("id", id);
        // jsonObject.put("infoTypeName", INFO_TITLE);
        // jsonObject.put("userName", username);
        // jsonObject.put("userIds", receivers);

        mceRequestVO.setInfoType(String.valueOf(INFO_TYPE));
        mceRequestVO.setId(id);
        mceRequestVO.setInfoTypeName(INFO_TITLE);
        mceRequestVO.setUserName(username);
        mceRequestVO.setUserIds(receivers);
        mceRequestVO.setTenantId(tenantId);
        try {
            // 推送消息中心
            //ApiResult<Void> voidApiResult = remoteMceService.addMce(jsonObject.toString());
            ApiResult<Void> voidApiResult = remoteMceService.addMceReceive(mceRequestVO);

            if (voidApiResult.isSuccess()) {
                log.info("申请消息发送成功！");
            } else {
                log.error("申请消息发送失败！");
            }
        } catch (Exception e) {
            log.error("申请发送消息时发生异常", e);
        }
        log.info(username + "的申请发送消息中心完成-----------------------------------");
    }

}
