manual_edb.go 38 KB

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