Browse Source

'fix'

master
u 2 years ago
parent
commit
fa0cf3baa4
  1. 97
      hasaki-gen-crud/template.go

97
hasaki-gen-crud/template.go

@ -2,11 +2,15 @@ package main
import (
"fmt"
"reflect"
"time"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hsklogger/L"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskutils"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
"xorm.io/builder"
"xorm.io/xorm"
)
@ -173,7 +177,9 @@ func (TempLowHead3A60 *Temp3A60) Load() (err error) {
defer session.Close()
typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
isJoin := false
for _, v := range typesAndSqlxAndCndition {
isJoin = true
session = session.Join(v[0], v[1], v[2])
}
@ -184,9 +190,31 @@ func (TempLowHead3A60 *Temp3A60) Load() (err error) {
session = session.Where(whereCond)
}
_, err = session.Get(TempLowHead3A60)
if err != nil {
return
if !isJoin {
_, err = session.Get(TempLowHead3A60)
if err != nil {
return
}
} else {
types := reflect.TypeOf(TempLowHead3A60)
structField := []string{}
for i := 0; i < types.Elem().NumField(); i++ {
if types.Elem().Field(i).Type.Kind() == reflect.Struct || types.Elem().Field(i).Type.Kind() == reflect.Slice || types.Elem().Field(i).Type.Kind() == reflect.Map {
structField = append(structField, string(types.Elem().Field(i).Tag.Get("json")))
}
}
session = session.Table(TempLowHead3A60)
raw := map[string]interface{}{}
session.Get(&raw)
for _, v := range structField {
raw[v] = gjson.Parse(cast.ToString(raw[v])).Value()
}
hskutils.Interface2Struct(raw, TempLowHead3A60)
}
TempLowHead3A60.init()
@ -208,7 +236,9 @@ func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
}()
typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
isJoin := false
for _, v := range typesAndSqlxAndCndition {
isJoin = true
session = session.Join(v[0], v[1], v[2])
}
@ -226,7 +256,33 @@ func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
session = session.OrderBy(order)
}
session.Find(&data)
if !isJoin {
session.Find(&data)
} else {
types := reflect.TypeOf(TempLowHead3A60)
structField := []string{}
for i := 0; i < types.Elem().NumField(); i++ {
if types.Elem().Field(i).Type.Kind() == reflect.Struct || types.Elem().Field(i).Type.Kind() == reflect.Slice || types.Elem().Field(i).Type.Kind() == reflect.Map {
structField = append(structField, string(types.Elem().Field(i).Tag.Get("json")))
}
}
session = session.Table(TempLowHead3A60)
raw := []map[string]interface{}{}
session.Find(&raw)
for i := range raw {
item := Temp3A60{}
for _, v := range structField {
raw[i][v] = gjson.Parse(cast.ToString(raw[i][v])).Value()
}
hskutils.Interface2Struct(raw[i], &item)
data = append(data, item)
}
}
for i := range data {
data[i].init()
}
@ -247,7 +303,9 @@ func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60,
}()
typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
isJoin := false
for _, v := range typesAndSqlxAndCndition {
isJoin = true
session = session.Join(v[0], v[1], v[2])
}
if q != "" {
@ -264,7 +322,36 @@ func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60,
session = session.OrderBy(order)
}
count, _ = session.Limit(pageSize, page*pageSize).FindAndCount(&data)
session = session.Limit(pageSize, page*pageSize)
if !isJoin {
count, _ = session.FindAndCount(&data)
} else {
types := reflect.TypeOf(TempLowHead3A60)
structField := []string{}
for i := 0; i < types.Elem().NumField(); i++ {
if types.Elem().Field(i).Type.Kind() == reflect.Struct || types.Elem().Field(i).Type.Kind() == reflect.Slice || types.Elem().Field(i).Type.Kind() == reflect.Map {
structField = append(structField, string(types.Elem().Field(i).Tag.Get("json")))
}
}
session = session.Table(TempLowHead3A60)
raw := []map[string]interface{}{}
count, _ = session.FindAndCount(&raw)
for i := range raw {
item := Temp3A60{}
for _, v := range structField {
raw[i][v] = gjson.Parse(cast.ToString(raw[i][v])).Value()
}
hskutils.Interface2Struct(raw[i], &item)
data = append(data, item)
}
}
for i := range data {
data[i].init()
}

Loading…
Cancel
Save