package cn.axzo.workflow.core.conf.handler;

import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.util.List;

/**
 * BpmnTaskDelegateAssigner 数据映射转换
 *
 * @author wangli
 * @since 2024-09-07 22:40
 */
@Slf4j
@MappedTypes({List.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class ListAssigneeTypeHandler extends AbstractJsonTypeHandler<List<BpmnTaskDelegateAssigner>> {
    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public ListAssigneeTypeHandler(Class<?> type) {
        if (log.isTraceEnabled()) {
            log.trace("JacksonTypeHandler(" + type + ")");
        }
        Assert.notNull(type, "Type argument cannot be null", new Object[0]);
    }

    protected List<BpmnTaskDelegateAssigner> parse(String json) {
        try {
            // 这里进行了json解析，同样在这里也可以进行字段查询后的处理，如对象内部的手机号字段的加密展示等
            return objectMapper.readValue(json, new TypeReference<List<BpmnTaskDelegateAssigner>>() {
            });
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }

    protected String toJson(List<BpmnTaskDelegateAssigner> obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static void setObjectMapper(ObjectMapper om) {
        objectMapper = om;
    }
}
