1 Star 0 Fork 0

tomatomeatman/GolangRepository

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ControllerUtil.go 38.20 KB
一键复制 编辑 原始数据 按行查看 历史
tomatomeatman 提交于 2025-01-07 22:53 +08:00 . 3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
package app
import (
"bytes"
"encoding/json"
"fmt"
"io"
uu "net/url"
"reflect"
"strings"
"time"
"gitee.com/tomatomeatman/golang-repository/bricks/model/dbinfo"
"gitee.com/tomatomeatman/golang-repository/bricks/model/msgentity"
"gitee.com/tomatomeatman/golang-repository/bricks/model/set"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/function/data/timeutil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/function/urlutil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/ginutil"
Log "github.com/cihub/seelog"
)
type ControllerUtil struct{}
/**
* 新增
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) Add(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".Add"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.Add(ctx, entity, params)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 批量新增
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) Adds(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
objStr := urlutil.GetParam(ctx.Request, "objs", "").(string)
if objStr == "" {
return msgentity.Err(9001, "未能获取'objs'参数")
}
var objs []map[string]interface{}
err := json.Unmarshal([]byte(objStr), &objs)
if err != nil {
Log.Error("参数'objs'转换出错:", err)
return msgentity.Err(9002, "参数'objs'转换出错")
}
if len(objs) < 1 {
return msgentity.Err(9003, "参数'objs'转换后没有数据")
}
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
entitys := make([]dbinfo.Entity, len(objs))
for i := 0; i < len(objs); i++ {
entitys = append(entitys, entity.New())
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".Adds"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.Adds(ctx, entitys, objs)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 新增树节点
* @param ctx http请求对象
* @param entity 检查用数据结构
* @return
*/
func (controlUtil ControllerUtil) AddNode(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".AddNode"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.AddNode(ctx, entity, params)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 删除
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) Del(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
id := urlutil.GetParamToId(ctx.Request, dbinfo.EntityKeyName(entity)) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return msgentity.Err(9012, "未能获取'", dbinfo.EntityKeyName(entity), "'参数")
}
}
iVersion := urlutil.GetParamToVersion(ctx.Request, dbinfo.EntityHasVersion(entity), dbinfo.TableVersionName) //取请求参数中的版本号
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".Del"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.Del(ctx, entity, id, iVersion)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 修改
* @param ctx http请求对象
* @param control 控制层接口
* @return msgentity.MsgEntity
*/
func (controlUtil ControllerUtil) Edit(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
id := urlutil.GetParamToId(ctx.Request, dbinfo.EntityKeyName(entity)) //取请求参数中的记录编号
iVersion := urlutil.GetParamToVersion(ctx.Request, dbinfo.EntityHasVersion(entity), dbinfo.TableVersionName) //取请求参数中的版本号
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
data := me.Gdata.(map[string]interface{})
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = data[dbinfo.EntityKeyName(entity)]
if id == nil || (fmt.Sprintf("%v", id) == "") {
return msgentity.Err(1001, "未能获取'", dbinfo.EntityKeyName(entity), "'参数")
}
}
if iVersion == -1 {
if val, ok := data[dbinfo.TableVersionName]; ok {
iVersion = val.(int)
}
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".Edit"
me = AopUtil{}.CallBeforeFunc(name, ctx)
if !me.Gsuccess {
return me
}
me = service.Edit(ctx, entity, id, iVersion, data)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 批量修改
* @param ctx http请求对象
* @param control 控制层接口
* @return msgentity.MsgEntity
*/
func (controlUtil ControllerUtil) Edits(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
datas := me.Gdata.([]map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".Edits"
me = AopUtil{}.CallBeforeFunc(name, ctx)
if !me.Gsuccess {
return me
}
me = service.Edits(ctx, entity, datas)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据主键查询数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindById(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
id := urlutil.GetParamToId(ctx.Request, dbinfo.EntityKeyName(entity)) //取请求参数中的记录编号
if (nil == id) || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
}
if nil == id || (fmt.Sprintf("%v", id) == "") {
return msgentity.Err(1001, "记录编号为空")
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindById"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.FindById(ctx, entity, id)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查询所有数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindAll(ctx ginutil.Context, control Controller, attachWhere ...set.Set) *msgentity.MsgEntity {
whereInfo := []dbinfo.WhereInfo{}
if nil != attachWhere && len(attachWhere) > 0 {
for i := 0; i < len(attachWhere); i++ {
whereInfo = append(whereInfo, dbinfo.WhereInfo{Name: attachWhere[i].Key, Value: attachWhere[i].Value})
}
}
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindAll"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.FindAll(ctx, entity, whereInfo)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查询时间范围内数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindByDate(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
sDateSt := urlutil.GetParam(ctx.Request, "sDateSt", "").(string) //记录开始时间
sDateEd := urlutil.GetParam(ctx.Request, "sDateEd", "").(string) //记录结束时间
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindByDate"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.FindByDate(ctx, entity, sDateSt, sDateEd)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查找指定行数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindByRow(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
id := urlutil.GetParamToId(ctx.Request, dbinfo.EntityKeyName(entity)) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return msgentity.Err(1001, "未能获取'", dbinfo.EntityKeyName(entity), "'参数")
}
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindByRow"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.FindByRow(ctx, entity, id)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查询分页数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindByPage(ctx ginutil.Context, control Controller, injectionCondition ...set.Set) *msgentity.MsgEntity {
me := urlutil.GetBody(ctx.Request, dbinfo.FindByPageParam{})
if !me.Gsuccess {
return me
}
findByPageParam := me.Gdata.(dbinfo.FindByPageParam)
if findByPageParam.Gpage.GiSize < 1 {
findByPageParam.Gpage.GiSize = 10
}
if findByPageParam.Gpage.GiCurrent < 1 {
findByPageParam.Gpage.GiCurrent = 1
}
if (nil != injectionCondition) || (len(injectionCondition) > 0) { //将注入条件覆盖到查询条件
if nil == findByPageParam.Gcondition {
findByPageParam.Gcondition = map[string]interface{}{}
}
for _, set := range injectionCondition {
findByPageParam.Gcondition.(map[string]interface{})[set.Key] = set.Value
}
}
sLikeStr := strings.TrimSpace(findByPageParam.GsLikeStr) //全文检索条件
if sLikeStr != "" { //存在全文检索条件则需要考虑时间范围的问题
sLikeDateSt := strings.TrimSpace(findByPageParam.GsLikeDateSt) //模糊查询记录修改时间范围条件-开始
sLikeDateEd := strings.TrimSpace(findByPageParam.GsLikeDateEd) //模糊查询记录修改时间范围条件-结束
var dLikeDateEd time.Time
if sLikeDateEd == "" { //如果结束时间为空,则当前时间就是结束时间
dLikeDateEd = time.Now()
sLikeDateEd = dLikeDateEd.Format("2006-01-02 15:04:05")
} else {
dLikeDateEd = timeutil.ToDate(sLikeDateEd)
}
if sLikeDateSt == "" { //如果开始时间为空,则用结束时间-时间限制
iLikeTimeLimit := control.LikeTimeLimit()
sLikeDateSt = timeutil.AddDay(dLikeDateEd, -iLikeTimeLimit).Format("2006-01-02 15:04:05")
}
findByPageParam.GsLikeDateSt = sLikeDateSt
findByPageParam.GsLikeDateEd = sLikeDateEd
}
findByPageParam.GsHideFields = strings.TrimSpace(findByPageParam.GsHideFields)
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindByPage"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindByPage(ctx, entity, findByPageParam)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取所有参数,并转换成对应实体属性类型(map[string]interface{})
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) GetParams(ctx ginutil.Context, entity interface{}) *msgentity.MsgEntity {
if reflect.TypeOf(entity).Kind() == reflect.Struct {
br, _ := io.ReadAll(ctx.Request.Body)
ctx.Request.Body.Close()
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(br))
json.NewDecoder(bytes.NewBuffer(br)).Decode(entity)
// fmt.Println(reflect.TypeOf(entity).String()) // map[string]interface {}
return msgentity.Success(entity, "转换结束")
}
return urlutil.GetParams(ctx.Request, entity)
}
/**
* 读取树形结构数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindByTree(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
sGroupColumn := urlutil.GetParam(ctx.Request, "sGroupColumn", "").(string)
sGroupName := urlutil.GetParam(ctx.Request, "sGroupName", "").(string)
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindByTree"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.FindByTree(ctx, entity, sGroupColumn, sGroupName)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) GetValueByFieldName(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
temp := urlutil.GetParam(ctx.Request, "fieldNames", "").(string)
fieldNames := strings.Split(temp, ",")
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
id := urlutil.GetParamToId(ctx.Request, dbinfo.EntityKeyName(entity)) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".GetValueByFieldName"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.GetValueByFieldName(ctx, entity, id, fieldNames)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) GetValueByField(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
id := urlutil.GetParamToId(ctx.Request, dbinfo.EntityKeyName(entity)) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return msgentity.Err(9012, "未能获取'", dbinfo.EntityKeyName(entity), "'参数")
}
}
fieldName := urlutil.GetParam(ctx.Request, "fieldName", "").(string)
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".GetValueByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.GetValueByField(ctx, entity, id, fieldName)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindByKey(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
whereInfo := []dbinfo.WhereInfo{}
for k, v := range params {
info := dbinfo.WhereInfo{Name: k, Value: v}
whereInfo = append(whereInfo, info)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindByKey(ctx, entity, whereInfo)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合中的第一个
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindOneByKey(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
temp := urlutil.GetParam(ctx.Request, "fields", "").(string)
fields := strings.Split(temp, ",")
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
whereInfo := []dbinfo.WhereInfo{}
for k, v := range params {
info := dbinfo.WhereInfo{Name: k, Value: v}
whereInfo = append(whereInfo, info)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindOneByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindOneByKey(ctx, entity, whereInfo, fields...)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合中的符合条件的第一条记录的指定字段
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindValueByKey(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
whereInfo := []dbinfo.WhereInfo{}
for k, v := range params {
info := dbinfo.WhereInfo{Name: k, Value: v}
whereInfo = append(whereInfo, info)
}
fieldName := urlutil.GetParam(ctx.Request, "fieldName", "").(string)
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindValueByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindValueByKey(ctx, entity, whereInfo, fieldName)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值查数量
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindCountByKey(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
whereInfo := []dbinfo.WhereInfo{}
for k, v := range params {
info := dbinfo.WhereInfo{Name: k, Value: v}
whereInfo = append(whereInfo, info)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindCountByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindCountByKey(ctx, entity, whereInfo)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取分组数据
* @param ctx http请求对象
* @param control 控制层接口
* @param fields 分组字段[字段名,字段别名]
*/
func (controlUtil ControllerUtil) Group(ctx ginutil.Context, control Controller, fields map[string]string) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".GroupByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.GroupByField(ctx, entity, "", fields)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取分组数据且取数量
* @param ctx http请求对象
* @param control 控制层接口
* @param fields 分组字段[字段名,字段别名]
*/
func (controlUtil ControllerUtil) GroupAndCount(ctx ginutil.Context, control Controller, fields map[string]string) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".GroupByFieldAndCount"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.GroupByFieldAndCount(ctx, entity, "", fields)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取分组数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) GroupByField(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
fieldsTemp := me.Gdata.(map[string]interface{})
fields := make(map[string]string)
for k, v := range fieldsTemp {
fields[k] = fmt.Sprintf("%v", v)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".GroupByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.GroupByField(ctx, entity, "", fields)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最大值
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) MaxByField(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
field := urlutil.GetParam(ctx.Request, "field", "").(string)
if field == "" {
return msgentity.Err(9012, "field参数为空")
}
params := me.Gdata.(map[string]interface{})
whereInfo := []dbinfo.WhereInfo{}
for k, v := range params {
info := dbinfo.WhereInfo{Name: k, Value: v}
whereInfo = append(whereInfo, info)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".MaxByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.MaxByField(ctx, entity, "", field, whereInfo)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最小值
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) MinByField(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
field := urlutil.GetParam(ctx.Request, "field", "").(string)
if field == "" {
return msgentity.Err(9012, "field参数为空")
}
params := me.Gdata.(map[string]interface{})
whereInfo := []dbinfo.WhereInfo{}
for k, v := range params {
info := dbinfo.WhereInfo{Name: k, Value: v}
whereInfo = append(whereInfo, info)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".MinByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.MinByField(ctx, entity, "", field, whereInfo)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最小值
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) HasById(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
// tableInfo := dbinfo. entity..TableInfo()
id := urlutil.GetParamToId(ctx.Request, dbinfo.EntityKeyName(entity)) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return msgentity.Err(1001, "未能获取'", dbinfo.EntityKeyName(entity), "'参数")
}
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".HasById"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.HasById(ctx, entity, id)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 检查关键值记录是否存在(返回1:存在;0:不存在)
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) HasByKey(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
keyName := urlutil.GetParam(ctx.Request, "keyName", "") //取请求参数中的字段名
keyValue := urlutil.GetParam(ctx.Request, "keyValue", "") //取请求参数中的字段值
if keyName == "" {
return msgentity.Err(9001, "keyName参数为空")
}
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".HasByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.HasByKey(ctx, entity, keyName.(string), keyValue)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 清理指定用户的缓存
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) ClearCache(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
sUser := urlutil.GetParam(ctx.Request, "sUser", "").(string) //取请求参数中的用户名
cacheName := urlutil.GetParam(ctx.Request, "cacheName", "").(string) //取请求参数中的缓存名
if cacheName == "" {
return msgentity.Err(8001, "指定'缓存库名称'参数为空!")
}
if sUser == "" {
cacheName = cacheName + sUser
}
Log.Error("清理缓存库:" + cacheName)
// if globalvariable.Del(cacheName) {//这种方式不合适
// return msgentity.Success(8999, "清理成功!")
// }
return msgentity.Err(8002, "清理失败!")
}
/**
* 查询组结构数据
* @param ctx http请求对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindByGroup(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
sGroupColumn := urlutil.GetParam(ctx.Request, "sGroupColumn", "").(string) //分组名(树节点)所在字段名
sGroupName := urlutil.GetParam(ctx.Request, "sGroupName", "").(string) //分组名(树节点)
if sGroupName == "" {
return controlUtil.FindByTree(ctx, control)
}
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".FindByGroup"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.FindByGroup(ctx, entity, sGroupColumn, sGroupName)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 添加数据到指定组下
* 警告:对象必须符合树形结构要求,如:sId、sPid
* @param ctx http请求对象
* @param control 控制层接口
* @param sGroupName 分组字段名称(树节点)
* @param sGroupValue 分组字段值(树节点)
* @param entity 实体对象
* @return
*/
func (controlUtil ControllerUtil) AddToGroup(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
sGroupName := urlutil.GetParam(ctx.Request, "sGroupName", "").(string) //分组字段名称(树节点)
sGroupValue := urlutil.GetParam(ctx.Request, "sGroupValue", "").(string) //分组字段值(树节点)
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9001, "控制层未设置ModuleEntit:", err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".AddToGroup"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.AddToGroup(ctx, entity, sGroupName, sGroupValue)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值翻转值(限布尔值类型,1转2,2转1)
* 警告:此方法只支持布尔值类型,且只支持翻转1和2
* @param ctx http请求对象
* @param control 控制层接口
* @return msgentity.MsgEntity
*/
func (controlUtil ControllerUtil) ReversalByKey(ctx ginutil.Context, control Controller) *msgentity.MsgEntity {
reversalColumn := urlutil.GetParam(ctx.Request, "sReversalColumn", "").(string) //翻转的字段名
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return msgentity.Err(9001, "控制层未设置ModuleEntit:", err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
whereInfo := []dbinfo.WhereInfo{}
for k, v := range params {
info := dbinfo.WhereInfo{Name: k, Value: v}
whereInfo = append(whereInfo, info)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := getSimplName(control) + ".ReversalByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.ReversalByKey(ctx, entity, whereInfo, reversalColumn)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 增加请求属性
* 注:函数应用于模块控制器时修改或传递到通用方法时,切勿滥用
* @param ctx http请求对象
* @Param attribs 参数键值对数组,[name,value],如果value不存在则为''
*/
func (controlUtil ControllerUtil) AddRequestAttrib(ctx ginutil.Context, attribs ...[]string) {
if (attribs == nil) || (len(attribs) < 1) {
return
}
ctx.Request.ParseForm() //警告:必须先 解析所有请求数据
form := ctx.Request.Form
if nil == form {
form = make(uu.Values)
ctx.Request.Form = form
}
for _, attrib := range attribs {
if (attrib == nil) || (len(attrib) < 1) {
continue
}
name := attrib[0]
value := ""
if (len(attrib) > 1) && (attrib[1] != "") {
value = attrib[1] //fmt.Sprintf("%v", attrib[1])
}
form.Set(name, value)
}
}
/**
* 上传文件
* @param ctx http请求对象
* @param control 控制层接口
* @param modelName 模块名称
* @return msgentity.MsgEntity
*/
func (controlUtil ControllerUtil) UpFile(ctx ginutil.Context, control Controller, modelName string) *msgentity.MsgEntity {
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
return service.UpFile(ctx, modelName)
}
/**
* 获取图片
* @param ctx http请求对象
* @param control 控制层接口
* @param modelName 模块名称
* @param filename 文件名
*/
func (controlUtil ControllerUtil) LookImg(ctx ginutil.Context, control Controller, modelName, filename string) {
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
service.LookImg(ctx, modelName, filename)
}
// func (controlUtil ControllerUtil) GetModuleEntity(controller Controller) (dbinfo.Entity, error) {
// moduleValue := reflect.ValueOf(controller).Elem().FieldByName("ModuleEntity")
// if moduleValue.Kind() != reflect.Ptr && moduleValue.Kind() != reflect.Struct {
// return nil, errors.New("未设置模块数据实体")
// }
// //直接调用Now
// nowMethod := moduleValue.MethodByName("New")
// entityElem := nowMethod.Call(nil)[0].Interface()
// result := entityElem.(dbinfo.Entity)
// // // 获取 Entity 的反射值,并建立新的实例(必须)
// // var entityElem reflect.Value
// // if moduleValue.Kind() == reflect.Ptr {
// // entityElem = reflect.New(moduleValue.Type().Elem())
// // } else {
// // entityElem = reflect.New(moduleValue.Type())
// // }
// // result := entityElem.Interface().(dbinfo.Entity).New()
// return result, nil
// }
// func (controlUtil ControllerUtil) GetModuleService(control Controller) (Service, error) {
// moduleValue := reflect.ValueOf(control).Elem().FieldByName("ModuleService")
// if moduleValue.Kind() != reflect.Ptr && moduleValue.Kind() != reflect.Struct {
// return &ServiceBaseFunc{}, errors.New("未设置模块业务实体")
// }
// // 获取 Service 的反射值
// var serviceElem reflect.Value
// if moduleValue.Kind() == reflect.Ptr {
// serviceElem = reflect.New(moduleValue.Type().Elem())
// } else {
// serviceElem = reflect.New(moduleValue.Type())
// }
// service := serviceElem.Interface().(Service)
// return service, nil
// }
//-----------------------------------------------//
/**
* 取对应模块名称
* object 对象
*/
func getSimplName(object interface{}) string {
name := reflect.TypeOf(object).String()
iSt := strings.Index(name, ".")
if iSt > -1 {
name = name[iSt+1:]
}
return name
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tomatomeatman/golang-repository.git
git@gitee.com:tomatomeatman/golang-repository.git
tomatomeatman
golang-repository
GolangRepository
61e401b0d628

搜索帮助