manual_edb.go 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/data"
  9. "eta/eta_api/services/elastic"
  10. etaTrialService "eta/eta_api/services/eta_trial"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/rdlucklib/rdluck_tools/paging"
  18. "github.com/shopspring/decimal"
  19. )
  20. // ManualEdbController 手工指标服务(鉴权)
  21. type ManualEdbController struct {
  22. controllers.BaseAuthController
  23. }
  24. // EdbDetail
  25. // @Title 指标列表
  26. // @Description 指标列表
  27. // @Param TradeCode query string true "指标编码"
  28. // @Success 200 {object} models.TargetDetailResp
  29. // @router /target/edb/detail [get]
  30. func (c *ManualEdbController) EdbDetail() {
  31. br := new(models.BaseResponse).Init()
  32. defer func() {
  33. c.Data["json"] = br
  34. c.ServeJSON()
  35. }()
  36. sysUser := c.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请重新登录"
  39. return
  40. }
  41. // 指标编码
  42. tradeCode := c.GetString("TradeCode")
  43. if tradeCode == `` {
  44. br.Msg = "请输入指标编码"
  45. br.IsSendEmail = false
  46. return
  47. }
  48. manualEdbInfo, err := models.GetTargetByTradeCode(tradeCode)
  49. if err != nil {
  50. br.Msg = "获取指标失败"
  51. br.ErrMsg = "获取指标失败,err:" + err.Error()
  52. return
  53. }
  54. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  55. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  56. //获取账户所拥有权限的分类id集合
  57. count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
  58. if err != nil {
  59. br.Msg = "获取分类数据失败"
  60. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  61. return
  62. }
  63. if count <= 0 {
  64. br.Msg = "无权访问"
  65. br.ErrMsg = "无权访问"
  66. br.IsSendEmail = false
  67. return
  68. }
  69. }
  70. classifyList := make([]*models.EdbdataClassify, 0)
  71. {
  72. classify, err := models.GetManualClassifyByClassifyId(manualEdbInfo.ClassifyId)
  73. if err != nil {
  74. br.Msg = "获取分类信息失败"
  75. br.ErrMsg = "获取分类信息失败,err:" + err.Error()
  76. return
  77. }
  78. parentClassify, err := models.GetManualClassifyByClassifyId(classify.ParentId)
  79. if err != nil {
  80. br.Msg = "获取分类信息失败"
  81. br.ErrMsg = "获取父级分类信息失败,err:" + err.Error()
  82. return
  83. }
  84. classifyList = []*models.EdbdataClassify{
  85. parentClassify, classify,
  86. }
  87. }
  88. // 明细数据
  89. {
  90. dataList, err := models.GetEdbDataListByCode(manualEdbInfo.TradeCode)
  91. if err != nil {
  92. br.Msg = "获取明细数据失败"
  93. br.ErrMsg = "获取明细数据失败,err:" + err.Error()
  94. return
  95. }
  96. // 获取下期时间 -- 不用了,由前端处理
  97. /*dataNextDateTime, err := fillDataNextDateTime(dataList)
  98. if err != nil {
  99. return
  100. }*/
  101. manualEdbInfo.DataList = dataList
  102. }
  103. resp := models.TargetDetailResp{
  104. Detail: manualEdbInfo,
  105. ClassifyList: classifyList,
  106. }
  107. br.Ret = 200
  108. br.Success = true
  109. br.Msg = "获取成功"
  110. //br.Data = resp
  111. br.Data = resp
  112. }
  113. // 封装指标数据下期时间
  114. func fillDataNextDateTime(list []*models.Edbdata) ([]*models.Edbdata, error) {
  115. if len(list) == 0 {
  116. return nil, nil
  117. }
  118. nextDataDateTime := models.Edbdata{}
  119. nextDataDateTime.TradeCode = list[0].TradeCode
  120. indexCodes := []string{list[0].TradeCode}
  121. edbinfoList, err := models.GetEdbinfoListByCodeListByCodeIdList(indexCodes)
  122. if err != nil {
  123. return nil, err
  124. }
  125. if len(edbinfoList) == 0 {
  126. return list, nil
  127. }
  128. date := utils.StrDateToDate(list[0].Dt)
  129. frequency := edbinfoList[0].Frequency
  130. switch frequency {
  131. case "日度":
  132. nextDataDateTime.Dt = date.AddDate(0, 0, 1).Format(utils.FormatDate)
  133. case "周度":
  134. nextDataDateTime.Dt = date.AddDate(0, 0, 7).Format(utils.FormatDate)
  135. case "旬度":
  136. nextDataDateTime.Dt = date.AddDate(0, 0, 10).Format(utils.FormatDate)
  137. case "月度":
  138. nextDataDateTime.Dt = date.AddDate(0, 1, 0).Format(utils.FormatDate)
  139. case "季度":
  140. nextDataDateTime.Dt = date.AddDate(0, 3, 0).Format(utils.FormatDate)
  141. case "半年度":
  142. nextDataDateTime.Dt = date.AddDate(0, 6, 0).Format(utils.FormatDate)
  143. case "年度":
  144. nextDataDateTime.Dt = date.AddDate(1, 0, 0).Format(utils.FormatDate)
  145. }
  146. // nextDataDateTime 添加到list第一个
  147. list = append([]*models.Edbdata{&nextDataDateTime}, list...)
  148. return list, nil
  149. }
  150. // ClassifyEdbList
  151. // @Title 分类指标列表
  152. // @Description 指标列表
  153. // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
  154. // @Param ClassifyId query string true "分类id"
  155. // @Success 200 {object} models.TargetDetailResp
  156. // @router /target/classify/edb/list [get]
  157. func (c *ManualEdbController) ClassifyEdbList() {
  158. br := new(models.BaseResponse).Init()
  159. defer func() {
  160. c.Data["json"] = br
  161. c.ServeJSON()
  162. }()
  163. sysUser := c.SysUser
  164. if sysUser == nil {
  165. br.Msg = "请登录"
  166. br.Ret = 408
  167. return
  168. }
  169. resp := new(models.EdbdataClassifyResp)
  170. classifyId, _ := c.GetInt("ClassifyId") //分类
  171. if classifyId <= 0 {
  172. br.Msg = "请传入分类"
  173. br.ErrMsg = "请传入分类"
  174. br.IsSendEmail = false
  175. }
  176. // 频度
  177. frequency := c.GetString("Frequency")
  178. var condition string
  179. var pars []interface{}
  180. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  181. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  182. //获取账户所拥有权限的分类id集合
  183. count, err := models.GetCountManualUserClassify(sysUser.AdminId, classifyId)
  184. if err != nil {
  185. br.Msg = "获取分类数据失败"
  186. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  187. return
  188. }
  189. if count <= 0 {
  190. br.Msg = "无权访问"
  191. br.ErrMsg = "无权访问"
  192. br.IsSendEmail = false
  193. return
  194. }
  195. }
  196. list := make([]*models.EdbdataClassifyList, 0)
  197. condition += ` AND a.classify_id = ? `
  198. pars = append(pars, classifyId)
  199. if frequency != "" {
  200. condition += ` AND a.frequency = ?`
  201. pars = append(pars, frequency)
  202. }
  203. tmpList, err := models.GetEdbInfoList(condition, pars, 0, 0)
  204. if err != nil {
  205. br.Msg = "获取失败"
  206. br.ErrMsg = "获取失败,Err:" + err.Error()
  207. return
  208. }
  209. for _, v := range tmpList {
  210. list = append(list, &models.EdbdataClassifyList{
  211. ClassifyId: 0,
  212. ClassifyName: v.SecName,
  213. ParentId: v.ClassifyId,
  214. Child: nil,
  215. TradeCode: v.TradeCode,
  216. UniqueCode: utils.MD5(v.TradeCode),
  217. })
  218. }
  219. resp.List = list
  220. br.Ret = 200
  221. br.Success = true
  222. br.Msg = "获取成功"
  223. br.Data = resp
  224. }
  225. // EdbSearch
  226. // @Title 指标搜索
  227. // @Description 指标搜索
  228. // @Param Keyword query string false "关键字搜索"
  229. // @Success 200 {object} models.TargetItemListResp
  230. // @router /target/edb/search [get]
  231. func (c *ManualEdbController) EdbSearch() {
  232. br := new(models.BaseResponse).Init()
  233. defer func() {
  234. c.Data["json"] = br
  235. c.ServeJSON()
  236. }()
  237. sysUser := c.SysUser
  238. if sysUser == nil {
  239. br.Msg = "请重新登录"
  240. return
  241. }
  242. pageSize, _ := c.GetInt("PageSize")
  243. currentIndex, _ := c.GetInt("CurrentIndex")
  244. var startSize int
  245. if pageSize <= 0 {
  246. pageSize = utils.PageSize15
  247. }
  248. if currentIndex <= 0 {
  249. currentIndex = 1
  250. }
  251. startSize = utils.StartIndex(currentIndex, pageSize)
  252. resp := new(models.EdbListResp)
  253. //关键字
  254. keyword := c.GetString("Keyword")
  255. keyword = strings.TrimSpace(keyword)
  256. var condition string
  257. var pars []interface{}
  258. // es搜索
  259. highlightMap := make(map[string]string)
  260. if keyword != "" {
  261. _, list, e := elastic.SearchDataSourceIndex(utils.EsDataSourceIndexName, keyword, utils.DATA_SOURCE_MANUAL, 0, []int{}, []int{}, []string{}, startSize, pageSize)
  262. if e != nil {
  263. br.Msg = "获取失败"
  264. br.ErrMsg = fmt.Sprintf("ES-搜索手工指标列表失败, %v", e)
  265. return
  266. }
  267. if len(list) == 0 {
  268. resp.List = make([]*models.EdbInfoListItem, 0)
  269. br.Ret = 200
  270. br.Success = true
  271. br.Msg = "获取成功"
  272. br.Data = resp
  273. return
  274. }
  275. var codes []string
  276. for _, v := range list {
  277. codes = append(codes, v.IndexCode)
  278. highlightMap[v.IndexCode] = v.SearchText
  279. }
  280. condition += fmt.Sprintf(` AND a.TRADE_CODE IN (%s)`, utils.GetOrmInReplace(len(list)))
  281. pars = append(pars, codes)
  282. }
  283. //userId := sysUser.AdminId
  284. //超管账号可以查看分类下的所有频度数据
  285. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  286. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  287. if err != nil {
  288. br.Msg = "获取分类数据失败"
  289. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  290. return
  291. }
  292. num := len(classifyIdList)
  293. if num > 0 {
  294. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  295. pars = append(pars, classifyIdList)
  296. }
  297. }
  298. //if keyword != "" {
  299. // condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  300. // pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  301. //}
  302. total, err := models.GetCountEdbInfoList(condition, pars)
  303. if err != nil {
  304. br.Msg = "获取失败"
  305. br.ErrMsg = "获取失败,Err:" + err.Error()
  306. return
  307. }
  308. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  309. if err != nil {
  310. br.Msg = "获取失败"
  311. br.ErrMsg = "获取失败,Err:" + err.Error()
  312. return
  313. }
  314. for _, v := range list {
  315. v.UniqueCode = utils.MD5(v.TradeCode)
  316. // ES搜索信息
  317. v.Source = utils.DATA_SOURCE_MANUAL
  318. v.SourceName = "手工指标录入"
  319. s := highlightMap[v.TradeCode]
  320. if s == "" {
  321. s = v.SecName
  322. }
  323. v.ClassifyUniqueCode = utils.MD5(strconv.Itoa(v.ClassifyId))
  324. v.SearchText = s
  325. }
  326. resp.List = list
  327. resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
  328. br.Ret = 200
  329. br.Success = true
  330. br.Msg = "获取成功"
  331. br.Data = resp
  332. }
  333. // EdbList
  334. // @Title 获取研究员指标
  335. // @Description 获取研究员指标
  336. // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
  337. // @Param Keyword query string false "关键字搜索"
  338. // @Param TradeCode query string false "指标唯一编码"
  339. // @Param ClassifyId query int false "分类id"
  340. // @Param UserId query int false "用户id"
  341. // @Success 200 {object} models.TargetItemListResp
  342. // @router /target/edb/list [get]
  343. func (c *ManualEdbController) EdbList() {
  344. br := new(models.BaseResponse).Init()
  345. defer func() {
  346. c.Data["json"] = br
  347. c.ServeJSON()
  348. }()
  349. sysUser := c.SysUser
  350. if sysUser == nil {
  351. br.Msg = "请重新登录"
  352. return
  353. }
  354. pageSize, _ := c.GetInt("PageSize")
  355. currentIndex, _ := c.GetInt("CurrentIndex")
  356. orderField := c.GetString(`OrderField`)
  357. orderType := c.GetString("OrderType")
  358. var startSize int
  359. if pageSize <= 0 {
  360. pageSize = utils.PageSize20
  361. }
  362. if currentIndex <= 0 {
  363. currentIndex = 1
  364. }
  365. startSize = utils.StartIndex(currentIndex, pageSize)
  366. // 频度
  367. frequency := c.GetString("Frequency")
  368. //关键字
  369. keyword := c.GetString("Keyword")
  370. //关键字
  371. classifyId, _ := c.GetInt("ClassifyId", 0)
  372. //用户ID
  373. userId, _ := c.GetInt("UserId", 0)
  374. var condition string
  375. var pars []interface{}
  376. // 权限用户id
  377. permissionUserId := sysUser.AdminId
  378. //超管账号可以查看分类下的所有频度数据
  379. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  380. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  381. if err != nil {
  382. br.Msg = "获取分类数据失败"
  383. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  384. return
  385. }
  386. num := len(classifyIdList)
  387. if num > 0 {
  388. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  389. pars = append(pars, classifyIdList)
  390. } else {
  391. resp := models.EdbListResp{
  392. List: []*models.EdbInfoListItem{},
  393. Paging: paging.GetPaging(currentIndex, pageSize, 0),
  394. }
  395. br.Ret = 200
  396. br.Success = true
  397. br.Msg = "获取成功"
  398. br.Data = resp
  399. return
  400. }
  401. } else {
  402. permissionUserId = 0
  403. }
  404. // 关键词
  405. if keyword != "" {
  406. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  407. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  408. }
  409. if frequency != "" {
  410. condition += ` AND a.frequency = ?`
  411. pars = append(pars, frequency)
  412. }
  413. // 所属分类
  414. if classifyId > 0 {
  415. // 获取有用权限的分类
  416. classifyList, err := models.GetEdbdataClassify(int64(permissionUserId))
  417. if err != nil {
  418. return
  419. }
  420. var isParent bool
  421. classifyIdList := make([]int, 0)
  422. for _, v := range classifyList {
  423. if v.ClassifyId == classifyId {
  424. isParent = true
  425. classifyIdList = append(classifyIdList, v.ClassifyId)
  426. if v.Child != nil && len(v.Child) > 0 {
  427. for _, vv := range v.Child {
  428. classifyIdList = append(classifyIdList, vv.ClassifyId)
  429. }
  430. }
  431. break
  432. }
  433. }
  434. num := len(classifyIdList)
  435. if num > 0 && isParent {
  436. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  437. pars = append(pars, classifyIdList)
  438. } else {
  439. condition += ` AND a.classify_id = ? `
  440. pars = append(pars, classifyId)
  441. }
  442. }
  443. // 所属用户
  444. if userId > 0 {
  445. condition += ` AND a.user_id = ? `
  446. pars = append(pars, userId)
  447. }
  448. total, err := models.GetCountEdbInfoList(condition, pars)
  449. if err != nil {
  450. br.Msg = "获取失败"
  451. br.ErrMsg = "获取失败,Err:" + err.Error()
  452. return
  453. }
  454. list, err := models.GetEdbInfoSortList(condition, pars, startSize, pageSize, orderField, orderType)
  455. if err != nil {
  456. br.Msg = "获取失败"
  457. br.ErrMsg = "获取失败,Err:" + err.Error()
  458. return
  459. }
  460. // 获取预测时间
  461. fillNextDateTime(list)
  462. resp := models.EdbListResp{
  463. List: list,
  464. Paging: paging.GetPaging(currentIndex, pageSize, total),
  465. }
  466. br.Ret = 200
  467. br.Success = true
  468. br.Msg = "获取成功"
  469. br.Data = resp
  470. }
  471. // 封装指标列表下期时间
  472. func fillNextDateTime(list []*models.EdbInfoListItem) {
  473. for _, item := range list {
  474. date := utils.StrDateToDate(item.EndDate)
  475. switch item.Frequency {
  476. case "日度":
  477. item.NextDateTime = date.AddDate(0, 0, 1).Format(utils.FormatDate)
  478. case "周度":
  479. item.NextDateTime = date.AddDate(0, 0, 7).Format(utils.FormatDate)
  480. case "旬度":
  481. item.NextDateTime = date.AddDate(0, 0, 10).Format(utils.FormatDate)
  482. case "月度":
  483. item.NextDateTime = date.AddDate(0, 1, 0).Format(utils.FormatDate)
  484. case "季度":
  485. item.NextDateTime = date.AddDate(0, 3, 0).Format(utils.FormatDate)
  486. case "半年度":
  487. item.NextDateTime = date.AddDate(0, 6, 0).Format(utils.FormatDate)
  488. case "年度":
  489. item.NextDateTime = date.AddDate(1, 0, 0).Format(utils.FormatDate)
  490. }
  491. }
  492. }
  493. // EditExcelData
  494. // @Title 根据excel的样式去编辑指标
  495. // @Description 根据excel的样式去编辑指标
  496. // @Param request body models.ManualEdbExcelStyleEditReq true "type json string"
  497. // @Success 200
  498. // @router /target/edb/excel_style/edit [post]
  499. func (c *ManualEdbController) EditExcelData() {
  500. br := new(models.BaseResponse).Init()
  501. var err error
  502. errs := make([]string, 0)
  503. defer func() {
  504. if len(errs) > 0 {
  505. utils.FileLog.Info("编辑EXCEL数据 新增或修改数据失败,Err:" + strings.Join(errs, "\n"))
  506. }
  507. c.Data["json"] = br
  508. c.ServeJSON()
  509. }()
  510. sysUser := c.SysUser
  511. if sysUser == nil {
  512. br.Msg = "请重新登录"
  513. return
  514. }
  515. var req data_manage.ManualEdbExcelStyleEditReq
  516. err = json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  517. if err != nil {
  518. br.Msg = "参数解析异常!"
  519. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  520. return
  521. }
  522. if req.TradeCode == `` {
  523. br.Msg = "指标编码异常!"
  524. br.ErrMsg = "指标编码异常!"
  525. return
  526. }
  527. if req.Unit == `` {
  528. br.Msg = "单位填写异常!"
  529. br.ErrMsg = "单位填写异常!"
  530. return
  531. }
  532. if req.Frequency == `` {
  533. br.Msg = "频度填写异常!"
  534. br.ErrMsg = "频度填写异常!"
  535. return
  536. }
  537. if req.ClassifyId <= 0 {
  538. br.Msg = "品种填写异常!"
  539. br.ErrMsg = "品种填写异常!"
  540. return
  541. }
  542. // 获取指标信息
  543. manualEdbInfo, err := models.GetEdbinfoByTradeCode(req.TradeCode)
  544. if err != nil {
  545. br.Msg = `找不到该指标`
  546. br.Msg = `找不到该指标,ERR:` + err.Error()
  547. return
  548. }
  549. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  550. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  551. count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
  552. if err != nil {
  553. br.Msg = "获取分类数据失败"
  554. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  555. return
  556. }
  557. if count <= 0 {
  558. br.Msg = "无权访问"
  559. br.ErrMsg = "无权访问"
  560. br.IsSendEmail = false
  561. return
  562. }
  563. }
  564. dateValueMap := make(map[string]string)
  565. //取到所有数据
  566. for _, dateValue := range req.Data {
  567. // 检验时间格式
  568. _, err := time.Parse(utils.FormatDate, dateValue.Date)
  569. if err != nil {
  570. continue
  571. }
  572. dateValueMap[dateValue.Date] = strconv.FormatFloat(dateValue.Value, 'f', -1, 64)
  573. }
  574. //操作指标,新增指标及数据等
  575. {
  576. var isUpdateEdb, isUpdateData bool
  577. secName := strings.TrimSpace(req.EdbName)
  578. frequency := strings.TrimSpace(req.Frequency)
  579. unit := strings.TrimSpace(req.Unit)
  580. // 指标基础信息变更
  581. {
  582. updateCols := make([]string, 0)
  583. if manualEdbInfo.SecName != secName {
  584. // 校验是否存在相同指标名称
  585. var condition string
  586. var pars []interface{}
  587. condition += " AND SEC_NAME=? AND left(TRADE_CODE,1)='W' AND REMARK='手动' AND TRADE_CODE != ? "
  588. pars = append(pars, secName, req.TradeCode)
  589. count, err := models.GetManualEdbCountByCondition(condition, pars)
  590. if err != nil {
  591. br.Msg = `指标保存失败`
  592. br.ErrMsg = `获取同名指标信息异常,ERR:` + err.Error()
  593. return
  594. }
  595. if count > 0 {
  596. br.Msg = `已存在同名指标`
  597. br.ErrMsg = `已存在同名指标`
  598. br.IsSendEmail = false
  599. return
  600. }
  601. updateCols = append(updateCols, "SecName")
  602. manualEdbInfo.SecName = secName
  603. }
  604. if manualEdbInfo.ClassifyId != req.ClassifyId {
  605. updateCols = append(updateCols, "ClassifyId")
  606. manualEdbInfo.ClassifyId = req.ClassifyId
  607. }
  608. if manualEdbInfo.Frequency != frequency {
  609. updateCols = append(updateCols, "Frequency")
  610. manualEdbInfo.Frequency = frequency
  611. }
  612. if manualEdbInfo.Unit != unit {
  613. updateCols = append(updateCols, "Unit")
  614. manualEdbInfo.Unit = unit
  615. }
  616. if len(updateCols) > 0 {
  617. isUpdateEdb = true
  618. manualEdbInfo.ModifyTime = time.Now().Format(utils.FormatDateTime)
  619. updateCols = append(updateCols, "ModifyTime")
  620. err = manualEdbInfo.Update(updateCols)
  621. if err != nil {
  622. br.Msg = `指标保存失败`
  623. br.ErrMsg = `指标保存失败,ERR:` + err.Error()
  624. return
  625. }
  626. }
  627. }
  628. // 当前已经存在的指标明细数据
  629. targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
  630. if tmpErr != nil {
  631. err = tmpErr
  632. }
  633. existDataMap := make(map[string]string)
  634. deleteDataMap := make(map[string]string)
  635. for _, tmpData := range targetDataList {
  636. existDataMap[tmpData.Dt] = tmpData.Close
  637. deleteDataMap[tmpData.Dt] = tmpData.Dt
  638. }
  639. addDataList := make([]*models.Edbdata, 0)
  640. for createDate, closeVal := range dateValueMap {
  641. if createDate == "" || closeVal == "" {
  642. continue
  643. }
  644. //判断数据是否已经存在
  645. tmpVal, ok3 := existDataMap[createDate]
  646. // 不存在,那么后面进行插入
  647. if !ok3 {
  648. addDataList = append(addDataList, &models.Edbdata{
  649. TradeCode: manualEdbInfo.TradeCode,
  650. Dt: createDate,
  651. Close: closeVal,
  652. ModifyTime: time.Now(),
  653. })
  654. continue
  655. }
  656. delete(deleteDataMap, createDate)
  657. // 库里面的数据
  658. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  659. if tmpErr != nil {
  660. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  661. err = tmpErr
  662. continue
  663. }
  664. // 用户填写的数据
  665. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  666. if tmpErr != nil {
  667. fmt.Println("closeVal Parse err:", tmpErr.Error())
  668. err = tmpErr
  669. continue
  670. }
  671. if !tmpValDecimal.Equal(closeValDecimal) {
  672. isUpdateData = true
  673. fmt.Println("更新数值")
  674. err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
  675. if err != nil {
  676. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  677. errs = append(errs, err.Error())
  678. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  679. }
  680. }
  681. }
  682. // 新增指标
  683. if len(addDataList) > 0 {
  684. isUpdateData = true
  685. err = models.OnlyMultiAddEdbdata(addDataList)
  686. if err != nil {
  687. fmt.Println("MultiAddEdbdata err:", err.Error())
  688. errs = append(errs, err.Error())
  689. }
  690. }
  691. // 删除不需要的日期数据
  692. num := len(deleteDataMap)
  693. if num > 0 {
  694. isUpdateData = true
  695. deleteDateList := make([]string, 0)
  696. for date := range deleteDataMap {
  697. deleteDateList = append(deleteDateList, date)
  698. }
  699. err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
  700. if err != nil {
  701. fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
  702. errs = append(errs, err.Error())
  703. }
  704. }
  705. // 添加记录
  706. if isUpdateEdb {
  707. record := &models.EdbinfoOpRecord{
  708. TradeCode: manualEdbInfo.TradeCode,
  709. Remark: "编辑指标",
  710. UserId: sysUser.AdminId,
  711. UserName: sysUser.RealName,
  712. CreateTime: time.Now(),
  713. }
  714. go func() {
  715. _ = record.Create()
  716. }()
  717. }
  718. // 更新数据
  719. if isUpdateData {
  720. record := &models.EdbinfoOpRecord{
  721. TradeCode: manualEdbInfo.TradeCode,
  722. Remark: "更新数据",
  723. UserId: sysUser.AdminId,
  724. UserName: sysUser.RealName,
  725. CreateTime: time.Now(),
  726. }
  727. go func() {
  728. data.ModifyManualEdbMaxMinDate(manualEdbInfo.TradeCode)
  729. _ = record.Create()
  730. }()
  731. }
  732. //将该指标的code加入到 “手工数据导入后刷新” 缓存
  733. if utils.Re == nil {
  734. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, manualEdbInfo.TradeCode)
  735. if err != nil {
  736. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  737. }
  738. }
  739. }
  740. br.Ret = 200
  741. br.Success = true
  742. br.Msg = "保存成功"
  743. }
  744. // BatchEdbList
  745. // @Title 获取研究员指标
  746. // @Description 获取研究员指标
  747. // @Param request body models.BatchAddEdbReq true "type json string"
  748. // @Success 200 {object} models.TargetItemListResp
  749. // @router /target/edb/batch/list [post]
  750. func (c *ManualEdbController) BatchEdbList() {
  751. br := new(models.BaseResponse).Init()
  752. defer func() {
  753. c.Data["json"] = br
  754. c.ServeJSON()
  755. }()
  756. sysUser := c.SysUser
  757. if sysUser == nil {
  758. br.Msg = "请重新登录"
  759. return
  760. }
  761. var req models.BatchManualEdbReq
  762. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  763. pageSize := req.PageSize
  764. currentIndex := req.CurrentIndex
  765. var startSize int
  766. if pageSize <= 0 {
  767. pageSize = utils.PageSize20
  768. }
  769. if currentIndex <= 0 {
  770. currentIndex = 1
  771. }
  772. startSize = utils.StartIndex(currentIndex, pageSize)
  773. req.Keyword = strings.TrimSpace(req.Keyword)
  774. var condition string
  775. var pars []interface{}
  776. // 是否加到指标库
  777. condition += ` AND a.is_join_edb = ? `
  778. pars = append(pars, 0)
  779. //超管账号可以查看分类下的所有频度数据
  780. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  781. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  782. if err != nil {
  783. br.Msg = "获取分类数据失败"
  784. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  785. return
  786. }
  787. num := len(classifyIdList)
  788. if num > 0 {
  789. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  790. pars = append(pars, classifyIdList)
  791. }
  792. }
  793. // 关键词
  794. if req.Keyword != "" {
  795. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  796. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  797. }
  798. // 所属分类
  799. lenFrequency := len(req.FrequencyList)
  800. if lenFrequency > 0 {
  801. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  802. pars = append(pars, req.FrequencyList)
  803. }
  804. // 所属分类
  805. lenClassify := len(req.ClassifyIdList)
  806. if lenClassify > 0 {
  807. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  808. pars = append(pars, req.ClassifyIdList)
  809. }
  810. // 所属用户
  811. lenUser := len(req.UserIdList)
  812. if lenUser > 0 {
  813. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  814. pars = append(pars, req.UserIdList)
  815. }
  816. total, err := models.GetCountEdbInfoList(condition, pars)
  817. if err != nil {
  818. br.Msg = "获取失败"
  819. br.ErrMsg = "获取失败,Err:" + err.Error()
  820. return
  821. }
  822. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  823. if err != nil {
  824. br.Msg = "获取失败"
  825. br.ErrMsg = "获取失败,Err:" + err.Error()
  826. return
  827. }
  828. resp := models.EdbListResp{
  829. List: list,
  830. Paging: paging.GetPaging(currentIndex, pageSize, total),
  831. }
  832. br.Ret = 200
  833. br.Success = true
  834. br.Msg = "获取成功"
  835. br.Data = resp
  836. br.IsAddLog = false
  837. }
  838. // BatchAddEdbCheck
  839. // @Title 新增校验
  840. // @Description 新增校验
  841. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  842. // @Success 200 string "操作成功"
  843. // @router /target/edb/batch/add/check [post]
  844. func (c *ManualEdbController) BatchAddEdbCheck() {
  845. br := new(models.BaseResponse).Init()
  846. defer func() {
  847. if br.ErrMsg == "" {
  848. br.IsSendEmail = false
  849. }
  850. c.Data["json"] = br
  851. c.ServeJSON()
  852. }()
  853. sysUser := c.SysUser
  854. if sysUser == nil {
  855. br.Msg = "请登录"
  856. br.ErrMsg = "请登录,SysUser Is Empty"
  857. br.Ret = 408
  858. return
  859. }
  860. // 最大批量添加的数量
  861. codeMax := 30
  862. var req models.BatchManualEdbReq
  863. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  864. req.Keyword = strings.TrimSpace(req.Keyword)
  865. var condition string
  866. var pars []interface{}
  867. //
  868. condition += ` AND a.is_join_edb = 0 `
  869. //超管账号可以查看分类下的所有频度数据
  870. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  871. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  872. if err != nil {
  873. br.Msg = "获取分类数据失败"
  874. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  875. return
  876. }
  877. num := len(classifyIdList)
  878. if num > 0 {
  879. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  880. pars = append(pars, classifyIdList)
  881. }
  882. }
  883. if req.ListAll {
  884. // 关键词
  885. if req.Keyword != "" {
  886. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  887. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  888. }
  889. // 所属分类
  890. lenFrequency := len(req.FrequencyList)
  891. if lenFrequency > 0 {
  892. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  893. pars = append(pars, req.FrequencyList)
  894. }
  895. // 所属分类
  896. lenClassify := len(req.ClassifyIdList)
  897. if lenClassify > 0 {
  898. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  899. pars = append(pars, req.ClassifyIdList)
  900. }
  901. // 所属用户
  902. lenUser := len(req.UserIdList)
  903. if lenUser > 0 {
  904. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  905. pars = append(pars, req.UserIdList)
  906. }
  907. lenTradeList := len(req.TradeCodeList)
  908. // 指标
  909. if lenTradeList > 0 {
  910. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  911. pars = append(pars, req.TradeCodeList)
  912. }
  913. } else {
  914. lenTradeList := len(req.TradeCodeList)
  915. if lenTradeList <= 0 {
  916. br.Msg = "请选择指标"
  917. br.ErrMsg = "请选择指标"
  918. return
  919. }
  920. // 指标
  921. if lenTradeList > 0 {
  922. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  923. pars = append(pars, req.TradeCodeList)
  924. }
  925. }
  926. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  927. if err != nil {
  928. br.Msg = "获取失败"
  929. br.ErrMsg = "获取失败,Err:" + err.Error()
  930. return
  931. }
  932. if len(list) > codeMax {
  933. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  934. return
  935. }
  936. br.Data = list
  937. br.Msg = "校验成功"
  938. br.Ret = 200
  939. br.Success = true
  940. }
  941. // NameCheck
  942. // @Title 重名校验
  943. // @Description 批量新增
  944. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  945. // @Success 200 string "操作成功"
  946. // @router /target/edb/batch/add/name_check [post]
  947. func (c *ManualEdbController) NameCheck() {
  948. br := new(models.BaseResponse).Init()
  949. defer func() {
  950. if br.ErrMsg == "" {
  951. br.IsSendEmail = false
  952. }
  953. c.Data["json"] = br
  954. c.ServeJSON()
  955. }()
  956. sysUser := c.SysUser
  957. if sysUser == nil {
  958. br.Msg = "请登录"
  959. br.ErrMsg = "请登录,SysUser Is Empty"
  960. br.Ret = 408
  961. return
  962. }
  963. var req []*data_manage.AddEdbInfoReq
  964. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  965. br.Msg = "参数解析异常!"
  966. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  967. return
  968. }
  969. if len(req) == 0 {
  970. br.Msg = "请选择指标"
  971. return
  972. }
  973. type NameCheckResult struct {
  974. EdbCode string
  975. EdbName string
  976. Exist bool
  977. }
  978. indexNames := make([]string, 0)
  979. resp := make([]*NameCheckResult, 0)
  980. for _, v := range req {
  981. v.EdbCode = strings.TrimSpace(v.EdbCode)
  982. if v.EdbCode == "" {
  983. br.Msg = "指标ID不可为空"
  984. return
  985. }
  986. v.EdbName = strings.TrimSpace(v.EdbName)
  987. if v.EdbName == "" {
  988. br.Msg = "请输入指标名称"
  989. return
  990. }
  991. indexNames = append(indexNames, v.EdbName)
  992. resp = append(resp, &NameCheckResult{
  993. EdbCode: v.EdbCode,
  994. EdbName: v.EdbName,
  995. })
  996. }
  997. // 重名校验
  998. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  999. if e != nil {
  1000. br.Msg = "操作失败"
  1001. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  1002. return
  1003. }
  1004. nameExists := make(map[string]bool)
  1005. for _, v := range edbList {
  1006. nameExists[v.EdbName] = true
  1007. }
  1008. if len(nameExists) > 0 {
  1009. for _, v := range resp {
  1010. v.Exist = nameExists[v.EdbName]
  1011. }
  1012. }
  1013. br.Data = resp
  1014. br.Msg = "操作成功"
  1015. br.Ret = 200
  1016. br.Success = true
  1017. }
  1018. // BatchAdd2Edb
  1019. // @Title 批量新增到指标库
  1020. // @Description 批量新增到指标库
  1021. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  1022. // @Success 200 string "操作成功"
  1023. // @router /target/edb/batch/add [post]
  1024. func (c *ManualEdbController) BatchAdd2Edb() {
  1025. br := new(models.BaseResponse).Init()
  1026. defer func() {
  1027. if br.ErrMsg == "" {
  1028. br.IsSendEmail = false
  1029. }
  1030. c.Data["json"] = br
  1031. c.ServeJSON()
  1032. }()
  1033. sysUser := c.SysUser
  1034. if sysUser == nil {
  1035. br.Msg = "请登录"
  1036. br.ErrMsg = "请登录,SysUser Is Empty"
  1037. br.Ret = 408
  1038. return
  1039. }
  1040. // 最大批量添加的数量
  1041. codeMax := 30
  1042. deleteCache := true
  1043. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
  1044. defer func() {
  1045. if deleteCache {
  1046. _ = utils.Rc.Delete(cacheKey)
  1047. }
  1048. }()
  1049. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1050. deleteCache = false
  1051. br.Msg = "系统处理中,请稍后重试!"
  1052. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  1053. return
  1054. }
  1055. var req []*data_manage.AddEdbInfoReq
  1056. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  1057. br.Msg = "参数解析异常!"
  1058. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1059. return
  1060. }
  1061. if len(req) == 0 {
  1062. br.Msg = "请选择指标"
  1063. return
  1064. }
  1065. if len(req) > codeMax {
  1066. br.Msg = "批量添加指标数量不得超过30个"
  1067. return
  1068. }
  1069. indexNames := make([]string, 0)
  1070. for _, v := range req {
  1071. v.EdbCode = strings.TrimSpace(v.EdbCode)
  1072. if v.EdbCode == "" {
  1073. br.Msg = "指标ID不可为空"
  1074. return
  1075. }
  1076. v.EdbName = strings.TrimSpace(v.EdbName)
  1077. if v.EdbName == "" {
  1078. br.Msg = "请输入指标名称"
  1079. return
  1080. }
  1081. indexNames = append(indexNames, v.EdbName)
  1082. v.Frequency = strings.TrimSpace(v.Frequency)
  1083. if v.Frequency == "" {
  1084. br.Msg = "请选择频度"
  1085. return
  1086. }
  1087. v.Unit = strings.TrimSpace(v.Unit)
  1088. if v.Unit == "" {
  1089. br.Msg = "请输入单位"
  1090. return
  1091. }
  1092. if v.ClassifyId <= 0 {
  1093. br.Msg = "请选择分类"
  1094. return
  1095. }
  1096. }
  1097. // 限定同一时间最多批量新增30个指标
  1098. for _, v := range req {
  1099. var r data.ManualIndexSource2EdbReq
  1100. r.EdbCode = v.EdbCode
  1101. r.EdbName = v.EdbName
  1102. r.Frequency = v.Frequency
  1103. r.Unit = v.Unit
  1104. r.ClassifyId = v.ClassifyId
  1105. r.AdminId = sysUser.AdminId
  1106. r.AdminRealName = sysUser.RealName
  1107. edbInfo, e, errMsg, skip := data.ManualIndexSource2Edb(r, c.Lang)
  1108. if e != nil {
  1109. br.Msg = "操作失败"
  1110. if errMsg != "" {
  1111. br.Msg = errMsg
  1112. }
  1113. br.ErrMsg = e.Error()
  1114. return
  1115. }
  1116. if skip {
  1117. continue
  1118. }
  1119. // 试用平台更新用户累计新增指标数
  1120. if utils.BusinessCode == utils.BusinessCodeSandbox {
  1121. go func() {
  1122. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  1123. if e != nil {
  1124. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  1125. utils.FileLog.Info(tips)
  1126. return
  1127. }
  1128. if adminItem.DepartmentName != "ETA试用客户" {
  1129. return
  1130. }
  1131. var ur etaTrialService.EtaTrialUserReq
  1132. ur.Mobile = adminItem.Mobile
  1133. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  1134. }()
  1135. }
  1136. // 新增操作日志
  1137. {
  1138. edbLog := new(data_manage.EdbInfoLog)
  1139. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1140. edbLog.SourceName = edbInfo.SourceName
  1141. edbLog.Source = edbInfo.Source
  1142. edbLog.EdbCode = edbInfo.EdbCode
  1143. edbLog.EdbName = edbInfo.EdbName
  1144. edbLog.ClassifyId = edbInfo.ClassifyId
  1145. edbLog.SysUserId = sysUser.AdminId
  1146. edbLog.SysUserRealName = sysUser.RealName
  1147. edbLog.CreateTime = time.Now()
  1148. edbLog.Content = string(c.Ctx.Input.RequestBody)
  1149. edbLog.Status = "新增指标"
  1150. edbLog.Method = c.Ctx.Input.URI()
  1151. go data_manage.AddEdbInfoLog(edbLog)
  1152. }
  1153. }
  1154. br.Msg = "操作成功"
  1155. br.Ret = 200
  1156. br.Success = true
  1157. br.IsAddLog = true
  1158. }
  1159. // BatchDelEdbCheck
  1160. // @Title 新增校验
  1161. // @Description 新增校验
  1162. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  1163. // @Success 200 string "操作成功"
  1164. // @router /target/edb/batch/del/check [post]
  1165. func (c *ManualEdbController) BatchDelEdbCheck() {
  1166. br := new(models.BaseResponse).Init()
  1167. defer func() {
  1168. if br.ErrMsg == "" {
  1169. br.IsSendEmail = false
  1170. }
  1171. c.Data["json"] = br
  1172. c.ServeJSON()
  1173. }()
  1174. sysUser := c.SysUser
  1175. if sysUser == nil {
  1176. br.Msg = "请登录"
  1177. br.ErrMsg = "请登录,SysUser Is Empty"
  1178. br.Ret = 408
  1179. return
  1180. }
  1181. var req models.BatchManualEdbReq
  1182. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1183. req.Keyword = strings.TrimSpace(req.Keyword)
  1184. var condition string
  1185. var pars []interface{}
  1186. // 就是为了找出已经加入指标库的指标
  1187. condition += ` AND a.is_join_edb = ? `
  1188. pars = append(pars, 1)
  1189. //超管账号可以查看分类下的所有频度数据
  1190. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1191. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  1192. if err != nil {
  1193. br.Msg = "获取分类数据失败"
  1194. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  1195. return
  1196. }
  1197. num := len(classifyIdList)
  1198. if num > 0 {
  1199. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1200. pars = append(pars, classifyIdList)
  1201. }
  1202. }
  1203. if req.ListAll {
  1204. // 关键词
  1205. if req.Keyword != "" {
  1206. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1207. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  1208. }
  1209. // 所属分类
  1210. lenFrequency := len(req.FrequencyList)
  1211. if lenFrequency > 0 {
  1212. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  1213. pars = append(pars, req.FrequencyList)
  1214. }
  1215. // 所属分类
  1216. lenClassify := len(req.ClassifyIdList)
  1217. if lenClassify > 0 {
  1218. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  1219. pars = append(pars, req.ClassifyIdList)
  1220. }
  1221. // 所属用户
  1222. lenUser := len(req.UserIdList)
  1223. if lenUser > 0 {
  1224. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  1225. pars = append(pars, req.UserIdList)
  1226. }
  1227. lenTradeList := len(req.TradeCodeList)
  1228. // 指标
  1229. if lenTradeList > 0 {
  1230. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1231. pars = append(pars, req.TradeCodeList)
  1232. }
  1233. } else {
  1234. lenTradeList := len(req.TradeCodeList)
  1235. if lenTradeList <= 0 {
  1236. br.Msg = "请选择指标"
  1237. br.ErrMsg = "请选择指标"
  1238. return
  1239. }
  1240. // 指标
  1241. if lenTradeList > 0 {
  1242. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1243. pars = append(pars, req.TradeCodeList)
  1244. }
  1245. }
  1246. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  1247. if err != nil {
  1248. br.Msg = "获取失败"
  1249. br.ErrMsg = "获取失败,Err:" + err.Error()
  1250. return
  1251. }
  1252. br.Data = list
  1253. br.Msg = "校验成功"
  1254. br.Ret = 200
  1255. br.Success = true
  1256. }
  1257. // BatchDel
  1258. // @Title 批量删除
  1259. // @Description 批量新增
  1260. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  1261. // @Success 200 string "操作成功"
  1262. // @router /target/edb/batch/del [post]
  1263. func (c *ManualEdbController) BatchDel() {
  1264. br := new(models.BaseResponse).Init()
  1265. defer func() {
  1266. if br.ErrMsg == "" {
  1267. br.IsSendEmail = false
  1268. }
  1269. c.Data["json"] = br
  1270. c.ServeJSON()
  1271. }()
  1272. sysUser := c.SysUser
  1273. if sysUser == nil {
  1274. br.Msg = "请登录"
  1275. br.ErrMsg = "请登录,SysUser Is Empty"
  1276. br.Ret = 408
  1277. return
  1278. }
  1279. deleteCache := true
  1280. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
  1281. defer func() {
  1282. if deleteCache {
  1283. _ = utils.Rc.Delete(cacheKey)
  1284. }
  1285. }()
  1286. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1287. deleteCache = false
  1288. br.Msg = "系统处理中,请稍后重试!"
  1289. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  1290. return
  1291. }
  1292. var req models.BatchManualEdbReq
  1293. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1294. req.Keyword = strings.TrimSpace(req.Keyword)
  1295. var condition string
  1296. var pars []interface{}
  1297. // 就是为了找出已经加入指标库的指标
  1298. condition += ` AND a.is_join_edb = ? `
  1299. pars = append(pars, 0)
  1300. //超管账号可以查看分类下的所有频度数据
  1301. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1302. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  1303. if err != nil {
  1304. br.Msg = "获取分类数据失败"
  1305. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  1306. return
  1307. }
  1308. num := len(classifyIdList)
  1309. if num > 0 {
  1310. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1311. pars = append(pars, classifyIdList)
  1312. }
  1313. }
  1314. if req.ListAll {
  1315. // 关键词
  1316. if req.Keyword != "" {
  1317. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1318. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  1319. }
  1320. // 所属分类
  1321. lenFrequency := len(req.FrequencyList)
  1322. if lenFrequency > 0 {
  1323. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  1324. pars = append(pars, req.FrequencyList)
  1325. }
  1326. // 所属分类
  1327. lenClassify := len(req.ClassifyIdList)
  1328. if lenClassify > 0 {
  1329. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  1330. pars = append(pars, req.ClassifyIdList)
  1331. }
  1332. // 所属用户
  1333. lenUser := len(req.UserIdList)
  1334. if lenUser > 0 {
  1335. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  1336. pars = append(pars, req.UserIdList)
  1337. }
  1338. lenTradeList := len(req.TradeCodeList)
  1339. // 指标
  1340. if lenTradeList > 0 {
  1341. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1342. pars = append(pars, req.TradeCodeList)
  1343. }
  1344. } else {
  1345. lenTradeList := len(req.TradeCodeList)
  1346. if lenTradeList <= 0 {
  1347. br.Msg = "请选择指标"
  1348. br.ErrMsg = "请选择指标"
  1349. return
  1350. }
  1351. // 指标
  1352. if lenTradeList > 0 {
  1353. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1354. pars = append(pars, req.TradeCodeList)
  1355. }
  1356. }
  1357. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  1358. if err != nil {
  1359. br.Msg = "删除失败"
  1360. br.ErrMsg = "删除失败,Err:" + err.Error()
  1361. return
  1362. }
  1363. // TODO 二次校验指标是否已经加到指标库
  1364. {
  1365. codeList := make([]string, 0)
  1366. // 限定同一时间最多批量新增30个指标
  1367. for _, v := range list {
  1368. codeList = append(codeList, v.TradeCode)
  1369. }
  1370. err = models.DelManualIndexByCodeList(codeList)
  1371. if err != nil {
  1372. br.Msg = "删除失败"
  1373. br.ErrMsg = "删除失败,Err:" + err.Error()
  1374. return
  1375. }
  1376. }
  1377. br.Msg = "删除成功"
  1378. br.Ret = 200
  1379. br.Success = true
  1380. br.IsAddLog = true
  1381. }
  1382. // ImportData
  1383. // @Title 导入数据
  1384. // @Description 导入数据
  1385. // @Param EntryFile query file true "文件"
  1386. // @Success 200 Ret=200 录入成功
  1387. // @router /import/data [post]
  1388. func (c *ManualEdbController) ImportData() {
  1389. br := new(models.BaseResponse).Init()
  1390. defer func() {
  1391. c.Data["json"] = br
  1392. c.ServeJSON()
  1393. }()
  1394. sysUser := c.SysUser
  1395. if sysUser == nil {
  1396. br.Msg = "请重新登录"
  1397. return
  1398. }
  1399. file, _, err := c.GetFile("EntryFile")
  1400. if err != nil {
  1401. br.Msg = "获取文件失败"
  1402. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  1403. return
  1404. }
  1405. path := "./static/数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1406. defer file.Close()
  1407. err = c.SaveToFile("EntryFile", path)
  1408. if err != nil {
  1409. br.Msg = "文件保存失败"
  1410. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1411. return
  1412. }
  1413. if utils.RunMode == "debug" {
  1414. defer os.Remove(path)
  1415. }
  1416. successCount, failCount, indexCount, err, errMsg := data.ImportManualData(path, sysUser)
  1417. if err != nil {
  1418. br.Msg = errMsg
  1419. br.ErrMsg = err.Error()
  1420. return
  1421. }
  1422. resp := models.EdbdataImportResp{
  1423. SuccessCount: successCount,
  1424. FailCount: failCount,
  1425. IndexCount: indexCount,
  1426. }
  1427. if failCount > 0 {
  1428. if successCount == 0 {
  1429. resp.Status = -1
  1430. resp.Msg = "导入失败"
  1431. } else {
  1432. resp.Status = 1
  1433. resp.Msg = "存在部分导入失败"
  1434. }
  1435. } else {
  1436. resp.Status = 0
  1437. }
  1438. br.Msg = "导入成功"
  1439. br.Ret = 200
  1440. br.Success = true
  1441. br.Data = resp
  1442. }
  1443. // RecordList
  1444. // @Title 获取指标操作记录列表
  1445. // @Description 获取指标操作记录列表
  1446. // @Param TradeCode query string false "指标编码"
  1447. // @Success 200 {object} models.EdbinfoOpRecordListResp
  1448. // @router /target/edb/op/record/list [get]
  1449. func (c *ManualEdbController) RecordList() {
  1450. br := new(models.BaseResponse).Init()
  1451. defer func() {
  1452. c.Data["json"] = br
  1453. c.ServeJSON()
  1454. }()
  1455. sysUser := c.SysUser
  1456. if sysUser == nil {
  1457. br.Msg = "请重新登录"
  1458. return
  1459. }
  1460. pageSize, _ := c.GetInt("PageSize")
  1461. currentIndex, _ := c.GetInt("CurrentIndex")
  1462. var startSize int
  1463. if pageSize <= 0 {
  1464. pageSize = utils.PageSize20
  1465. }
  1466. if currentIndex <= 0 {
  1467. currentIndex = 1
  1468. }
  1469. startSize = utils.StartIndex(currentIndex, pageSize)
  1470. // 频度
  1471. tradeCode := c.GetString("TradeCode")
  1472. if tradeCode == `` {
  1473. br.Msg = `请选择指标`
  1474. br.ErrMsg = `请选择指标`
  1475. br.IsSendEmail = false
  1476. return
  1477. }
  1478. total, list, err := models.GetEdbinfoOpRecordPageList(tradeCode, startSize, pageSize)
  1479. if err != nil {
  1480. br.Msg = "获取失败"
  1481. br.ErrMsg = "获取失败,Err:" + err.Error()
  1482. return
  1483. }
  1484. resp := models.EdbinfoOpRecordListResp{
  1485. List: list,
  1486. Paging: paging.GetPaging(currentIndex, pageSize, total),
  1487. }
  1488. br.Ret = 200
  1489. br.Success = true
  1490. br.Msg = "获取成功"
  1491. br.Data = resp
  1492. }
  1493. // BatchSaveExcelData
  1494. // @Title 批量保存手工数据
  1495. // @Description 批量保存手工数据
  1496. // @Param request body []models.ManualEdbExcelStyleEditReq true "type json string"
  1497. // @Success 200
  1498. // @router /target/edb/excel_style/batch_save [post]
  1499. func (c *ManualEdbController) BatchSaveExcelData() {
  1500. br := new(models.BaseResponse).Init()
  1501. var err error
  1502. errs := make([]string, 0)
  1503. defer func() {
  1504. if len(errs) > 0 {
  1505. utils.FileLog.Info("编辑EXCEL数据 新增或修改数据失败,Err:" + strings.Join(errs, "\n"))
  1506. }
  1507. c.Data["json"] = br
  1508. c.ServeJSON()
  1509. }()
  1510. sysUser := c.SysUser
  1511. if sysUser == nil {
  1512. br.Msg = "请重新登录"
  1513. return
  1514. }
  1515. var req []data_manage.ManualEdbExcelStyleEditReq
  1516. err = json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1517. if err != nil {
  1518. br.Msg = "参数解析异常!"
  1519. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1520. return
  1521. }
  1522. countReq := len(req)
  1523. if countReq <= 0 {
  1524. br.Msg = "无数据!"
  1525. br.ErrMsg = "无数据!"
  1526. br.IsSendEmail = false
  1527. return
  1528. }
  1529. if countReq <= 0 {
  1530. br.Msg = "超出上限,导入失败!"
  1531. br.ErrMsg = "超出上限,导入失败!"
  1532. br.IsSendEmail = false
  1533. return
  1534. }
  1535. // 当前来说,所有指标都是同一个分类,所以取的第一个的分类id,后面如果可以做不通分类的配置的话,那就需要做微调
  1536. editClassifyIdList := make([]int, 0)
  1537. tmpClassifyIdMap := make(map[int]bool)
  1538. for _, item := range req {
  1539. classifyId := item.ClassifyId
  1540. if classifyId <= 0 {
  1541. br.Msg = "品种填写异常!"
  1542. br.ErrMsg = "品种填写异常!"
  1543. return
  1544. }
  1545. if _, ok := tmpClassifyIdMap[classifyId]; ok {
  1546. continue
  1547. }
  1548. editClassifyIdList = append(editClassifyIdList, classifyId)
  1549. tmpClassifyIdMap[classifyId] = true
  1550. }
  1551. //超管账号可以查看分类下的所有频度数据
  1552. userId := sysUser.AdminId
  1553. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  1554. userId = 0
  1555. }
  1556. //获取账户所拥有权限的分类id集合
  1557. classifyIdList, err := data.GetUserManualClassifyIdList(userId)
  1558. if err != nil {
  1559. br.Msg = "获取分类数据失败"
  1560. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  1561. return
  1562. }
  1563. for _, classifyId := range editClassifyIdList {
  1564. if !utils.InArrayByInt(classifyIdList, classifyId) {
  1565. br.Msg = "无权访问"
  1566. br.ErrMsg = "无权访问"
  1567. br.IsSendEmail = false
  1568. return
  1569. }
  1570. }
  1571. // 指标map
  1572. targetMap := make(map[string]*models.Edbinfo)
  1573. addEdbTradeMap := make(map[string]bool)
  1574. updateEdbTradeMap := make(map[string]bool)
  1575. updateDataTradeMap := make(map[string]bool)
  1576. defer func() {
  1577. go func(addEdbTradeMap, updateEdbTradeMap, updateDataTradeMap map[string]bool) {
  1578. addRecordList := make([]*models.EdbinfoOpRecord, 0)
  1579. for tradeCode := range addEdbTradeMap {
  1580. addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
  1581. TradeCode: tradeCode,
  1582. Remark: "创建指标",
  1583. UserId: sysUser.AdminId,
  1584. UserName: sysUser.RealName,
  1585. CreateTime: time.Now(),
  1586. })
  1587. }
  1588. for tradeCode := range updateEdbTradeMap {
  1589. addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
  1590. TradeCode: tradeCode,
  1591. Remark: "编辑指标",
  1592. UserId: sysUser.AdminId,
  1593. UserName: sysUser.RealName,
  1594. CreateTime: time.Now(),
  1595. })
  1596. }
  1597. for tradeCode := range updateDataTradeMap {
  1598. addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
  1599. TradeCode: tradeCode,
  1600. Remark: "更新数据",
  1601. UserId: sysUser.AdminId,
  1602. UserName: sysUser.RealName,
  1603. CreateTime: time.Now(),
  1604. })
  1605. }
  1606. if len(addRecordList) > 0 {
  1607. obj := models.EdbinfoOpRecord{}
  1608. _ = obj.MulCreate(addRecordList)
  1609. }
  1610. //将该指标的code加入到 “手工数据导入后刷新” 缓存
  1611. if utils.Re == nil {
  1612. for tradeCode := range updateDataTradeMap {
  1613. // 更新手工数据的最大最小值
  1614. data.ModifyManualEdbMaxMinDate(tradeCode)
  1615. //将该指标的code加入到 “手工数据导入后刷新” 缓存
  1616. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, tradeCode)
  1617. if err != nil {
  1618. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  1619. }
  1620. }
  1621. }
  1622. }(addEdbTradeMap, updateEdbTradeMap, updateDataTradeMap)
  1623. }()
  1624. for _, item := range req {
  1625. if item.Unit == `` {
  1626. br.Msg = "单位填写异常!"
  1627. br.ErrMsg = "单位填写异常!"
  1628. return
  1629. }
  1630. if item.Frequency == `` {
  1631. br.Msg = "频度填写异常!"
  1632. br.ErrMsg = "频度填写异常!"
  1633. return
  1634. }
  1635. if item.ClassifyId <= 0 {
  1636. br.Msg = "品种填写异常!"
  1637. br.ErrMsg = "品种填写异常!"
  1638. return
  1639. }
  1640. // 是否新增指标
  1641. var isAddEdb bool
  1642. secName := strings.TrimSpace(item.EdbName)
  1643. frequency := strings.TrimSpace(item.Frequency)
  1644. unit := strings.TrimSpace(item.Unit)
  1645. manualEdbInfo, ok := targetMap[item.EdbName]
  1646. if !ok {
  1647. manualEdbInfo, err = models.GetTargetBySecName(secName)
  1648. if err != nil {
  1649. //如果是找不到该指标,那么新增指标
  1650. if err.Error() == utils.ErrNoRow() {
  1651. tmpErr := data.AddEdbInfo(secName, unit, frequency, "", sysUser.Mobile, item.ClassifyId, sysUser.AdminId, sysUser.RealName)
  1652. if tmpErr != nil {
  1653. utils.FileLogData.Error("AddEdbInfo err :%s", tmpErr.Error())
  1654. continue
  1655. }
  1656. isAddEdb = true
  1657. tmpTarget, tmpErr := models.GetTargetBySecName(secName)
  1658. manualEdbInfo = tmpTarget
  1659. targetMap[secName] = manualEdbInfo
  1660. // 指标新增
  1661. addEdbTradeMap[manualEdbInfo.TradeCode] = true
  1662. } else {
  1663. fmt.Println("导入数据 获取指标:Err:" + err.Error())
  1664. }
  1665. } else {
  1666. targetMap[secName] = manualEdbInfo
  1667. }
  1668. }
  1669. // 没有该分类品种权限的话,那么就过滤
  1670. if !utils.InArrayByInt(classifyIdList, manualEdbInfo.ClassifyId) {
  1671. continue
  1672. }
  1673. dateValueMap := make(map[string]string)
  1674. //取到所有数据
  1675. for _, dateValue := range item.Data {
  1676. // 检验时间格式
  1677. _, err := time.Parse(utils.FormatDate, dateValue.Date)
  1678. if err != nil {
  1679. continue
  1680. }
  1681. dateValueMap[dateValue.Date] = strconv.FormatFloat(dateValue.Value, 'f', -1, 64)
  1682. }
  1683. //操作指标,新增指标及数据等
  1684. {
  1685. // 如果不是新增指标,那么指标基础信息变更
  1686. if !isAddEdb {
  1687. updateCols := make([]string, 0)
  1688. if manualEdbInfo.SecName != secName {
  1689. // 校验是否存在相同指标名称
  1690. var condition string
  1691. var pars []interface{}
  1692. condition += " AND SEC_NAME=? AND left(TRADE_CODE,1)='W' AND REMARK='手动' AND TRADE_CODE != ? "
  1693. pars = append(pars, secName, manualEdbInfo.TradeCode)
  1694. count, err := models.GetManualEdbCountByCondition(condition, pars)
  1695. if err != nil {
  1696. br.Msg = `指标保存失败`
  1697. br.ErrMsg = `获取同名指标信息异常,ERR:` + err.Error()
  1698. return
  1699. }
  1700. if count > 0 {
  1701. br.Msg = `已存在同名指标`
  1702. br.ErrMsg = `已存在同名指标`
  1703. br.IsSendEmail = false
  1704. return
  1705. }
  1706. updateCols = append(updateCols, "SecName")
  1707. manualEdbInfo.SecName = secName
  1708. }
  1709. if manualEdbInfo.ClassifyId != item.ClassifyId {
  1710. updateCols = append(updateCols, "ClassifyId")
  1711. manualEdbInfo.ClassifyId = item.ClassifyId
  1712. }
  1713. if manualEdbInfo.Frequency != frequency {
  1714. updateCols = append(updateCols, "Frequency")
  1715. manualEdbInfo.Frequency = frequency
  1716. }
  1717. if manualEdbInfo.Unit != unit {
  1718. updateCols = append(updateCols, "Unit")
  1719. manualEdbInfo.Unit = unit
  1720. }
  1721. if len(updateCols) > 0 {
  1722. manualEdbInfo.ModifyTime = time.Now().Format(utils.FormatDateTime)
  1723. updateCols = append(updateCols, "ModifyTime")
  1724. err = manualEdbInfo.Update(updateCols)
  1725. if err != nil {
  1726. br.Msg = `指标保存失败`
  1727. br.ErrMsg = `指标保存失败,ERR:` + err.Error()
  1728. return
  1729. }
  1730. updateEdbTradeMap[manualEdbInfo.TradeCode] = true
  1731. }
  1732. }
  1733. // 当前已经存在的指标明细数据
  1734. targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
  1735. if tmpErr != nil {
  1736. err = tmpErr
  1737. }
  1738. existDataMap := make(map[string]string)
  1739. deleteDataMap := make(map[string]string)
  1740. for _, tmpData := range targetDataList {
  1741. existDataMap[tmpData.Dt] = tmpData.Close
  1742. deleteDataMap[tmpData.Dt] = tmpData.Dt
  1743. }
  1744. var isUpdateData bool
  1745. addDataList := make([]*models.Edbdata, 0)
  1746. for createDate, closeVal := range dateValueMap {
  1747. if createDate == "" || closeVal == "" {
  1748. continue
  1749. }
  1750. //判断数据是否已经存在
  1751. tmpVal, ok3 := existDataMap[createDate]
  1752. // 不存在,那么后面进行插入
  1753. if !ok3 {
  1754. addDataList = append(addDataList, &models.Edbdata{
  1755. TradeCode: manualEdbInfo.TradeCode,
  1756. Dt: createDate,
  1757. Close: closeVal,
  1758. ModifyTime: time.Now(),
  1759. })
  1760. continue
  1761. }
  1762. delete(deleteDataMap, createDate)
  1763. // 库里面的数据
  1764. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  1765. if tmpErr != nil {
  1766. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  1767. err = tmpErr
  1768. continue
  1769. }
  1770. // 用户填写的数据
  1771. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  1772. if tmpErr != nil {
  1773. fmt.Println("closeVal Parse err:", tmpErr.Error())
  1774. err = tmpErr
  1775. continue
  1776. }
  1777. if !tmpValDecimal.Equal(closeValDecimal) {
  1778. isUpdateData = true
  1779. fmt.Println("更新数值")
  1780. err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
  1781. if err != nil {
  1782. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  1783. errs = append(errs, err.Error())
  1784. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  1785. }
  1786. }
  1787. }
  1788. // 新增明细数据
  1789. if len(addDataList) > 0 {
  1790. isUpdateData = true
  1791. err = models.OnlyMultiAddEdbdata(addDataList)
  1792. if err != nil {
  1793. fmt.Println("MultiAddEdbdata err:", err.Error())
  1794. errs = append(errs, err.Error())
  1795. }
  1796. }
  1797. // 删除不需要的日期数据
  1798. num := len(deleteDataMap)
  1799. if num > 0 {
  1800. isUpdateData = true
  1801. deleteDateList := make([]string, 0)
  1802. for date := range deleteDataMap {
  1803. deleteDateList = append(deleteDateList, date)
  1804. }
  1805. err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
  1806. if err != nil {
  1807. fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
  1808. errs = append(errs, err.Error())
  1809. }
  1810. }
  1811. // 更新数据
  1812. if isUpdateData {
  1813. updateDataTradeMap[manualEdbInfo.TradeCode] = true
  1814. }
  1815. }
  1816. }
  1817. br.Ret = 200
  1818. br.Success = true
  1819. br.Msg = "保存成功"
  1820. }