- 新增 Entity 结构体定义,包含 Id、Number 和 DIndex 字段 - 实现 SetStringId 方法,支持将十六进制字符串转换为 int64 ID - 实现 GetStringId 方法,支持将 int64 ID 转换为十六进制字符串 - 实现自定义 UnmarshalJSON 方法,支持解析不同类型的 Id 字段 - 支持 Number 和 DIndex 字段的 JSON 解析 - 添加必要的导入包:encoding/json、fmt 和 strconv
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package ik3cloud
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
type Ik3cloud struct {
|
|
Department department // 部门
|
|
Staff staff // 员工
|
|
Position position // 职位
|
|
Contact contact // 联系人
|
|
Factory factory // 工厂
|
|
Custom custom // 客户
|
|
Product product // 产品
|
|
Receivable receivable // 应收
|
|
Payable payable // 应付
|
|
Dict dict // 字典
|
|
Payment payment // 付款单
|
|
Information information // 资料
|
|
}
|
|
|
|
type Entity struct {
|
|
Id int64 `json:"Id"`
|
|
Number string `json:"Number"`
|
|
DIndex int `json:"DIndex"`
|
|
}
|
|
|
|
// SetStringId @TITLE 设置字符串id
|
|
func (e *Entity) SetStringId(stringId string) {
|
|
e.Id, _ = strconv.ParseInt(stringId, 16, 64)
|
|
}
|
|
|
|
// GetStringId @TITLE 获取字符串id
|
|
func (e *Entity) GetStringId() string {
|
|
return strconv.FormatInt(e.Id, 16)
|
|
}
|
|
|
|
func (e *Entity) UnmarshalJSON(data []byte) error {
|
|
var raw map[string]interface{}
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 处理 Id 字段,支持字符串和数字类型
|
|
if idVal, ok := raw["Id"]; ok {
|
|
switch v := idVal.(type) {
|
|
case string:
|
|
e.SetStringId(v)
|
|
case float64: // JSON 中的数字默认为 float64
|
|
e.Id = int64(v)
|
|
case int64:
|
|
e.Id = v
|
|
default:
|
|
return fmt.Errorf("unsupported type for Id: %T", v)
|
|
}
|
|
}
|
|
|
|
// 其他字段处理
|
|
if num, ok := raw["Number"].(string); ok {
|
|
e.Number = num
|
|
}
|
|
if dIndex, ok := raw["DIndex"].(float64); ok {
|
|
e.DIndex = int(dIndex)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type Unique struct {
|
|
Numbers []string // 编码
|
|
Ids []int64 // id
|
|
}
|