You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

618 lines
15 KiB

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/hsktime"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskutils"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
"xorm.io/builder"
"xorm.io/xorm"
)
type Temp3A60 struct {
Id int
service string
QueueStatus string
}
// ----------------------------temp------------------------------------
// wherePrimaryKey 主键查询
func (TempLowHead3A60 *Temp3A60) wherePrimaryKey() (q interface{}, args []interface{}) {
//todo 根据需求修改
return "`id`=?", []interface{}{
TempLowHead3A60.Id,
}
}
// wherePrimaryKeyWithCond 主键查询
func (TempLowHead3A60 *Temp3A60) wherePrimaryKeyWithCond() builder.Cond {
//todo 根据需求修改
return nil
}
// whereDataWithCond 列表查询
func (TempLowHead3A60 *Temp3A60) whereDataWithCond() builder.Cond {
//todo 根据需求修改
return nil
}
// whereData 列表查询筛选
func (TempLowHead3A60 *Temp3A60) whereData() (q interface{}, args []interface{}) {
//todo
return "", nil
}
// orderData 列表查询排序
func (TempLowHead3A60 *Temp3A60) orderData() string {
//todo
return "`id` desc"
}
// init 数据初始化,可附加更多数据
func (TempLowHead3A60 *Temp3A60) init() {
//todo
}
func (TempLowHead3A60 *Temp3A60) joinTable() (typesAndsqlxAndcondition [][]string) {
//todo
return [][]string{
// {"LEFT", "(SELECT id as external_brand_id,name as brand_name FROM `ox_brand`) as tmp", "tmp.external_brand_id=brand_id"},
}
}
// saveAllCols 保存时是否全量保存
func (TempLowHead3A60 *Temp3A60) isSaveAllCols() bool {
return false
}
// insertPretreatment 新增前预处理
func (TempLowHead3A60 *Temp3A60) insertPretreatment() error {
//todo
return nil
}
// updatePretreatment 修改前预处理
func (TempLowHead3A60 *Temp3A60) updatePretreatment() error {
//todo
return nil
}
// deletePretreatment 删除前预处理
func (TempLowHead3A60 *Temp3A60) deletePretreatment() error {
//todo
return nil
}
func (TempLowHead3A60 *Temp3A60) getXorm() *xorm.Engine {
//todo
return hskdb.GetXormAuto()
}
// 队列状态
const (
TempLowHead3A60_AutoQueueWait = 2 * time.Second
TempLowHead3A60_AutoQueueStatusNone = "None"
TempLowHead3A60_AutoQueueStatusWait = "Wait"
TempLowHead3A60_AutoQueueStatusIng = "Ing"
TempLowHead3A60_AutoQueueStatusSuccess = "Success"
TempLowHead3A60_AutoQueueStatusFail = "Fail"
)
// 不同service对应的不同队列状态
var TempLowHead3A60_AutoQueueStatusFieldMap = map[string]string{
"service": "queue_status",
}
// 队列事件
func (TempLowHead3A60 *Temp3A60) queueEvent() error {
//todo
return nil
}
// ----------------------------temp------------------------------------
// Save 保存或者新增,通过wherePrimaryKey查不到数据时,新增数据,否则修改数据,参数:isAllCols...
func (TempLowHead3A60 *Temp3A60) Save(mustCols ...string) (err error) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
tmp := Temp3A60{}
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
session := TempLowHead3A60.getXorm().NewSession()
defer session.Close()
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
_, err = session.Get(&tmp)
if err != nil {
return
}
if tmp.Id == 0 {
err = TempLowHead3A60.insertPretreatment()
if err != nil {
return
}
_, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60)
} else {
err = TempLowHead3A60.updatePretreatment()
if err != nil {
return
}
if len(mustCols) > 0 {
_, err = TempLowHead3A60.getXorm().MustCols(mustCols...).Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
} else {
if TempLowHead3A60.isSaveAllCols() {
_, err = TempLowHead3A60.getXorm().AllCols().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
} else {
_, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
}
}
}
return err
}
func (TempLowHead3A60 *Temp3A60) Service(key string) *Temp3A60 {
TempLowHead3A60.service = key
return TempLowHead3A60
}
func (TempLowHead3A60 *Temp3A60) getFieldTypes() map[string]reflect.Kind {
types := reflect.TypeOf(TempLowHead3A60)
fieldType := map[string]reflect.Kind{}
for i := 0; i < types.Elem().NumField(); i++ {
if types.Elem().Field(i).Type.String() == "hsktime.Time" {
fieldType[string(types.Elem().Field(i).Tag.Get("json"))] = reflect.Kind(100)
continue
}
fieldType[string(types.Elem().Field(i).Tag.Get("json"))] = types.Elem().Field(i).Type.Kind()
}
return fieldType
}
func (TempLowHead3A60 *Temp3A60) fixSqlRowMap(fieldType map[string]reflect.Kind, raw map[string]interface{}) map[string]interface{} {
for i := range raw {
// log.Println(i, fieldType[i], cast.ToString(raw[i]))
switch fieldType[i] {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
raw[i] = cast.ToInt64(cast.ToString(raw[i]))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
raw[i] = cast.ToUint64(cast.ToString(raw[i]))
case reflect.Float32, reflect.Float64:
raw[i] = cast.ToFloat64(cast.ToString(raw[i]))
case reflect.String:
raw[i] = cast.ToString(raw[i])
case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct, reflect.Ptr:
raw[i] = gjson.Parse(cast.ToString(raw[i])).Value() //todo
case 100:
// 2023-09-04 09:53:19 +0000 UTC
_time, _ := time.Parse(
"2006-01-02 15:04:05 +0000 UTC",
cast.ToString(raw[i]),
)
raw[i] = hsktime.Time(_time)
default:
raw[i] = cast.ToString(raw[i])
}
}
return raw
}
// Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据
func (TempLowHead3A60 *Temp3A60) Load() (err error) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
session := TempLowHead3A60.getXorm().NewSession()
defer session.Close()
typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
isJoin := false
for _, v := range typesAndSqlxAndCndition {
isJoin = true
session = session.Join(v[0], v[1], v[2])
}
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
if !isJoin {
_, err = session.Get(TempLowHead3A60)
if err != nil {
return
}
} else {
fieldType := TempLowHead3A60.getFieldTypes()
session = session.Table(TempLowHead3A60)
raw := map[string]interface{}{}
session.Get(&raw)
raw = TempLowHead3A60.fixSqlRowMap(fieldType, raw)
hskutils.Interface2Struct(raw, TempLowHead3A60)
}
TempLowHead3A60.init()
return
}
// Data 不进行分页的数据列表
func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
q, args := TempLowHead3A60.whereData()
_xorm := TempLowHead3A60.getXorm()
session := _xorm.NewSession()
defer func() {
session.Close()
}()
typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
isJoin := false
for _, v := range typesAndSqlxAndCndition {
isJoin = true
session = session.Join(v[0], v[1], v[2])
}
if q != "" {
session = session.Where(q, args...)
}
whereCond := TempLowHead3A60.whereDataWithCond()
if whereCond != nil {
session = session.Where(whereCond)
}
order := TempLowHead3A60.orderData()
if order != "" {
session = session.OrderBy(order)
}
if !isJoin {
session.Find(&data)
} else {
fieldType := TempLowHead3A60.getFieldTypes()
session = session.Table(TempLowHead3A60)
raw := []map[string]interface{}{}
session.Find(&raw)
for i := range raw {
item := Temp3A60{}
raw[i] = TempLowHead3A60.fixSqlRowMap(fieldType, raw[i])
hskutils.Interface2Struct(raw[i], &item)
data = append(data, item)
}
}
for i := range data {
data[i].service = TempLowHead3A60.service
data[i].init()
}
return
}
// PageData 进行分页的数据列表,page从0开始
func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count int64) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
q, args := TempLowHead3A60.whereData()
_xorm := TempLowHead3A60.getXorm()
session := _xorm.NewSession()
defer func() {
session.Close()
}()
typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
isJoin := false
for _, v := range typesAndSqlxAndCndition {
isJoin = true
session = session.Join(v[0], v[1], v[2])
}
if q != "" {
session = session.Where(q, args...)
}
whereCond := TempLowHead3A60.whereDataWithCond()
if whereCond != nil {
session = session.Where(whereCond)
}
order := TempLowHead3A60.orderData()
if order != "" {
session = session.OrderBy(order)
}
session = session.Limit(pageSize, page*pageSize)
if !isJoin {
count, _ = session.FindAndCount(&data)
} else {
fieldType := TempLowHead3A60.getFieldTypes()
session = session.Table(TempLowHead3A60)
raw := []map[string]interface{}{}
count, _ = session.FindAndCount(&raw)
for i := range raw {
item := Temp3A60{}
raw[i] = TempLowHead3A60.fixSqlRowMap(fieldType, raw[i])
hskutils.Interface2Struct(raw[i], &item)
data = append(data, item)
}
}
for i := range data {
data[i].service = TempLowHead3A60.service
data[i].init()
}
return
}
// Delete 删除数据
func (TempLowHead3A60 *Temp3A60) Delete() (err error) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
session := TempLowHead3A60.getXorm().NewSession()
defer session.Close()
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
err = TempLowHead3A60.deletePretreatment()
if err != nil {
return
}
_, err = session.Delete(TempLowHead3A60)
return
}
// ----------------------------temp------------------------------------
func StartQueueTaskPrefixHigHead3A60Temp3A60() {
serviceName := "Prefix3A60"
f := TempLowHead3A60_AutoQueueStatusFieldMap[serviceName]
go func() {
defer func() {
err := recover()
if err != nil {
L.Error("StartQueueTaskPrefixHigHead3A60Temp3A60 意外退出:", err)
}
time.Sleep(time.Second * 5)
StartQueueTaskPrefixHigHead3A60Temp3A60()
}()
for {
onec := &Temp3A60{}
hskdb.GetXormAuto().Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).OrderBy("rand()").Get(onec)
session := onec.getXorm().NewSession()
defer session.Close()
session = session.Table(onec)
whereQuery, whereArgs := onec.wherePrimaryKey()
whereCond := onec.wherePrimaryKeyWithCond()
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
i, err := session.Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusIng})
if err == nil && i == 1 {
func() {
{
onec.service = serviceName
err := onec.queueEvent()
session := onec.getXorm().NewSession()
defer session.Close()
session = session.Table(onec)
whereQuery, whereArgs := onec.wherePrimaryKey()
whereCond := onec.wherePrimaryKeyWithCond()
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
if err != nil {
session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusFail})
} else {
session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusSuccess})
}
}
}()
}
time.Sleep(TempLowHead3A60_AutoQueueWait)
}
}()
}
func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, authoritys ...func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60)) {
var authority = func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60) {
}
if len(authoritys) > 0 {
authority = authoritys[0]
}
router.Any("/Prefix3A60/TempLowHead3A60/tryToInitialize", func(ctx *hskgin.GinContextHelper) {
req := &Temp3A60{}
err := hskdb.XormSync2(&Temp3A60{})
ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req)
})
/*
@yapi
@group TempLowHead3A60
@name 获取(Prefix3A60-TempLowHead3A60)单个数据
@path /Prefix3A60/TempLowHead3A60/get
@markdown
##
```
传入主键获取对应数据
```
@end
*/
router.Any("/Prefix3A60/TempLowHead3A60/get", func(ctx *hskgin.GinContextHelper) {
req := &Temp3A60{}
ctx.Bind(req)
authority(ctx, req)
req.service = "Prefix3A60"
err := req.Load()
ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req)
})
/*
@yapi
@group TempLowHead3A60
@name 保存(Prefix3A60-TempLowHead3A60)单个数据
@path /Prefix3A60/TempLowHead3A60/save
@markdown
##
```
传入整体数据插入或者保存(取决于主键是否存在)
```
@end
*/
router.Any("/Prefix3A60/TempLowHead3A60/save", func(ctx *hskgin.GinContextHelper) {
req := &Temp3A60{}
ctx.Bind(&req)
authority(ctx, req)
req.service = "Prefix3A60"
err := req.Save()
ctx.CheckErrDisplayByError(err)
err = req.Load()
ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req)
})
/*
@yapi
@group TempLowHead3A60
@name 获取(Prefix3A60-TempLowHead3A60)无分页数据
@path /Prefix3A60/TempLowHead3A60/data
@markdown
##
```
获取数据列表,无分页
```
@end
*/
router.Any("/Prefix3A60/TempLowHead3A60/data", func(ctx *hskgin.GinContextHelper) {
req := &Temp3A60{}
ctx.Bind(&req)
authority(ctx, req)
req.service = "Prefix3A60"
data := req.Data()
ctx.DisplayByData(data)
})
/*
@yapi
@group TempLowHead3A60
@name 获取(Prefix3A60-TempLowHead3A60)分页数据
@path /Prefix3A60/TempLowHead3A60/list
@markdown
##
```
获取数据列表,分页,page参数
```
@end
*/
router.Any("/Prefix3A60/TempLowHead3A60/list", func(ctx *hskgin.GinContextHelper) {
req := &Temp3A60{}
ctx.Bind(&req)
authority(ctx, req)
page := ctx.ParamRequired2Page(true)
req.service = "Prefix3A60"
data, count := req.PageData(page, 10)
ctx.DisplayByPage(data, count)
})
/*
@yapi
@group TempLowHead3A60
@name 删除一条(Prefix3A60-TempLowHead3A60)数据
@path /Prefix3A60/TempLowHead3A60/delete
@markdown
##
```
传入主键参数,删除一条数据
```
@end
*/
router.Any("/Prefix3A60/TempLowHead3A60/delete", func(ctx *hskgin.GinContextHelper) {
req := &Temp3A60{}
ctx.Bind(&req)
authority(ctx, req)
req.service = "Prefix3A60"
err := req.Delete()
ctx.CheckErrDisplayByError(err)
ctx.DisplayBySuccess("删除成功")
})
}