manual_edb.go 52 KB

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