package cn.axzo.msg.center.service.pending.request;

import cn.axzo.basics.common.exception.ServiceException;
import cn.axzo.msg.center.service.dto.PersonDTO;
import cn.axzo.msg.center.service.enums.BizCategoryEnum;
import cn.axzo.msg.center.service.enums.BizFinalStateEnum;
import cn.axzo.msg.center.service.enums.OrganizationTypeEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @description
 *
 * @author cold_blade
 * @date 2023/9/26
 * @version 1.0
 */
@Slf4j
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PendingMessageCopyToRequest implements Serializable {

    private static final long serialVersionUID = -4523292375275477706L;

    /**
     * 发起者
     */
    @NotNull(message = "promoter is required")
    private PersonDTO promoter;
    /**
     * 执行者
     */
    @NotEmpty(message = "executor can't be empty")
    private List<PersonDTO> executor;
    /**
     * 模板编码
     */
    @NotBlank(message = "templateCode is required")
    private String templateCode;
    /**
     * 消息所属组织类型
     */
    private OrganizationTypeEnum orgType;
    /**
     * 消息所属工作台ID
     */
    @NotNull
    private Long workspaceId;

    /**
     * 消息所属企业id
     */
    private Long ouId;
//    /**
//     * 删除工作台名称,通过id查
//     * 消息所属项目部名称
//     */
//    private String workspaceName;
    /**
     * 业务类型
     */
    @NotNull(message = "bizCategory is required")
    private BizCategoryEnum bizCategory;
    /**
     * 关联业务主键
     */
    private String bizCode;
    /**
     * 流程类代办的流程结点编码
     */
    private String subBizCode;
    /**
     * 业务描述eg:流程结点描述
     */
    private String bizDesc;
    /**
     * 业务扩展参数,JSON字符串格式
     */
    private String bizExtParams;
    /**
     * 路由参数（json string）
     */
    private String routerParams;
    /**
     * 业务标签
     */
    private String bizFlag;
    /**
     * 业务终态，可为空
     */
    private BizFinalStateEnum bizFinalState;
    /**
     * 待办的截止时间
     */
    private Date deadline;

    // !! helper

    @JsonIgnore
    private JSONObject bizExtParamsObj;

    @JsonIgnore
    private JSONObject routerParamsObj;

    public JSONObject getBizExtParamsObj() {
        if (bizExtParams == null)
            return null;
        if (bizExtParamsObj == null)
            bizExtParamsObj = parseJson("bizExtParams", bizExtParams);
        return bizExtParamsObj;
    }

    public JSONObject getRouterParamsObj() {
        if (routerParams == null)
            return null;
        if (routerParamsObj == null)
            routerParamsObj = parseJson("routerParams", routerParams);
        return routerParamsObj;
    }

    private static JSONObject parseJson(String name, String jsonStr) {
        try {
            return JSON.parseObject(jsonStr);
        } catch (Exception e) {
            log.warn("无效的json格式, name={}, jsonStr={}", name, jsonStr, e);
            throw new ServiceException(String.format("无效的参数格式, 要求是JSON对象. %s=%s", name, jsonStr));
        }
    }

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