123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028 |
- package data_manage
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services/data"
- "eta/eta_api/services/elastic"
- etaTrialService "eta/eta_api/services/eta_trial"
- "eta/eta_api/utils"
- "fmt"
- "os"
- "strconv"
- "strings"
- "time"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/shopspring/decimal"
- )
- // ManualEdbController 手工指标服务(鉴权)
- type ManualEdbController struct {
- controllers.BaseAuthController
- }
- // EdbDetail
- // @Title 指标列表
- // @Description 指标列表
- // @Param TradeCode query string true "指标编码"
- // @Success 200 {object} models.TargetDetailResp
- // @router /target/edb/detail [get]
- func (c *ManualEdbController) EdbDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- // 指标编码
- tradeCode := c.GetString("TradeCode")
- if tradeCode == `` {
- br.Msg = "请输入指标编码"
- br.IsSendEmail = false
- return
- }
- manualEdbInfo, err := models.GetTargetByTradeCode(tradeCode)
- if err != nil {
- br.Msg = "获取指标失败"
- br.ErrMsg = "获取指标失败,err:" + err.Error()
- return
- }
- // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- //获取账户所拥有权限的分类id集合
- count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.ErrMsg = "获取分类数据失败,err:" + err.Error()
- return
- }
- if count <= 0 {
- br.Msg = "无权访问"
- br.ErrMsg = "无权访问"
- br.IsSendEmail = false
- return
- }
- }
- classifyList := make([]*models.EdbdataClassify, 0)
- {
- classify, err := models.GetManualClassifyByClassifyId(manualEdbInfo.ClassifyId)
- if err != nil {
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取分类信息失败,err:" + err.Error()
- return
- }
- parentClassify, err := models.GetManualClassifyByClassifyId(classify.ParentId)
- if err != nil {
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取父级分类信息失败,err:" + err.Error()
- return
- }
- classifyList = []*models.EdbdataClassify{
- parentClassify, classify,
- }
- }
- // 明细数据
- {
- dataList, err := models.GetEdbDataListByCode(manualEdbInfo.TradeCode)
- if err != nil {
- br.Msg = "获取明细数据失败"
- br.ErrMsg = "获取明细数据失败,err:" + err.Error()
- return
- }
- // 获取下期时间 -- 不用了,由前端处理
- /*dataNextDateTime, err := fillDataNextDateTime(dataList)
- if err != nil {
- return
- }*/
- manualEdbInfo.DataList = dataList
- }
- resp := models.TargetDetailResp{
- Detail: manualEdbInfo,
- ClassifyList: classifyList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- //br.Data = resp
- br.Data = resp
- }
- // 封装指标数据下期时间
- func fillDataNextDateTime(list []*models.Edbdata) ([]*models.Edbdata, error) {
- if len(list) == 0 {
- return nil, nil
- }
- nextDataDateTime := models.Edbdata{}
- nextDataDateTime.TradeCode = list[0].TradeCode
- indexCodes := []string{list[0].TradeCode}
- edbinfoList, err := models.GetEdbinfoListByCodeListByCodeIdList(indexCodes)
- if err != nil {
- return nil, err
- }
- if len(edbinfoList) == 0 {
- return list, nil
- }
- date := utils.StrDateToDate(list[0].Dt)
- frequency := edbinfoList[0].Frequency
- switch frequency {
- case "日度":
- nextDataDateTime.Dt = date.AddDate(0, 0, 1).Format(utils.FormatDate)
- case "周度":
- nextDataDateTime.Dt = date.AddDate(0, 0, 7).Format(utils.FormatDate)
- case "旬度":
- nextDataDateTime.Dt = date.AddDate(0, 0, 10).Format(utils.FormatDate)
- case "月度":
- nextDataDateTime.Dt = date.AddDate(0, 1, 0).Format(utils.FormatDate)
- case "季度":
- nextDataDateTime.Dt = date.AddDate(0, 3, 0).Format(utils.FormatDate)
- case "半年度":
- nextDataDateTime.Dt = date.AddDate(0, 6, 0).Format(utils.FormatDate)
- case "年度":
- nextDataDateTime.Dt = date.AddDate(1, 0, 0).Format(utils.FormatDate)
- }
- // nextDataDateTime 添加到list第一个
- list = append([]*models.Edbdata{&nextDataDateTime}, list...)
- return list, nil
- }
- // ClassifyEdbList
- // @Title 分类指标列表
- // @Description 指标列表
- // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
- // @Param ClassifyId query string true "分类id"
- // @Success 200 {object} models.TargetDetailResp
- // @router /target/classify/edb/list [get]
- func (c *ManualEdbController) ClassifyEdbList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.Ret = 408
- return
- }
- resp := new(models.EdbdataClassifyResp)
- classifyId, _ := c.GetInt("ClassifyId") //分类
- if classifyId <= 0 {
- br.Msg = "请传入分类"
- br.ErrMsg = "请传入分类"
- br.IsSendEmail = false
- }
- // 频度
- frequency := c.GetString("Frequency")
- var condition string
- var pars []interface{}
- // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- //获取账户所拥有权限的分类id集合
- count, err := models.GetCountManualUserClassify(sysUser.AdminId, classifyId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.ErrMsg = "获取分类数据失败,err:" + err.Error()
- return
- }
- if count <= 0 {
- br.Msg = "无权访问"
- br.ErrMsg = "无权访问"
- br.IsSendEmail = false
- return
- }
- }
- list := make([]*models.EdbdataClassifyList, 0)
- condition += ` AND a.classify_id = ? `
- pars = append(pars, classifyId)
- if frequency != "" {
- condition += ` AND a.frequency = ?`
- pars = append(pars, frequency)
- }
- tmpList, err := models.GetEdbInfoList(condition, pars, 0, 0)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- for _, v := range tmpList {
- list = append(list, &models.EdbdataClassifyList{
- ClassifyId: 0,
- ClassifyName: v.SecName,
- ParentId: v.ClassifyId,
- Child: nil,
- TradeCode: v.TradeCode,
- UniqueCode: utils.MD5(v.TradeCode),
- })
- }
- resp.List = list
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // EdbSearch
- // @Title 指标搜索
- // @Description 指标搜索
- // @Param Keyword query string false "关键字搜索"
- // @Success 200 {object} models.TargetItemListResp
- // @router /target/edb/search [get]
- func (c *ManualEdbController) EdbSearch() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize15
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- resp := new(models.EdbListResp)
- //关键字
- keyword := c.GetString("Keyword")
- keyword = strings.TrimSpace(keyword)
- var condition string
- var pars []interface{}
- // es搜索
- highlightMap := make(map[string]string)
- if keyword != "" {
- _, list, e := elastic.SearchDataSourceIndex(utils.EsDataSourceIndexName, keyword, utils.DATA_SOURCE_MANUAL, 0, []int{}, []int{}, []string{}, startSize, pageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("ES-搜索手工指标列表失败, %v", e)
- return
- }
- if len(list) == 0 {
- resp.List = make([]*models.EdbInfoListItem, 0)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- var codes []string
- for _, v := range list {
- codes = append(codes, v.IndexCode)
- highlightMap[v.IndexCode] = v.SearchText
- }
- condition += fmt.Sprintf(` AND a.TRADE_CODE IN (%s)`, utils.GetOrmInReplace(len(list)))
- pars = append(pars, codes)
- }
- //userId := sysUser.AdminId
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyIdList)
- if num > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- //if keyword != "" {
- // condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- // pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- //}
- total, err := models.GetCountEdbInfoList(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- for _, v := range list {
- v.UniqueCode = utils.MD5(v.TradeCode)
- // ES搜索信息
- v.Source = utils.DATA_SOURCE_MANUAL
- v.SourceName = "手工指标录入"
- s := highlightMap[v.TradeCode]
- if s == "" {
- s = v.SecName
- }
- v.ClassifyUniqueCode = utils.MD5(strconv.Itoa(v.ClassifyId))
- v.SearchText = s
- }
- resp.List = list
- resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // EdbList
- // @Title 获取研究员指标
- // @Description 获取研究员指标
- // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
- // @Param Keyword query string false "关键字搜索"
- // @Param TradeCode query string false "指标唯一编码"
- // @Param ClassifyId query int false "分类id"
- // @Param UserId query int false "用户id"
- // @Success 200 {object} models.TargetItemListResp
- // @router /target/edb/list [get]
- func (c *ManualEdbController) EdbList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- orderField := c.GetString(`OrderField`)
- orderType := c.GetString("OrderType")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- // 频度
- frequency := c.GetString("Frequency")
- //关键字
- keyword := c.GetString("Keyword")
- //关键字
- classifyId, _ := c.GetInt("ClassifyId", 0)
- //用户ID
- userId, _ := c.GetInt("UserId", 0)
- var condition string
- var pars []interface{}
- // 权限用户id
- permissionUserId := sysUser.AdminId
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyIdList)
- if num > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- } else {
- resp := models.EdbListResp{
- List: []*models.EdbInfoListItem{},
- Paging: paging.GetPaging(currentIndex, pageSize, 0),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- } else {
- permissionUserId = 0
- }
- // 关键词
- if keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- }
- if frequency != "" {
- condition += ` AND a.frequency = ?`
- pars = append(pars, frequency)
- }
- // 所属分类
- if classifyId > 0 {
- // 获取有用权限的分类
- classifyList, err := models.GetEdbdataClassify(int64(permissionUserId))
- if err != nil {
- return
- }
- var isParent bool
- classifyIdList := make([]int, 0)
- for _, v := range classifyList {
- if v.ClassifyId == classifyId {
- isParent = true
- classifyIdList = append(classifyIdList, v.ClassifyId)
- if v.Child != nil && len(v.Child) > 0 {
- for _, vv := range v.Child {
- classifyIdList = append(classifyIdList, vv.ClassifyId)
- }
- }
- break
- }
- }
- num := len(classifyIdList)
- if num > 0 && isParent {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- } else {
- condition += ` AND a.classify_id = ? `
- pars = append(pars, classifyId)
- }
- }
- // 所属用户
- if userId > 0 {
- condition += ` AND a.user_id = ? `
- pars = append(pars, userId)
- }
- total, err := models.GetCountEdbInfoList(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- list, err := models.GetEdbInfoSortList(condition, pars, startSize, pageSize, orderField, orderType)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- // 获取预测时间
- fillNextDateTime(list)
- resp := models.EdbListResp{
- List: list,
- Paging: paging.GetPaging(currentIndex, pageSize, total),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // 封装指标列表下期时间
- func fillNextDateTime(list []*models.EdbInfoListItem) {
- for _, item := range list {
- date := utils.StrDateToDate(item.EndDate)
- switch item.Frequency {
- case "日度":
- item.NextDateTime = date.AddDate(0, 0, 1).Format(utils.FormatDate)
- case "周度":
- item.NextDateTime = date.AddDate(0, 0, 7).Format(utils.FormatDate)
- case "旬度":
- item.NextDateTime = date.AddDate(0, 0, 10).Format(utils.FormatDate)
- case "月度":
- item.NextDateTime = date.AddDate(0, 1, 0).Format(utils.FormatDate)
- case "季度":
- item.NextDateTime = date.AddDate(0, 3, 0).Format(utils.FormatDate)
- case "半年度":
- item.NextDateTime = date.AddDate(0, 6, 0).Format(utils.FormatDate)
- case "年度":
- item.NextDateTime = date.AddDate(1, 0, 0).Format(utils.FormatDate)
- }
- }
- }
- // EditExcelData
- // @Title 根据excel的样式去编辑指标
- // @Description 根据excel的样式去编辑指标
- // @Param request body models.ManualEdbExcelStyleEditReq true "type json string"
- // @Success 200
- // @router /target/edb/excel_style/edit [post]
- func (c *ManualEdbController) EditExcelData() {
- br := new(models.BaseResponse).Init()
- var err error
- errs := make([]string, 0)
- defer func() {
- if len(errs) > 0 {
- utils.FileLog.Info("编辑EXCEL数据 新增或修改数据失败,Err:" + strings.Join(errs, "\n"))
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- var req data_manage.ManualEdbExcelStyleEditReq
- err = json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.TradeCode == `` {
- br.Msg = "指标编码异常!"
- br.ErrMsg = "指标编码异常!"
- return
- }
- if req.Unit == `` {
- br.Msg = "单位填写异常!"
- br.ErrMsg = "单位填写异常!"
- return
- }
- if req.Frequency == `` {
- br.Msg = "频度填写异常!"
- br.ErrMsg = "频度填写异常!"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "品种填写异常!"
- br.ErrMsg = "品种填写异常!"
- return
- }
- // 获取指标信息
- manualEdbInfo, err := models.GetEdbinfoByTradeCode(req.TradeCode)
- if err != nil {
- br.Msg = `找不到该指标`
- br.Msg = `找不到该指标,ERR:` + err.Error()
- return
- }
- // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.ErrMsg = "获取分类数据失败,err:" + err.Error()
- return
- }
- if count <= 0 {
- br.Msg = "无权访问"
- br.ErrMsg = "无权访问"
- br.IsSendEmail = false
- return
- }
- }
- dateValueMap := make(map[string]string)
- //取到所有数据
- for _, dateValue := range req.Data {
- // 检验时间格式
- _, err := time.Parse(utils.FormatDate, dateValue.Date)
- if err != nil {
- continue
- }
- dateValueMap[dateValue.Date] = strconv.FormatFloat(dateValue.Value, 'f', -1, 64)
- }
- //操作指标,新增指标及数据等
- {
- var isUpdateEdb, isUpdateData bool
- secName := strings.TrimSpace(req.EdbName)
- frequency := strings.TrimSpace(req.Frequency)
- unit := strings.TrimSpace(req.Unit)
- // 指标基础信息变更
- {
- updateCols := make([]string, 0)
- if manualEdbInfo.SecName != secName {
- // 校验是否存在相同指标名称
- var condition string
- var pars []interface{}
- condition += " AND SEC_NAME=? AND left(TRADE_CODE,1)='W' AND REMARK='手动' AND TRADE_CODE != ? "
- pars = append(pars, secName, req.TradeCode)
- count, err := models.GetManualEdbCountByCondition(condition, pars)
- if err != nil {
- br.Msg = `指标保存失败`
- br.ErrMsg = `获取同名指标信息异常,ERR:` + err.Error()
- return
- }
- if count > 0 {
- br.Msg = `已存在同名指标`
- br.ErrMsg = `已存在同名指标`
- br.IsSendEmail = false
- return
- }
- updateCols = append(updateCols, "SecName")
- manualEdbInfo.SecName = secName
- }
- if manualEdbInfo.ClassifyId != req.ClassifyId {
- updateCols = append(updateCols, "ClassifyId")
- manualEdbInfo.ClassifyId = req.ClassifyId
- }
- if manualEdbInfo.Frequency != frequency {
- updateCols = append(updateCols, "Frequency")
- manualEdbInfo.Frequency = frequency
- }
- if manualEdbInfo.Unit != unit {
- updateCols = append(updateCols, "Unit")
- manualEdbInfo.Unit = unit
- }
- if len(updateCols) > 0 {
- isUpdateEdb = true
- manualEdbInfo.ModifyTime = time.Now().Format(utils.FormatDateTime)
- updateCols = append(updateCols, "ModifyTime")
- err = manualEdbInfo.Update(updateCols)
- if err != nil {
- br.Msg = `指标保存失败`
- br.ErrMsg = `指标保存失败,ERR:` + err.Error()
- return
- }
- }
- }
- // 当前已经存在的指标明细数据
- targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
- if tmpErr != nil {
- err = tmpErr
- }
- existDataMap := make(map[string]string)
- deleteDataMap := make(map[string]string)
- for _, tmpData := range targetDataList {
- existDataMap[tmpData.Dt] = tmpData.Close
- deleteDataMap[tmpData.Dt] = tmpData.Dt
- }
- addDataList := make([]*models.Edbdata, 0)
- for createDate, closeVal := range dateValueMap {
- if createDate == "" || closeVal == "" {
- continue
- }
- //判断数据是否已经存在
- tmpVal, ok3 := existDataMap[createDate]
- // 不存在,那么后面进行插入
- if !ok3 {
- addDataList = append(addDataList, &models.Edbdata{
- TradeCode: manualEdbInfo.TradeCode,
- Dt: createDate,
- Close: closeVal,
- ModifyTime: time.Now(),
- })
- continue
- }
- delete(deleteDataMap, createDate)
- // 库里面的数据
- tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
- if tmpErr != nil {
- fmt.Println("tmpVal Parse err:", tmpErr.Error())
- err = tmpErr
- continue
- }
- // 用户填写的数据
- closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
- if tmpErr != nil {
- fmt.Println("closeVal Parse err:", tmpErr.Error())
- err = tmpErr
- continue
- }
- if !tmpValDecimal.Equal(closeValDecimal) {
- isUpdateData = true
- fmt.Println("更新数值")
- err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
- if err != nil {
- fmt.Println("ModifyTargetsDataByImport err:", err.Error())
- errs = append(errs, err.Error())
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }
- }
- // 新增指标
- if len(addDataList) > 0 {
- isUpdateData = true
- err = models.OnlyMultiAddEdbdata(addDataList)
- if err != nil {
- fmt.Println("MultiAddEdbdata err:", err.Error())
- errs = append(errs, err.Error())
- }
- }
- // 删除不需要的日期数据
- num := len(deleteDataMap)
- if num > 0 {
- isUpdateData = true
- deleteDateList := make([]string, 0)
- for date := range deleteDataMap {
- deleteDateList = append(deleteDateList, date)
- }
- err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
- if err != nil {
- fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
- errs = append(errs, err.Error())
- }
- }
- // 添加记录
- if isUpdateEdb {
- record := &models.EdbinfoOpRecord{
- TradeCode: manualEdbInfo.TradeCode,
- Remark: "编辑指标",
- UserId: sysUser.AdminId,
- UserName: sysUser.RealName,
- CreateTime: time.Now(),
- }
- go func() {
- _ = record.Create()
- }()
- }
- // 更新数据
- if isUpdateData {
- record := &models.EdbinfoOpRecord{
- TradeCode: manualEdbInfo.TradeCode,
- Remark: "更新数据",
- UserId: sysUser.AdminId,
- UserName: sysUser.RealName,
- CreateTime: time.Now(),
- }
- go func() {
- data.ModifyManualEdbMaxMinDate(manualEdbInfo.TradeCode)
- _ = record.Create()
- }()
- }
- //将该指标的code加入到 “手工数据导入后刷新” 缓存
- if utils.Re == nil {
- err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, manualEdbInfo.TradeCode)
- if err != nil {
- fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
- }
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- }
- // BatchEdbList
- // @Title 获取研究员指标
- // @Description 获取研究员指标
- // @Param request body models.BatchAddEdbReq true "type json string"
- // @Success 200 {object} models.TargetItemListResp
- // @router /target/edb/batch/list [post]
- func (c *ManualEdbController) BatchEdbList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- var req models.BatchManualEdbReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- pageSize := req.PageSize
- currentIndex := req.CurrentIndex
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- req.Keyword = strings.TrimSpace(req.Keyword)
- var condition string
- var pars []interface{}
- // 是否加到指标库
- condition += ` AND a.is_join_edb = ? `
- pars = append(pars, 0)
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyIdList)
- if num > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- // 关键词
- if req.Keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
- }
- // 所属分类
- lenFrequency := len(req.FrequencyList)
- if lenFrequency > 0 {
- condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
- pars = append(pars, req.FrequencyList)
- }
- // 所属分类
- lenClassify := len(req.ClassifyIdList)
- if lenClassify > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
- pars = append(pars, req.ClassifyIdList)
- }
- // 所属用户
- lenUser := len(req.UserIdList)
- if lenUser > 0 {
- condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
- pars = append(pars, req.UserIdList)
- }
- total, err := models.GetCountEdbInfoList(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := models.EdbListResp{
- List: list,
- Paging: paging.GetPaging(currentIndex, pageSize, total),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- br.IsAddLog = false
- }
- // BatchAddEdbCheck
- // @Title 新增校验
- // @Description 新增校验
- // @Param request body data_manage.BatchManualEdbReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/add/check [post]
- func (c *ManualEdbController) BatchAddEdbCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 最大批量添加的数量
- codeMax := 30
- var req models.BatchManualEdbReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- req.Keyword = strings.TrimSpace(req.Keyword)
- var condition string
- var pars []interface{}
- //
- condition += ` AND a.is_join_edb = 0 `
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyIdList)
- if num > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- if req.ListAll {
- // 关键词
- if req.Keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
- }
- // 所属分类
- lenFrequency := len(req.FrequencyList)
- if lenFrequency > 0 {
- condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
- pars = append(pars, req.FrequencyList)
- }
- // 所属分类
- lenClassify := len(req.ClassifyIdList)
- if lenClassify > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
- pars = append(pars, req.ClassifyIdList)
- }
- // 所属用户
- lenUser := len(req.UserIdList)
- if lenUser > 0 {
- condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
- pars = append(pars, req.UserIdList)
- }
- lenTradeList := len(req.TradeCodeList)
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- } else {
- lenTradeList := len(req.TradeCodeList)
- if lenTradeList <= 0 {
- br.Msg = "请选择指标"
- br.ErrMsg = "请选择指标"
- return
- }
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- }
- list, err := models.GetEdbInfoList(condition, pars, 0, 0)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if len(list) > codeMax {
- br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
- return
- }
- br.Data = list
- br.Msg = "校验成功"
- br.Ret = 200
- br.Success = true
- }
- // NameCheck
- // @Title 重名校验
- // @Description 批量新增
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/add/name_check [post]
- func (c *ManualEdbController) NameCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req []*data_manage.AddEdbInfoReq
- if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if len(req) == 0 {
- br.Msg = "请选择指标"
- return
- }
- type NameCheckResult struct {
- EdbCode string
- EdbName string
- Exist bool
- }
- indexNames := make([]string, 0)
- resp := make([]*NameCheckResult, 0)
- for _, v := range req {
- v.EdbCode = strings.TrimSpace(v.EdbCode)
- if v.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- v.EdbName = strings.TrimSpace(v.EdbName)
- if v.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- indexNames = append(indexNames, v.EdbName)
- resp = append(resp, &NameCheckResult{
- EdbCode: v.EdbCode,
- EdbName: v.EdbName,
- })
- }
- // 重名校验
- edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
- return
- }
- nameExists := make(map[string]bool)
- for _, v := range edbList {
- nameExists[v.EdbName] = true
- }
- if len(nameExists) > 0 {
- for _, v := range resp {
- v.Exist = nameExists[v.EdbName]
- }
- }
- br.Data = resp
- br.Msg = "操作成功"
- br.Ret = 200
- br.Success = true
- }
- // BatchAdd2Edb
- // @Title 批量新增到指标库
- // @Description 批量新增到指标库
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/add [post]
- func (c *ManualEdbController) BatchAdd2Edb() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 最大批量添加的数量
- codeMax := 30
- deleteCache := true
- cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req []*data_manage.AddEdbInfoReq
- if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if len(req) == 0 {
- br.Msg = "请选择指标"
- return
- }
- if len(req) > codeMax {
- br.Msg = "批量添加指标数量不得超过30个"
- return
- }
- indexNames := make([]string, 0)
- for _, v := range req {
- v.EdbCode = strings.TrimSpace(v.EdbCode)
- if v.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- v.EdbName = strings.TrimSpace(v.EdbName)
- if v.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- indexNames = append(indexNames, v.EdbName)
- v.Frequency = strings.TrimSpace(v.Frequency)
- if v.Frequency == "" {
- br.Msg = "请选择频度"
- return
- }
- v.Unit = strings.TrimSpace(v.Unit)
- if v.Unit == "" {
- br.Msg = "请输入单位"
- return
- }
- if v.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- }
- // 限定同一时间最多批量新增30个指标
- for _, v := range req {
- var r data.ManualIndexSource2EdbReq
- r.EdbCode = v.EdbCode
- r.EdbName = v.EdbName
- r.Frequency = v.Frequency
- r.Unit = v.Unit
- r.ClassifyId = v.ClassifyId
- r.AdminId = sysUser.AdminId
- r.AdminRealName = sysUser.RealName
- edbInfo, e, errMsg, skip := data.ManualIndexSource2Edb(r, c.Lang)
- if e != nil {
- br.Msg = "操作失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = e.Error()
- return
- }
- if skip {
- continue
- }
- // 试用平台更新用户累计新增指标数
- if utils.BusinessCode == utils.BusinessCodeSandbox {
- go func() {
- adminItem, e := system.GetSysAdminById(sysUser.AdminId)
- if e != nil {
- tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
- utils.FileLog.Info(tips)
- return
- }
- if adminItem.DepartmentName != "ETA试用客户" {
- return
- }
- var ur etaTrialService.EtaTrialUserReq
- ur.Mobile = adminItem.Mobile
- _, _ = etaTrialService.UpdateUserIndexNum(ur)
- }()
- }
- // 新增操作日志
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUser.AdminId
- edbLog.SysUserRealName = sysUser.RealName
- edbLog.CreateTime = time.Now()
- edbLog.Content = string(c.Ctx.Input.RequestBody)
- edbLog.Status = "新增指标"
- edbLog.Method = c.Ctx.Input.URI()
- go data_manage.AddEdbInfoLog(edbLog)
- }
- }
- br.Msg = "操作成功"
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- }
- // BatchDelEdbCheck
- // @Title 新增校验
- // @Description 新增校验
- // @Param request body data_manage.BatchManualEdbReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/del/check [post]
- func (c *ManualEdbController) BatchDelEdbCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.BatchManualEdbReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- req.Keyword = strings.TrimSpace(req.Keyword)
- var condition string
- var pars []interface{}
- // 就是为了找出已经加入指标库的指标
- condition += ` AND a.is_join_edb = ? `
- pars = append(pars, 1)
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyIdList)
- if num > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- if req.ListAll {
- // 关键词
- if req.Keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
- }
- // 所属分类
- lenFrequency := len(req.FrequencyList)
- if lenFrequency > 0 {
- condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
- pars = append(pars, req.FrequencyList)
- }
- // 所属分类
- lenClassify := len(req.ClassifyIdList)
- if lenClassify > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
- pars = append(pars, req.ClassifyIdList)
- }
- // 所属用户
- lenUser := len(req.UserIdList)
- if lenUser > 0 {
- condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
- pars = append(pars, req.UserIdList)
- }
- lenTradeList := len(req.TradeCodeList)
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- } else {
- lenTradeList := len(req.TradeCodeList)
- if lenTradeList <= 0 {
- br.Msg = "请选择指标"
- br.ErrMsg = "请选择指标"
- return
- }
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- }
- list, err := models.GetEdbInfoList(condition, pars, 0, 0)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- br.Data = list
- br.Msg = "校验成功"
- br.Ret = 200
- br.Success = true
- }
- // BatchDel
- // @Title 批量删除
- // @Description 批量新增
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/del [post]
- func (c *ManualEdbController) BatchDel() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req models.BatchManualEdbReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- req.Keyword = strings.TrimSpace(req.Keyword)
- var condition string
- var pars []interface{}
- // 就是为了找出已经加入指标库的指标
- condition += ` AND a.is_join_edb = ? `
- pars = append(pars, 0)
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyIdList)
- if num > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- if req.ListAll {
- // 关键词
- if req.Keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
- }
- // 所属分类
- lenFrequency := len(req.FrequencyList)
- if lenFrequency > 0 {
- condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
- pars = append(pars, req.FrequencyList)
- }
- // 所属分类
- lenClassify := len(req.ClassifyIdList)
- if lenClassify > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
- pars = append(pars, req.ClassifyIdList)
- }
- // 所属用户
- lenUser := len(req.UserIdList)
- if lenUser > 0 {
- condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
- pars = append(pars, req.UserIdList)
- }
- lenTradeList := len(req.TradeCodeList)
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- } else {
- lenTradeList := len(req.TradeCodeList)
- if lenTradeList <= 0 {
- br.Msg = "请选择指标"
- br.ErrMsg = "请选择指标"
- return
- }
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- }
- list, err := models.GetEdbInfoList(condition, pars, 0, 0)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,Err:" + err.Error()
- return
- }
- // TODO 二次校验指标是否已经加到指标库
- {
- codeList := make([]string, 0)
- // 限定同一时间最多批量新增30个指标
- for _, v := range list {
- codeList = append(codeList, v.TradeCode)
- }
- err = models.DelManualIndexByCodeList(codeList)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,Err:" + err.Error()
- return
- }
- }
- br.Msg = "删除成功"
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- }
- // ImportData
- // @Title 导入数据
- // @Description 导入数据
- // @Param EntryFile query file true "文件"
- // @Success 200 Ret=200 录入成功
- // @router /import/data [post]
- func (c *ManualEdbController) ImportData() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- file, _, err := c.GetFile("EntryFile")
- if err != nil {
- br.Msg = "获取文件失败"
- br.ErrMsg = "获取文件失败,Err:" + err.Error()
- return
- }
- path := "./static/数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- defer file.Close()
- err = c.SaveToFile("EntryFile", path)
- if err != nil {
- br.Msg = "文件保存失败"
- br.ErrMsg = "文件保存失败,Err:" + err.Error()
- return
- }
- if utils.RunMode == "debug" {
- defer os.Remove(path)
- }
- successCount, failCount, indexCount, err, errMsg := data.ImportManualData(path, sysUser)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- resp := models.EdbdataImportResp{
- SuccessCount: successCount,
- FailCount: failCount,
- IndexCount: indexCount,
- }
- if failCount > 0 {
- if successCount == 0 {
- resp.Status = -1
- resp.Msg = "导入失败"
- } else {
- resp.Status = 1
- resp.Msg = "存在部分导入失败"
- }
- } else {
- resp.Status = 0
- }
- br.Msg = "导入成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // RecordList
- // @Title 获取指标操作记录列表
- // @Description 获取指标操作记录列表
- // @Param TradeCode query string false "指标编码"
- // @Success 200 {object} models.EdbinfoOpRecordListResp
- // @router /target/edb/op/record/list [get]
- func (c *ManualEdbController) RecordList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- // 频度
- tradeCode := c.GetString("TradeCode")
- if tradeCode == `` {
- br.Msg = `请选择指标`
- br.ErrMsg = `请选择指标`
- br.IsSendEmail = false
- return
- }
- total, list, err := models.GetEdbinfoOpRecordPageList(tradeCode, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := models.EdbinfoOpRecordListResp{
- List: list,
- Paging: paging.GetPaging(currentIndex, pageSize, total),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // BatchSaveExcelData
- // @Title 批量保存手工数据
- // @Description 批量保存手工数据
- // @Param request body []models.ManualEdbExcelStyleEditReq true "type json string"
- // @Success 200
- // @router /target/edb/excel_style/batch_save [post]
- func (c *ManualEdbController) BatchSaveExcelData() {
- br := new(models.BaseResponse).Init()
- var err error
- errs := make([]string, 0)
- defer func() {
- if len(errs) > 0 {
- utils.FileLog.Info("编辑EXCEL数据 新增或修改数据失败,Err:" + strings.Join(errs, "\n"))
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- var req []data_manage.ManualEdbExcelStyleEditReq
- err = json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- countReq := len(req)
- if countReq <= 0 {
- br.Msg = "无数据!"
- br.ErrMsg = "无数据!"
- br.IsSendEmail = false
- return
- }
- if countReq <= 0 {
- br.Msg = "超出上限,导入失败!"
- br.ErrMsg = "超出上限,导入失败!"
- br.IsSendEmail = false
- return
- }
- // 当前来说,所有指标都是同一个分类,所以取的第一个的分类id,后面如果可以做不通分类的配置的话,那就需要做微调
- editClassifyIdList := make([]int, 0)
- tmpClassifyIdMap := make(map[int]bool)
- for _, item := range req {
- classifyId := item.ClassifyId
- if classifyId <= 0 {
- br.Msg = "品种填写异常!"
- br.ErrMsg = "品种填写异常!"
- return
- }
- if _, ok := tmpClassifyIdMap[classifyId]; ok {
- continue
- }
- editClassifyIdList = append(editClassifyIdList, classifyId)
- tmpClassifyIdMap[classifyId] = true
- }
- //超管账号可以查看分类下的所有频度数据
- userId := sysUser.AdminId
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- userId = 0
- }
- //获取账户所拥有权限的分类id集合
- classifyIdList, err := data.GetUserManualClassifyIdList(userId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- for _, classifyId := range editClassifyIdList {
- if !utils.InArrayByInt(classifyIdList, classifyId) {
- br.Msg = "无权访问"
- br.ErrMsg = "无权访问"
- br.IsSendEmail = false
- return
- }
- }
- // 指标map
- targetMap := make(map[string]*models.Edbinfo)
- addEdbTradeMap := make(map[string]bool)
- updateEdbTradeMap := make(map[string]bool)
- updateDataTradeMap := make(map[string]bool)
- defer func() {
- go func(addEdbTradeMap, updateEdbTradeMap, updateDataTradeMap map[string]bool) {
- addRecordList := make([]*models.EdbinfoOpRecord, 0)
- for tradeCode := range addEdbTradeMap {
- addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
- TradeCode: tradeCode,
- Remark: "创建指标",
- UserId: sysUser.AdminId,
- UserName: sysUser.RealName,
- CreateTime: time.Now(),
- })
- }
- for tradeCode := range updateEdbTradeMap {
- addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
- TradeCode: tradeCode,
- Remark: "编辑指标",
- UserId: sysUser.AdminId,
- UserName: sysUser.RealName,
- CreateTime: time.Now(),
- })
- }
- for tradeCode := range updateDataTradeMap {
- addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
- TradeCode: tradeCode,
- Remark: "更新数据",
- UserId: sysUser.AdminId,
- UserName: sysUser.RealName,
- CreateTime: time.Now(),
- })
- }
- if len(addRecordList) > 0 {
- obj := models.EdbinfoOpRecord{}
- _ = obj.MulCreate(addRecordList)
- }
- //将该指标的code加入到 “手工数据导入后刷新” 缓存
- if utils.Re == nil {
- for tradeCode := range updateDataTradeMap {
- // 更新手工数据的最大最小值
- data.ModifyManualEdbMaxMinDate(tradeCode)
- //将该指标的code加入到 “手工数据导入后刷新” 缓存
- err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, tradeCode)
- if err != nil {
- fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
- }
- }
- }
- }(addEdbTradeMap, updateEdbTradeMap, updateDataTradeMap)
- }()
- for _, item := range req {
- if item.Unit == `` {
- br.Msg = "单位填写异常!"
- br.ErrMsg = "单位填写异常!"
- return
- }
- if item.Frequency == `` {
- br.Msg = "频度填写异常!"
- br.ErrMsg = "频度填写异常!"
- return
- }
- if item.ClassifyId <= 0 {
- br.Msg = "品种填写异常!"
- br.ErrMsg = "品种填写异常!"
- return
- }
- // 是否新增指标
- var isAddEdb bool
- secName := strings.TrimSpace(item.EdbName)
- frequency := strings.TrimSpace(item.Frequency)
- unit := strings.TrimSpace(item.Unit)
- manualEdbInfo, ok := targetMap[item.EdbName]
- if !ok {
- manualEdbInfo, err = models.GetTargetBySecName(secName)
- if err != nil {
- //如果是找不到该指标,那么新增指标
- if err.Error() == utils.ErrNoRow() {
- tmpErr := data.AddEdbInfo(secName, unit, frequency, "", sysUser.Mobile, item.ClassifyId, sysUser.AdminId, sysUser.RealName)
- if tmpErr != nil {
- utils.FileLogData.Error("AddEdbInfo err :%s", tmpErr.Error())
- continue
- }
- isAddEdb = true
- tmpTarget, tmpErr := models.GetTargetBySecName(secName)
- manualEdbInfo = tmpTarget
- targetMap[secName] = manualEdbInfo
- // 指标新增
- addEdbTradeMap[manualEdbInfo.TradeCode] = true
- } else {
- fmt.Println("导入数据 获取指标:Err:" + err.Error())
- }
- } else {
- targetMap[secName] = manualEdbInfo
- }
- }
- // 没有该分类品种权限的话,那么就过滤
- if !utils.InArrayByInt(classifyIdList, manualEdbInfo.ClassifyId) {
- continue
- }
- dateValueMap := make(map[string]string)
- //取到所有数据
- for _, dateValue := range item.Data {
- // 检验时间格式
- _, err := time.Parse(utils.FormatDate, dateValue.Date)
- if err != nil {
- continue
- }
- dateValueMap[dateValue.Date] = strconv.FormatFloat(dateValue.Value, 'f', -1, 64)
- }
- //操作指标,新增指标及数据等
- {
- // 如果不是新增指标,那么指标基础信息变更
- if !isAddEdb {
- updateCols := make([]string, 0)
- if manualEdbInfo.SecName != secName {
- // 校验是否存在相同指标名称
- var condition string
- var pars []interface{}
- condition += " AND SEC_NAME=? AND left(TRADE_CODE,1)='W' AND REMARK='手动' AND TRADE_CODE != ? "
- pars = append(pars, secName, manualEdbInfo.TradeCode)
- count, err := models.GetManualEdbCountByCondition(condition, pars)
- if err != nil {
- br.Msg = `指标保存失败`
- br.ErrMsg = `获取同名指标信息异常,ERR:` + err.Error()
- return
- }
- if count > 0 {
- br.Msg = `已存在同名指标`
- br.ErrMsg = `已存在同名指标`
- br.IsSendEmail = false
- return
- }
- updateCols = append(updateCols, "SecName")
- manualEdbInfo.SecName = secName
- }
- if manualEdbInfo.ClassifyId != item.ClassifyId {
- updateCols = append(updateCols, "ClassifyId")
- manualEdbInfo.ClassifyId = item.ClassifyId
- }
- if manualEdbInfo.Frequency != frequency {
- updateCols = append(updateCols, "Frequency")
- manualEdbInfo.Frequency = frequency
- }
- if manualEdbInfo.Unit != unit {
- updateCols = append(updateCols, "Unit")
- manualEdbInfo.Unit = unit
- }
- if len(updateCols) > 0 {
- manualEdbInfo.ModifyTime = time.Now().Format(utils.FormatDateTime)
- updateCols = append(updateCols, "ModifyTime")
- err = manualEdbInfo.Update(updateCols)
- if err != nil {
- br.Msg = `指标保存失败`
- br.ErrMsg = `指标保存失败,ERR:` + err.Error()
- return
- }
- updateEdbTradeMap[manualEdbInfo.TradeCode] = true
- }
- }
- // 当前已经存在的指标明细数据
- targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
- if tmpErr != nil {
- err = tmpErr
- }
- existDataMap := make(map[string]string)
- deleteDataMap := make(map[string]string)
- for _, tmpData := range targetDataList {
- existDataMap[tmpData.Dt] = tmpData.Close
- deleteDataMap[tmpData.Dt] = tmpData.Dt
- }
- var isUpdateData bool
- addDataList := make([]*models.Edbdata, 0)
- for createDate, closeVal := range dateValueMap {
- if createDate == "" || closeVal == "" {
- continue
- }
- //判断数据是否已经存在
- tmpVal, ok3 := existDataMap[createDate]
- // 不存在,那么后面进行插入
- if !ok3 {
- addDataList = append(addDataList, &models.Edbdata{
- TradeCode: manualEdbInfo.TradeCode,
- Dt: createDate,
- Close: closeVal,
- ModifyTime: time.Now(),
- })
- continue
- }
- delete(deleteDataMap, createDate)
- // 库里面的数据
- tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
- if tmpErr != nil {
- fmt.Println("tmpVal Parse err:", tmpErr.Error())
- err = tmpErr
- continue
- }
- // 用户填写的数据
- closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
- if tmpErr != nil {
- fmt.Println("closeVal Parse err:", tmpErr.Error())
- err = tmpErr
- continue
- }
- if !tmpValDecimal.Equal(closeValDecimal) {
- isUpdateData = true
- fmt.Println("更新数值")
- err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
- if err != nil {
- fmt.Println("ModifyTargetsDataByImport err:", err.Error())
- errs = append(errs, err.Error())
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }
- }
- // 新增明细数据
- if len(addDataList) > 0 {
- isUpdateData = true
- err = models.OnlyMultiAddEdbdata(addDataList)
- if err != nil {
- fmt.Println("MultiAddEdbdata err:", err.Error())
- errs = append(errs, err.Error())
- }
- }
- // 删除不需要的日期数据
- num := len(deleteDataMap)
- if num > 0 {
- isUpdateData = true
- deleteDateList := make([]string, 0)
- for date := range deleteDataMap {
- deleteDateList = append(deleteDateList, date)
- }
- err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
- if err != nil {
- fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
- errs = append(errs, err.Error())
- }
- }
- // 更新数据
- if isUpdateData {
- updateDataTradeMap[manualEdbInfo.TradeCode] = true
- }
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- }
|