package com.jeeplus.weChat.util;

import cn.hutool.extra.spring.SpringUtil;
import com.jeeplus.flowable.model.Flow;
import com.jeeplus.flowable.utils.ProcessDefCache;
import com.jeeplus.weChat.domain.WXTransportTemplate;
import com.jeeplus.weChat.service.WxSendService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.flowable.task.api.Task;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.net.URLEncoder;

/**
 * @author 王强
 * @version 1.0
 * @date 2024-07-24 14:30
 */
@Log4j2
public class SendMessageUtil {

    // 给下一节点审核人发送通知
    public static void sendMessage(Task task, List<String> openIds, Flow flow, String projectUrl) throws UnsupportedEncodingException {

        for (int i = 0; i < openIds.size(); i++) {

            if (StringUtils.isNotBlank(openIds.get(i))) {
                WXTransportTemplate template = new WXTransportTemplate();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                Map<String, Object> map = task.getProcessVariables();

                template.setOpenid(openIds.get(i));
                template.setTitleName((String) map.get("title"));
                template.setProcessName(ProcessDefCache.get ( task.getProcessDefinitionId ( ) ).getName ( ));
                template.setCurrentStage(task.getName());
                template.setProcessInitiator((String) map.get("userName"));
                template.setCreateTime(format.format(task.getCreateTime()));

                //  url 所需要的参数
                // 获取流程定义ID、任务定义Key、流程实例ID等信息
                String flowTitle = (String) map.get("title");
                String title = task.getName();
                String formType = flow.getFormType();
                String formReadOnly = flow.isFormReadOnly() + "";
                String formUrl = flow.getFormUrl();
                String procDefKey = flow.getProcDefKey();
                String taskDefKey = flow.getTaskDefKey();
                String procInsId = flow.getProcInsId();
                String procDefId = flow.getProcDefId();
                String taskId = task.getId();
                String businessId = flow.getBusinessId();
                String openId = openIds.get(i);
                // 将所有的参数拼接成链接
                // 构建flow JSON字符串，将openId放在businessId后面
                String flowJson = String.format(
                        "{\"formTitle\":\"%s\",\"title\":\"%s\",\"formType\":\"%s\",\"formReadOnly\":%s,\"formUrl\":\"%s\",\"procDefKey\":\"%s\",\"taskDefKey\":\"%s\",\"procInsId\":\"%s\",\"procDefId\":\"%s\",\"taskId\":\"%s\",\"businessId\":\"%s\",\"openId\":\"%s\"}",
                        flowTitle,
                        title,
                        formType,
                        formReadOnly,
                        formUrl,
                        procDefKey,
                        taskDefKey,
                        procInsId,
                        procDefId,
                        taskId,
                        businessId,
                        openId // 将openId移到businessId后面
                );

                // 对整个JSON字符串进行URL编码
                String encodedFlow = URLEncoder.encode(flowJson, "UTF-8");

                // 构建完整的URL
                String url = String.format(
                        "http://cpaoa.xgccpm.com/h5/#/pages/workbench/task/TaskForm?flow=%s",
                        encodedFlow
                );

                template.setUrl(url);
                // 微信打开的URL http://kqe4km.natappfree.cc/h5/?code=081qCoHa1XMR0I0Ic5Ha1l21YL3qCoH9&state=STATE#/pages/workbench/task/TaskForm?flow=%7B%22formTitle%22%3A%22%E5%BC%A0%E9%9D%99%20%E5%9C%A8%202024-08-20%2016%3A48%20%E5%8F%91%E8%B5%B7%E4%BA%86%5B%E9%87%87%E8%B4%AD%E7%94%B3%E8%AF%B7%5D%22,%22title%22%3A%22%E5%AE%A1%E6%89%B9%E3%80%90%E7%BB%BC%E5%90%88%E7%AE%A1%E7%90%86%E9%83%A8%E4%B8%BB%E4%BB%BB%E5%AE%A1%E6%89%B9%E3%80%91%22,%22formType%22%3A%222%22,%22formReadOnly%22%3Afalse,%22formUrl%22%3A%22%2FmaterialManagement%2Fpurchase%2FPurchaseForm%22,%22procDefKey%22%3A%22Process_1669961023358%22,%22taskDefKey%22%3A%22UserTask_0w6fh5r%22,%22procInsId%22%3A%22f6672cc5-5ed0-11ef-bcf8-387a0e63bd5c%22,%22procDefId%22%3A%22Process_1669961023358%3A9%3A521031c7-359b-11ee-b319-00163e1dafc2%22,%22taskId%22%3A%22f6694fae-5ed0-11ef-bcf8-387a0e63bd5c%22,%22businessId%22%3A%22afd001588750407385ecb58c9c8229f7%22%7D
                // 设置url
                // http://localhost:8000/h5/#/pages/workbench/task/TaskForm?flow=%7B%22formTitle%22%3A%22%E5%BC%A0%E9%9D%99%20%E5%9C%A8%202024-08-14%2015%3A59%20%E5%8F%91%E8%B5%B7%E4%BA%86%20%5B%E9%87%87%E8%B4%AD%E7%94%B3%E8%AF%B7%5D%22,%22title%22%3A%22%E5%AE%A1%E6%89%B9%E3%80%90%E7%BB%BC%E5%90%88%E7%AE%A1%E7%90%86%E9%83%A8%E4%B8%BB%E4%BB%BB%E5%AE%A1%E6%89%B9%E3%80%91%22,%22formType%22%3A%222%22,%22formReadOnly%22%3Afalse,%22formUrl%22%3A%22%2FmaterialManagement%2Fpurchase%2FPurchaseForm%22,%22procDefKey%22%3A%22Process_1669961023358%22,%22taskDefKey%22%3A%22UserTask_0w6fh5r%22,%22procInsId%22%3A%22393afba7-5a13-11ef-b57b-387a0e63bd5c%22,%22procDefId%22%3A%22Process_1669961023358%3A9%3A521031c7-359b-11ee-b319-00163e1dafc2%22,%22taskId%22%3A%22e41b0ecf-5e8e-11ef-8676-387a0e63bd5c%22,%22businessId%22%3A%22afd001588750407385ecb58c9c8229f7%22%7D
                // http://localhost:8000/h5/#/pages/workbench/task/TaskForm?flow={"formTitle":"张静 在 2024-08-14 15:59 发起了 [采购申请]","title":"审批【综合管理部主任审批】","formType":"2","formReadOnly":false,"formUrl":"/materialManagement/purchase/PurchaseForm","procDefKey":"Process_1669961023358","taskDefKey":"UserTask_0w6fh5r","procInsId":"393afba7-5a13-11ef-b57b-387a0e63bd5c","procDefId":"Process_1669961023358:9:521031c7-359b-11ee-b319-00163e1dafc2","taskId":"e41b0ecf-5e8e-11ef-8676-387a0e63bd5c","businessId":"afd001588750407385ecb58c9c8229f7","openId":"oOy2A6Oqr0Zx42L_zgTlcQXrnngE"}
                SpringUtil.getBean ( WxSendService.class ).sendProcessTemplate(template);
            }

        }

    }
}
