1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791 |
- package api
- import (
- "../../pkg/app"
- "../../pkg/e"
- "../../pkg/logging"
- "../../pkg/setting"
- "../../routers/restful"
- "bytes"
- "database/sql"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/360EntSecGroup-Skylar/excelize"
- "github.com/Anderson-Lu/gofasion/gofasion"
- "github.com/gin-gonic/gin"
- "github.com/tealeg/xlsx"
- "github.com/xormplus/xorm"
- "io/ioutil"
- "net/http"
- "os"
- "path"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- // @Summary 根据SQL执行得到数据集
- // @Tags getData
- // @Accept json
- // @Produce json
- // @Param sql query string true "sql"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/getdata [POST]
- func GetData(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlstr := fsion.Get("sql").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- queryData, err := getDataBySql(sqlstr, offset, pagecount, returntype, nil)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- }
- func Testpartitions(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- databaseName := fsion.Get("databaseName").ValueStr() //获取新数据库名称
- user := fsion.Get("user").ValueStr() //获取新数据库用户
- passWord := fsion.Get("passWord").ValueStr() //获取新数据库名称
- host := fsion.Get("host").ValueStr() //获取新数据库名称
- port := fsion.Get("port").ValueStr() //获取新数据库名称
- sqls, err1 := restful.Engine.SQL("SELECT * FROM sql_new ").QueryString() //获取新数据库表
- if sqls == nil || err1 != nil {
- appG.Response(http.StatusOK, e.SUCCESS, "失败:"+err1.Error())
- }
- sqlstrs := strings.Split(sqls[0]["sql"], ";") //分割sql
- connecting := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, "mysql")
- mydb, err := sql.Open("mysql", connecting) //连接新连接
- if err != nil {
- println("err1", err.Error())
- }
- _, err = mydb.Exec("CREATE DATABASE IF NOT EXISTS `" + databaseName + "`") // 创建数据库
- if err != nil {
- println("err2", err.Error())
- }
- mydb.Close()
- connecting = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, databaseName) //连接新数据库,创建表格
- mydb, err = sql.Open("mysql", connecting)
- if err != nil {
- println("err3", err.Error())
- }
- defer mydb.Close()
- if err == nil {
- mydb.SetMaxOpenConns(10)
- mydb.SetMaxIdleConns(10)
- }
- errmsg := ""
- if err == nil {
- //tx ,_ :=mydb.Begin()
- for _, sqlstr := range sqlstrs {
- //println("sqlstr---",sqlstr)
- if sqlstr == "" {
- continue
- }
- _, err = mydb.Exec(sqlstr)
- if err != nil {
- println("err4", err.Error())
- errmsg = err.Error()
- //err1 :=tx.Rollback()
- }
- }
- //_, err = mydb.Exec("CREATE TABLE `sql_new` (`sql` text DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;")
- //err =tx.Commit()
- }
- if errmsg == "" {
- appG.Response(http.StatusOK, e.SUCCESS, "成功"+databaseName)
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, "失败"+errmsg)
- }
- }
- func TestData(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlstr := fsion.Get("sql").ValueStr()
- token := ""
- if tempv, exists := c.Get("jwt_username"); exists {
- token = tempv.(string)
- }
- appG.Response(http.StatusOK, e.SUCCESS, "你录入的名字是:"+sqlstr+";token:"+token)
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags GetDataByName
- // @Accept json
- // @Produce json
- // @Param sqlname body string true "sqlname"
- // @Param offset body int true "offset"
- // @Param pagecount body int true "pagecount"
- // @Param returntype body string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/GetDataByName [POST]
- func GetDataByName(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- logging.Error("tx.Begin 事务启动失败__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- //fmt.Println("sql========\n",sqlnamestr,sql)
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
- logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
- }
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion.HasKey("params") {
- params := fsion.Get("params").Array()
- logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- if returntype == "xlsx" {
- var xlsxfile *xlsx.File
- xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
- //c.File("c:/dd.xlsx")
- c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- _ = xlsxfile.Write(c.Writer)
- } else {
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- //fmt.Printf("%v+\n",queryData)
- if err != nil {
- logging.Error("GetDataByName err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- func GetDataBySqlIN(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- logging.Error("tx.Begin 事务启动失败__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- //fmt.Println("sql========\n",sqlnamestr,sql)
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
- logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
- }
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion.HasKey("params") {
- params := fsion.Get("params").Array()
- logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- if returntype == "xlsx" {
- var xlsxfile *xlsx.File
- xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
- //c.File("c:/dd.xlsx")
- c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- _ = xlsxfile.Write(c.Writer)
- } else {
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- //fmt.Printf("%v+\n",queryData)
- if err != nil {
- logging.Error("GetDataByName err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- func geterrmsg(errstr string) (msgstr string) {
- var hzRegexp = regexp.MustCompile("^[\u4e00-\u9fa5]$") // 判断是否为汉字
- rs := []rune(errstr)
- if hzRegexp.MatchString(string(rs[0])) {
- msgstr = errstr
- return
- }
- if strings.Contains(errstr, "Duplicate entry") {
- strs := strings.Split(errstr, ` key '`)
- if len(strs) > 1 {
- if len(strs[len(strs)-1]) > 1 {
- errstr = "Duplicate entry :" + strs[len(strs)-1]
- }
- }
- }
- msg, err := restful.Engine.SQL("SELECT msg FROM err_msg where err = ? ", errstr).QueryString()
- if err != nil {
- msgstr = errstr
- return
- }
- if msg == nil || len(msg) == 0 {
- var param []interface{}
- param = append(param, errstr, errstr)
- go func() {
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- logging.Error("tx.Begin 事务启动失败__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- _, err = restful.ExecQueryT("insert into err_msg(err,msg) VALUES(?,?) ON DUPLICATE KEY UPDATE err=err", param, tx)
- if err != nil {
- logging.Error("insert into err_msg(err,msg) err", err)
- }
- }()
- msgstr = errstr
- } else {
- msgstr = msg[0]["msg"]
- }
- return
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags GetDataByNameForm
- // @Accept json
- // @Produce json
- // @Param sqlname body json true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Param s formData json true "ss"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/GetDataByNameForm [POST]
- func GetDataByNameForm(c *gin.Context) {
- appG := app.Gin{C: c}
- //sqlnamestr := c.PostForm("measureNumber")
- //dataByte, _ := ioutil.ReadAll(c.Request.Body)
- //fsion := gofasion.NewFasion(string(dataByte))
- //sqlnamestr := fsion.Get("name").ValueStr()
- //offset := fsion.Get("offset").ValueInt()
- //pagecount := fsion.Get("pagecount").ValueInt()
- //returntype := fsion.Get("returntype").ValueStr()
- //params := fsion.Get("params").Array()
- c.Request.ParseForm()
- params := c.Request.Form
- offset := 0
- pageSize_ := 0
- returntype := params.Get("page")
- if returntype == "" {
- returntype = "0"
- }
- if tempv, exists := c.Get("jwt_username"); exists {
- params.Set("JwtUserName", tempv.(string))
- } else {
- params.Set("JwtUserName", "")
- }
- sqlnamestr := params.Get("name")
- logging.Info("GetDataByNameForm ", c.Keys, sqlnamestr)
- s_params := make([]interface{}, 0)
- if returntype == "1" {
- pageNum, error := strconv.Atoi(params.Get("pageNum"))
- if error != nil {
- appG.ResponseEq(http.StatusOK, e.ERROR, 0, "页码不是整数")
- }
- pageSize, error := strconv.Atoi(params.Get("pageSize"))
- if error != nil {
- appG.ResponseEq(http.StatusOK, e.ERROR, 0, "每页行数不是整数")
- }
- pageSize_ = pageSize
- offset = pageNum
- }
- sql, p := restful.GetSqlByNameDB(sqlnamestr)
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- s_params = append(s_params, params.Get(strings.Trim(value, " ")))
- }
- }
- if sql != "" {
- if returntype == "xlsx" {
- var xlsxfile *xlsx.File
- xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
- c.Header("Content-Type", "application/octet-stream")
- c.Header("Content-Disposition", "attachment; filename="+sqlnamestr+".xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- _ = xlsxfile.Write(c.Writer)
- } else {
- queryData, err := getDataBySql(sql, offset, pageSize_, returntype, s_params)
- if err != nil {
- appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
- } else {
- appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
- }
- }
- } else {
- appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
- }
- }
- // @Summary 递归获取所有
- // @Tags menu
- // @Accept json
- // @Produce json
- // @Param Ton query string true "Ton"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Router /authdata/GetRoleMenus [GET]
- func GetRecuDataByName(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- parentidname := fsion.Get("idname").ValueStr()
- sql, p := restful.GetSqlByNameDB(sqlnamestr)
- params := make(map[string]interface{})
- s_params := make([]interface{}, 0)
- if fsion.HasKey("params") {
- for _, v_params := range fsion.Get("params").Array() {
- s_params = append(s_params, v_params.ValueStr())
- }
- params["params"] = s_params
- } else {
- jparams := fsion.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- params[value] = tempv.(string)
- } else {
- params[value] = ""
- }
- } else {
- params[value] = jparams.Get(strings.Trim(value, " ")).ValueStr()
- }
- }
- }
- }
- queryData, err := GetRecuData_BySql(sql, p, parentidname, params, fsion.HasKey("params"))
- logging.Info("sqlname parammaps", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- }
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags GetDataByNames
- // @Accept json
- // @Produce json
- // @RequestBody sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/GetDataByNames [POST]
- func GetDataByNames(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlParams := fsion.Array()
- queryData := make(map[string]interface{})
- for _, v := range sqlParams {
- fsion2 := gofasion.NewFasion(v.Json())
- logging.Info("GetDataByNames", c.Keys, fsion2.Get("name").ValueStr(), c.Request.RemoteAddr)
- sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
- if sql == "" {
- errorm := make(map[string]interface{})
- errorm["code"] = e.ERROR
- errorm["message"] = "sql没找到"
- queryData[fsion2.Get("name").ValueStr()] = errorm
- continue
- } else if sql != "" {
- offset := fsion2.Get("offset").ValueInt()
- pagecount := fsion2.Get("pagecount").ValueInt()
- returntype := fsion2.Get("returntype").ValueStr()
- s_params := make([]interface{}, 0)
- if fsion2.HasKey("parammaps") {
- parammaps := fsion2.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion2.HasKey("params") {
- params := fsion2.Get("params").Array()
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
- if err != nil {
- errorm := make(map[string]interface{})
- errorm["code"] = e.ERROR
- errorm["message"] = err.Error()
- queryData[fsion2.Get("name").ValueStr()] = errorm
- logging.Error("GetDataByNames err: ", err)
- continue
- } else {
- queryData[fsion2.Get("name").ValueStr()] = queryData1
- }
- }
- }
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags PostDataByNameForm
- // @Accept json
- // @Produce json
- // @Param sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误朱红彬彬"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/PostDataByNameForm [POST]
- func PostDataByNameForm(c *gin.Context) {
- appG := app.Gin{C: c}
- c.Request.ParseForm()
- params := c.Request.Form
- sqlnamestr := params.Get("name")
- logging.Info("PostDataByNameForm ", c.Keys, sqlnamestr)
- s_params := make([]interface{}, 0)
- sql, p := restful.GetSqlByNameDB(sqlnamestr)
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 {
- for _, value := range paramslist {
- s_params = append(s_params, params.Get(strings.Trim(value, " ")))
- }
- }
- if sql != "" {
- queryData, err := execDataBySql(sql, s_params)
- if err != nil {
- appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
- } else {
- appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
- }
- } else {
- appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
- }
- }
- func GetReportform(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- parammaps := fsion.Get("parammaps")
- logging.Info("GetReportform ", c.Keys, sqlnamestr, parammaps)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- queryData, err := praseReportform(sqlnamestr, parammaps, tx)
- if err != nil{
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- }else {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- }
- func GetUpkeepPlan(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- month := fsion.Get("month").ValueDefaultStr("map")
- logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion.HasKey("params") {
- params := fsion.Get("params").Array()
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- if returntype == "xlsx" {
- var xlsxfile *xlsx.File
- xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
- //c.File("c:/dd.xlsx")
- c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- _ = xlsxfile.Write(c.Writer)
- } else {
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
- if v, ok := value["id"]; ok {
- //month = "2020-03"
- seviceplans, _ := tx.SQL("SELECT * FROM seviceplan where eqId = ? and month=? ", v, month).QueryString() //获取保养计划
- if seviceplans != nil {
- for _, va := range seviceplans { //根据保养计划获取保养单
- //day,_ :=strconv.Atoi(va["day"])
- bigupkeeps, _ := tx.SQL("SELECT * FROM bigupkeep where seviceplanId = ?", va["id"]).QueryString() //获取保养计划中的保养单
- //buk := make(map[string]map[string]string)
- for _, bu := range bigupkeeps { //日期为key ,保养单为value 存到map中
- jstr := ""
- if bu["plantime"][8:9] == "0" {
- jstr = "m" + bu["plantime"][9:]
- } else {
- jstr = "m" + bu["plantime"][8:]
- }
- if bu["flowCompeleted"] < "2" && bu["plantime"] < time.Now().Format("2006-01-02") { //如果保养状态是0并且小于当前日期 则状态已延期2
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#2" + "#" + bu["id"] + "#" + bu["pushbackId"]
- } else {
- if bu["flowCompeleted"] >= "2" {
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#1" + "#" + bu["id"] + "#" + bu["pushbackId"]
- } else {
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#0" + "#" + bu["id"] + "#" + bu["pushbackId"]
- }
- }
- }
- }
- }
- }
- }
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- func GetArrList(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- sqlnamestr1 := fsion.Get("name1").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- month := fsion.Get("month").ValueDefaultStr("map")
- logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //STR :=Httppostdata("http://192.168.1.104:8081/authdata/GetDataBySqlIN","POST",string(dataByte),tx)
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- }
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- paramslist1 := strings.Split(p1, ",")
- for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
- s_params1 := make([]interface{}, 0)
- if len(paramslist1) > 0 && p1 != "" {
- for _, v := range paramslist1 {
- s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
- }
- }
- arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
- }
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- func GetRJSBData(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- sqlnamestr1 := fsion.Get("name1").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("plancount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- month := fsion.Get("month").ValueDefaultStr("map")
- logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //STR :=Httppostdata("http://192.168.1.104:8081/authdata/GetDataBySqlIN","POST",string(dataByte),tx)
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- }
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- paramslist1 := strings.Split(p1, ",")
- for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
- s_params1 := make([]interface{}, 0)
- if len(paramslist1) > 0 && p1 != "" {
- for _, v := range paramslist1 {
- s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
- }
- }
- arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
- }
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- func GetAccount(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- logging.Error("tx.Begin 事务启动失败__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- //fmt.Println("sql========\n",sqlnamestr,sql)
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
- logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
- }
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion.HasKey("params") {
- params := fsion.Get("params").Array()
- logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- if returntype == "xlsx" {
- var xlsxfile *xlsx.File
- xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
- //c.File("c:/dd.xlsx")
- c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
- c.Header("Content-Transfer-Encoding", "binary")
- _ = xlsxfile.Write(c.Writer)
- } else {
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- if err != nil {
- logging.Error("GetDataByName err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- //appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags PostDataByName
- // @Accept json
- // @Produce json
- // @Param sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/PostDataByName [POST]
- func PostDataByName(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- s_params := make([]interface{}, 0)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- if sql != "" {
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- s_params = append(s_params, ids)
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion.HasKey("params") {
- params := fsion.Get("params").Array()
- logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
- //ids,err := setting.SnowIds.NextId()
- //if err !=nil{
- // ids = time.Now().UnixNano()
- // logging.Info("create SnowIds err",err)
- //}
- //s_params = append(s_params, ids)
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- queryData, err := execDataBySqlT(sql, s_params, tx)
- if err != nil {
- logging.Error("PostDataByName err: ", err)
- //appG.Response(http.StatusOK, e.ERROR, err.Error())
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- func PostRJSBData(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- logging.Info("PostRJSBData err: ", string(dataByte))
- s_params := make([]interface{}, 0)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
- sql1, p1 := restful.GetSqlByNameDBT("updated1YJ", tx)
- sql2, p2 := restful.GetSqlByNameDBT("updated1execYJ", tx)
- sql3, p3 := restful.GetSqlByNameDBT("updated2YJ", tx)
- if !fsion.HasKey("parammaps") {
- appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
- return
- }
- parammaps := fsion.Get("parammaps")
- // ==========================1========================
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- _, err = tx.SQL(sql, s_params...).Execute()
- if err != nil {
- fmt.Println(err)
- logging.Error("PostRJSBData err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- // ==========================2========================
- s_params = make([]interface{}, 0)
- paramslist = strings.Split(p1, ",")
- if len(paramslist) > 0 && p1 != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- _, err = tx.SQL(sql1, s_params...).Execute()
- if err != nil {
- logging.Error("PostRJSBData1 err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- s_params = make([]interface{}, 0)
- // ==========================3========================
- paramslist = strings.Split(p2, ",")
- if len(paramslist) > 0 && p2 != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- _, err = tx.SQL(sql2, s_params...).Execute()
- if err != nil {
- logging.Error("PostRJSBData2 err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- s_params = make([]interface{}, 0)
- // ==========================4========================
- paramslist = strings.Split(p3, ",")
- if len(paramslist) > 0 && p3 != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- _, err = tx.SQL(sql3, s_params...).Execute()
- if err != nil {
- logging.Error("PostRJSBData3 err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- appG.Response(http.StatusOK, e.SUCCESS, "ok")
- }
- func PostRJSBDatas(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsions := gofasion.NewFasion(string(dataByte)).Array()
- logging.Info("PostRJSBDatas err: ", string(dataByte))
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
- sql1, p1 := restful.GetSqlByNameDBT("updated1YJV2", tx)
- sql2, p2 := restful.GetSqlByNameDBT("updated1execYJV2", tx)
- sql3, p3 := restful.GetSqlByNameDBT("updated2YJV2", tx)
- for _, fsion := range fsions {
- if !fsion.HasKey("parammaps") {
- appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
- return
- }
- parammaps := fsion.Get("parammaps")
- s_params := make([]interface{}, 0)
- // ==========================1========================
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- fmt.Println("}}}}}}}}}}}}}}}}}}}",s_params)
- _, err = tx.SQL(sql, s_params...).Execute()
- if err != nil {
- fmt.Println(err)
- logging.Error("PostRJSBData err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- // ==========================2========================
- s_params = make([]interface{}, 0)
- paramslist = strings.Split(p1, ",")
- if len(paramslist) > 0 && p1 != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- _, err = tx.SQL(sql1, s_params...).Execute()
- if err != nil {
- logging.Error("PostRJSBData1 err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- s_params = make([]interface{}, 0)
- // ==========================3========================
- paramslist = strings.Split(p2, ",")
- if len(paramslist) > 0 && p2 != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- _, err = tx.SQL(sql2, s_params...).Execute()
- if err != nil {
- logging.Error("PostRJSBData2 err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- s_params = make([]interface{}, 0)
- // ==========================4========================
- paramslist = strings.Split(p3, ",")
- if len(paramslist) > 0 && p3 != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- _, err = tx.SQL(sql3, s_params...).Execute()
- if err != nil {
- logging.Error("PostRJSBData3 err: ", err)
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- return
- }
- }
- appG.Response(http.StatusOK, e.SUCCESS, "ok")
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags ExecDataByConfig
- // @Accept json
- // @Produce json
- // @Param sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/PostDataByName [POST]
- func ExecDataByConfig(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- tempval_ := make(map[string]interface{})
- tempCommon := make(map[string]interface{})
- tempval := make([]map[string]interface{}, 0)
- err := json.Unmarshal(dataByte, &tempval_)
- logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
- if err != nil {
- fmt.Println("error:", err)
- } else {
- if v, ok := tempval_["common"].(map[string]interface{}); ok {
- tempCommon = v
- }
- if v, ok := tempval_["data"].([]interface{}); ok {
- for _, Tvalue := range v {
- if v1, ok := Tvalue.(map[string]interface{}); ok {
- tempval = append(tempval, v1)
- }
- }
- }
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- if err != nil {
- logging.Error("tx.Begin 事务启动失败__error:", err)
- }
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- if err == nil {
- if tempv, exists := c.Get("jwt_username"); exists {
- tempCommon["jwt_username"] = tempv.(string)
- }
- for _, paramvalue := range tempval {
- if _, ok := paramvalue["resultname"]; !ok {
- paramvalue["resultname"] = paramvalue["name"]
- }
- _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil, tx)
- if err != nil {
- logging.Error("ExecDataParam error:", err)
- break
- }
- }
- if err == nil {
- if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
- appG.Response(http.StatusOK, e.SUCCESS, tempval)
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, "成功执行!")
- }
- } else {
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- //appG.Response(http.StatusOK, e.ERROR, err.Error())
- }
- }
- }
- if err != nil {
- msg := geterrmsg(err.Error())
- appG.Response(http.StatusOK, e.ERROR, msg)
- //appG.Response(http.StatusOK, e.ERROR, err.Error())
- }
- }
- func interfacetostring(val interface{}) string {
- switch val.(type) {
- case string:
- ss := val.(string)
- return ss
- break
- case int:
- ss := val.(int)
- return strconv.Itoa(ss)
- break
- case int64:
- ss := val.(int64)
- return strconv.FormatInt(ss, 10)
- break
- case float64:
- ss := val.(float64)
- return strconv.FormatFloat(ss, 'E', -1, 32)
- break
- }
- return ""
- }
- func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
- if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
- tempstrb := strings.IndexAny(valuestring, "@")
- tempstre := strings.IndexAny(valuestring, ".")
- if tempstrb > -1 && tempstre > -1 {
- for _, ParamMapList := range ParamMap {
- if v, ok := ParamMapList["resultname"]; ok {
- if v == valuestring[tempstrb+1:tempstre] {
- if v1, ok := ParamMapList["resultmaps"]; ok {
- if newA, ok := v1.([]map[string]interface{}); ok {
- return interfacetostring(newA[0][valuestring[tempstre+1:]])
- } else if newA, ok := v1.(map[string]interface{}); ok {
- return interfacetostring(newA[valuestring[tempstre+1:]])
- }
- }
- }
- }
- }
- } else {
- return valuestring
- }
- } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
- return strconv.FormatInt(int64(valuestring), 10)
- }
- return ""
- }
- func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
- if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
- tempstrb := strings.IndexAny(valuestring, "@")
- tempstre := strings.IndexAny(valuestring, ".")
- if tempstrb > -1 && tempstre > -1 {
- if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
- if s, ok := v.(string); ok {
- return s
- } else if s, ok := v.(float64); ok {
- return strconv.FormatInt(int64(s), 10)
- }
- }
- }
- }
- return ""
- }
- func ExecDataParam(fasion map[string]interface{}, CommonParamMap map[string]interface{}, ParamMap []map[string]interface{}, ParentParamMap map[string]interface{}, sameParamMap []map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
- sqlname := fasion["name"].(string)
- sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
- if v, ok := fasion["type"]; ok {
- sqltype = v.(string)
- }
- sql := ""
- p := ""
- s_params := make([]interface{}, 0)
- if sqltype != "" {
- sql, p = restful.GetSqlByName(sqlname, tx)
- //sqlApi, _ := models.GetApisqlByName(sqlname)
- if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- ss := ""
- if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- s_params = append(s_params, ids)
- } else {
- if ss == "" && CommonParamMap != nil {
- if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
- ss = valuestring
- tempstrb := strings.IndexAny(valuestring, "@")
- tempstre := strings.IndexAny(valuestring, ".")
- if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
- if CommonParamMap[valuestring[tempstre+1:]] != nil {
- ss = CommonParamMap[valuestring[tempstre+1:]].(string)
- }
- } else {
- ss = ""
- }
- }
- }
- if ss == "" && ParentParamMap != nil {
- ss = getParamValue_(v, value, ParentParamMap)
- }
- if ss == "" && sameParamMap != nil {
- ss = getParamValue(v, value, sameParamMap)
- }
- if ss == "" && ParamMap != nil {
- ss = getParamValue(v, value, ParamMap)
- }
- s_params = append(s_params, ss)
- }
- }
- }
- }
- }
- if sql == "" {
- sqltype = ""
- }
- fmt.Printf("%v+ \n", s_params, sqlname)
- if sqltype == "s1" {
- tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
- if v, ok := tempValParam.(map[string]interface{}); ok {
- fasion["resultmaps"] = v["list"].([]map[string]interface{})
- }
- if err != nil {
- return nil, err
- }
- err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
- if err != nil {
- return nil, err
- }
- return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
- } else if sqltype == "sm" {
- tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
- if v, ok := tempValParam.(map[string]interface{}); ok {
- fasion["resultmaps"] = v["list"].([]map[string]interface{})
- } else {
- fmt.Println("sm", tempValParam)
- }
- if err != nil {
- return nil, err
- }
- err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
- if err != nil {
- return nil, err
- }
- return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
- } else if sqltype == "v" {
- tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
- if v, ok := tempValParam.(map[string]interface{}); ok {
- fasion["resultmaps"] = v["list"].([]map[string]interface{})
- } else {
- fmt.Println("v", tempValParam)
- }
- if err != nil {
- return nil, err
- }
- if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
- return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
- }
- } else if sqltype == "e" {
- tempVal_arrary := make([]map[string]interface{}, 0)
- tempVal_map := make(map[string]interface{})
- tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
- if err == nil {
- fasion["resultmaps"] = tempValParam.(map[string]interface{})
- tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
- tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
- tempVal_arrary = append(tempVal_arrary, tempVal_map)
- return tempVal_arrary, err
- }
- return nil, err
- } else {
- if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
- err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
- if err != nil {
- return nil, err
- }
- return nil, err
- }
- }
- return nil, nil
- }
- func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *xorm.Session) error {
- var err error
- subarray, ok := fasion["children"]
- if ok {
- for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
- tempVal_arrary := make([]map[string]interface{}, 0)
- for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
- tempVal_ := make(map[string]interface{})
- if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
- tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
- } else {
- tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
- }
- tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
- if err != nil {
- return err
- }
- tempVal_arrary = append(tempVal_arrary, tempVal_)
- }
- v.(map[string]interface{})["children"] = tempVal_arrary
- }
- }
- return nil
- }
- // @Summary 删除图片
- // @Tags RemovePicByName
- // @Accept json
- // @Produce json
- // @Param sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/PostDataByName [POST]
- func RemovePicByName(c *gin.Context) {
- logging.Info("RemovePicByName ", c.Keys)
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- picname := fsion.Get("picname").ValueStr()
- sql, _ := restful.GetSqlByNameDB(sqlnamestr)
- params := fsion.Get("params").Array()
- s_params := make([]interface{}, 0)
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- if sql != "" {
- queryData, err := execDataBySql(sql, s_params)
- if picname != "" {
- // Destination
- distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
- err := os.Remove(distPath) //删除文件test.txt
- if err != nil {
- //如果删除失败则输出 file remove Error!
- fmt.Println("file remove Error!")
- //输出错误详细信息
- fmt.Printf("%s", err)
- } else {
- //如果删除成功则输出 file remove OK!
- fmt.Print("file remove OK!")
- }
- distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
- err = os.Remove(distPath) //删除文件test.txt
- if err != nil {
- //如果删除失败则输出 file remove Error!
- fmt.Println("file remove Error!")
- //输出错误详细信息
- fmt.Printf("%s", err)
- } else {
- //如果删除成功则输出 file remove OK!
- fmt.Print("file remove OK!")
- }
- }
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- // @Summary 删除文件
- // @Tags RemoveFileByName
- // @Accept json
- // @Produce json
- // @Param sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/PostDataByName [POST]
- func RemoveFileByName(c *gin.Context) {
- appG := app.Gin{C: c}
- id := c.Param("id")
- logging.Info("DownloadFile ", c.Keys, id)
- eqdoc, err := restful.Engine.SQL("SELECT * FROM eq_doc where id = ? ", id).QueryString()
- if eqdoc == nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- return
- }
- distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqdoc[0]["docpath"], eqdoc[0]["newdocname"])
- err = os.Remove(distPath) //删除文件test.txt
- if err != nil {
- //如果删除失败则输出 file remove Error!
- fmt.Println("file remove Error!")
- //输出错误详细信息
- fmt.Printf("%s", err)
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- return
- }
- fmt.Print("file remove OK!")
- _, err = restful.Engine.SQL("delete FROM eq_doc where id = ? ", id).QueryString()
- appG.Response(http.StatusOK, e.SUCCESS, "OK")
- //dataByte, _ := ioutil.ReadAll(c.Request.Body)
- //fsion := gofasion.NewFasion(string(dataByte))
- //sqlnamestr := fsion.Get("name").ValueStr()
- //filename := fsion.Get("filename").ValueStr()
- //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
- //params := fsion.Get("params").Array()
- //s_params := make([]interface{}, 0)
- //for _, v_params := range params {
- // s_params = append(s_params, v_params.ValueStr())
- //}
- //if sql != "" {
- // queryData, err := execDataBySql(sql, s_params)
- //
- // if filename != "" {
- // // Destination
- // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
- //
- // err := os.Remove(distPath) //删除文件test.txt
- // if err != nil {
- // //如果删除失败则输出 file remove Error!
- // fmt.Println("file remove Error!")
- // //输出错误详细信息
- // fmt.Printf("%s", err)
- // } else {
- // //如果删除成功则输出 file remove OK!
- // fmt.Print("file remove OK!")
- // }
- // }
- // if err != nil {
- // appG.Response(http.StatusOK, e.ERROR, err.Error())
- // } else {
- // appG.Response(http.StatusOK, e.SUCCESS, "OK")
- // }
- //} else {
- // appG.Response(http.StatusOK, e.SUCCESS, nil)
- //}
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags PostDataByName
- // @Accept json
- // @Produce json
- // @Param sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/PostDataByName [POST]
- func UpdateDataRelation(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- tablename := fsion.Get("name").ValueStr()
- dataname := fsion.Get("dataname").ValueStr()
- datavalue := fsion.Get("datavalue").ValueStr()
- valuename := fsion.Get("valuename").ValueStr()
- logging.Info("UpdateDataRelation ", c.Keys, c.Request.RemoteAddr, tablename, dataname, datavalue, valuename)
- values := fsion.Get("values").Array()
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
- " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
- d_params := make([]interface{}, 4)
- d_params[0] = datavalue
- s_params := ""
- for _, v_values := range values {
- d_params[1] = v_values.ValueStr()
- if s_params == "" {
- s_params = v_values.ValueStr()
- } else {
- s_params = s_params + "," + v_values.ValueStr()
- }
- d_params[2] = d_params[1]
- d_params[3] = d_params[0]
- _, err := execDataBySqlT(insertSqlstr, d_params, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- break
- }
- }
- if s_params != "" {
- deleteSqlstr := "delete from " + tablename + " " +
- " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
- queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else if err == nil {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- } else {
- deleteSqlstr := "delete from " + tablename + " " +
- " where " + dataname + " = " + datavalue
- queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else if err == nil {
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- }
- }
- // @Summary 根据APISQL表中SQL执行得到数据集
- // @Tags PostDatasByName
- // @Accept json
- // @Produce json
- // @Param sqlname query string true "sqlname"
- // @Param offset query int true "offset"
- // @Param pagecount query int true "pagecount"
- // @Param returntype query string true "returntype"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/PostDatasByName [POST]
- func PostDataByNames(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlParams := fsion.Array()
- queryData := make(map[string]interface{})
- for _, v := range sqlParams {
- fsion2 := gofasion.NewFasion(v.Json())
- logging.Info("PostDataByNames ", c.Keys, fsion2.Get("name").ValueStr())
- sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
- if sql == "" {
- errorm := make(map[string]interface{})
- errorm["code"] = e.ERROR
- errorm["message"] = "sql没有找到"
- errorm["param"] = ""
- queryData[fsion2.Get("name").ValueStr()] = errorm
- continue
- } else if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion2.HasKey("parammaps") {
- parammaps := fsion2.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- s_params = append(s_params, ids)
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- } else if fsion2.HasKey("params") {
- params := fsion2.Get("params").Array()
- //ids,err := setting.SnowIds.NextId()
- //if err !=nil{
- // ids = time.Now().UnixNano()
- // logging.Info("create SnowIds err",err)
- //}
- //s_params = append(s_params, ids)
- for _, v_params := range params {
- s_params = append(s_params, v_params.ValueStr())
- }
- }
- _, err := execDataBySql(sql, s_params)
- if err != nil {
- logging.Error("PostDataByName err: ", err)
- errorm := make(map[string]interface{})
- errorm["code"] = e.ERROR
- errorm["message"] = err.Error()
- errorm["param"] = s_params
- queryData[fsion2.Get("name").ValueStr()] = errorm
- continue
- } else {
- errorm := make(map[string]interface{})
- errorm["code"] = e.SUCCESS
- errorm["message"] = "success"
- queryData[fsion2.Get("name").ValueStr()] = errorm
- continue
- }
- } else {
- errorm := make(map[string]interface{})
- errorm["code"] = e.ERROR
- errorm["message"] = "NO SQL"
- errorm["param"] = ""
- queryData[fsion2.Get("name").ValueStr()] = errorm
- }
- }
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
- if offset > 0 {
- offset = offset - 1
- }
- //if pagecount == 0 {
- // pagecount = setting.AppSetting.PageSize
- //}else if pagecount>setting.AppSetting.PageSize{
- // pagecount = setting.AppSetting.PageSize
- //}
- if returntype == "list" {
- queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
- return queryData, err
- } else if returntype == "listlist" {
- queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
- return queryData, err
- } else {
- queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
- return queryData, err
- }
- }
- func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *xorm.Session) (interface{}, error) {
- if offset > 0 {
- offset = offset - 1
- }
- //if pagecount == 0 {
- // pagecount = setting.AppSetting.PageSize
- //}else if pagecount>setting.AppSetting.PageSize{
- // pagecount = setting.AppSetting.PageSize
- //}
- if returntype == "list" {
- queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx)
- return queryData, err
- } else if returntype == "listlist" {
- queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx)
- return queryData, err
- } else {
- queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx)
- return queryData, err
- }
- }
- func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
- paramslist := strings.Split(Params, ",")
- s_params := make([]interface{}, 0)
- if isList {
- for _, v_params := range thisParams["params"].([]interface{}) {
- s_params = append(s_params, v_params)
- }
- } else {
- paramslist := strings.Split(Params, ",")
- if len(paramslist) > 0 && Params != "" {
- for _, value := range paramslist {
- s_params = append(s_params, thisParams[strings.Trim(value, " ")])
- }
- }
- }
- queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
- for _, queryData_ := range queryData {
- if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
- if isList {
- //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
- if queryData_[idname] != nil {
- thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
- }
- } else {
- if queryData_[idname] != nil {
- thisParams[idname] = queryData_[idname].(interface{})
- }
- if len(paramslist) > 0 && Params != "" {
- s_params = make([]interface{}, 0)
- for _, value := range paramslist {
- s_params = append(s_params, thisParams[strings.Trim(value, " ")])
- }
- }
- }
- queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
- } else {
- }
- }
- return queryData, err
- }
- func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
- queryData, err := restful.ExecQuery(sqlstr, params)
- return queryData, err
- }
- func execDataBySqlT(sqlstr string, params []interface{}, tx *xorm.Session) (interface{}, error) {
- queryData, err := restful.ExecQueryT(sqlstr, params, tx)
- return queryData, err
- }
- func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
- queryData, err := restful.ExecQuery(sqlstr, params)
- return queryData, err
- }
- // @Summary 导入excel文件到数据库,excel第一行作为参数
- // @Tags ImportExcel
- // @Accept json
- // @Produce json
- // @Param file query string true "文件名"
- // @Param name header string true "执行sql的名字"
- // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
- // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
- // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
- // @Router /data/ImportExcel [POST]
- func ImportExcel(c *gin.Context) {
- appG := app.Gin{C: c}
- inputdata := ""
- c.Request.ParseForm()
- //tempv1, _ := c.Get("jwt_username")
- //fmt.Println("=======================",tempv1)
- params := c.Request.Form
- file, _, err := c.Request.FormFile("file")
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err)
- return
- }
- sqlnamestrs := params.Get("name")
- sheetname := params.Get("sheetname")
- pastureid := params.Get("pastureid")
- //importParams := params.Get("importParams")
- logging.Info("ImportExcel ", c.Keys, c.Request.RemoteAddr, sqlnamestrs)
- //paramslist := strings.Split(importParams, ",")
- //paramslist = append(paramslist, "jwt_username")
- requiredParams := params.Get("requiredParams")
- dateParams := params.Get("dateParams")
- numParams := params.Get("numParams")
- dateParamslist := strings.Split(dateParams, ",")
- requiredParamslist := strings.Split(requiredParams, ",")
- numParamslist := strings.Split(numParams, ",")
- queryData := make([]interface{}, 0)
- sqlslist := strings.Split(sqlnamestrs, ",")
- xlsx1, err := excelize.OpenReader(file)
- if err != nil {
- fmt.Println(err)
- return
- }
- ok := 0
- error := 0
- isdone := true
- rows, _ := xlsx1.GetRows(sheetname)
- if len(rows) > 10000 { // 限制1000条
- rows = rows[:10000]
- }
- for i, row := range rows {
- if i > 0 {
- entry := make(map[string]interface{})
- outentry := make(map[string]interface{})
- for j, colCell := range rows[0] {
- for k, colCell1 := range row {
- if j == k {
- outentry[colCell] = colCell1
- entry[colCell] = colCell1
- }
- }
- }
- inputdata = ""
- a := ""
- for _, v := range dateParamslist {
- if v == ""{
- continue
- }
- if d, ok := outentry[strings.Trim(v, " ")]; ok {
- dtime,err := TransfTime(d.(string))
- if err != nil {
- a = strings.Trim(v, " ")
- break
- }
- outentry[strings.Trim(v, " ")] = dtime
- }
- }
- if a != "" {
- outentry["error_msg"] = a + " 格式错误"
- error++
- queryData = append(queryData, outentry)
- continue
- }
- a = ""
- for _, v := range requiredParamslist {
- if v == ""{
- continue
- }
- if d, ok := outentry[strings.Trim(v, " ")]; !ok || d == "" {
- a = strings.Trim(v, " ")
- break
- }
- }
- if a != "" {
- outentry["error_msg"] = a + " 未填或格式错误"
- error++
- queryData = append(queryData, outentry)
- continue
- }
- a = ""
- for _, v := range numParamslist {
- if v == ""{
- continue
- }
- d, ok := outentry[strings.Trim(v, " ")]
- if !ok || d == "" {
- outentry[strings.Trim(v, " ")] = 0
- }else {
- var fl float64
- switch d.(type) {
- case string :
- fl,err = strconv.ParseFloat(d.(string),64)
- }
- if err != nil || fl<0 {
- a = strings.Trim(v, " ")
- break
- }
- }
- }
- if a != "" {
- outentry["error_msg"] = a + " 为数值类型且不可为负数"
- error++
- queryData = append(queryData, outentry)
- continue
- }
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- for _, sqlnamestr := range sqlslist {
- sql, paramslists := restful.GetSqlByNameDB(sqlnamestr)
- paramslist := strings.Split(paramslists, ",")
- if len(paramslist) > 0 {
- s_params := make([]interface{}, 0)
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- }else if strings.ToLower(strings.Trim(value, " ")) == "pastureid"{
- s_params = append(s_params, pastureid)
- } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- s_params = append(s_params, ids)
- }else {
- if outentry[strings.Trim(value, " ")] == nil {
- s_params = append(s_params,"")
- }else{
- s_params = append(s_params, outentry[strings.Trim(value, " ")])
- }
- }
- }
- //fmt.Println("================",s_params)
- outentry["input"] = inputdata
- returnmsgmap1, err := tx.SQL(sql, s_params...).Query().List()
- if err != nil {
- if tx != nil {
- tx.Rollback()
- }
- if tx != nil {
- tx.Close()
- }
- msg := geterrmsg(err.Error())
- outentry["error_msg"] = msg
- error++
- queryData = append(queryData, outentry)
- isdone = true
- break
- } else{
- if len(returnmsgmap1) > 0 {
- if va,ok :=returnmsgmap1[0]["msg"] ; ok && va != "" {
- if tx != nil {
- tx.Rollback()
- }
- if tx != nil {
- tx.Close()
- }
- msg := geterrmsg(va.(string))
- outentry["error_msg"] = msg
- error++
- queryData = append(queryData, outentry)
- isdone = true
- break
- }
- }
- isdone = false
- }
- }
- }
- switch {
- case err != nil:
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }
- if !isdone{
- ok++
- isdone = true
- }
- }
- data := make(map[string]interface{})
- data["result"] = queryData
- data["success"] = ok
- data["err_count"] = error
- appG.Response(http.StatusOK, e.SUCCESS, data)
- }
- func TransfTime(timestr string) (string,error) {
- if timestr == "" {
- return time.Now().Format("2006-01-02"),nil
- }
- days, err := strconv.Atoi(timestr)
- if err != nil {
- if len(timestr) == 8 && strings.Count(timestr, "-") == 2 {
- return "20" + timestr[6:] + "-" + timestr[:5],nil
- }
- _,err :=time.Parse("2006-01-02",timestr)
- if err != nil{
- t,err :=time.Parse("2006/01/02",timestr)
- if err != nil{
- t,err :=time.Parse("2006.01.02",timestr)
- if err != nil {
- return "",err
- }else {
- return t.Format("2006-01-02"),nil
- }
- }else {
- return t.Format("2006-01-02"),nil
- }
- }else {
- return timestr,nil
- }
- }
- times, _ := time.Parse("2006-01-02", "1899-12-30")
- times = times.AddDate(0, 0, days)
- return times.Format("2006-01-02"),nil
- }
- func Createchart(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- chart_name := fsion.Get("chart_name").ValueStr()
- desc := fsion.Get("desc").ValueStr()
- pastureid := fsion.Get("pastureid").ValueStr()
- source_id := fsion.Get("source_id").ValueStr()
- emp := fsion.Get("emp").ValueStr()
- content := fsion.Get("content").Json()
- //fmt.Println("=============",parammaW.Json())
- var mapResult map[string]interface{}
- err1 := json.Unmarshal([]byte(content), &mapResult)
- if err1 != nil {
- fmt.Println("JsonToMapDemo err: ", err1)
- }
- //fmt.Println("---------",mapResult)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- logging.Error("tx.Begin 事务启动失败__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- exec, err := tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
- ids, pastureid, chart_name, 0, emp, desc, source_id)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, exec)
- }
- }
- func Updatedashboard(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- dashboard_id := fsion.Get("dashboard_id").ValueStr()
- pastureid := fsion.Get("pastureid").ValueStr()
- content := fsion.Get("content").Json()
- //fmt.Println("=============",parammaW.Json())
- var mapResult map[string]interface{}
- err1 := json.Unmarshal([]byte(content), &mapResult)
- if err1 != nil {
- fmt.Println("JsonToMapDemo err: ", err1)
- }
- layout := mapResult["layout"]
- fmt.Println("---------", layout)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- logging.Error("tx.Begin 事务启动失败__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //fmt.Println("sql========\n",sqlnamestr,sql)
- exec, err := tx.SQL("select content from dashboard where pastureid=? and id=?", pastureid, dashboard_id).Query().List()
- if err != nil {
- println("2", err.Error())
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- if exec != nil {
- if exec[0]["content"] == nil {
- mapR := make(map[string]interface{})
- mapR["layout"] = layout
- mjson, _ := json.Marshal(mapR)
- mString := string(mjson)
- exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
- if err3 != nil {
- println("3", err3.Error())
- appG.Response(http.StatusOK, e.ERROR, err3.Error())
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, exec1)
- }
- } else {
- var mapR map[string]interface{}
- err2 := json.Unmarshal([]byte(exec[0]["content"].(string)), &mapR)
- if err1 != nil {
- fmt.Println("JsonToMapDemo err: ", err2)
- }
- mapR["layout"] = layout
- mjson, _ := json.Marshal(mapR)
- mString := string(mjson)
- exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
- if err3 != nil {
- println("3", err3.Error())
- appG.Response(http.StatusOK, e.ERROR, err3.Error())
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, exec1)
- }
- }
- } else {
- appG.Response(http.StatusOK, e.ERROR, "未找到对应的仪表盘")
- }
- }
- }
- func ExecSQL(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- content := fsion.Get("content").Json()
- pastureid := fsion.Get("pastureid").ValueStr()
- //pastureid := fsion.Get("pastureid").ValueStr()
- //println(pastureid,"==================")
- //fmt.Println("=============",parammaW.Json())
- var mapResult map[string]interface{}
- err1 := json.Unmarshal([]byte(content), &mapResult)
- if err1 != nil {
- fmt.Println("JsonToMapDemo err: ", err1)
- }
- //fmt.Println("---------",mapResult)
- sqlstr := "select "
- groupstr := " GROUP BY "
- for _, value := range mapResult["selectedDimension"].([]interface{}) {
- sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
- groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
- }
- for _, value := range mapResult["selectedCalcul"].([]interface{}) {
- if value.(map[string]interface{})["calculFunc"].(string) == "none" {
- sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
- groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
- } else {
- tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
- sqlstr = sqlstr + tem + ","
- }
- }
- if _, ok := mapResult["selectedCalcul2"]; ok {
- for _, value := range mapResult["selectedCalcul2"].([]interface{}) {
- if value.(map[string]interface{})["calculFunc"].(string) == "none" {
- sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
- groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
- } else {
- tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
- sqlstr = sqlstr + tem + ","
- }
- }
- }
- //
- sqlstr = sqlstr[0 : len(sqlstr)-1]
- sqlstr = sqlstr + " from " + mapResult["dataSrc"].(string)
- //if len(mapResult["filters"].([]interface {}))>0{
- sqlstr = sqlstr + " where pastureid=" + pastureid
- //}
- //
- if len(mapResult["filters"].([]interface{})) > 0 {
- sqlstr = sqlstr + " and "
- }
- println(len(mapResult["filters"].([]interface{})),sqlstr)
- for _, value := range mapResult["filters"].([]interface{}) {
- switch value.(map[string]interface{})["filterOperator"].(string) {
- case ">", "=", "<", ">=", "<=", "!=", "like":
- sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + value.(map[string]interface{})["filterOperator"].(string) + " " + value.(map[string]interface{})["value"].(map[string]interface{})["value1"].(string) + " AND "
- case "IN":
- sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + " " + value.(map[string]interface{})["filterOperator"].(string) + "("
- for _, value1 := range value.(map[string]interface{})["value"].(map[string]interface{})["arrValue"].([]interface{}) {
- sqlstr = sqlstr + value1.(string) + ","
- }
- sqlstr = sqlstr[0 : len(sqlstr)-1]
- sqlstr = sqlstr + ")" + " AND "
- case "BETWEEN":
- sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + " " + value.(map[string]interface{})["filterOperator"].(string) + " " + value.(map[string]interface{})["value"].(map[string]interface{})["value1"].(string) + " and " + value.(map[string]interface{})["value"].(map[string]interface{})["value2"].(string) + " AND "
- }
- }
- if len(mapResult["filters"].([]interface{})) > 0 {
- sqlstr = sqlstr[0 : len(sqlstr)-4]
- }
- //
- groupstr = groupstr[0 : len(groupstr)-1]
- sqlstr = sqlstr + groupstr + " "
- if len(mapResult["orderByStrs"].([]interface{})) > 0 {
- sqlstr = sqlstr + " order by "
- }
- //
- for key, value := range mapResult["orderByStrs"].([]interface{}) {
- if key == len(mapResult["orderByStrs"].([]interface{}))-1 {
- sqlstr = sqlstr + value.(string) + " "
- } else {
- sqlstr = sqlstr + value.(string) + ","
- }
- }
- //
- sqlstr = sqlstr + " limit " + strconv.Itoa(int(mapResult["limit"].(float64)))
- //fmt.Println("====++---------",sqlstr)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- logging.Error("tx.Begin 事务启动失败__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- //fmt.Println("sql========\n",sqlnamestr,sql)
- //ids,err := setting.SnowIds.NextId()
- //if err !=nil{
- // ids = time.Now().UnixNano()
- // logging.Info("create SnowIds err",err)
- //}
- // exec,err :=tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
- //ids,pastureid,chart_name,0,emp,desc,source_id)
- exec, err := tx.SQL(sqlstr).Query().List()
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, exec)
- }
- }
- func Dashboards(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := fsion.Get("name").ValueStr()
- //sqlnamestr1 := fsion.Get("name1").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- month := fsion.Get("month").ValueDefaultStr("map")
- logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
- tx := restful.Engine.NewSession()
- err := tx.Begin()
- defer func() {
- switch {
- case err != nil:
- fmt.Println("__error:", err)
- if tx != nil {
- tx.Rollback()
- }
- default:
- if tx != nil {
- err = tx.Commit()
- }
- }
- if tx != nil {
- tx.Close()
- }
- }()
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- //sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if tempv, exists := c.Get("jwt_username"); exists {
- s_params = append(s_params, tempv.(string))
- } else {
- s_params = append(s_params, "")
- }
- } else {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- }
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, err.Error())
- } else {
- //paramslist1 := strings.Split(p1, ",")
- dids := make([]interface{}, 0)
- for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
- var mapResult map[string]interface{}
- dids = append(dids, value["dashboard_id"])
- if value["content"] != nil {
- err1 := json.Unmarshal([]byte(value["content"].(string)), &mapResult)
- if err1 != nil {
- println("JsonToMapDemo1 err: ", err1)
- }
- //fmt.Printf("%v+\n",value["content"].(string))
- temap := make(map[string]interface{})
- tem := mapResult["layout"].([]interface{})
- if v, ok := value["chartids"]; ok {
- chartids := strings.Split(v.(string), ",")
- for _, va := range chartids {
- a := false
- for _, v2 := range tem {
- if v2.(map[string]interface{})["id"].(string) == va {
- a = true
- //println("va1======",va)
- }
- }
- if !a {
- laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
- va + "\",\"id\":\"" + va +
- "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":100,\"yOffSet\":18}"
- var mapTem map[string]interface{}
- err1 := json.Unmarshal([]byte(laytem), &mapTem)
- if err1 != nil {
- println("JsonToMapDemo err: ", err1)
- }
- tem = append(tem, mapTem)
- }
- }
- temap["layout"] = tem
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
- }
- } else {
- temap := make(map[string]interface{})
- tem := make([]interface{}, 0)
- if v, ok := value["chartids"]; ok {
- chartids := strings.Split(v.(string), ",")
- for _, va := range chartids {
- laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
- va + "\",\"id\":\"" + va +
- "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":0,\"yOffSet\":18}"
- var mapTem map[string]interface{}
- err1 := json.Unmarshal([]byte(laytem), &mapTem)
- if err1 != nil {
- println("JsonToMapDemo err: ", err1)
- }
- tem = append(tem, mapTem)
- }
- temap["layout"] = tem
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
- }
- }
- }
- queryData.(map[string]interface{})["order"] = dids
- queryData.(map[string]interface{})["dashboards"] = queryData.(map[string]interface{})["list"]
- appG.Response(http.StatusOK, e.SUCCESS, queryData)
- }
- } else {
- appG.Response(http.StatusOK, e.SUCCESS, nil)
- }
- }
- func HttpPostData(url, RequestMethod, parammaps string, tx *xorm.Session) string {
- fsion := gofasion.NewFasion(parammaps)
- sqlnamestr := fsion.Get("name").ValueStr()
- sqlnamestr1 := fsion.Get("name1").ValueStr()
- offset := fsion.Get("offset").ValueDefaultInt(0)
- pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
- returntype := fsion.Get("returntype").ValueDefaultStr("map")
- sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
- sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
- body := []byte("")
- if sql != "" {
- s_params := make([]interface{}, 0)
- if fsion.HasKey("parammaps") {
- parammaps := fsion.Get("parammaps")
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
- }
- }
- }
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
- if err != nil {
- } else {
- paramslist1 := strings.Split(p1, ",")
- for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
- s_params1 := make([]interface{}, 0)
- if len(paramslist1) > 0 && p1 != "" {
- for _, v := range paramslist1 {
- s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
- }
- }
- arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
- queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
- body, _ = json.Marshal(queryData)
- }
- }
- }
- bs := bytes.NewBuffer(body)
- client := &http.Client{}
- //body1 := bytes.NewBuffer([]byte("{\"username\":\"00028\",\"password\":\"123456\"}"))
- req, err := http.NewRequest(RequestMethod,
- url,
- bs,
- )
- if err != nil {
- }
- req.Header.Set("Content-Type", "application/json;charset=utf-8")
- req.Header.Set("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjAwMDI4IiwicGFzc3dvcmQiOiJlMTBhZGMzOTQ5YmE1OWFiYmU1NmUwNTdmMjBmODgzZSIsImV4cCI6MTYxOTM4NjU0MiwiaXNzIjoiaHR0cHM6Ly9naXRodWIuY29tL2twdHl1bi9nby1hZG1pbi8ifQ.sJwnE9HTOVnDNOaeHqOQzi7cSydYkWS4uQHxtjY7UMY")
- if err != nil {
- // handle error
- }
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- return ""
- }
- defer resp.Body.Close()
- result_body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println(err)
- return ""
- }
- fmt.Println(string(result_body))
- return ""
- }
|