Ai
1 Star 0 Fork 0

tomatomeatman/GolangRepository

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ControllerUtil.go 34.22 KB
一键复制 编辑 原始数据 按行查看 历史
laowei 提交于 2024-07-17 15:33 +08:00 . 1
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
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/emity"
"gitee.com/tomatomeatman/golang-repository/bricks/model/global"
"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/system"
"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{}
/**
* 新增
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) Add(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
params := me.Gdata.(map[string]interface{})
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".Add"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "Add", ctx, entity, params)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 批量新增
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) Adds(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
objStr := urlutil.UrlUtil{}.GetParam(ctx, "objs", "").(string)
if objStr == "" {
return emity.MsgEmity{}.Err(9001, "未能获取'objs'参数")
}
var objs []map[string]interface{}
err := json.Unmarshal([]byte(objStr), &objs)
if err != nil {
Log.Error("参数'objs'转换出错:", err)
return emity.MsgEmity{}.Err(9002, "参数'objs'转换出错")
}
if len(objs) < 1 {
return emity.MsgEmity{}.Err(9003, "参数'objs'转换后没有数据")
}
entitys := []interface{}{}
for i := 0; i < len(objs); i++ {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
entitys = append(entitys, entity)
}
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".Adds"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "Adds", ctx, entitys, objs)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 删除
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) Del(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
id := urlutil.UrlUtil{}.GetParamToId(ctx, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.UrlUtil{}.GetParamToId(ctx, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
iVersion := urlutil.UrlUtil{}.GetParamToVersion(ctx, tableInfo.GbHasVersion, global.TableVersionName) //取请求参数中的版本号
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".Del"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "Del", ctx, entity, id, iVersion)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 修改
* @param entity 对象类型
* @param id 记录编号值
* @param iVersion 记录版本号
* @param data 待更新的字段和值
* @return emity.MsgEmity
*/
func (controlUtil ControllerUtil) Edit(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
id := urlutil.UrlUtil{}.GetParamToId(ctx, tableInfo.GsKeyName) //取请求参数中的记录编号
iVersion := urlutil.UrlUtil{}.GetParamToVersion(ctx, tableInfo.GbHasVersion, global.TableVersionName) //取请求参数中的版本号
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
data := me.Gdata.(map[string]interface{})
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = data[tableInfo.GsKeyName]
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
if iVersion == -1 {
if val, ok := data["iVersion"]; ok {
iVersion = val.(int)
}
}
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".Edit"
me = AopUtil{}.CallBeforeFunc(name, ctx)
if !me.Gsuccess {
return me
}
result := system.ReflectUtils{}.DoMethod(service, "Edit", ctx, entity, id, iVersion, data)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据主键查询数据
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindById(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
id := urlutil.UrlUtil{}.GetParamToId(ctx, tableInfo.GsKeyName) //取请求参数中的记录编号
if (nil == id) || (fmt.Sprintf("%v", id) == "") {
id = urlutil.UrlUtil{}.GetParamToId(ctx, "id")
}
if nil == id || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "记录编号为空")
}
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindById"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindById", ctx, entity, id)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查询所有数据
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindAll(ctx ginutil.Context, control interface{}, attachWhere ...set.Set) *emity.MsgEmity {
where := map[string]interface{}{} //map[string]interface{}{"sCreator": "00000001", "iState": 1}
if nil != attachWhere && len(attachWhere) > 0 {
for i := 0; i < len(attachWhere); i++ {
where[attachWhere[i].Key] = attachWhere[i].Value
}
}
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindAll"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindAll", ctx, entity, where)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查询时间范围内数据
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindByDate(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
sDateSt := urlutil.UrlUtil{}.GetParam(ctx, "sDateSt", "").(string) //记录开始时间
sDateEd := urlutil.UrlUtil{}.GetParam(ctx, "sDateEd", "").(string) //记录结束时间
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindByDate"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindByDate", ctx, entity, sDateSt, sDateEd)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查找指定行数据
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindByRow(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
id := urlutil.UrlUtil{}.GetParamToId(ctx, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.UrlUtil{}.GetParamToId(ctx, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindByRow"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindByRow", ctx, entity, id)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查询分页数据
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindByPage(ctx ginutil.Context, control interface{}, injectionCondition ...set.Set) *emity.MsgEmity {
me := urlutil.UrlUtil{}.GetBody(ctx, 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.TimeUtil{}.ToDate(sLikeDateEd)
}
if sLikeDateSt == "" { //如果开始时间为空,则用结束时间-时间限制
iLikeTimeLimit := ModuleUtil{}.GetLikeTimeLimit(control)
sLikeDateSt = timeutil.TimeUtil{}.AddDay(dLikeDateEd, -iLikeTimeLimit).Format("2006-01-02 15:04:05")
}
findByPageParam.GsLikeDateSt = sLikeDateSt
findByPageParam.GsLikeDateEd = sLikeDateEd
}
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindByPage"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindByPage", ctx, entity, findByPageParam)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取所有参数,并转换成对应实体属性类型(map[string]interface{})
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) GetParams(ctx ginutil.Context, entity interface{}) *emity.MsgEmity {
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 emity.MsgEmity{}.Success(entity, "转换结束")
}
return urlutil.UrlUtil{}.GetParams(ctx, entity)
}
/**
* 读取树形结构数据
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindByTree(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
sGroupColumn := urlutil.UrlUtil{}.GetParam(ctx, "sGroupColumn", "").(string)
sGroupName := urlutil.UrlUtil{}.GetParam(ctx, "sGroupName", "").(string)
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindByTree"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindByTree", ctx, entity, sGroupColumn, sGroupName)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) GetValueByFieldName(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
temp := urlutil.UrlUtil{}.GetParam(ctx, "fieldNames", "").(string)
fieldNames := strings.Split(temp, ",")
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
id := urlutil.UrlUtil{}.GetParamToId(ctx, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.UrlUtil{}.GetParamToId(ctx, "id")
}
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".GetValueByFieldName"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "GetValueByFieldName", ctx, entity, id, fieldNames)
t := result[0].Interface()
me := t.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) GetValueByField(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
id := urlutil.UrlUtil{}.GetParamToId(ctx, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.UrlUtil{}.GetParamToId(ctx, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
fieldName := urlutil.UrlUtil{}.GetParam(ctx, "fieldName", "").(string)
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".GetValueByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "GetValueByField", ctx, entity, id, fieldName)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindByKey(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindByKey", ctx, entity, where)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合中的第一个
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindOneByKey(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
temp := urlutil.UrlUtil{}.GetParam(ctx, "fields", "").(string)
fields := strings.Split(temp, ",")
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindOneByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindOneByKey", ctx, entity, where, fields)
t := result[0].Interface()
me = t.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合中的符合条件的第一条记录的指定字段
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindValueByKey(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
fieldName := urlutil.UrlUtil{}.GetParam(ctx, "fieldName", "").(string)
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindValueByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindValueByKey", ctx, entity, where, fieldName)
t := result[0].Interface()
me = t.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值查数量
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) FindCountByKey(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindCountByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindCountByKey", ctx, entity, where)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取分组数据
* ctx GinHttp上下文对象
* control 控制层
* fields 分组字段[字段名,字段别名]
*/
func (controlUtil ControllerUtil) Group(ctx ginutil.Context, control interface{}, fields map[string]string) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".GroupByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "GroupByField", ctx, entity, "", fields)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取分组数据且取数量
* ctx GinHttp上下文对象
* control 控制层
* fields 分组字段[字段名,字段别名]
*/
func (controlUtil ControllerUtil) GroupAndCount(ctx ginutil.Context, control interface{}, fields map[string]string) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".GroupByFieldAndCount"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "GroupByFieldAndCount", ctx, entity, "", fields)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取分组数据
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) GroupByField(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
fields := me.Gdata.(map[string]interface{})
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".GroupByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "GroupByField", ctx, entity, "", fields)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最大值
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) MaxByField(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
field := urlutil.UrlUtil{}.GetParams(ctx, "field")
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".MaxByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "MaxByField", ctx, entity, "", field, where)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最小值
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) MinByField(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
field := urlutil.UrlUtil{}.GetParams(ctx, "field")
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".MinByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "MinByField", ctx, entity, "", field, where)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最小值
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) HasById(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
id := urlutil.UrlUtil{}.GetParamToId(ctx, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.UrlUtil{}.GetParamToId(ctx, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".HasById"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "HasById", ctx, entity, id)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最小值
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) HasByKey(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
keyName := urlutil.UrlUtil{}.GetParam(ctx, "keyName", "") //取请求参数中的字段名
keyValue := urlutil.UrlUtil{}.GetParam(ctx, "keyValue", "") //取请求参数中的字段值
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".HasByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "HasByKey", ctx, entity, keyName, keyValue)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 清理指定用户的缓存
* ctx GinHttp上下文对象
* control 控制层
*/
func (controlUtil ControllerUtil) ClearCache(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
sUser := urlutil.UrlUtil{}.GetParam(ctx, "sUser", "").(string) //取请求参数中的用户名
cacheName := urlutil.UrlUtil{}.GetParam(ctx, "cacheName", "").(string) //取请求参数中的缓存名
if cacheName == "" {
return emity.MsgEmity{}.Err(8001, "指定'缓存库名称'参数为空!")
}
if sUser == "" {
cacheName = cacheName + sUser
}
if (global.GlobalVariable{}.Del(cacheName)) {
return emity.MsgEmity{}.Success(8999, "清理成功!")
}
return emity.MsgEmity{}.Err(8002, "清理失败!")
}
/**
* 查询组结构数据
* ctx GinHttp上下文对象
* control 控制层
* @return
*/
func (controlUtil ControllerUtil) FindByGroup(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
sGroupColumn := urlutil.UrlUtil{}.GetParam(ctx, "sGroupColumn", "").(string) //分组名(树节点)所在字段名
sGroupName := urlutil.UrlUtil{}.GetParam(ctx, "sGroupName", "").(string) //分组名(树节点)
if sGroupName == "" {
return controlUtil.FindByTree(ctx, control)
}
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".FindByGroup"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "FindByGroup", ctx, entity, sGroupColumn, sGroupName)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 添加数据到指定组下
* 警告:对象必须符合树形结构要求,如:sId、sPid
* ctx GinHttp上下文对象
* control 控制层
* @param sGroupName 分组字段名称(树节点)
* @param sGroupValue 分组字段值(树节点)
* @param entity 实体对象
* @return
*/
func (controlUtil ControllerUtil) AddToGroup(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
sGroupName := urlutil.UrlUtil{}.GetParam(ctx, "sGroupName", "").(string) //分组字段名称(树节点)
sGroupValue := urlutil.UrlUtil{}.GetParam(ctx, "sGroupValue", "").(string) //分组字段值(树节点)
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".AddToGroup"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "AddToGroup", ctx, entity, sGroupName, sGroupValue)
temp := result[0].Interface()
me := temp.(*emity.MsgEmity)
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 entity 实体类
* @Param where 存放查询条件
* @Param reversalColumn 翻转的字段名
* @return emity.MsgEmity
*/
func (controlUtil ControllerUtil) ReversalByKey(ctx ginutil.Context, control interface{}) *emity.MsgEmity {
reversalColumn := urlutil.UrlUtil{}.GetParam(ctx, "sReversalColumn", "").(string) //翻转的字段名
entity := ModuleUtil{}.NowModuleEntityAndInit(control) //按模块数据实体创建新实例,并实例化
me := urlutil.UrlUtil{}.GetParams(ctx, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service := ModuleUtil{}.GetModuleService(control)
name := ModuleUtil{}.GetSimplName(control) + ".ReversalByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
result := system.ReflectUtils{}.DoMethod(service, "ReversalByKey", ctx, entity, where, reversalColumn)
temp := result[0].Interface()
me = temp.(*emity.MsgEmity)
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 (nil == attribs) || (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 (nil == attrib) || (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 modelName 模块名称
* @return emity.MsgEmity
*/
func (controlUtil ControllerUtil) UpFile(ctx ginutil.Context, modelName string) *emity.MsgEmity {
return CommonService{}.UpFile(ctx, modelName)
}
/**
* 获取图片
* @param ctx http请求对象
* @param modelName 模块名称
* @param filename 文件名
*/
func (controlUtil ControllerUtil) LookImg(ctx ginutil.Context, modelName, filename string) {
CommonService{}.LookImg(ctx, modelName, filename)
}
//-----------------------------------------------//
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tomatomeatman/golang-repository.git
git@gitee.com:tomatomeatman/golang-repository.git
tomatomeatman
golang-repository
GolangRepository
cddfa7f15f20

搜索帮助