manual_edb.go 41 KB

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