manual_edb.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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. etaTrialService "eta/eta_api/services/eta_trial"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "github.com/shopspring/decimal"
  14. "os"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // ManualEdbController 手工指标服务(鉴权)
  20. type ManualEdbController struct {
  21. controllers.BaseAuthController
  22. }
  23. // EdbDetail
  24. // @Title 指标列表
  25. // @Description 指标列表
  26. // @Param TradeCode query string true "指标编码"
  27. // @Success 200 {object} models.TargetDetailResp
  28. // @router /target/edb/detail [get]
  29. func (c *ManualEdbController) EdbDetail() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. c.Data["json"] = br
  33. c.ServeJSON()
  34. }()
  35. sysUser := c.SysUser
  36. if sysUser == nil {
  37. br.Msg = "请重新登录"
  38. return
  39. }
  40. // 指标编码
  41. tradeCode := c.GetString("TradeCode")
  42. if tradeCode == `` {
  43. br.Msg = "请输入指标编码"
  44. br.IsSendEmail = false
  45. return
  46. }
  47. manualEdbInfo, err := models.GetTargetByTradeCode(tradeCode)
  48. if err != nil {
  49. br.Msg = "获取指标失败"
  50. br.ErrMsg = "获取指标失败,err:" + err.Error()
  51. return
  52. }
  53. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  54. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  55. //获取账户所拥有权限的分类id集合
  56. count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
  57. if err != nil {
  58. br.Msg = "获取分类数据失败"
  59. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  60. return
  61. }
  62. if count <= 0 {
  63. br.Msg = "无权访问"
  64. br.ErrMsg = "无权访问"
  65. br.IsSendEmail = false
  66. return
  67. }
  68. }
  69. classifyList := make([]*models.EdbdataClassify, 0)
  70. {
  71. classify, err := models.GetManualClassifyByClassifyId(manualEdbInfo.ClassifyId)
  72. if err != nil {
  73. br.Msg = "获取分类信息失败"
  74. br.ErrMsg = "获取分类信息失败,err:" + err.Error()
  75. return
  76. }
  77. parentClassify, err := models.GetManualClassifyByClassifyId(classify.ParentId)
  78. if err != nil {
  79. br.Msg = "获取分类信息失败"
  80. br.ErrMsg = "获取父级分类信息失败,err:" + err.Error()
  81. return
  82. }
  83. classifyList = []*models.EdbdataClassify{
  84. parentClassify, classify,
  85. }
  86. }
  87. // 明细数据
  88. {
  89. dataList, err := models.GetEdbDataListByCode(manualEdbInfo.TradeCode)
  90. if err != nil {
  91. br.Msg = "获取明细数据失败"
  92. br.ErrMsg = "获取明细数据失败,err:" + err.Error()
  93. return
  94. }
  95. manualEdbInfo.DataList = dataList
  96. }
  97. resp := models.TargetDetailResp{
  98. Detail: manualEdbInfo,
  99. ClassifyList: classifyList,
  100. }
  101. br.Ret = 200
  102. br.Success = true
  103. br.Msg = "获取成功"
  104. //br.Data = resp
  105. br.Data = resp
  106. }
  107. // ClassifyEdbList
  108. // @Title 分类指标列表
  109. // @Description 指标列表
  110. // @Param ClassifyId query string true "分类id"
  111. // @Success 200 {object} models.TargetDetailResp
  112. // @router /target/classify/edb/list [get]
  113. func (c *ManualEdbController) ClassifyEdbList() {
  114. br := new(models.BaseResponse).Init()
  115. defer func() {
  116. c.Data["json"] = br
  117. c.ServeJSON()
  118. }()
  119. sysUser := c.SysUser
  120. if sysUser == nil {
  121. br.Msg = "请登录"
  122. br.Ret = 408
  123. return
  124. }
  125. resp := new(models.EdbdataClassifyResp)
  126. classifyId, _ := c.GetInt("ClassifyId") //分类
  127. if classifyId <= 0 {
  128. br.Msg = "请传入分类"
  129. br.ErrMsg = "请传入分类"
  130. br.IsSendEmail = false
  131. }
  132. var condition string
  133. var pars []interface{}
  134. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  135. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  136. //获取账户所拥有权限的分类id集合
  137. count, err := models.GetCountManualUserClassify(sysUser.AdminId, classifyId)
  138. if err != nil {
  139. br.Msg = "获取分类数据失败"
  140. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  141. return
  142. }
  143. if count <= 0 {
  144. br.Msg = "无权访问"
  145. br.ErrMsg = "无权访问"
  146. br.IsSendEmail = false
  147. return
  148. }
  149. }
  150. list := make([]*models.EdbdataClassifyList, 0)
  151. condition += ` AND a.classify_id = ? `
  152. pars = append(pars, classifyId)
  153. tmpList, err := models.GetEdbInfoList(condition, pars, 0, 0)
  154. if err != nil {
  155. br.Msg = "获取失败"
  156. br.ErrMsg = "获取失败,Err:" + err.Error()
  157. return
  158. }
  159. for _, v := range tmpList {
  160. list = append(list, &models.EdbdataClassifyList{
  161. ClassifyId: 0,
  162. ClassifyName: v.SecName,
  163. ParentId: v.ClassifyId,
  164. Child: nil,
  165. TradeCode: v.TradeCode,
  166. UniqueCode: utils.MD5(v.TradeCode),
  167. })
  168. }
  169. resp.List = list
  170. br.Ret = 200
  171. br.Success = true
  172. br.Msg = "获取成功"
  173. br.Data = resp
  174. }
  175. // EdbSearch
  176. // @Title 指标搜索
  177. // @Description 指标搜索
  178. // @Param Keyword query string false "关键字搜索"
  179. // @Success 200 {object} models.TargetItemListResp
  180. // @router /target/edb/search [get]
  181. func (c *ManualEdbController) EdbSearch() {
  182. br := new(models.BaseResponse).Init()
  183. defer func() {
  184. c.Data["json"] = br
  185. c.ServeJSON()
  186. }()
  187. sysUser := c.SysUser
  188. if sysUser == nil {
  189. br.Msg = "请重新登录"
  190. return
  191. }
  192. pageSize, _ := c.GetInt("PageSize")
  193. currentIndex, _ := c.GetInt("CurrentIndex")
  194. var startSize int
  195. if pageSize <= 0 {
  196. pageSize = utils.PageSize20
  197. }
  198. if currentIndex <= 0 {
  199. currentIndex = 1
  200. }
  201. startSize = utils.StartIndex(currentIndex, pageSize)
  202. //关键字
  203. keyword := c.GetString("Keyword")
  204. var condition string
  205. var pars []interface{}
  206. //userId := sysUser.AdminId
  207. //超管账号可以查看分类下的所有频度数据
  208. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  209. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  210. if err != nil {
  211. br.Msg = "获取分类数据失败"
  212. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  213. return
  214. }
  215. num := len(classifyIdList)
  216. if num > 0 {
  217. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  218. pars = append(pars, classifyIdList)
  219. }
  220. }
  221. if keyword != "" {
  222. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  223. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  224. }
  225. total, err := models.GetCountEdbInfoList(condition, pars)
  226. if err != nil {
  227. br.Msg = "获取失败"
  228. br.ErrMsg = "获取失败,Err:" + err.Error()
  229. return
  230. }
  231. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  232. if err != nil {
  233. br.Msg = "获取失败"
  234. br.ErrMsg = "获取失败,Err:" + err.Error()
  235. return
  236. }
  237. for _, v := range list {
  238. v.UniqueCode = utils.MD5(v.TradeCode)
  239. }
  240. resp := models.EdbListResp{
  241. List: list,
  242. Paging: paging.GetPaging(currentIndex, pageSize, total),
  243. }
  244. br.Ret = 200
  245. br.Success = true
  246. br.Msg = "获取成功"
  247. br.Data = resp
  248. }
  249. // EdbList
  250. // @Title 获取研究员指标
  251. // @Description 获取研究员指标
  252. // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
  253. // @Param Keyword query string false "关键字搜索"
  254. // @Param TradeCode query string false "指标唯一编码"
  255. // @Param ClassifyId query int false "分类id"
  256. // @Param UserId query int false "用户id"
  257. // @Success 200 {object} models.TargetItemListResp
  258. // @router /target/edb/list [get]
  259. func (c *ManualEdbController) EdbList() {
  260. br := new(models.BaseResponse).Init()
  261. defer func() {
  262. c.Data["json"] = br
  263. c.ServeJSON()
  264. }()
  265. sysUser := c.SysUser
  266. if sysUser == nil {
  267. br.Msg = "请重新登录"
  268. return
  269. }
  270. pageSize, _ := c.GetInt("PageSize")
  271. currentIndex, _ := c.GetInt("CurrentIndex")
  272. var startSize int
  273. if pageSize <= 0 {
  274. pageSize = utils.PageSize20
  275. }
  276. if currentIndex <= 0 {
  277. currentIndex = 1
  278. }
  279. startSize = utils.StartIndex(currentIndex, pageSize)
  280. // 频度
  281. frequency := c.GetString("Frequency")
  282. //关键字
  283. keyword := c.GetString("Keyword")
  284. //关键字
  285. classifyId, _ := c.GetInt("ClassifyId", 0)
  286. //用户ID
  287. userId, _ := c.GetInt("UserId", 0)
  288. var condition string
  289. var pars []interface{}
  290. // 权限用户id
  291. permissionUserId := sysUser.AdminId
  292. //超管账号可以查看分类下的所有频度数据
  293. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  294. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  295. if err != nil {
  296. br.Msg = "获取分类数据失败"
  297. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  298. return
  299. }
  300. num := len(classifyIdList)
  301. if num > 0 {
  302. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  303. pars = append(pars, classifyIdList)
  304. }
  305. } else {
  306. permissionUserId = 0
  307. }
  308. // 关键词
  309. if keyword != "" {
  310. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  311. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  312. }
  313. if frequency != "" {
  314. condition += ` AND a.frequency = ?`
  315. pars = append(pars, frequency)
  316. }
  317. // 所属分类
  318. if classifyId > 0 {
  319. // 获取有用权限的分类
  320. classifyList, err := models.GetEdbdataClassify(int64(permissionUserId))
  321. if err != nil {
  322. return
  323. }
  324. var isParent bool
  325. classifyIdList := make([]int, 0)
  326. for _, v := range classifyList {
  327. if v.ClassifyId == classifyId {
  328. isParent = true
  329. classifyIdList = append(classifyIdList, v.ClassifyId)
  330. if v.Child != nil && len(v.Child) > 0 {
  331. for _, vv := range v.Child {
  332. classifyIdList = append(classifyIdList, vv.ClassifyId)
  333. }
  334. }
  335. break
  336. }
  337. }
  338. num := len(classifyIdList)
  339. if num > 0 && isParent {
  340. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  341. pars = append(pars, classifyIdList)
  342. } else {
  343. condition += ` AND a.classify_id = ? `
  344. pars = append(pars, classifyId)
  345. }
  346. }
  347. // 所属用户
  348. if userId > 0 {
  349. condition += ` AND a.user_id = ? `
  350. pars = append(pars, userId)
  351. }
  352. total, err := models.GetCountEdbInfoList(condition, pars)
  353. if err != nil {
  354. br.Msg = "获取失败"
  355. br.ErrMsg = "获取失败,Err:" + err.Error()
  356. return
  357. }
  358. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  359. if err != nil {
  360. br.Msg = "获取失败"
  361. br.ErrMsg = "获取失败,Err:" + err.Error()
  362. return
  363. }
  364. resp := models.EdbListResp{
  365. List: list,
  366. Paging: paging.GetPaging(currentIndex, pageSize, total),
  367. }
  368. br.Ret = 200
  369. br.Success = true
  370. br.Msg = "获取成功"
  371. br.Data = resp
  372. }
  373. // EditExcelData
  374. // @Title 根据excel的样式去编辑指标
  375. // @Description 根据excel的样式去编辑指标
  376. // @Param request body models.ManualEdbExcelStyleEditReq true "type json string"
  377. // @Success 200
  378. // @router /target/edb/excel_style/edit [post]
  379. func (c *ManualEdbController) EditExcelData() {
  380. br := new(models.BaseResponse).Init()
  381. var err error
  382. errs := make([]string, 0)
  383. defer func() {
  384. if len(errs) > 0 {
  385. utils.FileLog.Info("编辑EXCEL数据 新增或修改数据失败,Err:" + strings.Join(errs, "\n"))
  386. }
  387. c.Data["json"] = br
  388. c.ServeJSON()
  389. }()
  390. sysUser := c.SysUser
  391. if sysUser == nil {
  392. br.Msg = "请重新登录"
  393. return
  394. }
  395. var req data_manage.ManualEdbExcelStyleEditReq
  396. err = json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  397. if err != nil {
  398. br.Msg = "参数解析异常!"
  399. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  400. return
  401. }
  402. if req.TradeCode == `` {
  403. br.Msg = "指标编码异常!"
  404. br.ErrMsg = "指标编码异常!"
  405. return
  406. }
  407. if req.Unit == `` {
  408. br.Msg = "单位填写异常!"
  409. br.ErrMsg = "单位填写异常!"
  410. return
  411. }
  412. if req.Frequency == `` {
  413. br.Msg = "频度填写异常!"
  414. br.ErrMsg = "频度填写异常!"
  415. return
  416. }
  417. if req.ClassifyId <= 0 {
  418. br.Msg = "品种填写异常!"
  419. br.ErrMsg = "品种填写异常!"
  420. return
  421. }
  422. // 获取指标信息
  423. manualEdbInfo, err := models.GetEdbinfoByTradeCode(req.TradeCode)
  424. if err != nil {
  425. br.Msg = `找不到该指标`
  426. br.Msg = `找不到该指标,ERR:` + err.Error()
  427. return
  428. }
  429. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  430. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  431. count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
  432. if err != nil {
  433. br.Msg = "获取分类数据失败"
  434. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  435. return
  436. }
  437. if count <= 0 {
  438. br.Msg = "无权访问"
  439. br.ErrMsg = "无权访问"
  440. br.IsSendEmail = false
  441. return
  442. }
  443. }
  444. dateValueMap := make(map[string]string)
  445. //取到所有数据
  446. for _, dateValue := range req.Data {
  447. dateValueMap[dateValue.Date] = strconv.FormatFloat(dateValue.Value, 'f', -1, 64)
  448. }
  449. //操作指标,新增指标及数据等
  450. {
  451. var isUpdateEdb, isUpdateData bool
  452. secName := strings.TrimSpace(req.EdbName)
  453. frequency := strings.TrimSpace(req.Frequency)
  454. unit := strings.TrimSpace(req.Unit)
  455. // 指标基础信息变更
  456. {
  457. updateCols := make([]string, 0)
  458. if manualEdbInfo.SecName != secName {
  459. // 校验是否存在相同指标名称
  460. var condition string
  461. var pars []interface{}
  462. condition += " AND SEC_NAME=? AND left(TRADE_CODE,1)='W' AND REMARK='手动' AND TRADE_CODE != ? "
  463. pars = append(pars, secName, req.TradeCode)
  464. count, err := models.GetManualEdbCountByCondition(condition, pars)
  465. if err != nil {
  466. br.Msg = `指标保存失败`
  467. br.ErrMsg = `获取同名指标信息异常,ERR:` + err.Error()
  468. return
  469. }
  470. if count > 0 {
  471. br.Msg = `已存在同名指标`
  472. br.ErrMsg = `已存在同名指标`
  473. br.IsSendEmail = false
  474. return
  475. }
  476. updateCols = append(updateCols, "SecName")
  477. manualEdbInfo.SecName = secName
  478. }
  479. if manualEdbInfo.ClassifyId != req.ClassifyId {
  480. updateCols = append(updateCols, "ClassifyId")
  481. manualEdbInfo.ClassifyId = req.ClassifyId
  482. }
  483. if manualEdbInfo.Frequency != frequency {
  484. updateCols = append(updateCols, "Frequency")
  485. manualEdbInfo.Frequency = frequency
  486. }
  487. if manualEdbInfo.Unit != unit {
  488. updateCols = append(updateCols, "Unit")
  489. manualEdbInfo.Unit = unit
  490. }
  491. if len(updateCols) > 0 {
  492. isUpdateEdb = true
  493. manualEdbInfo.ModifyTime = time.Now().Format(utils.FormatDateTime)
  494. updateCols = append(updateCols, "ModifyTime")
  495. err = manualEdbInfo.Update(updateCols)
  496. if err != nil {
  497. br.Msg = `指标保存失败`
  498. br.ErrMsg = `指标保存失败,ERR:` + err.Error()
  499. return
  500. }
  501. }
  502. }
  503. // 当前已经存在的指标明细数据
  504. targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
  505. if tmpErr != nil {
  506. err = tmpErr
  507. }
  508. existDataMap := make(map[string]string)
  509. deleteDataMap := make(map[string]string)
  510. for _, tmpData := range targetDataList {
  511. existDataMap[tmpData.Dt] = tmpData.Close
  512. deleteDataMap[tmpData.Dt] = tmpData.Dt
  513. }
  514. addDataList := make([]*models.Edbdata, 0)
  515. for createDate, closeVal := range dateValueMap {
  516. if createDate == "" || closeVal == "" {
  517. continue
  518. }
  519. //判断数据是否已经存在
  520. tmpVal, ok3 := existDataMap[createDate]
  521. // 不存在,那么后面进行插入
  522. if !ok3 {
  523. addDataList = append(addDataList, &models.Edbdata{
  524. TradeCode: manualEdbInfo.TradeCode,
  525. Dt: createDate,
  526. Close: closeVal,
  527. ModifyTime: time.Now(),
  528. })
  529. continue
  530. }
  531. delete(deleteDataMap, createDate)
  532. // 库里面的数据
  533. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  534. if tmpErr != nil {
  535. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  536. err = tmpErr
  537. continue
  538. }
  539. // 用户填写的数据
  540. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  541. if tmpErr != nil {
  542. fmt.Println("closeVal Parse err:", tmpErr.Error())
  543. err = tmpErr
  544. continue
  545. }
  546. if !tmpValDecimal.Equal(closeValDecimal) {
  547. isUpdateData = true
  548. fmt.Println("更新数值")
  549. err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
  550. if err != nil {
  551. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  552. errs = append(errs, err.Error())
  553. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  554. }
  555. }
  556. }
  557. // 新增指标
  558. if len(addDataList) > 0 {
  559. isUpdateData = true
  560. err = models.OnlyMultiAddEdbdata(addDataList)
  561. if err != nil {
  562. fmt.Println("MultiAddEdbdata err:", err.Error())
  563. errs = append(errs, err.Error())
  564. }
  565. }
  566. // 删除不需要的日期数据
  567. num := len(deleteDataMap)
  568. if num > 0 {
  569. isUpdateData = true
  570. deleteDateList := make([]string, 0)
  571. for date := range deleteDataMap {
  572. deleteDateList = append(deleteDateList, date)
  573. }
  574. err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
  575. if err != nil {
  576. fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
  577. errs = append(errs, err.Error())
  578. }
  579. }
  580. // 添加记录
  581. if isUpdateEdb {
  582. record := &models.EdbinfoOpRecord{
  583. TradeCode: manualEdbInfo.TradeCode,
  584. Remark: "编辑指标",
  585. UserId: sysUser.AdminId,
  586. UserName: sysUser.RealName,
  587. CreateTime: time.Now(),
  588. }
  589. go func() {
  590. _ = record.Create()
  591. }()
  592. }
  593. // 更新数据
  594. if isUpdateData {
  595. record := &models.EdbinfoOpRecord{
  596. TradeCode: manualEdbInfo.TradeCode,
  597. Remark: "更新数据",
  598. UserId: sysUser.AdminId,
  599. UserName: sysUser.RealName,
  600. CreateTime: time.Now(),
  601. }
  602. go func() {
  603. data.ModifyManualEdbMaxMinDate(manualEdbInfo.TradeCode)
  604. _ = record.Create()
  605. }()
  606. }
  607. //将该指标的code加入到 “手工数据导入后刷新” 缓存
  608. if utils.Re == nil {
  609. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, manualEdbInfo.TradeCode)
  610. if err != nil {
  611. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  612. }
  613. }
  614. }
  615. br.Ret = 200
  616. br.Success = true
  617. br.Msg = "保存成功"
  618. }
  619. // BatchEdbList
  620. // @Title 获取研究员指标
  621. // @Description 获取研究员指标
  622. // @Param request body models.BatchAddEdbReq true "type json string"
  623. // @Success 200 {object} models.TargetItemListResp
  624. // @router /target/edb/batch/list [post]
  625. func (c *ManualEdbController) BatchEdbList() {
  626. br := new(models.BaseResponse).Init()
  627. defer func() {
  628. c.Data["json"] = br
  629. c.ServeJSON()
  630. }()
  631. sysUser := c.SysUser
  632. if sysUser == nil {
  633. br.Msg = "请重新登录"
  634. return
  635. }
  636. var req models.BatchManualEdbReq
  637. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  638. pageSize := req.PageSize
  639. currentIndex := req.CurrentIndex
  640. var startSize int
  641. if pageSize <= 0 {
  642. pageSize = utils.PageSize20
  643. }
  644. if currentIndex <= 0 {
  645. currentIndex = 1
  646. }
  647. startSize = utils.StartIndex(currentIndex, pageSize)
  648. req.Keyword = strings.TrimSpace(req.Keyword)
  649. var condition string
  650. var pars []interface{}
  651. // 是否加到指标库
  652. condition += ` AND a.is_join_edb = ? `
  653. pars = append(pars, 0)
  654. //超管账号可以查看分类下的所有频度数据
  655. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  656. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  657. if err != nil {
  658. br.Msg = "获取分类数据失败"
  659. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  660. return
  661. }
  662. num := len(classifyIdList)
  663. if num > 0 {
  664. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  665. pars = append(pars, classifyIdList)
  666. }
  667. }
  668. // 关键词
  669. if req.Keyword != "" {
  670. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  671. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  672. }
  673. // 所属分类
  674. lenFrequency := len(req.FrequencyList)
  675. if lenFrequency > 0 {
  676. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  677. pars = append(pars, req.FrequencyList)
  678. }
  679. // 所属分类
  680. lenClassify := len(req.ClassifyIdList)
  681. if lenClassify > 0 {
  682. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  683. pars = append(pars, req.ClassifyIdList)
  684. }
  685. // 所属用户
  686. lenUser := len(req.UserIdList)
  687. if lenUser > 0 {
  688. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  689. pars = append(pars, req.UserIdList)
  690. }
  691. total, err := models.GetCountEdbInfoList(condition, pars)
  692. if err != nil {
  693. br.Msg = "获取失败"
  694. br.ErrMsg = "获取失败,Err:" + err.Error()
  695. return
  696. }
  697. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  698. if err != nil {
  699. br.Msg = "获取失败"
  700. br.ErrMsg = "获取失败,Err:" + err.Error()
  701. return
  702. }
  703. resp := models.EdbListResp{
  704. List: list,
  705. Paging: paging.GetPaging(currentIndex, pageSize, total),
  706. }
  707. br.Ret = 200
  708. br.Success = true
  709. br.Msg = "获取成功"
  710. br.Data = resp
  711. br.IsAddLog = false
  712. }
  713. // BatchAddEdbCheck
  714. // @Title 新增校验
  715. // @Description 新增校验
  716. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  717. // @Success 200 string "操作成功"
  718. // @router /target/edb/batch/add/check [post]
  719. func (c *ManualEdbController) BatchAddEdbCheck() {
  720. br := new(models.BaseResponse).Init()
  721. defer func() {
  722. if br.ErrMsg == "" {
  723. br.IsSendEmail = false
  724. }
  725. c.Data["json"] = br
  726. c.ServeJSON()
  727. }()
  728. sysUser := c.SysUser
  729. if sysUser == nil {
  730. br.Msg = "请登录"
  731. br.ErrMsg = "请登录,SysUser Is Empty"
  732. br.Ret = 408
  733. return
  734. }
  735. // 最大批量添加的数量
  736. codeMax := 30
  737. var req models.BatchManualEdbReq
  738. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  739. req.Keyword = strings.TrimSpace(req.Keyword)
  740. var condition string
  741. var pars []interface{}
  742. //
  743. condition += ` AND a.is_join_edb = 0 `
  744. //超管账号可以查看分类下的所有频度数据
  745. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  746. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  747. if err != nil {
  748. br.Msg = "获取分类数据失败"
  749. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  750. return
  751. }
  752. num := len(classifyIdList)
  753. if num > 0 {
  754. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  755. pars = append(pars, classifyIdList)
  756. }
  757. }
  758. if req.ListAll {
  759. // 关键词
  760. if req.Keyword != "" {
  761. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  762. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  763. }
  764. // 所属分类
  765. lenFrequency := len(req.FrequencyList)
  766. if lenFrequency > 0 {
  767. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  768. pars = append(pars, req.FrequencyList)
  769. }
  770. // 所属分类
  771. lenClassify := len(req.ClassifyIdList)
  772. if lenClassify > 0 {
  773. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  774. pars = append(pars, req.ClassifyIdList)
  775. }
  776. // 所属用户
  777. lenUser := len(req.UserIdList)
  778. if lenUser > 0 {
  779. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  780. pars = append(pars, req.UserIdList)
  781. }
  782. lenTradeList := len(req.TradeCodeList)
  783. // 指标
  784. if lenTradeList > 0 {
  785. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  786. pars = append(pars, req.TradeCodeList)
  787. }
  788. } else {
  789. lenTradeList := len(req.TradeCodeList)
  790. if lenTradeList <= 0 {
  791. br.Msg = "请选择指标"
  792. br.ErrMsg = "请选择指标"
  793. return
  794. }
  795. // 指标
  796. if lenTradeList > 0 {
  797. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  798. pars = append(pars, req.TradeCodeList)
  799. }
  800. }
  801. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  802. if err != nil {
  803. br.Msg = "获取失败"
  804. br.ErrMsg = "获取失败,Err:" + err.Error()
  805. return
  806. }
  807. if len(list) > codeMax {
  808. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  809. return
  810. }
  811. br.Data = list
  812. br.Msg = "校验成功"
  813. br.Ret = 200
  814. br.Success = true
  815. }
  816. // NameCheck
  817. // @Title 重名校验
  818. // @Description 批量新增
  819. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  820. // @Success 200 string "操作成功"
  821. // @router /target/edb/batch/add/name_check [post]
  822. func (c *ManualEdbController) NameCheck() {
  823. br := new(models.BaseResponse).Init()
  824. defer func() {
  825. if br.ErrMsg == "" {
  826. br.IsSendEmail = false
  827. }
  828. c.Data["json"] = br
  829. c.ServeJSON()
  830. }()
  831. sysUser := c.SysUser
  832. if sysUser == nil {
  833. br.Msg = "请登录"
  834. br.ErrMsg = "请登录,SysUser Is Empty"
  835. br.Ret = 408
  836. return
  837. }
  838. var req []*data_manage.AddEdbInfoReq
  839. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  840. br.Msg = "参数解析异常!"
  841. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  842. return
  843. }
  844. if len(req) == 0 {
  845. br.Msg = "请选择指标"
  846. return
  847. }
  848. type NameCheckResult struct {
  849. EdbCode string
  850. EdbName string
  851. Exist bool
  852. }
  853. indexNames := make([]string, 0)
  854. resp := make([]*NameCheckResult, 0)
  855. for _, v := range req {
  856. v.EdbCode = strings.TrimSpace(v.EdbCode)
  857. if v.EdbCode == "" {
  858. br.Msg = "指标ID不可为空"
  859. return
  860. }
  861. v.EdbName = strings.TrimSpace(v.EdbName)
  862. if v.EdbName == "" {
  863. br.Msg = "请输入指标名称"
  864. return
  865. }
  866. indexNames = append(indexNames, v.EdbName)
  867. resp = append(resp, &NameCheckResult{
  868. EdbCode: v.EdbCode,
  869. EdbName: v.EdbName,
  870. })
  871. }
  872. // 重名校验
  873. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  874. if e != nil {
  875. br.Msg = "操作失败"
  876. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  877. return
  878. }
  879. nameExists := make(map[string]bool)
  880. for _, v := range edbList {
  881. nameExists[v.EdbName] = true
  882. }
  883. if len(nameExists) > 0 {
  884. for _, v := range resp {
  885. v.Exist = nameExists[v.EdbName]
  886. }
  887. }
  888. br.Data = resp
  889. br.Msg = "操作成功"
  890. br.Ret = 200
  891. br.Success = true
  892. }
  893. // BatchAdd2Edb
  894. // @Title 批量新增到指标库
  895. // @Description 批量新增到指标库
  896. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  897. // @Success 200 string "操作成功"
  898. // @router /target/edb/batch/add [post]
  899. func (c *ManualEdbController) BatchAdd2Edb() {
  900. br := new(models.BaseResponse).Init()
  901. defer func() {
  902. if br.ErrMsg == "" {
  903. br.IsSendEmail = false
  904. }
  905. c.Data["json"] = br
  906. c.ServeJSON()
  907. }()
  908. sysUser := c.SysUser
  909. if sysUser == nil {
  910. br.Msg = "请登录"
  911. br.ErrMsg = "请登录,SysUser Is Empty"
  912. br.Ret = 408
  913. return
  914. }
  915. // 最大批量添加的数量
  916. codeMax := 30
  917. deleteCache := true
  918. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
  919. defer func() {
  920. if deleteCache {
  921. _ = utils.Rc.Delete(cacheKey)
  922. }
  923. }()
  924. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  925. deleteCache = false
  926. br.Msg = "系统处理中,请稍后重试!"
  927. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  928. return
  929. }
  930. var req []*data_manage.AddEdbInfoReq
  931. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  932. br.Msg = "参数解析异常!"
  933. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  934. return
  935. }
  936. if len(req) == 0 {
  937. br.Msg = "请选择指标"
  938. return
  939. }
  940. if len(req) > codeMax {
  941. br.Msg = "批量添加指标数量不得超过30个"
  942. return
  943. }
  944. indexNames := make([]string, 0)
  945. for _, v := range req {
  946. v.EdbCode = strings.TrimSpace(v.EdbCode)
  947. if v.EdbCode == "" {
  948. br.Msg = "指标ID不可为空"
  949. return
  950. }
  951. v.EdbName = strings.TrimSpace(v.EdbName)
  952. if v.EdbName == "" {
  953. br.Msg = "请输入指标名称"
  954. return
  955. }
  956. indexNames = append(indexNames, v.EdbName)
  957. v.Frequency = strings.TrimSpace(v.Frequency)
  958. if v.Frequency == "" {
  959. br.Msg = "请选择频度"
  960. return
  961. }
  962. v.Unit = strings.TrimSpace(v.Unit)
  963. if v.Unit == "" {
  964. br.Msg = "请输入单位"
  965. return
  966. }
  967. if v.ClassifyId <= 0 {
  968. br.Msg = "请选择分类"
  969. return
  970. }
  971. }
  972. // 限定同一时间最多批量新增30个指标
  973. for _, v := range req {
  974. var r data.ManualIndexSource2EdbReq
  975. r.EdbCode = v.EdbCode
  976. r.EdbName = v.EdbName
  977. r.Frequency = v.Frequency
  978. r.Unit = v.Unit
  979. r.ClassifyId = v.ClassifyId
  980. r.AdminId = sysUser.AdminId
  981. r.AdminRealName = sysUser.RealName
  982. edbInfo, e, errMsg, skip := data.ManualIndexSource2Edb(r, c.Lang)
  983. if e != nil {
  984. br.Msg = "操作失败"
  985. if errMsg != "" {
  986. br.Msg = errMsg
  987. }
  988. br.ErrMsg = e.Error()
  989. return
  990. }
  991. if skip {
  992. continue
  993. }
  994. // 试用平台更新用户累计新增指标数
  995. if utils.BusinessCode == utils.BusinessCodeSandbox {
  996. go func() {
  997. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  998. if e != nil {
  999. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  1000. utils.FileLog.Info(tips)
  1001. return
  1002. }
  1003. if adminItem.DepartmentName != "ETA试用客户" {
  1004. return
  1005. }
  1006. var ur etaTrialService.EtaTrialUserReq
  1007. ur.Mobile = adminItem.Mobile
  1008. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  1009. }()
  1010. }
  1011. // 新增操作日志
  1012. {
  1013. edbLog := new(data_manage.EdbInfoLog)
  1014. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1015. edbLog.SourceName = edbInfo.SourceName
  1016. edbLog.Source = edbInfo.Source
  1017. edbLog.EdbCode = edbInfo.EdbCode
  1018. edbLog.EdbName = edbInfo.EdbName
  1019. edbLog.ClassifyId = edbInfo.ClassifyId
  1020. edbLog.SysUserId = sysUser.AdminId
  1021. edbLog.SysUserRealName = sysUser.RealName
  1022. edbLog.CreateTime = time.Now()
  1023. edbLog.Content = string(c.Ctx.Input.RequestBody)
  1024. edbLog.Status = "新增指标"
  1025. edbLog.Method = c.Ctx.Input.URI()
  1026. go data_manage.AddEdbInfoLog(edbLog)
  1027. }
  1028. }
  1029. br.Msg = "操作成功"
  1030. br.Ret = 200
  1031. br.Success = true
  1032. br.IsAddLog = true
  1033. }
  1034. // BatchDelEdbCheck
  1035. // @Title 新增校验
  1036. // @Description 新增校验
  1037. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  1038. // @Success 200 string "操作成功"
  1039. // @router /target/edb/batch/del/check [post]
  1040. func (c *ManualEdbController) BatchDelEdbCheck() {
  1041. br := new(models.BaseResponse).Init()
  1042. defer func() {
  1043. if br.ErrMsg == "" {
  1044. br.IsSendEmail = false
  1045. }
  1046. c.Data["json"] = br
  1047. c.ServeJSON()
  1048. }()
  1049. sysUser := c.SysUser
  1050. if sysUser == nil {
  1051. br.Msg = "请登录"
  1052. br.ErrMsg = "请登录,SysUser Is Empty"
  1053. br.Ret = 408
  1054. return
  1055. }
  1056. var req models.BatchManualEdbReq
  1057. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1058. req.Keyword = strings.TrimSpace(req.Keyword)
  1059. var condition string
  1060. var pars []interface{}
  1061. // 就是为了找出已经加入指标库的指标
  1062. condition += ` AND a.is_join_edb = ? `
  1063. pars = append(pars, 1)
  1064. //超管账号可以查看分类下的所有频度数据
  1065. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1066. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  1067. if err != nil {
  1068. br.Msg = "获取分类数据失败"
  1069. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  1070. return
  1071. }
  1072. num := len(classifyIdList)
  1073. if num > 0 {
  1074. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1075. pars = append(pars, classifyIdList)
  1076. }
  1077. }
  1078. if req.ListAll {
  1079. // 关键词
  1080. if req.Keyword != "" {
  1081. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1082. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  1083. }
  1084. // 所属分类
  1085. lenFrequency := len(req.FrequencyList)
  1086. if lenFrequency > 0 {
  1087. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  1088. pars = append(pars, req.FrequencyList)
  1089. }
  1090. // 所属分类
  1091. lenClassify := len(req.ClassifyIdList)
  1092. if lenClassify > 0 {
  1093. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  1094. pars = append(pars, req.ClassifyIdList)
  1095. }
  1096. // 所属用户
  1097. lenUser := len(req.UserIdList)
  1098. if lenUser > 0 {
  1099. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  1100. pars = append(pars, req.UserIdList)
  1101. }
  1102. lenTradeList := len(req.TradeCodeList)
  1103. // 指标
  1104. if lenTradeList > 0 {
  1105. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1106. pars = append(pars, req.TradeCodeList)
  1107. }
  1108. } else {
  1109. lenTradeList := len(req.TradeCodeList)
  1110. if lenTradeList <= 0 {
  1111. br.Msg = "请选择指标"
  1112. br.ErrMsg = "请选择指标"
  1113. return
  1114. }
  1115. // 指标
  1116. if lenTradeList > 0 {
  1117. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1118. pars = append(pars, req.TradeCodeList)
  1119. }
  1120. }
  1121. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  1122. if err != nil {
  1123. br.Msg = "获取失败"
  1124. br.ErrMsg = "获取失败,Err:" + err.Error()
  1125. return
  1126. }
  1127. br.Data = list
  1128. br.Msg = "校验成功"
  1129. br.Ret = 200
  1130. br.Success = true
  1131. }
  1132. // BatchDel
  1133. // @Title 批量删除
  1134. // @Description 批量新增
  1135. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  1136. // @Success 200 string "操作成功"
  1137. // @router /target/edb/batch/del [post]
  1138. func (c *ManualEdbController) BatchDel() {
  1139. br := new(models.BaseResponse).Init()
  1140. defer func() {
  1141. if br.ErrMsg == "" {
  1142. br.IsSendEmail = false
  1143. }
  1144. c.Data["json"] = br
  1145. c.ServeJSON()
  1146. }()
  1147. sysUser := c.SysUser
  1148. if sysUser == nil {
  1149. br.Msg = "请登录"
  1150. br.ErrMsg = "请登录,SysUser Is Empty"
  1151. br.Ret = 408
  1152. return
  1153. }
  1154. deleteCache := true
  1155. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
  1156. defer func() {
  1157. if deleteCache {
  1158. _ = utils.Rc.Delete(cacheKey)
  1159. }
  1160. }()
  1161. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1162. deleteCache = false
  1163. br.Msg = "系统处理中,请稍后重试!"
  1164. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  1165. return
  1166. }
  1167. var req models.BatchManualEdbReq
  1168. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1169. req.Keyword = strings.TrimSpace(req.Keyword)
  1170. var condition string
  1171. var pars []interface{}
  1172. // 就是为了找出已经加入指标库的指标
  1173. condition += ` AND a.is_join_edb = ? `
  1174. pars = append(pars, 0)
  1175. //超管账号可以查看分类下的所有频度数据
  1176. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1177. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  1178. if err != nil {
  1179. br.Msg = "获取分类数据失败"
  1180. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  1181. return
  1182. }
  1183. num := len(classifyIdList)
  1184. if num > 0 {
  1185. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1186. pars = append(pars, classifyIdList)
  1187. }
  1188. }
  1189. if req.ListAll {
  1190. // 关键词
  1191. if req.Keyword != "" {
  1192. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1193. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  1194. }
  1195. // 所属分类
  1196. lenFrequency := len(req.FrequencyList)
  1197. if lenFrequency > 0 {
  1198. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  1199. pars = append(pars, req.FrequencyList)
  1200. }
  1201. // 所属分类
  1202. lenClassify := len(req.ClassifyIdList)
  1203. if lenClassify > 0 {
  1204. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  1205. pars = append(pars, req.ClassifyIdList)
  1206. }
  1207. // 所属用户
  1208. lenUser := len(req.UserIdList)
  1209. if lenUser > 0 {
  1210. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  1211. pars = append(pars, req.UserIdList)
  1212. }
  1213. lenTradeList := len(req.TradeCodeList)
  1214. // 指标
  1215. if lenTradeList > 0 {
  1216. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1217. pars = append(pars, req.TradeCodeList)
  1218. }
  1219. } else {
  1220. lenTradeList := len(req.TradeCodeList)
  1221. if lenTradeList <= 0 {
  1222. br.Msg = "请选择指标"
  1223. br.ErrMsg = "请选择指标"
  1224. return
  1225. }
  1226. // 指标
  1227. if lenTradeList > 0 {
  1228. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1229. pars = append(pars, req.TradeCodeList)
  1230. }
  1231. }
  1232. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  1233. if err != nil {
  1234. br.Msg = "删除失败"
  1235. br.ErrMsg = "删除失败,Err:" + err.Error()
  1236. return
  1237. }
  1238. // TODO 二次校验指标是否已经加到指标库
  1239. {
  1240. codeList := make([]string, 0)
  1241. // 限定同一时间最多批量新增30个指标
  1242. for _, v := range list {
  1243. codeList = append(codeList, v.TradeCode)
  1244. }
  1245. err = models.DelManualIndexByCodeList(codeList)
  1246. if err != nil {
  1247. br.Msg = "删除失败"
  1248. br.ErrMsg = "删除失败,Err:" + err.Error()
  1249. return
  1250. }
  1251. }
  1252. br.Msg = "删除成功"
  1253. br.Ret = 200
  1254. br.Success = true
  1255. br.IsAddLog = true
  1256. }
  1257. // ImportData
  1258. // @Title 导入数据
  1259. // @Description 导入数据
  1260. // @Param EntryFile query file true "文件"
  1261. // @Success 200 Ret=200 录入成功
  1262. // @router /import/data [post]
  1263. func (c *ManualEdbController) ImportData() {
  1264. br := new(models.BaseResponse).Init()
  1265. defer func() {
  1266. c.Data["json"] = br
  1267. c.ServeJSON()
  1268. }()
  1269. sysUser := c.SysUser
  1270. if sysUser == nil {
  1271. br.Msg = "请重新登录"
  1272. return
  1273. }
  1274. file, _, err := c.GetFile("EntryFile")
  1275. if err != nil {
  1276. br.Msg = "获取文件失败"
  1277. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  1278. return
  1279. }
  1280. path := "./static/数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1281. defer file.Close()
  1282. err = c.SaveToFile("EntryFile", path)
  1283. if err != nil {
  1284. br.Msg = "文件保存失败"
  1285. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1286. return
  1287. }
  1288. if utils.RunMode == "debug" {
  1289. defer os.Remove(path)
  1290. }
  1291. successCount, failCount, err, errMsg := data.ImportManualData(path, sysUser)
  1292. if err != nil {
  1293. br.Msg = errMsg
  1294. br.ErrMsg = err.Error()
  1295. return
  1296. }
  1297. resp := models.EdbdataImportResp{
  1298. SuccessCount: successCount,
  1299. FailCount: failCount,
  1300. }
  1301. if failCount > 0 {
  1302. if successCount == 0 {
  1303. resp.Status = -1
  1304. resp.Msg = "导入失败"
  1305. } else {
  1306. resp.Status = 1
  1307. resp.Msg = "存在部分导入失败"
  1308. }
  1309. } else {
  1310. resp.Status = 0
  1311. }
  1312. br.Msg = "导入成功"
  1313. br.Ret = 200
  1314. br.Success = true
  1315. br.Data = resp
  1316. }
  1317. // RecordList
  1318. // @Title 获取指标操作记录列表
  1319. // @Description 获取指标操作记录列表
  1320. // @Param TradeCode query string false "指标编码"
  1321. // @Success 200 {object} models.EdbinfoOpRecordListResp
  1322. // @router /target/edb/op/record/list [get]
  1323. func (c *ManualEdbController) RecordList() {
  1324. br := new(models.BaseResponse).Init()
  1325. defer func() {
  1326. c.Data["json"] = br
  1327. c.ServeJSON()
  1328. }()
  1329. sysUser := c.SysUser
  1330. if sysUser == nil {
  1331. br.Msg = "请重新登录"
  1332. return
  1333. }
  1334. pageSize, _ := c.GetInt("PageSize")
  1335. currentIndex, _ := c.GetInt("CurrentIndex")
  1336. var startSize int
  1337. if pageSize <= 0 {
  1338. pageSize = utils.PageSize20
  1339. }
  1340. if currentIndex <= 0 {
  1341. currentIndex = 1
  1342. }
  1343. startSize = utils.StartIndex(currentIndex, pageSize)
  1344. // 频度
  1345. tradeCode := c.GetString("TradeCode")
  1346. if tradeCode == `` {
  1347. br.Msg = `请选择指标`
  1348. br.ErrMsg = `请选择指标`
  1349. br.IsSendEmail = false
  1350. return
  1351. }
  1352. total, list, err := models.GetEdbinfoOpRecordPageList(tradeCode, startSize, pageSize)
  1353. if err != nil {
  1354. br.Msg = "获取失败"
  1355. br.ErrMsg = "获取失败,Err:" + err.Error()
  1356. return
  1357. }
  1358. resp := models.EdbinfoOpRecordListResp{
  1359. List: list,
  1360. Paging: paging.GetPaging(currentIndex, pageSize, total),
  1361. }
  1362. br.Ret = 200
  1363. br.Success = true
  1364. br.Msg = "获取成功"
  1365. br.Data = resp
  1366. }