package cn.axzo.msg.center.message.domain.param;

import cn.axzo.core.utils.converter.BeanConverter;
import cn.axzo.msg.center.api.custombutton.ProposedButtons;
import cn.axzo.msg.center.api.request.v3.PendingSendInfo;
import cn.axzo.msg.center.service.dto.PersonDTO;
import cn.axzo.msg.center.service.enums.OrganizationTypeEnum;
import cn.axzo.msg.center.service.enums.YesOrNo;
import cn.axzo.msg.center.service.pending.request.PendingMessagePushRequest;
import cn.axzo.msg.center.service.util.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;

/**
 * @author cold_blade
 * @version 1.0
 * @description
 * @date 2023/10/5
 */
@Slf4j
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PendingMessagePushParam extends PendingSendInfo implements Serializable {

    /**
     * 待办所属组织类型
     */
    private OrganizationTypeEnum orgType;
    /**
     * 发起者
     */
    private PersonDTO promoter;
    /**
     * 执行者
     */
    private List<PersonDTO> executor;
    /**
     * 模板编码
     */
    private String templateCode;
    /**
     * 消息所属企业Id
     * <p>如果是没有企业的工人可以不传，其它任何情况下都必传。
     */
    private Long ouId;
    /**
     * 待办所属项目部Id
     */
    private Long workspaceId;
    /**
     * 发送者属企业Id
     */
    private Long promoterOuId;
    /**
     * 发送者项目部Id
     */
    private Long promoterWorkspaceId;
    /**
     * 关联业务主键
     */
    private String bizCode;
    /**
     * 业务扩展参数,JSON字符串格式
     */
    private String bizExtParams;
    /**
     * 路由参数（json string）
     */
    private String routerParams;

    /**
     * 是否支持批量处理
     */
    private YesOrNo supportBatchProcess;

    /**
     * 提前设定的按钮
     */
    private ProposedButtons proposedButtons;

    public ProposedButtons determineProposedButtons() {
        return proposedButtons == null ? new ProposedButtons() : proposedButtons;
    }

    public static PendingMessagePushParam from(PendingMessagePushRequest request) {
        return BeanConverter.convert(request, PendingMessagePushParam.class);
    }

    public JSONObject normalize() {
        JSONObject bizExtParams = JSONUtils.parseObjectOrThrow("bizExtParams", getBizExtParams());
        JSONObject routerExtParams = JSONUtils.parseObjectOrThrow("routerParams", getRouterParams());
        JSONObject jsonRequest = JSON.parseObject(JSON.toJSONString(this));
        jsonRequest.put("bizExtParams", bizExtParams);
        jsonRequest.put("routerParams", routerExtParams);
        return jsonRequest;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}