package com.malk.harrison.entity;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Data
public class XyTodo {
    //系统标识（每个系统和环境不同，需要联系管理员获取）
    private String sysCode;

    //密钥（每个系统和环境不同，需要联系管理员获取）
    private String secureCode;

    //唯一流水号
    private String busNo;

    //标题
    private String title;

    //业务层级类型，例如："人力资源流程,员工出差申请",层级必须不少于二级。
    private String busType;

    //当前状况（流程描述，即当前节点名称）
    private String currentStatus;

    //待办 PC 地址，传输相对路径时需要向管理员提供唯一地址前缀，该地址需能过 OA 单点进行跳转
    private String pcUrl;

    //待办 APP 地址，传输相对路径时需要向管理员提供唯一地址前缀，该地址需能过 OA 单点进行跳转
    private String appUrl;

    //操作类型：1、待办；2、已办；3、待阅
    //如果新的推送类型一样，本次推送不生效，所以不能通过重复推送的办法去修改例如标题，审批状态等数据，如果需要修改数据，请对接专门的修改接口
    private int optType;

    //创建人域账号
    private String creator;

    //创建人姓名
    private String creatorName;

    //创建时间，样例 : "yyyy-MM-dd HH:mm:ss"
    private String createDateTime;

    //接收人域账号，字符串形式，支持多人逗号隔开
    private String receivers;

    //接收人姓名，字符串形式，支持多人逗号隔开
    private String receiverNames;

    //接收时间，样例 : "yyyy-MM-dd HH:mm:ss"
    private String receiveDateTime;

    //是否支持批量操作
    private boolean isSupportBatch;

    //审批时间，新的数据必须大于等于数据库中的旧数据，本次推送才生效
    private String approveDateTime;

    //关键字字段（该字段为搜索条件，对单据进行定位）
    private List<String> keywords;

    //流程 id（主要用于统一待办添加关注功能，以及催办功能）
    private String flowId;

    public Map toMap() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        // 禁用属性名自动检测，直接使用字段名
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
        return mapper.convertValue(this, Map.class);
    }

    public XyTodo() {
    }

    public XyTodo(String sysCode, String secureCode, String busType) {
        this.sysCode = sysCode;
        this.secureCode = secureCode;
        this.busType = busType;
    }

    public void setCurrentStatus(String currentStatus) {
        if (Objects.nonNull(this.currentStatus)){
            String[] split = this.currentStatus.split(",");

            boolean isExist = false;
            for (String s : split) {
                if (s.equals(currentStatus)) {
                    isExist = true;
                }
            }

            if (!isExist) {
                this.currentStatus += "," + currentStatus;
            }
        }else {
            this.currentStatus = currentStatus;
        }
    }

    public void setReceiverNames(String receiverNames) {
        if (Objects.nonNull(this.receiverNames)){
            String[] split = this.receiverNames.split(",");

            boolean isExist = false;
            for (String s : split) {
                if (s.equals(receiverNames)) {
                    isExist = true;
                }
            }

            if (!isExist) {
                this.receiverNames += "," + receiverNames;
            }
        }else {
            this.receiverNames = receiverNames;
        }
    }

    public void setReceivers(String receivers) {
        if (Objects.nonNull(this.receivers)){
            String[] split = this.receivers.split(",");

            boolean isExist = false;
            for (String s : split) {
                if (s.equals(receivers)) {
                    isExist = true;
                }
            }

            if (!isExist) {
                this.receivers += "," + receivers;
            }
        }else{
            this.receivers = receivers;
        }

    }
}
