1 Star 0 Fork 0

tomatomeatman/GolangRepository

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CommonService.go 73.98 KB
一键复制 编辑 原始数据 按行查看 历史
laowei 提交于 2024-07-17 17:27 . 1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
package app
import (
"fmt"
"net/http"
"os"
"path/filepath"
"reflect"
"strconv"
"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/utils/function/data/integerutil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/function/data/jsonutil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/function/data/maputil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/function/data/stringutil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/function/data/uuidutil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/function/system"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/ginutil"
"gitee.com/tomatomeatman/golang-repository/bricks/utils/gorm"
Log "github.com/cihub/seelog"
uuid "github.com/satori/go.uuid"
)
var (
recordKeyJam = "" //创建sRecordKey用的干扰串
upFilePath = "" //上传文件路径
)
type CommonService struct{}
/**
* 新增
* entity 检查用数据结构
* data 数据
*/
func (service CommonService) Add(ctx ginutil.Context, entity interface{}, data map[string]interface{}) *emity.MsgEmity {
// if (system.ReflectUtils{}.HasField(entity, "GsPid")) {
// return service.AddNode(ctx, entity, data)
// }
return service.AddCommon(ctx, entity, data)
}
/**
* 新增普通数据
* entity 检查用数据结构
* data 数据
*/
func (service CommonService) AddCommon(ctx ginutil.Context, entity interface{}, aData map[string]interface{}) *emity.MsgEmity {
stringutil.StringUtil{}.TrimAttribute(aData) //清除对象各个属性的值的前后空格
//maputil.MapUtil{}.RemoveData(data, []string{"uId", "sId", "iId", "sCreator", "sModifieder", "dCreateDate", "dModifiedDate"}) //清理不能由前端定义的字段
maputil.MapUtil{}.RemoveData(aData, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TableCreatorName, global.TableCreateDateName, global.TableModifiederName, global.TableModifiedDateName, global.TableVersionName}) //清理不能由前端定义的字段
//me := dbinfo.DataInfo{}.ValidAttr(data, entity, []string{"uId", "sId", "iId", "sCreator", "sModifieder", "dCreateDate", "dModifiedDate"}) //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
me := dbinfo.DataInfo{}.ValidAttr(aData, entity, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TableCreatorName, global.TableCreateDateName, global.TableModifiederName, global.TableModifiedDateName}) //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
if !me.Gsuccess {
return me.IncCode(7000)
}
me = dbinfo.DataInfo{}.MapToEntity(aData, entity)
if !me.Gsuccess {
return me.IncCode(7010)
}
commons := me.Gdata
commons = dbinfo.DataInfo{}.SetDataInfoDefault(commons) //对对象中添加了dataInfo注解的属性添加默认值
fmt.Println(commons)
me = service.SupplyDbEntityAttrByAdd(ctx, commons, "", "")
if !me.Gsuccess {
return me.IncCode(7030)
}
me = dbinfo.DataInfo{}.ValidAttrByAdd(commons, []string{}) //对对象中添加了dbinfo.DataInfo注解的属性检查限制
if !me.Gsuccess {
return me.IncCode(7020)
}
me = service.ValidEntityRepeatByAdd(ctx, commons) //验证新增数据是否存在重复
if !me.Gsuccess {
return me.IncCode(7030)
}
return CommonDao{}.AddCommon(commons)
}
/**
* 新增树节点
* entity 检查用数据结构
* data 数据
*/
func (service CommonService) AddNode(ctx ginutil.Context, entity interface{}, aData map[string]interface{}) *emity.MsgEmity {
stringutil.StringUtil{}.TrimAttribute(aData) //清除对象各个属性的值的前后空格
//maputil.MapUtil{}.RemoveData(data, []string{"uId", "sId", "iId", "sCreator", "sModifieder", "dCreateDate", "dModifiedDate"}) //清理不能由前端定义的字段
maputil.MapUtil{}.RemoveData(aData, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TableCreatorName, global.TableCreateDateName, global.TableModifiederName, global.TableModifiedDateName, global.TableVersionName}) //清理不能由前端定义的字段
//me := dbinfo.DataInfo{}.ValidAttr(data, entity, []string{"uId", "sId", "iId", "sCreator", "sModifieder", "dCreateDate", "dModifiedDate"}) //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
me := dbinfo.DataInfo{}.ValidAttr(aData, entity, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TableCreatorName, global.TableCreateDateName, global.TableModifiederName, global.TableModifiedDateName}) //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
if !me.Gsuccess {
return me.IncCode(7000)
}
me = dbinfo.DataInfo{}.MapToEntity(aData, entity)
if !me.Gsuccess {
return me.IncCode(7010)
}
commons := me.Gdata
dbinfo.DataInfo{}.SetDataInfoDefault(commons) //对对象中添加了dataInfo注解的属性添加默认值
me = service.SupplyDbEntityAttrByAdd(ctx, commons, "", "")
if !me.Gsuccess {
return me.IncCode(7030)
}
me = dbinfo.DataInfo{}.ValidAttrByAdd(commons, []string{}) //对对象中添加了dbinfo.DataInfo注解的属性检查限制
if !me.Gsuccess {
return me.IncCode(7020)
}
me = service.ValidEntityRepeatByAdd(ctx, commons) //验证新增数据是否存在重复
if !me.Gsuccess {
return me.IncCode(7030)
}
return CommonDao{}.AddNode(commons)
}
// 批量新增
func (service CommonService) Adds(ctx ginutil.Context, entitys []interface{}, list []map[string]interface{}) *emity.MsgEmity {
if len(list) < 1 {
return emity.MsgEmity{}.Err(8001, "没有需要保存的数据")
}
entitysNew := []interface{}{}
for k, vData := range list {
stringutil.StringUtil{}.TrimAttribute(vData) //清除对象各个属性的值的前后空格
entity := entitys[k]
maputil.MapUtil{}.RemoveData(vData, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TableCreatorName, global.TableCreateDateName, global.TableModifiederName, global.TableModifiedDateName, global.TableVersionName}) //清理不能由前端定义的字段
me := dbinfo.DataInfo{}.ValidAttr(vData, entity, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TableCreatorName, global.TableCreateDateName, global.TableModifiederName, global.TableModifiedDateName}) //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
if !me.Gsuccess {
return me.IncCode(7000)
}
me = dbinfo.DataInfo{}.MapToEntity(vData, entity)
if !me.Gsuccess {
return me.IncCode(7010)
}
commons := me.Gdata
dbinfo.DataInfo{}.SetDataInfoDefault(commons) //对对象中添加了dataInfo注解的属性添加默认值
me = service.SupplyDbEntityAttrByAdd(ctx, commons, "", "")
if !me.Gsuccess {
return me.IncCode(7030)
}
me = dbinfo.DataInfo{}.ValidAttrByAdd(commons, []string{}) //对对象中添加了dbinfo.DataInfo注解的属性检查限制
if !me.Gsuccess {
return me.IncCode(7020)
}
me = service.ValidEntityRepeatByAdd(ctx, commons) //验证新增数据是否存在重复
if !me.Gsuccess {
return me.IncCode(7030)
}
entitysNew = append(entitysNew, commons)
}
return CommonDao{}.Adds(entitysNew)
}
// 批量新增
func (service CommonService) AddList(ctx ginutil.Context, entitys []interface{}) *emity.MsgEmity {
if len(entitys) < 1 {
return emity.MsgEmity{}.Err(8001, "没有需要保存的数据")
}
for _, entity := range entitys {
dbinfo.DataInfo{}.SetDataInfoDefault(entity) //对对象中添加了dataInfo注解的属性添加默认值
me := service.SupplyDbEntityAttrByAdd(ctx, entity, "", "")
if !me.Gsuccess {
return me.IncCode(7030)
}
}
return CommonDao{}.Adds(entitys)
}
/**
* 修改状态
* @param entity 实体类
* @param id 编号
* @param iState 状态值
* @param iVersion 记录版本号
* @param sMemo 备注
* @param unidirectional 是否单向 设置为单向则新值必须大于旧值才能执行
* @return emity.MsgEmity 返回执行情况
*/
func (service CommonService) ChangeState(ctx ginutil.Context, entity interface{}, id interface{}, iState int, iVersion int, sMemo string, unidirectional bool) *emity.MsgEmity {
if fmt.Sprintf("%v", id) == "" {
return emity.MsgEmity{}.Err(8001, "记录编号参数为空!")
}
return CommonDao{}.ChangeState(entity, id, iState, iVersion, sMemo, unidirectional)
}
/**
* 修改步骤值(如果设置为单向则新值必须大于旧值)
* @param id 编号
* @param iSetp 步骤值
* @param iVersion 记录版本号
* @param sMemo 备注
* @param unidirectional 是否单向 设置为单向则新值必须大于旧值才能执行
* @param entity 实体类
* @return emity.MsgEmity 返回执行情况
*/
func (service CommonService) ChangeSetp(ctx ginutil.Context, entity interface{}, id interface{}, iSetp int, iVersion int, sMemo string, unidirectional bool) *emity.MsgEmity {
if fmt.Sprintf("%v", id) == "" {
return emity.MsgEmity{}.Err(8001, "记录编号参数为空!")
}
return CommonDao{}.ChangeSetp(entity, id, iSetp, iVersion, sMemo, unidirectional)
}
/**
* 删除
* @param entity 对象类型
* @param id 记录编号值
* @param iVersion 记录版本号
* @return emity.MsgEmity
*/
func (service CommonService) Del(ctx ginutil.Context, entity interface{}, id interface{}, iVersion int) *emity.MsgEmity {
if id == nil {
return emity.MsgEmity{}.Err(8001, "记录编号为空")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
if tableInfo.GbHasVersion && (iVersion < 1) && (iVersion != integerutil.IntegerUtil{}.MaxInt()) {
return emity.MsgEmity{}.Err(8002, "记录版本号不正确")
}
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 7) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
physics := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 1) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
if !physics && tableInfo.GbHasDelSign { //有逻辑删除字段标识才能进行逻辑删除
return CommonDao{}.DelSign(entity, id, iVersion, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator) //标识删除
}
return CommonDao{}.Del(entity, id, iVersion, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 删除
* @param entity 对象类型
* @param where 执行条件
* @return emity.MsgEmity
*/
func (service CommonService) DelByMap(ctx ginutil.Context, entity interface{}, where map[string]interface{}) *emity.MsgEmity {
if entity == nil {
return emity.MsgEmity{}.Err(8001, "结构体参数为nil")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 7) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
physics := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 1) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
if !physics && tableInfo.GbHasDelSign { //有逻辑删除字段标识才能进行逻辑删除
return CommonDao{}.DelSignByMap(entity, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator) //标识删除
}
return CommonDao{}.DelByMap(entity, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 按实体保留map中的数据
* object 待检查对象
* data 数据
* fieldPrefix 字段前缀(可不传)
*/
func (service CommonService) HoldByEntity(entity interface{}, data map[string]interface{}, fieldPrefix ...string) map[string]interface{} {
var rve reflect.Type
typeOf := reflect.TypeOf(entity) //通过反射获取type定义
if typeOf.Kind() == reflect.Ptr { //是否指针类型
rve = reflect.ValueOf(entity).Elem().Type() // 取得struct变量的指针
} else if typeOf.String() == "reflect.Value" {
rve = entity.(reflect.Value).Type()
} else {
rve = reflect.ValueOf(entity).Type()
}
result := map[string]interface{}{}
for _, prefix := range fieldPrefix {
for key, value := range data {
str := key
if "" != prefix {
str = prefix + key
}
field, b := rve.FieldByName(str)
if !b {
continue
}
str = field.Tag.Get("dataInfo")
if "" != str {
result[key] = value
}
}
}
return data
//typeOf := reflect.TypeOf(entity) //通过反射获取type定义
//if typeOf.Kind() == reflect.Ptr { //是否指针类型
// typeOf = typeOf.Elem()
//}
//prefix := ""
//for _, key := range fieldPrefix {
// prefix = prefix + key
//}
//for key := range data {
// str := key
// if "" != prefix {
// str = prefix + key
// }
// _, b := typeOf.FieldByName(str)
// if !b {
// delete(data, key)
// }
//}
//return data
}
func (service CommonService) getBaseEntity(entity interface{}) interface{} {
var rte reflect.Type
var rve reflect.Value
typeOf := reflect.TypeOf(entity) //通过反射获取type定义
if typeOf.Kind() == reflect.Ptr { //是否指针类型
rve = reflect.ValueOf(entity).Elem()
rte = rve.Type() // 取得struct变量的指针
} else if typeOf.String() == "reflect.Value" {
rve = entity.(reflect.Value)
rte = rve.Type()
} else {
rve = reflect.ValueOf(entity)
rte = reflect.ValueOf(entity).Type()
}
for k := 0; k < rte.NumField(); k++ {
field := rte.Field(k)
if field.Anonymous && strings.HasSuffix(field.Name, "Base") {
return rve.Field(k) //找到
}
}
return entity //没找到
}
/**
* 修改
* @param entity 对象类型
* @param id 记录编号值
* @param iVersion 记录版本号
* @param data 待更新的字段和值
* @return emity.MsgEmity
*/
func (service CommonService) Edit(ctx ginutil.Context, entity interface{}, id interface{}, iVersion int, aData map[string]interface{}) *emity.MsgEmity {
if fmt.Sprintf("%v", id) == "" {
return emity.MsgEmity{}.Err(7001, "记录编号参数为空!")
}
baseEntity := system.ReflectUtils{}.GetBaseEntity(entity) //取基础数据库实体
aData = system.ReflectUtils{}.HoldByEntity(baseEntity, aData, "", "G") //按实体保留map中的数据
// maputil.MapUtil{}.RemoveData(data, []string{"uId", "sId", "iId", "sPath", "sCreator", "sModifieder", "dCreateDate", "dModifiedDate", "iVersion",
// "u_id", "s_id", "i_id", "s_path", "s_creator", "s_modifieder", "d_create_date", "d_modified_date", "i_version"}) //清理不能由前端定义的字段
maputil.MapUtil{}.RemoveData(aData, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TablePathKey, global.TableCreatorName, global.TableCreateDateName, global.TableModifiederName, global.TableModifiedDateName}) //清理不能由前端定义的字段
if len(aData) < 1 {
return emity.MsgEmity{}.Err(7002, "更新操作提供的参数为空!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(baseEntity)
if tableInfo.GbHasVersion && (1 > iVersion) {
return emity.MsgEmity{}.Err(7003, "记录版本号参数必须大于0!")
}
stringutil.StringUtil{}.TrimAttribute(aData) //清除对象各个属性的值的前后空格
// me := dbinfo.DataInfo{}.ValidAttr(data, baseEntity, []string{"uId", "sId", "iId", "sCreator", "sModifieder", "dCreateDate", "dModifiedDate", //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
// "u_id", "s_id", "i_id", "s_path", "s_creator", "s_modifieder", "d_create_date", "d_modified_date", "i_version"}) //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
me := dbinfo.DataInfo{}.ValidAttr(aData, baseEntity, []string{global.TableMajorKeyString, global.TableMajorKeyAutoInt, global.TableMajorKeyUuId, global.TablePathKey, global.TableCreateDateName, global.TableModifiedDateName}) //对对象中添加了dbinfo.DataInfo注解的不为nil的属性检查限制
if !me.Gsuccess {
return me.IncCode(7020)
}
me = service.ValidEntityRepeatByEdit(ctx, baseEntity, id, aData) //验证更新数据是否存在重复
if !me.Gsuccess {
return me.IncCode(7030)
}
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 6) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.Edit(baseEntity, id, iVersion, aData, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 根据主键查询数据
* id 主键
* entity 检查用数据结构
*/
func (service CommonService) FindById(ctx ginutil.Context, entity interface{}, id interface{}) *emity.MsgEmity {
if id == nil {
return emity.MsgEmity{}.Err(1001, "记录编号为空")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindById(entity, id, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
// 查询所有数据
func (service CommonService) FindAll(ctx ginutil.Context, entity interface{}, where map[string]interface{}) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindAll(entity, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
// 查询指定时间内数据
func (service CommonService) FindByDate(ctx ginutil.Context, entity interface{}, sDateSt string, sDateEd string) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindByDate(entity, sDateSt, sDateEd, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
// 查询指定行数据
func (service CommonService) FindByRow(ctx ginutil.Context, entity interface{}, id interface{}) *emity.MsgEmity {
if id == nil {
return emity.MsgEmity{}.Err(1001, "记录编号为空")
}
sId := fmt.Sprintf("%v", id)
if sId == "" {
return emity.MsgEmity{}.Err(1002, "记录编号为空")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindByRow(entity, id, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
// 查询分页数据
func (service CommonService) FindByPage(ctx ginutil.Context, entity interface{}, findByPageParam dbinfo.FindByPageParam) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindByPage(entity, findByPageParam, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 补充数据库实体类的数据--新增
* @param dbEntity
* @param selfId 自提供的编号,防止从序列中获取
* @param idValuePrefix Id前缀
* @return
*/
func (service CommonService) SupplyDbEntityAttrByAdd(ctx ginutil.Context, dbEntity interface{}, selfId string, idValuePrefix string) *emity.MsgEmity {
if nil == dbEntity {
return emity.MsgEmity{}.Err(1001, "待存储的数据实体不能为nil")
}
dbinfo.DataInfo{}.SetDataInfoDefault(dbEntity) //对对象中添加了dataInfo注解的属性添加默认值
sLoginUserId := ModuleUtil{}.CurrentLoginUserId(ctx) //取当前登录用户编号
date := time.Now()
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableCreatorName, sLoginUserId) // 设置当前操作用户
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableModifiederName, sLoginUserId) // 设置当前操作用户
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableCreateDateName, date) // 设置新增时间
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableModifiedDateName, date) // 设置修改时间
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableIndexName, 1) // 设置排序值
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableStateName, 1) // 设置状态值
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableVersionName, 1) // 设置版本号
tableInfo := dbinfo.TableInfo{}.GetByEntity(dbEntity)
vNew := selfId
if (global.TableMajorKeyUuId == tableInfo.GsKeyName) && "" == selfId { //按UUID设置值
if tableInfo.GiKeyLen == 32 {
vNew = strings.Replace(uuid.NewV4().String(), "-", "", -1) //取uuid
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableMajorKeyUuId, strings.ToUpper(vNew))
} else if tableInfo.GiKeyLen == 22 {
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableMajorKeyUuId, uuidutil.U62Util{}.Get(idValuePrefix))
} else {
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableMajorKeyUuId, uuidutil.U62Util{}.Get(idValuePrefix))
}
} else if (global.TableMajorKeyString == tableInfo.GsKeyName) && ("" == selfId) { //如果不是自增长,并且没有自定义id值,才能进行从序列中取新id的方法
if tableInfo.GbHasPath { //对象中存在sPath,则id只需要4位
pidValue := system.ReflectUtils{}.GetFieldValue(dbEntity, global.GtablePidKey)
pid := fmt.Sprintf("%v", pidValue)
if nil != pidValue {
pid = global.TableTreeRootValue
}
me := CommonDao{}.NewChildId(tableInfo, pid)
if !me.Gsuccess {
return me
}
vNew = me.Gdata.(string)
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableMajorKeyString, vNew)
} else if "" == idValuePrefix {
vNew = ModuleUtil{}.GetNewId(tableInfo.GiKeyLen, tableInfo.GsTableName)
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableMajorKeyString, vNew)
} else {
iLength := tableInfo.GiKeyLen - len(idValuePrefix)
vNew = idValuePrefix + ModuleUtil{}.GetNewId(iLength, tableInfo.GsTableName)
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableMajorKeyString, vNew)
}
}
//--如果有sPid字段则进行部分处理--//
if tableInfo.GbHasPid {
sPid := system.ReflectUtils{}.GetFieldValue(dbEntity, global.GtablePidKey)
if (nil == sPid) || ("" == sPid.(string)) { //如果sPid为空,则默认赋值'00'
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtablePidKey, global.GtableTreeRootValue)
}
}
//--如果有sPath字段则进行部分处理--//
if tableInfo.GbHasPath {
sPid := system.ReflectUtils{}.GetFieldValue(dbEntity, global.GtablePidKey)
sPath := system.ReflectUtils{}.GetFieldValue(dbEntity, global.GtablePathKey)
if ((nil == sPath) || ("" == sPath.(string)) || ("/00/" == sPath.(string))) && (global.GtableTreeRootValue == sPid.(string)) { //如果sPid为空,则默认赋值'00'
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtablePathKey, "/00/"+vNew+"/")
} else {
sPath := CommonDao{}.GetPath(sPid.(string), tableInfo.GsDbName, tableInfo.GsTableName)
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtablePathKey, sPath+vNew+"/")
}
}
//--如果有sRecordKey字段则进行部分处理--//
if tableInfo.GbHasRecordKey {
if "" == recordKeyJam {
recordKeyJam = AppUtil{}.ReadConfigKey("App", "RecordKeyJam", "12345678").(string)
}
key := system.ReflectUtils{}.DoMethod(dbEntity, "CreateRecordKey", reflect.ValueOf(recordKeyJam))
if len(key) > 0 {
system.ReflectUtils{}.SetFieldValue(dbEntity, global.GtableRecordKeyName, key[0])
}
}
return emity.MsgEmity{}.Success(1999, "补充数据完毕!")
}
/**
* 验证新增数据是否存在重复
*
*/
func (service CommonService) ValidEntityRepeatByAdd(ctx ginutil.Context, dbEntity interface{}) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(dbEntity)
customService := global.GlobalVariable{}.Get(tableInfo.GsTableName + "_ModuleService")
//-- 树形结构 --//
if tableInfo.GbHasPid {
if nil == customService { //如果没有自定义业务层
return service.CommonCheckRepeatByAddAndTree(ctx, dbEntity) //通用树型结构表添加数据时重复检查方法
}
method := system.ReflectUtils{}.GetMethod(customService, "CheckRepeatByAddAndTree")
if !method.IsValid() { //如果自定义业务层定义了自检方法
return service.CommonCheckRepeatByAddAndTree(ctx, dbEntity) //通用树型结构表添加数据时重复检查方法
}
result := system.ReflectUtils{}.DoMethod(customService, "CheckRepeatByAddAndTree", dbEntity)
me := result[0].Interface()
return me.(*emity.MsgEmity)
}
//--不是树形数据则使用普通方法检查--//
if nil == customService { //如果没有自定义业务层
return service.CommonCheckRepeatByAdd(ctx, dbEntity) //通用添加数据时重复检查方法
}
method := system.ReflectUtils{}.GetMethod(customService, "CheckRepeatByAdd")
if !method.IsValid() { //如果自定义业务层定义了自检方法
return service.CommonCheckRepeatByAdd(ctx, dbEntity) //通用添加数据时重复检查方法
}
result := system.ReflectUtils{}.DoMethod(customService, "CheckRepeatByAdd", dbEntity)
me := result[0].Interface()
return me.(*emity.MsgEmity)
}
/**
* 验证更新数据是否存在重复
*
*/
func (service CommonService) ValidEntityRepeatByEdit(ctx ginutil.Context, entity interface{}, id interface{}, data map[string]interface{}) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
customService := global.GlobalVariable{}.Get(tableInfo.GsTableName + "_ModuleService")
//-- 树形结构 --//
if tableInfo.GbHasPid {
if nil == customService { //如果没有自定义业务层
return service.CommonCheckRepeatByEditAndTree(ctx, entity, id, data["sName"]) //通用树型结构表添加数据时重复检查方法
}
method := system.ReflectUtils{}.GetMethod(customService, "CheckRepeatByEditAndTree")
if !method.IsValid() { //如果自定义业务层定义了自检方法
return service.CommonCheckRepeatByEditAndTree(ctx, entity, id, data["sName"]) //通用树型结构表添加数据时重复检查方法
}
result := system.ReflectUtils{}.DoMethod(customService, "CheckRepeatByEditAndTree", id, data["sName"])
me := result[0].Interface()
return me.(*emity.MsgEmity)
}
//--不是树形数据则使用普通方法检查--//
if nil == customService { //如果没有自定义业务层
return service.CommonCheckRepeatByEdit(ctx, entity, id) //通用添加数据时重复检查方法
}
method := system.ReflectUtils{}.GetMethod(customService, "CheckRepeatByEdit")
if !method.IsValid() { //如果自定义业务层定义了自检方法
return service.CommonCheckRepeatByEdit(ctx, entity, id) //通用添加数据时重复检查方法
}
result := system.ReflectUtils{}.DoMethod(customService, "CheckRepeatByEdit", data, id)
me := result[0].Interface()
return me.(*emity.MsgEmity)
}
/**
* 通用树型结构表添加数据时重复检查方法
* dbEntity
*/
func (service CommonService) CommonCheckRepeatByAddAndTree(ctx ginutil.Context, dbEntity interface{}) *emity.MsgEmity {
vName := system.ReflectUtils{}.GetFieldValue(dbEntity, global.GtableTreeNodeName)
if nil == vName {
return emity.MsgEmity{}.Err(1001, "节点名称为空")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(dbEntity)
sName := vName.(string)
var sPid string
vPid := system.ReflectUtils{}.GetFieldValue(dbEntity, global.GtablePidKey)
if nil != vPid {
sPid = vPid.(string)
} else {
sPid = global.TableTreeRootValue
}
if sPid == "" {
sPid = global.TableTreeRootValue
}
where := make(map[string]interface{})
//同一层节点下,展现名不能相同//
var build strings.Builder
build.WriteString("SELECT SUM(iCount) AS iCount FROM (")
if tableInfo.GbHasSign {
build.WriteString(" select SIGN(COUNT(1) * 10) as iCount from ${sDbName}${sTableName}")
build.WriteString(" where ")
build.WriteString(global.TablesSign)
build.WriteString(" = @sSign")
build.WriteString(global.TablesSign)
build.WriteString(" UNION ALL ")
where[global.TablesSign] = system.ReflectUtils{}.GetFieldValue(dbEntity, global.GtablesSign)
}
build.WriteString(" select SIGN(COUNT(1)) as iCount from ${sDbName}${sTableName}")
build.WriteString(" where ${global.TablePidKey} = @sPid and ${global.TableTreeNodeName} = @sName")
build.WriteString(") TMP")
txt := build.String()
txt = strings.Replace(txt, "${sDbName}", gorm.GetDbName(tableInfo.GsDbName), -1)
txt = strings.Replace(txt, "${sTableName}", tableInfo.GsTableName, -1)
txt = strings.Replace(txt, "${global.TablePidKey}", global.TablePidKey, -1)
txt = strings.Replace(txt, "${global.TableTreeNodeName}", global.TableTreeNodeName, -1)
where[global.TablePidKey] = sPid
where[global.TableTreeNodeName] = sName
var iCount int
dbResult := gorm.Raw(txt, where).Find(&iCount)
if dbResult.Error != nil {
Log.Error("查询发生异常:", dbResult.Error)
return emity.MsgEmity{}.Err(1002, "查询数据发生异常:", dbResult.Error)
}
if iCount != 0 {
return emity.MsgEmity{}.Err(1003, "节点重复")
}
return emity.MsgEmity{}.Success(1999, "节点未重复")
}
/**
* 通用树型结构表添加数据时重复检查方法
* entity
*/
func (service CommonService) CommonCheckRepeatByEditAndTree(ctx ginutil.Context, entity interface{}, id interface{}, sName interface{}) *emity.MsgEmity {
if (nil == sName) || ("" == sName) || ("<nil>" == sName) {
return emity.MsgEmity{}.Err(1001, "节点名称为空")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
where := make(map[string]interface{})
//同一层节点下,展现名不能相同//
var build strings.Builder
build.WriteString("SELECT SUM(iCount) AS iCount FROM (")
if tableInfo.GbHasSign {
build.WriteString(" select SIGN(COUNT(1) * 10) as iCount from ${sDbName}${sTableName}")
build.WriteString(" where ")
build.WriteString(global.TablesSign)
build.WriteString(" = @")
build.WriteString(global.TablesSign)
build.WriteString(" and ${sId} <> @sId")
build.WriteString(" UNION ALL ")
where[global.TablesSign] = system.ReflectUtils{}.GetFieldValue(entity, global.GtablesSign)
}
build.WriteString(" select SIGN(COUNT(1)) as iCount from ${sDbName}${sTableName}")
build.WriteString(" where ${sId} <> @sId")
build.WriteString(" and ${global.TablePidKey} = (select a.${global.TablePidKey} from ${sDbName}${sTableName} a where a.${sId} = @sId)")
build.WriteString(" and ${global.TableTreeNodeName} = #{sName}")
build.WriteString(") TMP")
txt := build.String()
txt = strings.Replace(txt, "${sDbName}", gorm.GetDbName(tableInfo.GsDbName), -1)
txt = strings.Replace(txt, "${sTableName}", tableInfo.GsTableName, -1)
txt = strings.Replace(txt, "${sId}", tableInfo.GsKeyName, -1)
txt = strings.Replace(txt, "${global.TablePidKey}", global.TablePidKey, -1)
txt = strings.Replace(txt, "${global.TableTreeNodeName}", global.TableTreeNodeName, -1)
txt = strings.Replace(txt, "#{sName}", "@sName", -1)
where["sId"] = id
where["sName"] = sName
var iCount int
dbResult := gorm.Raw(txt, where).Find(&iCount)
if dbResult.Error != nil {
Log.Error("查询发生异常:", dbResult.Error)
return emity.MsgEmity{}.Err(1002, "查询数据发生异常:", dbResult.Error)
}
if iCount != 0 {
return emity.MsgEmity{}.Err(1003, "节点重复")
}
return emity.MsgEmity{}.Success(1999, "节点未重复")
}
/**
* 通用添加数据时重复检查方法
* dbEntity
*/
func (service CommonService) CommonCheckRepeatByAdd(ctx ginutil.Context, dbEntity interface{}) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(dbEntity)
vCheckRepeatCombination := global.GlobalVariable{}.Get(tableInfo.GsTableName + "_CheckRepeatCombination")
vCheckRepeatAlone := global.GlobalVariable{}.Get(tableInfo.GsTableName + "_CheckRepeatAlone")
k := 0
//检查待新增内容是否存在重复数据(多字段组合重复即重复)集合
if nil != vCheckRepeatCombination {
checkRepeatCombination := vCheckRepeatCombination.([]string)
var build strings.Builder
build.WriteString("SELECT COUNT(1) AS iCount FROM ${sDbName}${sTableName} WHERE 1=1 ")
var temp strings.Builder
temp.WriteString("[")
where := make(map[string]interface{})
for _, value := range checkRepeatCombination {
build.WriteString(" AND ")
build.WriteString(value)
build.WriteString(" = @")
build.WriteString(value)
where[value] = system.ReflectUtils{}.GetFieldValue(dbEntity, "G"+value)
temp.WriteString("、")
temp.WriteString(value)
}
txt := strings.Replace(build.String(), "1=1 AND ", "", -1)
txt = strings.Replace(txt, "${sDbName}", gorm.GetDbName(tableInfo.GsDbName), -1)
txt = strings.Replace(txt, "${sTableName}", tableInfo.GsTableName, -1)
iCount := 0
dbResult := gorm.Raw(txt, where).Find(&iCount)
if dbResult.Error != nil {
Log.Error("验证数据是否重复发生异常:", dbResult.Error)
return emity.MsgEmity{}.Err(1001, "验证数据是否重复发生异常:", dbResult.Error)
}
if iCount != 0 {
temp.WriteString("]组合发现数据重复")
return emity.MsgEmity{}.Err(1002, strings.Replace(temp.String(), "、", "", 1))
}
k++
}
//检查待新增内容是否存在重复数据(单独字段重复即重复)集合
if nil != vCheckRepeatAlone {
checkRepeatAlone := vCheckRepeatAlone.(map[string]int)
var build strings.Builder
build.WriteString("SELECT SUM(iCount) FROM (")
where := make(map[string]interface{})
for key, value := range checkRepeatAlone {
build.WriteString(" union all select (SIGN(COUNT(1)) * ")
build.WriteString(strconv.Itoa(value))
build.WriteString(") as iCount ")
build.WriteString(" from ${sDbName}${sTableName} ")
build.WriteString(" where ")
build.WriteString(key)
build.WriteString("= @")
build.WriteString(key)
where[key] = system.ReflectUtils{}.GetFieldValue(dbEntity, "G"+key)
}
build.WriteString(") TMP")
txt := strings.Replace(build.String(), " union all ", " ", 1)
txt = strings.Replace(txt, "${sDbName}", gorm.GetDbName(tableInfo.GsDbName), -1)
txt = strings.Replace(txt, "${sTableName}", tableInfo.GsTableName, -1)
iCount := 0
dbResult := gorm.Raw(txt, where).Find(&iCount)
if dbResult.Error != nil {
Log.Error("验证数据是否重复发生异常:", dbResult.Error)
return emity.MsgEmity{}.Err(1003, "验证数据是否重复发生异常:", dbResult.Error)
}
if iCount != 0 {
var temp strings.Builder
str := fmt.Sprintf("%0*d", len(checkRepeatAlone), iCount)
array := []rune(str) //得到字符数组
ml := len(checkRepeatAlone)
for key, value := range checkRepeatAlone {
i := len(strconv.Itoa(value))
if array[ml-i] == 48 { // ASCII对应: '0' => 48, '1' => 49
continue
}
temp.WriteString("、")
temp.WriteString(key)
}
temp.WriteString("存在重复")
return emity.MsgEmity{}.Err(1004, strings.Replace(temp.String(), "、", "", 1))
}
k++
}
if 0 == k {
return emity.MsgEmity{}.Success("没有设定验证函数,通过")
}
return emity.MsgEmity{}.Success("经验证,通过")
}
/**
* 通用更新数据时重复检查方法
* entity
*/
func (service CommonService) CommonCheckRepeatByEdit(ctx ginutil.Context, entity interface{}, id interface{}) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
vCheckRepeatCombination := global.GlobalVariable{}.Get(tableInfo.GsTableName + "_CheckRepeatCombination")
vCheckRepeatAlone := global.GlobalVariable{}.Get(tableInfo.GsTableName + "_CheckRepeatAlone")
k := 0
//检查待修改内容是否存在重复数据(多字段组合重复即重复)集合
if nil != vCheckRepeatCombination {
checkRepeatCombination := vCheckRepeatCombination.([]string)
var build strings.Builder
build.WriteString("SELECT COUNT(1) AS iCount FROM ${sDbName}${sTableName} WHERE 1=1 ")
var temp strings.Builder
temp.WriteString("[")
where := make(map[string]interface{})
where[tableInfo.GsKeyName] = id
build.WriteString(" AND ")
build.WriteString(tableInfo.GsKeyName)
build.WriteString(" = @")
build.WriteString(tableInfo.GsKeyName)
for _, value := range checkRepeatCombination {
build.WriteString(" AND ")
build.WriteString(value)
build.WriteString(" = @")
build.WriteString(value)
where[value] = system.ReflectUtils{}.GetFieldValue(entity, "G"+value)
temp.WriteString("、")
temp.WriteString(value)
}
txt := strings.Replace(build.String(), "1=1 AND ", "", -1)
txt = strings.Replace(txt, "${sDbName}", gorm.GetDbName(tableInfo.GsDbName), -1)
txt = strings.Replace(txt, "${sTableName}", tableInfo.GsTableName, -1)
iCount := 0
dbResult := gorm.Raw(txt, where).Find(&iCount)
if dbResult.Error != nil {
Log.Error("验证数据是否重复发生异常:", dbResult.Error)
return emity.MsgEmity{}.Err(1001, "验证数据是否重复发生异常:", dbResult.Error)
}
if iCount != 0 {
temp.WriteString("]组合发现数据重复")
return emity.MsgEmity{}.Err(1002, strings.Replace(temp.String(), "、", "", 1))
}
k++
}
//检查待修改内容是否存在重复数据(单独字段重复即重复)集合
if nil != vCheckRepeatAlone {
checkRepeatAlone := vCheckRepeatAlone.(map[string]int)
var build strings.Builder
build.WriteString("SELECT SUM(iCount) FROM (")
where := make(map[string]interface{})
where[tableInfo.GsKeyName] = id
for key, value := range checkRepeatAlone {
build.WriteString(" union all select (SIGN(COUNT(1)) * ")
build.WriteString(strconv.Itoa(value))
build.WriteString(") as iCount ")
build.WriteString(" from ${sDbName}${sTableName} ")
build.WriteString(" where ")
build.WriteString(key)
build.WriteString("= @")
build.WriteString(key)
build.WriteString(" and ")
build.WriteString(tableInfo.GsKeyName)
build.WriteString(" = @")
build.WriteString(tableInfo.GsKeyName)
where[key] = system.ReflectUtils{}.GetFieldValue(entity, "G"+key)
}
build.WriteString(") TMP")
txt := strings.Replace(build.String(), " union all ", " ", 1)
txt = strings.Replace(txt, "${sDbName}", gorm.GetDbName(tableInfo.GsDbName), -1)
txt = strings.Replace(txt, "${sTableName}", tableInfo.GsTableName, -1)
iCount := 0
dbResult := gorm.Raw(txt, where).Find(&iCount)
if dbResult.Error != nil {
Log.Error("验证数据是否重复发生异常:", dbResult.Error)
return emity.MsgEmity{}.Err(1003, "验证数据是否重复发生异常:", dbResult.Error)
}
if iCount != 0 {
var temp strings.Builder
str := fmt.Sprintf("%0*d", len(checkRepeatAlone), iCount)
array := []rune(str) //得到字符数组
ml := len(checkRepeatAlone)
for key, value := range checkRepeatAlone {
i := len(strconv.Itoa(value))
if array[ml-i] == 48 { // ASCII对应: '0' => 48, '1' => 49
continue
}
temp.WriteString("、")
temp.WriteString(key)
}
temp.WriteString("存在重复")
return emity.MsgEmity{}.Err(1004, strings.Replace(temp.String(), "、", "", 1))
}
k++
}
if 0 == k {
return emity.MsgEmity{}.Success("没有设定验证函数,通过")
}
return emity.MsgEmity{}.Success("经验证,通过")
}
/**
* 读取树形结构数据
* @param entity
* @param sGroupColumn
* @param sGroupName
* @return
*/
func (service CommonService) FindByTree(ctx ginutil.Context, entity interface{}, sGroupColumn, sGroupName string) *emity.MsgEmity {
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
if !tableInfo.GbHasPid {
return emity.MsgEmity{}.Err(1001, "指定分组字段不存在!")
}
//-- 从文件读取 --//
var filePath string
if sGroupName == "" {
filePath = "./temp/cache/" + tableInfo.GsTableName + "/tree.txt"
} else {
filePath = "./temp/cache/" + tableInfo.GsTableName + "/Group_" + sGroupName + ".txt"
}
me := jsonutil.JsonUtil{}.FormFile(filePath, map[string]interface{}{})
if me.Gsuccess {
return emity.MsgEmity{}.Success(me.Gdata, "在文件中找到") //返回结果
}
//-- 从数据库读取 --//
var build strings.Builder
build.WriteString("SELECT * FROM ${sDbName}${sTableName} WHERE ")
build.WriteString(global.TableMajorKeyString)
build.WriteString(" > 0")
where := []interface{}{}
if "" != sGroupName {
build.WriteString(" AND ")
build.WriteString(global.TablePathKey)
build.WriteString(" LIKE (")
build.WriteString(" select CONCAT(a.")
build.WriteString(global.TablePathKey)
build.WriteString(", '%')")
build.WriteString(" from ${sDbName}${sTableName} a")
if "" != sGroupColumn {
build.WriteString(" where a.")
build.WriteString(sGroupColumn) //指定字段作为分组标识
build.WriteString(" = ?")
} else if tableInfo.GbHasOnlyign { //启用唯一标识作为关键字
build.WriteString(" where a.")
build.WriteString(global.TableOnlyignName) //启用唯一标识作为关键字
build.WriteString(" = ?") //启用唯一标识作为关键字
} else {
build.WriteString(" where a.")
build.WriteString(global.TableTreeNodeName)
build.WriteString(" = ?")
}
build.WriteString(" )")
where = append(where, sGroupName)
}
build.WriteString(" ORDER BY ")
build.WriteString(global.TablePathKey)
txt := strings.Replace(build.String(), "${sDbName}", gorm.GetDbName(tableInfo.GsDbName), -1)
txt = strings.Replace(txt, "${sTableName}", tableInfo.GsTableName, -1)
rows, err := gorm.Raw(txt, where).Rows()
defer rows.Close()
if nil != err {
Log.Error("查询发生异常:", err)
return emity.MsgEmity{}.Err(1002, "查询发生异常:", err)
}
res := gorm.ScanRows2mapI(rows)
if res == nil {
return emity.MsgEmity{}.Err(1003, "查询后数据转换发生异常")
}
if len(res) < 1 {
return emity.MsgEmity{}.Err(1004, "数据为空")
}
sRootValue := global.TableTreeRootValue
if sGroupName != "" {
sRootValue = res[0][global.TableMajorKeyString].(string) //分组查询情况下必须要一个根节点(因为已经path排序)
}
me = CommonService{}.CreateTree(res, sRootValue, global.TableMajorKeyString, global.TablePidKey, "childs")
if !me.Gsuccess {
return me
}
if len((me.Gdata).([]interface{})) < 1 {
return emity.MsgEmity{}.Err(1005, "数据转换后构造树型数据为空")
}
jsonutil.JsonUtil{}.ToFile(me.Gdata, filePath) //保存到文件
return emity.MsgEmity{}.Success(me.Gdata, "查询成功")
}
/**
* List转树形结构
* @param source Map或切片结构
* @param rootName
* @param idFieldName
* @param pIdFieldName
* @param childFieldName
* @return
*/
func (this CommonService) CreateTree(source interface{},
rootName, idFieldName, pIdFieldName, childFieldName string) *emity.MsgEmity {
if nil == source {
return emity.MsgEmity{}.Err(5001, "没有数据无法进行树结构创建!")
}
if "" == strings.TrimSpace(idFieldName) {
return emity.MsgEmity{}.Err(5002, "对象字段中的编号属性名称必须提供!")
}
if "" == strings.TrimSpace(pIdFieldName) {
return emity.MsgEmity{}.Err(5003, "对象字段中的上级编号属性名称必须提供!")
}
if "" == strings.TrimSpace(childFieldName) {
return emity.MsgEmity{}.Err(5004, "对象字段中的子节点集合属性名称必须提供!")
}
sourceTypeName := reflect.TypeOf(source).String()
if strings.HasPrefix(sourceTypeName, "[]map[string]interface") {
return this.createTreeByMap(source.([]map[string]interface{}), rootName, idFieldName, pIdFieldName, childFieldName)
}
return this.createTreeByList(source.([]interface{}), rootName, idFieldName, pIdFieldName, childFieldName)
}
/**
* List转树形结构
* @param source 切片结构
* @param rootName
* @param idFieldName
* @param pIdFieldName
* @param childFieldName
* @return
*/
func (service CommonService) createTreeByList(source []interface{},
rootName, idFieldName, pIdFieldName, childFieldName string) *emity.MsgEmity {
if nil == source || (len(source) < 1) {
return emity.MsgEmity{}.Err(5001, "没有数据无法进行树结构创建!")
}
result := []interface{}{}
rt := reflect.TypeOf(source[0])
sourceTypeName := rt.String()
isMap := strings.HasPrefix(sourceTypeName, "map[string]interface") //如果数据类型是Map则取值不能用反射方式
if "" == strings.TrimSpace(rootName) {
rootName = global.TableTreeRootValue //未指定就默认为'00'做根节点
}
//-- 将所有数据进行预分组存储,同时筛选出根节点 --//
groupMap := map[string][]interface{}{} //待进行挂接的子分组
allMap := map[string]interface{}{}
for _, object := range source {
sGroupName := ""
if !isMap {
sGroupName = system.ReflectUtils{}.GetFieldValue(object, pIdFieldName).(string)
} else {
sGroupName = (object.(map[string]interface{}))[pIdFieldName].(string)
}
if "" == strings.TrimSpace(sGroupName) {
sGroupName = rootName //如果父节点信息为空,则默认为根节点
}
sGroupName = strings.TrimSpace(sGroupName) //所在组名
sid := ""
if isMap {
sid = (object.(map[string]interface{}))[idFieldName].(string)
} else {
sid = (system.ReflectUtils{}.GetFieldValue(object, idFieldName)).(string)
}
allMap[sid] = object
if rootName == sGroupName {
result = append(result, object) //加入根节点列表
continue
}
childs, ok := groupMap[sGroupName]
if !ok {
childs = []interface{}{}
groupMap[sGroupName] = childs
}
childs = append(childs, object)
groupMap[sGroupName] = childs
}
//--将所有分组寻找父节点--//
for key, value := range groupMap {
obj := allMap[key]
if nil == obj {
continue
}
sTypeName := reflect.TypeOf(obj).String()
if strings.HasPrefix(sTypeName, "map[string]interface") {
(obj.(map[string]interface{}))[childFieldName] = value
continue
}
system.ReflectUtils{}.SetFieldValue(obj, childFieldName, value) // 设置子节点集合
}
return emity.MsgEmity{}.Success(result, "构造成功!")
}
/**
* List转树形结构
* @param source Map结构
* @param rootName
* @param idFieldName
* @param pIdFieldName
* @param childFieldName
* @return
*/
func (service CommonService) createTreeByMap(source []map[string]interface{},
rootName, idFieldName, pIdFieldName, childFieldName string) *emity.MsgEmity {
if nil == source || (len(source) < 1) {
return emity.MsgEmity{}.Err(5001, "没有数据无法进行树结构创建!")
}
result := []interface{}{}
rt := reflect.TypeOf(source[0])
sourceTypeName := rt.String()
isMap := strings.HasPrefix(sourceTypeName, "map[string]interface") //如果数据类型是Map则取值不能用反射方式
if "" == strings.TrimSpace(rootName) {
rootName = global.TableTreeRootValue //未指定就默认为'00'做根节点
}
//-- 将所有数据进行预分组存储,同时筛选出根节点 --//
groupMap := map[string][]interface{}{} //待进行挂接的子分组
allMap := map[string]interface{}{}
for _, object := range source {
sGroupName := ""
if !isMap {
sGroupName = system.ReflectUtils{}.GetFieldValue(object, pIdFieldName).(string)
} else {
sGroupName = object[pIdFieldName].(string)
}
if "" == strings.TrimSpace(sGroupName) {
sGroupName = rootName //如果父节点信息为空,则默认为根节点
}
sGroupName = strings.TrimSpace(sGroupName) //所在组名
sid := ""
if isMap {
sid = object[idFieldName].(string)
} else {
sid = (system.ReflectUtils{}.GetFieldValue(object, idFieldName)).(string)
}
allMap[sid] = object
if rootName == sGroupName {
result = append(result, object) //加入根节点列表
continue
}
childs, ok := groupMap[sGroupName]
if !ok {
childs = []interface{}{}
groupMap[sGroupName] = childs
}
childs = append(childs, object)
groupMap[sGroupName] = childs
}
//--将所有分组寻找父节点--//
for key, value := range groupMap {
obj := allMap[key]
if nil == obj {
continue
}
sTypeName := reflect.TypeOf(obj).String()
if strings.HasPrefix(sTypeName, "map[string]interface") {
(obj.(map[string]interface{}))[childFieldName] = value
continue
}
system.ReflectUtils{}.SetFieldValue(obj, childFieldName, value) // 设置子节点集合
}
return emity.MsgEmity{}.Success(result, "构造成功!")
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* @param entity 实体类
* @param id
* @param fieldNames 待取数据的字段名称集合
* @return emity.MsgEmity 返回内容data中存放的是Map
*/
func (service CommonService) GetValueByFieldName(ctx ginutil.Context, entity interface{}, id interface{}, fieldNames []string) *emity.MsgEmity {
fieldNames = system.ReflectUtils{}.HoldByEntityToArray(entity, fieldNames, "", "G") //按实体保留数组中的数据
if len(fieldNames) < 1 {
return emity.MsgEmity{}.Err(7001, "没有对应的数据可查询!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
var build strings.Builder
build.WriteString("SELECT ")
for _, val := range fieldNames {
build.WriteString(",")
build.WriteString(val)
}
build.WriteString(" FROM ")
build.WriteString(gorm.GetDbName(tableInfo.GsDbName))
build.WriteString(tableInfo.GsTableName)
build.WriteString(" WHERE ")
build.WriteString(tableInfo.GsKeyName)
build.WriteString("=@")
build.WriteString(tableInfo.GsKeyName)
where := make(map[string]interface{})
where[tableInfo.GsKeyName] = id
if (!ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5)) { //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
build.WriteString(" AND ")
build.WriteString(global.TableCreatorName)
build.WriteString("=@")
build.WriteString(global.TableCreatorName)
where[global.TableCreatorName] = ModuleUtil{}.CurrentLoginUserId(ctx)
}
sql := strings.Replace(build.String(), ",", "", 1)
rows, err := gorm.Raw(sql, where).Rows()
defer rows.Close()
if nil != err {
Log.Error("查询发生异常:", err)
return emity.MsgEmity{}.Err(7002, "查询发生异常:", err)
}
res := gorm.ScanRows2mapI(rows)
if res == nil {
Log.Error("查询发生异常:", err)
return emity.MsgEmity{}.Err(7003, "查询发生异常:", err)
}
if len(res) < 1 {
return emity.MsgEmity{}.Err(7004, "数据不存在!")
}
return emity.MsgEmity{}.Success(res[0], "查询成功")
}
/**
* 根据字段名取指定记录编号的数据库表中对应字段的值
* @param entity 实体类
* @param id
* @param fieldName 待取数据的字段名称集合
* @return emity.MsgEmity 返回内容data中存放的是Map
*/
func (service CommonService) GetValueByField(ctx ginutil.Context, entity interface{}, id interface{}, fieldName string) *emity.MsgEmity {
fieldName = strings.TrimSpace(fieldName)
if "" == fieldName {
return emity.MsgEmity{}.Err(7001, "没有对应的数据可查询!")
}
if (!system.ReflectUtils{}.HasField(entity, fieldName)) {
return emity.MsgEmity{}.Err(7002, "指定字段不存在!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
var build strings.Builder
build.WriteString("SELECT ")
build.WriteString(fieldName)
build.WriteString(" FROM ")
build.WriteString(gorm.GetDbName(tableInfo.GsDbName))
build.WriteString(tableInfo.GsTableName)
build.WriteString(" WHERE ")
build.WriteString(tableInfo.GsKeyName)
build.WriteString("=@")
build.WriteString(tableInfo.GsKeyName)
where := make(map[string]interface{})
where[tableInfo.GsKeyName] = id
if (!ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5)) { //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
build.WriteString(" AND ")
build.WriteString(global.TableCreatorName)
build.WriteString("=@")
build.WriteString(global.TableCreatorName)
where[global.TableCreatorName] = ModuleUtil{}.CurrentLoginUserId(ctx)
}
sql := strings.Replace(build.String(), ",", "", 1)
rows, err := gorm.Raw(sql, where).Rows()
defer rows.Close()
if nil != err {
Log.Error("查询发生异常:", err)
return emity.MsgEmity{}.Err(7003, "查询发生异常:", err)
}
res := gorm.ScanRows2mapI(rows)
if res == nil {
Log.Error("查询发生异常:", err)
return emity.MsgEmity{}.Err(7004, "查询发生异常:", err)
}
if len(res) < 1 {
return emity.MsgEmity{}.Err(7005, "数据不存在!")
}
return emity.MsgEmity{}.Success(res[0], "查询成功")
}
/**
* 取记录对应的版本号
* @param entity 实体类
* @param idName 编号
* @return
*/
func (service CommonService) GetiVersion(ctx ginutil.Context, entity interface{}, id interface{}) *emity.MsgEmity {
if fmt.Sprintf("%v", id) == "" {
return emity.MsgEmity{}.Err(8001, "记录编号参数为空!")
}
return CommonDao{}.GetiVersion(entity, id)
}
/**
* 取记录对应的状态值
* @param entity 实体类
* @param idName 编号
* @return
*/
func (service CommonService) GetiState(ctx ginutil.Context, entity interface{}, id interface{}) *emity.MsgEmity {
if fmt.Sprintf("%v", id) == "" {
return emity.MsgEmity{}.Err(8001, "记录编号参数为空!")
}
return CommonDao{}.GetiState(entity, id)
}
/**
* 根据关键值取对象集合
* @Param entity 实体类
* @Param where 存放查询条件
* @return emity.MsgEmity
*/
func (service CommonService) FindByKey(ctx ginutil.Context, entity interface{}, where map[string]interface{}) *emity.MsgEmity {
where = system.ReflectUtils{}.HoldByEntity(entity, where, "", "G") //按实体保留map中的数据
if len(where) < 1 {
return emity.MsgEmity{}.Err(7001, "没有对应的查询条件!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindByKey(entity, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 根据关键值取对象集合中的第一个
* @Param entity 实体类
* @Param where 存放查询条件
* @param fields 指定要查询的字段集合
* @return emity.MsgEmity
*/
func (service CommonService) FindOneByKey(ctx ginutil.Context, entity interface{}, where map[string]interface{}, fields ...string) *emity.MsgEmity {
where = system.ReflectUtils{}.HoldByEntity(entity, where, "", "G") //按实体保留map中的数据
if len(where) < 1 {
return emity.MsgEmity{}.Err(8001, "没有对应的查询条件!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindOneByKey(entity, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator, fields...)
}
/**
* 根据关键值取对象集合中的符合条件的第一条记录的指定字段
* @Param entity 实体类
* @Param where 存放查询条件
* @param fieldName 指定要查询的字段
* @return emity.MsgEmity
*/
func (service CommonService) FindValueByKey(ctx ginutil.Context, entity interface{}, where map[string]interface{}, fieldName string) *emity.MsgEmity {
where = system.ReflectUtils{}.HoldByEntity(entity, where, "", "G") //按实体保留map中的数据
if len(where) < 1 {
return emity.MsgEmity{}.Err(8001, "没有对应的查询条件!")
}
fieldName = strings.TrimSpace(fieldName)
if "" == fieldName {
return emity.MsgEmity{}.Err(8002, "没有待查字段!")
}
if (!system.ReflectUtils{}.HasField(entity, fieldName)) {
return emity.MsgEmity{}.Err(8003, "指定字段不存在!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindValueByKey(entity, where, fieldName, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 根据指定字段进行分组查询
* @Param entity 实体类
* @Param where 存放查询条件
* @param fieldMap 指定要查询的字段集合(原字段, 别名)
* @return emity.MsgEmity
*/
func (service CommonService) FindByFields(ctx ginutil.Context, entity interface{}, where map[string]interface{}, fieldMap map[string]string) *emity.MsgEmity {
where = system.ReflectUtils{}.HoldByEntity(entity, where, "", "G") //按实体保留map中的数据
if nil == fieldMap || len(fieldMap) < 1 {
return emity.MsgEmity{}.Err(8002, "没有待查字段!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindByFields(entity, where, fieldMap, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 根据关键值查数量
* @Param entity 实体类
* @Param where 存放查询条件
* @return emity.MsgEmity
*/
func (service CommonService) FindCountByKey(ctx ginutil.Context, entity interface{}, where map[string]interface{}) *emity.MsgEmity {
where = system.ReflectUtils{}.HoldByEntity(entity, where, "", "G") //按实体保留map中的数据
if len(where) < 1 {
return emity.MsgEmity{}.Err(7001, "没有对应的查询条件!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.FindCountByKey(entity, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
// /**
// * 以事务方式执行Mapper下的多个方法
// * 注意:Mapper必须存在才能执行
// * @param mapper iBatis实体
// * @Param funcInfo 接口信息集合(以3对为一组) <函数名, 函数参数类型集合, 函数参数集合, 函数名, 函数参数类型集合, 函数参数集合....>
// * @return emity.MsgEmity 返回对象数组(各函数执行结果)
// */
// func (service CommonService) transactionMapping(Object mapper, Object...funcInfo) *emity.MsgEmity {
// return emity.MsgEmity{}.Success(res, "查询成功")
// }
/**
* 根据字段名取分组数据
* @param entity 实体类
* @param sCreator 指定用户
* @param fields 字段名与别名对象
* @return
*/
func (service CommonService) GroupByField(ctx ginutil.Context, entity interface{}, sCreator string, fields map[string]string) *emity.MsgEmity {
if len(fields) < 1 {
return emity.MsgEmity{}.Err(8001, "没有对应的待查字段!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.GroupByField(entity, sCreator, fields, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 根据字段名取分组数据及分组后数量
* @param entity 实体类
* @param sCreator 指定用户
* @param fields 字段名与别名对象
* @return
*/
func (service CommonService) GroupByFieldAndCount(ctx ginutil.Context, entity interface{}, sCreator string, fields map[string]string) *emity.MsgEmity {
if len(fields) < 1 {
return emity.MsgEmity{}.Err(8001, "没有对应的待查字段!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.GroupByFieldAndCount(entity, sCreator, fields, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 取表中指定字段的最大值
* @param entity 实体类
* @param sCreator 指定用户
* @param field 字段名
* @param where 查询条件字符串
* @return emity.MsgEmity
*/
func (service CommonService) MaxByField(ctx ginutil.Context, entity interface{}, sCreator string, field string, where map[string]interface{}) *emity.MsgEmity {
field = strings.TrimSpace(field)
if "" == field {
return emity.MsgEmity{}.Err(7001, "没有待查字段!")
}
if len(where) < 1 {
return emity.MsgEmity{}.Err(7002, "没有对应的查询条件!")
}
if (!system.ReflectUtils{}.HasField(entity, field)) {
return emity.MsgEmity{}.Err(7003, "指定字段不存在!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.MaxByField(entity, sCreator, field, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 取表中指定字段的最小值
* @param entity 实体类
* @param sCreator 指定用户
* @param field 字段名
* @param where 查询条件
* @return emity.MsgEmity
*/
func (service CommonService) MinByField(ctx ginutil.Context, entity interface{}, sCreator string, field string, where map[string]interface{}) *emity.MsgEmity {
field = strings.TrimSpace(field)
if "" == field {
return emity.MsgEmity{}.Err(7001, "没有待查字段!")
}
if len(where) < 1 {
return emity.MsgEmity{}.Err(7002, "没有对应的查询条件!")
}
if (!system.ReflectUtils{}.HasField(entity, field)) {
return emity.MsgEmity{}.Err(7003, "指定字段不存在!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.MinByField(entity, sCreator, field, where, ModuleUtil{}.CurrentLoginUserId(ctx), onlyCreator)
}
/**
* 检查关键值记录是否存在(返回1:存在;0:不存在)
* @param entity 实体类
* @Param id
* @return emity.MsgEmity
*/
func (service CommonService) HasById(ctx ginutil.Context, entity interface{}, id interface{}) *emity.MsgEmity {
if fmt.Sprintf("%v", id) == "" {
return emity.MsgEmity{}.Err(8001, "记录编号参数为空!")
}
return CommonDao{}.HasById(entity, id)
}
/**
* 检查关键值记录是否存在(返回1:存在;0:不存在)
* @param entity 实体类
* @Param keyName 字段名
* @Param keyValue 字段值
* @return emity.MsgEmity
*/
func (service CommonService) HasByKey(ctx ginutil.Context, entity interface{}, keyName string, keyValue interface{}) *emity.MsgEmity {
keyName = strings.TrimSpace(keyName)
if keyName == "" {
return emity.MsgEmity{}.Err(7001, "字段名参数为空!")
}
if (nil == keyValue) || ("" == fmt.Sprintf("%v", keyValue)) || ("<nil>" == fmt.Sprintf("%v", keyValue)) {
return emity.MsgEmity{}.Err(7002, "字段值参数为空!")
}
if (!system.ReflectUtils{}.HasField(entity, keyName)) {
return emity.MsgEmity{}.Err(7003, "指定字段不存在!")
}
dataInfo := dbinfo.DataInfo{}.GetDataInfoByName(entity, keyName)
if nil == dataInfo {
return emity.MsgEmity{}.Err(7004, "字段备注信息缺失")
}
switch dataInfo.GsDbFileType {
case "int":
temp, err := strconv.Atoi(keyValue.(string))
if err != nil {
return emity.MsgEmity{}.Err(7005, "字段值参数为不符合规范!")
}
keyValue = temp
case "bigint":
temp, err := strconv.ParseInt(keyValue.(string), 10, 64)
if err != nil {
return emity.MsgEmity{}.Err(7006, "字段值参数为不符合规范!")
}
keyValue = temp
}
return CommonDao{}.HasByKey(entity, keyName, keyValue)
}
/**
* 清理指定用户的缓存
* @param cacheName 缓存名
* @Param sUser 用户名
* @return emity.MsgEmity
*/
func (service CommonService) ClearCache(ctx ginutil.Context, cacheName string, sUser string) *emity.MsgEmity {
cacheName = strings.TrimSpace(cacheName)
if "" == cacheName {
return emity.MsgEmity{}.Err(7001, "指定'缓存库名称'参数为空!")
}
sUser = strings.TrimSpace(sUser)
if "" == sUser {
cacheName = cacheName + sUser
}
if (global.GlobalVariable{}.Del(cacheName)) {
return emity.MsgEmity{}.Success(7999, "清理成功!")
}
return emity.MsgEmity{}.Err(7002, "清理失败!")
}
/**
* 查询组结构数据
* @param r Http请求对象
* @param entity 实体类
* @param sGroupColumn 分组名(树节点)所在字段名
* @param sGroupName 分组名(树节点)
* @return
*/
func (service CommonService) FindByGroup(ctx ginutil.Context, entity interface{}, sGroupColumn, sGroupName string) *emity.MsgEmity {
return service.FindByTree(ctx, entity, sGroupColumn, sGroupName)
}
/**
* 添加数据到指定组下
* 警告:对象必须符合树形结构要求,如:sId、sPid
* @param r Http请求对象
* @param sGroupName 分组字段名称(树节点)
* @param sGroupValue 分组字段值(树节点)
* @param entity 实体对象
* @return
*/
func (service CommonService) AddToGroup(ctx ginutil.Context, entity interface{}, sGroupName, sGroupValue string) *emity.MsgEmity {
if sGroupName == "" {
return emity.MsgEmity{}.Err(8001, "节点参数不能为空!")
}
if nil == entity {
return emity.MsgEmity{}.Err(8002, "实体对象不能为空!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
if !tableInfo.GbHasPid {
return emity.MsgEmity{}.Err(1001, "指定分组字段不存在!")
}
if global.TableMajorKeyString != tableInfo.GsKeyName {
return emity.MsgEmity{}.Err(8003, "实体类型没有sId字段,不符合格式要求!")
}
if !tableInfo.GbHasPid {
return emity.MsgEmity{}.Err(8004, "实体类型没有sPid字段,不符合格式要求!")
}
//-- 查组所在记录编号 --//
var build strings.Builder
build.WriteString("SELECT ")
build.WriteString(global.TableMajorKeyString)
build.WriteString(" FROM ")
build.WriteString(gorm.GetDbName(tableInfo.GsDbName))
build.WriteString(tableInfo.GsTableName)
build.WriteString(" WHERE ")
build.WriteString(sGroupName)
build.WriteString("=? LIMIT 0, 1")
sPId := ""
dbResult := gorm.Raw(build.String(), sGroupValue).Find(&sPId)
if dbResult.Error != nil {
Log.Error("查询发生异常:", dbResult.Error)
return emity.MsgEmity{}.Err(8005, "查询发生异常:", dbResult.Error)
}
if sPId == "" {
return emity.MsgEmity{}.Err(8006, "指定组不存在,不能用此方法添加!")
}
system.ReflectUtils{}.SetFieldValue(entity, global.TablePidKey, sPId) // 父编号就是查出来的数据
dbinfo.DataInfo{}.SetDataInfoDefault(entity) //对对象中添加了dataInfo注解的属性添加默认值
me := service.SupplyDbEntityAttrByAdd(ctx, entity, "", "")
if !me.Gsuccess {
return me.IncCode(7030)
}
me = dbinfo.DataInfo{}.ValidAttrByAdd(entity, []string{}) //对对象中添加了dbinfo.DataInfo注解的属性检查限制
if !me.Gsuccess {
return me.IncCode(7020)
}
me = service.ValidEntityRepeatByAdd(ctx, entity) //验证新增数据是否存在重复
if !me.Gsuccess {
return me.IncCode(7030)
}
return CommonDao{}.AddCommon(entity)
}
/**
* 执行SQL脚本获取单行单列数据
* 注意:库名必须用${}进行包装,此脚本应只存在一条记录且为单列
* @param sql 待执行的SQL脚本
* @return emity.MsgEmity
*/
func (service CommonService) DoSql(sql string) *emity.MsgEmity {
return CommonDao{}.DoSql(sql)
}
/**
* 执行SQL脚本获取单行单列数据
* 注意:库名必须用${}进行包装,此脚本应只存在一条记录且为单列
* @param sql 待执行的SQL脚本
* @return emity.MsgEmity
*/
func (service CommonService) ExecSql(sql string) *emity.MsgEmity {
return CommonDao{}.ExecSql(sql)
}
/**
* 执行SQL脚本获取单行单列数据
* 注意:库名必须用${}进行包装,此脚本应只存在一条记录且为单列
* @param sql 待执行的SQL脚本
* @return emity.MsgEmity
*/
func (service CommonService) GetValue(sql string) *emity.MsgEmity {
return CommonDao{}.GetValue(sql)
}
/**
* 执行SQL脚本获取一行数据(多列)
* 注意:库名必须用${}进行包装,此脚本应只存在一条记录
* @param sql 待执行的SQL脚本
* @return emity.MsgEmity
*/
func (service CommonService) GetRow(sql string) *emity.MsgEmity {
return CommonDao{}.GetRow(sql)
}
/**
* 执行SQL脚本获取多行数据(多列)
* 注意:库名必须用${}进行包装,此脚本可返回多条记录
* @param sql SQL脚本
* @return emity.MsgEmity
*/
func (service CommonService) GetRows(sql string) *emity.MsgEmity {
return CommonDao{}.GetRows(sql)
}
/**
* 根据关键值翻转值(限布尔值类型,1转2,2转1)
* 警告:此方法只支持布尔值类型,且只支持翻转1和2
* @Param ctx http请求对象
* @Param entity 实体类
* @Param where 存放查询条件
* @Param reversalColumn 翻转的字段名
* @return emity.MsgEmity
*/
func (service CommonService) ReversalByKey(ctx ginutil.Context, entity interface{}, where map[string]interface{}, reversalColumn string) *emity.MsgEmity {
currentUser := ModuleUtil{}.CurrentLoginUserId(ctx)
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
onlyCreator := !ModuleUtil{}.EnableTag(tableInfo.GsTableName, 6) //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
return CommonDao{}.ReversalByKey(entity, where, reversalColumn, currentUser, onlyCreator)
}
/**
* 根据条件仅查询指定字段名数据
* @param entity 实体类
* @param where
* @param fieldNames 待取数据的字段名称集合
* @return emity.MsgEmity 返回内容data中存放的是Map
*/
func (service CommonService) FindField(ctx ginutil.Context, entity interface{}, where map[string]interface{}, fieldNames []string) *emity.MsgEmity {
fieldNames = system.ReflectUtils{}.HoldByEntityToArray(entity, fieldNames, "", "G") //按实体保留数组中的数据
if len(fieldNames) < 1 {
return emity.MsgEmity{}.Err(7001, "没有对应的数据可查询!")
}
tableInfo := dbinfo.TableInfo{}.GetByEntity(entity)
var build strings.Builder
build.WriteString("SELECT ")
for _, val := range fieldNames {
build.WriteString(",")
build.WriteString(val)
}
build.WriteString(" FROM ")
build.WriteString(gorm.GetDbName(tableInfo.GsDbName))
build.WriteString(tableInfo.GsTableName)
build.WriteString(" WHERE 1=1")
for k := range where {
build.WriteString(" AND ")
build.WriteString(k)
build.WriteString("=@")
build.WriteString(k)
}
if (!ModuleUtil{}.EnableTag(tableInfo.GsTableName, 5)) { //7个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆、物理删除
build.WriteString(" AND ")
build.WriteString(global.TableCreatorName)
build.WriteString("=@")
build.WriteString(global.TableCreatorName)
where[global.TableCreatorName] = ModuleUtil{}.CurrentLoginUserId(ctx)
}
sql := strings.Replace(build.String(), ",", "", 1)
sql = strings.Replace(sql, " WHERE 1=1 AND ", " WHERE ", -1)
rows, err := gorm.Raw(sql, where).Rows()
defer rows.Close()
if nil != err {
Log.Error("查询发生异常:", err)
return emity.MsgEmity{}.Err(7002, "查询发生异常:", err)
}
res := gorm.ScanRows2mapI(rows)
if res == nil {
Log.Error("查询发生异常:", err)
return emity.MsgEmity{}.Err(7003, "查询发生异常:", err)
}
if len(res) < 1 {
return emity.MsgEmity{}.Err(7004, "数据不存在!")
}
return emity.MsgEmity{}.Success(res[0], "查询成功")
}
/**
* 上传文件
* @param ctx http请求对象
* @param modelName 模块名称
* @return emity.MsgEmity
*/
func (service CommonService) UpFile(ctx ginutil.Context, modelName string) *emity.MsgEmity {
if upFilePath == "" {
upFilePath = AppUtil{}.ReadConfigKey("App", "UpFilePath", "./data/app/").(string)
if !strings.HasSuffix(upFilePath, "/") {
upFilePath = upFilePath + "/"
}
}
// 指定上传目录
targetDir := upFilePath + modelName
// 确保目录存在
if err := os.MkdirAll(targetDir, 0755); err != nil {
return emity.MsgEmity{}.Err(8001, "无法创建目录!")
}
// 单个文件上传示例
file, err := ctx.FormFile("file")
if err != nil {
return emity.MsgEmity{}.Err(8002, "'file'参数缺失!")
}
// 读取文件
src, err := file.Open()
if err != nil {
return emity.MsgEmity{}.Err(8003, "无法打开上传的文件!")
}
defer src.Close()
sFilename := uuidutil.UUIDUtil{}.Get()
// 保存文件到指定目录
dst, err := os.Create(filepath.Join(targetDir, sFilename))
if err != nil {
return emity.MsgEmity{}.Err(8004, "无法保存文件!")
}
defer dst.Close()
_, err = dst.ReadFrom(src)
if err != nil {
return emity.MsgEmity{}.Err(8005, "保存文件内容失败!")
}
return emity.MsgEmity{}.Success(sFilename, "上传成功!")
}
/**
* 获取图片
* @param ctx http请求对象
* @param modelName 模块名称
* @param filename 文件名
*/
func (service CommonService) LookImg(ctx ginutil.Context, modelName, filename string) {
if upFilePath == "" {
upFilePath = AppUtil{}.ReadConfigKey("App", "UpFilePath", "./data/app/").(string)
if !strings.HasSuffix(upFilePath, "/") {
upFilePath = upFilePath + "/"
}
}
filePath := upFilePath + modelName + "/" + filename // 假设文件存储在 ./texts 目录下
// 读取图片文件
content, err := os.ReadFile(filePath)
if err != nil {
ctx.AbortWithStatus(http.StatusInternalServerError)
return
}
// 获取文件的MIME类型
mimeType := http.DetectContentType(content[:512])
// 使用 c.Data() 发送图片二进制数据
ctx.Data(http.StatusOK, mimeType, content)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tomatomeatman/golang-repository.git
git@gitee.com:tomatomeatman/golang-repository.git
tomatomeatman
golang-repository
GolangRepository
cddfa7f15f20

搜索帮助