1 Star 0 Fork 0

tomatomeatman/GolangRepository

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ControllerUtil.go 35.19 KB
一键复制 编辑 原始数据 按行查看 历史
laowei 提交于 2024-07-31 14:17 +08:00 . 1
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
package app
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
uu "net/url"
"reflect"
"strings"
"time"
"gitee.com/tomatomeatman/golang-repository/bricks2/model/dbinfo"
"gitee.com/tomatomeatman/golang-repository/bricks2/model/emity"
"gitee.com/tomatomeatman/golang-repository/bricks2/model/globalconst"
"gitee.com/tomatomeatman/golang-repository/bricks2/model/globalvariable"
"gitee.com/tomatomeatman/golang-repository/bricks2/model/set"
"gitee.com/tomatomeatman/golang-repository/bricks2/utils/function/data/timeutil"
"gitee.com/tomatomeatman/golang-repository/bricks2/utils/function/urlutil"
"gitee.com/tomatomeatman/golang-repository/bricks2/utils/ginutil"
Log "github.com/cihub/seelog"
)
type ControllerUtil struct{}
/**
* 新增
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) Add(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.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{}
}
//return service.Add(ctx, entity, params)
name := ModuleUtil{}.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
}
/**
* 批量新增
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) Adds(ctx ginutil.Context, control Controller) *emity.MsgEmity {
objStr := urlutil.GetParam(ctx.Request, "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'转换后没有数据")
}
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.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 := ModuleUtil{}.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
}
/**
* 删除
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) Del(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
tableInfo := entity.TableInfo()
id := urlutil.GetParamToId(ctx.Request, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
iVersion := urlutil.GetParamToVersion(ctx.Request, tableInfo.GbHasVersion, globalconst.TableVersionName) //取请求参数中的版本号
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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 entity 对象类型
* @param id 记录编号值
* @param iVersion 记录版本号
* @param data 待更新的字段和值
* @return emity.MsgEmity
*/
func (controlUtil ControllerUtil) Edit(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
tableInfo := entity.TableInfo()
id := urlutil.GetParamToId(ctx.Request, tableInfo.GsKeyName) //取请求参数中的记录编号
iVersion := urlutil.GetParamToVersion(ctx.Request, tableInfo.GbHasVersion, globalconst.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[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, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 根据主键查询数据
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindById(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
tableInfo := entity.TableInfo()
id := urlutil.GetParamToId(ctx.Request, tableInfo.GsKeyName) //取请求参数中的记录编号
if (nil == id) || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
}
if nil == id || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "记录编号为空")
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 查询所有数据
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindAll(ctx ginutil.Context, control Controller, 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, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".FindAll"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me := service.FindAll(ctx, entity, where)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 查询时间范围内数据
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindByDate(ctx ginutil.Context, control Controller) *emity.MsgEmity {
sDateSt := urlutil.GetParam(ctx.Request, "sDateSt", "").(string) //记录开始时间
sDateEd := urlutil.GetParam(ctx.Request, "sDateEd", "").(string) //记录结束时间
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 查找指定行数据
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindByRow(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
tableInfo := entity.TableInfo()
id := urlutil.GetParamToId(ctx.Request, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 查询分页数据
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindByPage(ctx ginutil.Context, control Controller, injectionCondition ...set.Set) *emity.MsgEmity {
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 := ModuleUtil{}.GetLikeTimeLimit(control)
sLikeDateSt = timeutil.AddDay(dLikeDateEd, -iLikeTimeLimit).Format("2006-01-02 15:04:05")
}
findByPageParam.GsLikeDateSt = sLikeDateSt
findByPageParam.GsLikeDateEd = sLikeDateEd
}
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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{})
* ctx GinHttp上下文对象
* @param 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.GetParams(ctx.Request, entity)
}
/**
* 读取树形结构数据
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindByTree(ctx ginutil.Context, control Controller) *emity.MsgEmity {
sGroupColumn := urlutil.GetParam(ctx.Request, "sGroupColumn", "").(string)
sGroupName := urlutil.GetParam(ctx.Request, "sGroupName", "").(string)
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) GetValueByFieldName(ctx ginutil.Context, control Controller) *emity.MsgEmity {
temp := urlutil.GetParam(ctx.Request, "fieldNames", "").(string)
fieldNames := strings.Split(temp, ",")
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
tableInfo := entity.TableInfo()
id := urlutil.GetParamToId(ctx.Request, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) GetValueByField(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
tableInfo := entity.TableInfo()
id := urlutil.GetParamToId(ctx.Request, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
fieldName := urlutil.GetParam(ctx.Request, "fieldName", "").(string)
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 根据关键值取对象集合
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindByKey(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".FindByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindByKey(ctx, entity, where)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合中的第一个
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindOneByKey(ctx ginutil.Context, control Controller) *emity.MsgEmity {
temp := urlutil.GetParam(ctx.Request, "fields", "").(string)
fields := strings.Split(temp, ",")
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".FindOneByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindOneByKey(ctx, entity, where, fields...)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值取对象集合中的符合条件的第一条记录的指定字段
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindValueByKey(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
fieldName := urlutil.GetParam(ctx.Request, "fieldName", "").(string)
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".FindValueByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindValueByKey(ctx, entity, where, fieldName)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据关键值查数量
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) FindCountByKey(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".FindCountByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.FindCountByKey(ctx, entity, where)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 根据字段名取分组数据
* ctx GinHttp上下文对象
* @param control 控制层接口
* fields 分组字段[字段名,字段别名]
*/
func (controlUtil ControllerUtil) Group(ctx ginutil.Context, control Controller, fields map[string]string) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 根据字段名取分组数据且取数量
* ctx GinHttp上下文对象
* @param control 控制层接口
* fields 分组字段[字段名,字段别名]
*/
func (controlUtil ControllerUtil) GroupAndCount(ctx ginutil.Context, control Controller, fields map[string]string) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 根据字段名取分组数据
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) GroupByField(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.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 := ModuleUtil{}.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
}
/**
* 取表中指定字段的最大值
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) MaxByField(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
field := urlutil.GetParam(ctx.Request, "field", "").(string)
if field == "" {
return emity.MsgEmity{}.Err(9012, "field参数为空")
}
where := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".MaxByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.MaxByField(ctx, entity, "", field, where)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最小值
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) MinByField(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
field := urlutil.GetParam(ctx.Request, "field", "").(string)
if field == "" {
return emity.MsgEmity{}.Err(9012, "field参数为空")
}
where := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".MinByField"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.MinByField(ctx, entity, "", field, where)
if !me.Gsuccess {
return me
}
meAfter := AopUtil{}.CallAfterFunc(name, ctx, me)
if !meAfter.Gsuccess {
return meAfter
}
return me
}
/**
* 取表中指定字段的最小值
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) HasById(ctx ginutil.Context, control Controller) *emity.MsgEmity {
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
tableInfo := entity.TableInfo()
id := urlutil.GetParamToId(ctx.Request, tableInfo.GsKeyName) //取请求参数中的记录编号
if id == nil || (fmt.Sprintf("%v", id) == "") {
id = urlutil.GetParamToId(ctx.Request, "id")
if id == nil || (fmt.Sprintf("%v", id) == "") {
return emity.MsgEmity{}.Err(1001, "未能获取'", tableInfo.GsKeyName, "'参数")
}
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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:不存在)
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) HasByKey(ctx ginutil.Context, control Controller) *emity.MsgEmity {
keyName := urlutil.GetParam(ctx.Request, "keyName", "") //取请求参数中的字段名
keyValue := urlutil.GetParam(ctx.Request, "keyValue", "") //取请求参数中的字段值
if keyName == "" {
return emity.MsgEmity{}.Err(9001, "keyName参数为空")
}
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
}
/**
* 清理指定用户的缓存
* ctx GinHttp上下文对象
* @param control 控制层接口
*/
func (controlUtil ControllerUtil) ClearCache(ctx ginutil.Context, control Controller) *emity.MsgEmity {
sUser := urlutil.GetParam(ctx.Request, "sUser", "").(string) //取请求参数中的用户名
cacheName := urlutil.GetParam(ctx.Request, "cacheName", "").(string) //取请求参数中的缓存名
if cacheName == "" {
return emity.MsgEmity{}.Err(8001, "指定'缓存库名称'参数为空!")
}
if sUser == "" {
cacheName = cacheName + sUser
}
if globalvariable.Del(cacheName) {
return emity.MsgEmity{}.Success(8999, "清理成功!")
}
return emity.MsgEmity{}.Err(8002, "清理失败!")
}
/**
* 查询组结构数据
* ctx GinHttp上下文对象
* @param control 控制层接口
* @return
*/
func (controlUtil ControllerUtil) FindByGroup(ctx ginutil.Context, control Controller) *emity.MsgEmity {
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 emity.MsgEmity{}.Err(9011, err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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
* ctx GinHttp上下文对象
* @param control 控制层接口
* @param sGroupName 分组字段名称(树节点)
* @param sGroupValue 分组字段值(树节点)
* @param entity 实体对象
* @return
*/
func (controlUtil ControllerUtil) AddToGroup(ctx ginutil.Context, control Controller) *emity.MsgEmity {
sGroupName := urlutil.GetParam(ctx.Request, "sGroupName", "").(string) //分组字段名称(树节点)
sGroupValue := urlutil.GetParam(ctx.Request, "sGroupValue", "").(string) //分组字段值(树节点)
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9001, "控制层未设置ModuleEntit:", err)
}
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.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 emity.MsgEmity
*/
func (controlUtil ControllerUtil) ReversalByKey(ctx ginutil.Context, control Controller) *emity.MsgEmity {
reversalColumn := urlutil.GetParam(ctx.Request, "sReversalColumn", "").(string) //翻转的字段名
entity, err := control.GetModuleEntity(control) //按模块数据实体创建新实例,并实例化
if err != nil {
return emity.MsgEmity{}.Err(9001, "控制层未设置ModuleEntit:", err)
}
me := urlutil.GetParams(ctx.Request, entity)
if !me.Gsuccess {
return me
}
where := me.Gdata.(map[string]interface{})
service, err := control.GetModuleService(control)
if err != nil {
service = &ServiceBaseFunc{}
}
name := ModuleUtil{}.GetSimplName(control) + ".ReversalByKey"
meBefore := AopUtil{}.CallBeforeFunc(name, ctx)
if !meBefore.Gsuccess {
return meBefore
}
me = service.ReversalByKey(ctx, entity, where, 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 (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 control 控制层接口
* @param modelName 模块名称
* @return emity.MsgEmity
*/
func (controlUtil ControllerUtil) UpFile(ctx ginutil.Context, control Controller, modelName string) *emity.MsgEmity {
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("未设置模块数据实体")
}
// 获取 Entity 的反射值
var entityElem reflect.Value
if moduleValue.Kind() == reflect.Ptr {
entityElem = reflect.New(moduleValue.Type().Elem())
} else {
entityElem = reflect.New(moduleValue.Type())
}
entityValue := entityElem.Interface().(dbinfo.Entity)
return entityValue, 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
}
//-----------------------------------------------//
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tomatomeatman/golang-repository.git
git@gitee.com:tomatomeatman/golang-repository.git
tomatomeatman
golang-repository
GolangRepository
011e27951845

搜索帮助