manual_edb.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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. "os"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/rdlucklib/rdluck_tools/paging"
  17. "github.com/shopspring/decimal"
  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. // 检验时间格式
  523. _, err := time.Parse(utils.FormatDate, dateValue.Date)
  524. if err != nil {
  525. continue
  526. }
  527. dateValueMap[dateValue.Date] = strconv.FormatFloat(dateValue.Value, 'f', -1, 64)
  528. }
  529. //操作指标,新增指标及数据等
  530. {
  531. var isUpdateEdb, isUpdateData bool
  532. secName := strings.TrimSpace(req.EdbName)
  533. frequency := strings.TrimSpace(req.Frequency)
  534. unit := strings.TrimSpace(req.Unit)
  535. // 指标基础信息变更
  536. {
  537. updateCols := make([]string, 0)
  538. if manualEdbInfo.SecName != secName {
  539. // 校验是否存在相同指标名称
  540. var condition string
  541. var pars []interface{}
  542. condition += " AND SEC_NAME=? AND left(TRADE_CODE,1)='W' AND REMARK='手动' AND TRADE_CODE != ? "
  543. pars = append(pars, secName, req.TradeCode)
  544. count, err := models.GetManualEdbCountByCondition(condition, pars)
  545. if err != nil {
  546. br.Msg = `指标保存失败`
  547. br.ErrMsg = `获取同名指标信息异常,ERR:` + err.Error()
  548. return
  549. }
  550. if count > 0 {
  551. br.Msg = `已存在同名指标`
  552. br.ErrMsg = `已存在同名指标`
  553. br.IsSendEmail = false
  554. return
  555. }
  556. updateCols = append(updateCols, "SecName")
  557. manualEdbInfo.SecName = secName
  558. }
  559. if manualEdbInfo.ClassifyId != req.ClassifyId {
  560. updateCols = append(updateCols, "ClassifyId")
  561. manualEdbInfo.ClassifyId = req.ClassifyId
  562. }
  563. if manualEdbInfo.Frequency != frequency {
  564. updateCols = append(updateCols, "Frequency")
  565. manualEdbInfo.Frequency = frequency
  566. }
  567. if manualEdbInfo.Unit != unit {
  568. updateCols = append(updateCols, "Unit")
  569. manualEdbInfo.Unit = unit
  570. }
  571. if len(updateCols) > 0 {
  572. isUpdateEdb = true
  573. manualEdbInfo.ModifyTime = time.Now().Format(utils.FormatDateTime)
  574. updateCols = append(updateCols, "ModifyTime")
  575. err = manualEdbInfo.Update(updateCols)
  576. if err != nil {
  577. br.Msg = `指标保存失败`
  578. br.ErrMsg = `指标保存失败,ERR:` + err.Error()
  579. return
  580. }
  581. }
  582. }
  583. // 当前已经存在的指标明细数据
  584. targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
  585. if tmpErr != nil {
  586. err = tmpErr
  587. }
  588. existDataMap := make(map[string]string)
  589. deleteDataMap := make(map[string]string)
  590. for _, tmpData := range targetDataList {
  591. existDataMap[tmpData.Dt] = tmpData.Close
  592. deleteDataMap[tmpData.Dt] = tmpData.Dt
  593. }
  594. addDataList := make([]*models.Edbdata, 0)
  595. for createDate, closeVal := range dateValueMap {
  596. if createDate == "" || closeVal == "" {
  597. continue
  598. }
  599. //判断数据是否已经存在
  600. tmpVal, ok3 := existDataMap[createDate]
  601. // 不存在,那么后面进行插入
  602. if !ok3 {
  603. addDataList = append(addDataList, &models.Edbdata{
  604. TradeCode: manualEdbInfo.TradeCode,
  605. Dt: createDate,
  606. Close: closeVal,
  607. ModifyTime: time.Now(),
  608. })
  609. continue
  610. }
  611. delete(deleteDataMap, createDate)
  612. // 库里面的数据
  613. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  614. if tmpErr != nil {
  615. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  616. err = tmpErr
  617. continue
  618. }
  619. // 用户填写的数据
  620. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  621. if tmpErr != nil {
  622. fmt.Println("closeVal Parse err:", tmpErr.Error())
  623. err = tmpErr
  624. continue
  625. }
  626. if !tmpValDecimal.Equal(closeValDecimal) {
  627. isUpdateData = true
  628. fmt.Println("更新数值")
  629. err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
  630. if err != nil {
  631. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  632. errs = append(errs, err.Error())
  633. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  634. }
  635. }
  636. }
  637. // 新增指标
  638. if len(addDataList) > 0 {
  639. isUpdateData = true
  640. err = models.OnlyMultiAddEdbdata(addDataList)
  641. if err != nil {
  642. fmt.Println("MultiAddEdbdata err:", err.Error())
  643. errs = append(errs, err.Error())
  644. }
  645. }
  646. // 删除不需要的日期数据
  647. num := len(deleteDataMap)
  648. if num > 0 {
  649. isUpdateData = true
  650. deleteDateList := make([]string, 0)
  651. for date := range deleteDataMap {
  652. deleteDateList = append(deleteDateList, date)
  653. }
  654. err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
  655. if err != nil {
  656. fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
  657. errs = append(errs, err.Error())
  658. }
  659. }
  660. // 添加记录
  661. if isUpdateEdb {
  662. record := &models.EdbinfoOpRecord{
  663. TradeCode: manualEdbInfo.TradeCode,
  664. Remark: "编辑指标",
  665. UserId: sysUser.AdminId,
  666. UserName: sysUser.RealName,
  667. CreateTime: time.Now(),
  668. }
  669. go func() {
  670. _ = record.Create()
  671. }()
  672. }
  673. // 更新数据
  674. if isUpdateData {
  675. record := &models.EdbinfoOpRecord{
  676. TradeCode: manualEdbInfo.TradeCode,
  677. Remark: "更新数据",
  678. UserId: sysUser.AdminId,
  679. UserName: sysUser.RealName,
  680. CreateTime: time.Now(),
  681. }
  682. go func() {
  683. data.ModifyManualEdbMaxMinDate(manualEdbInfo.TradeCode)
  684. _ = record.Create()
  685. }()
  686. }
  687. //将该指标的code加入到 “手工数据导入后刷新” 缓存
  688. if utils.Re == nil {
  689. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, manualEdbInfo.TradeCode)
  690. if err != nil {
  691. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  692. }
  693. }
  694. }
  695. br.Ret = 200
  696. br.Success = true
  697. br.Msg = "保存成功"
  698. }
  699. // BatchEdbList
  700. // @Title 获取研究员指标
  701. // @Description 获取研究员指标
  702. // @Param request body models.BatchAddEdbReq true "type json string"
  703. // @Success 200 {object} models.TargetItemListResp
  704. // @router /target/edb/batch/list [post]
  705. func (c *ManualEdbController) BatchEdbList() {
  706. br := new(models.BaseResponse).Init()
  707. defer func() {
  708. c.Data["json"] = br
  709. c.ServeJSON()
  710. }()
  711. sysUser := c.SysUser
  712. if sysUser == nil {
  713. br.Msg = "请重新登录"
  714. return
  715. }
  716. var req models.BatchManualEdbReq
  717. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  718. pageSize := req.PageSize
  719. currentIndex := req.CurrentIndex
  720. var startSize int
  721. if pageSize <= 0 {
  722. pageSize = utils.PageSize20
  723. }
  724. if currentIndex <= 0 {
  725. currentIndex = 1
  726. }
  727. startSize = utils.StartIndex(currentIndex, pageSize)
  728. req.Keyword = strings.TrimSpace(req.Keyword)
  729. var condition string
  730. var pars []interface{}
  731. // 是否加到指标库
  732. condition += ` AND a.is_join_edb = ? `
  733. pars = append(pars, 0)
  734. //超管账号可以查看分类下的所有频度数据
  735. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  736. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  737. if err != nil {
  738. br.Msg = "获取分类数据失败"
  739. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  740. return
  741. }
  742. num := len(classifyIdList)
  743. if num > 0 {
  744. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  745. pars = append(pars, classifyIdList)
  746. }
  747. }
  748. // 关键词
  749. if req.Keyword != "" {
  750. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  751. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  752. }
  753. // 所属分类
  754. lenFrequency := len(req.FrequencyList)
  755. if lenFrequency > 0 {
  756. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  757. pars = append(pars, req.FrequencyList)
  758. }
  759. // 所属分类
  760. lenClassify := len(req.ClassifyIdList)
  761. if lenClassify > 0 {
  762. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  763. pars = append(pars, req.ClassifyIdList)
  764. }
  765. // 所属用户
  766. lenUser := len(req.UserIdList)
  767. if lenUser > 0 {
  768. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  769. pars = append(pars, req.UserIdList)
  770. }
  771. total, err := models.GetCountEdbInfoList(condition, pars)
  772. if err != nil {
  773. br.Msg = "获取失败"
  774. br.ErrMsg = "获取失败,Err:" + err.Error()
  775. return
  776. }
  777. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  778. if err != nil {
  779. br.Msg = "获取失败"
  780. br.ErrMsg = "获取失败,Err:" + err.Error()
  781. return
  782. }
  783. resp := models.EdbListResp{
  784. List: list,
  785. Paging: paging.GetPaging(currentIndex, pageSize, total),
  786. }
  787. br.Ret = 200
  788. br.Success = true
  789. br.Msg = "获取成功"
  790. br.Data = resp
  791. br.IsAddLog = false
  792. }
  793. // BatchAddEdbCheck
  794. // @Title 新增校验
  795. // @Description 新增校验
  796. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  797. // @Success 200 string "操作成功"
  798. // @router /target/edb/batch/add/check [post]
  799. func (c *ManualEdbController) BatchAddEdbCheck() {
  800. br := new(models.BaseResponse).Init()
  801. defer func() {
  802. if br.ErrMsg == "" {
  803. br.IsSendEmail = false
  804. }
  805. c.Data["json"] = br
  806. c.ServeJSON()
  807. }()
  808. sysUser := c.SysUser
  809. if sysUser == nil {
  810. br.Msg = "请登录"
  811. br.ErrMsg = "请登录,SysUser Is Empty"
  812. br.Ret = 408
  813. return
  814. }
  815. // 最大批量添加的数量
  816. codeMax := 30
  817. var req models.BatchManualEdbReq
  818. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  819. req.Keyword = strings.TrimSpace(req.Keyword)
  820. var condition string
  821. var pars []interface{}
  822. //
  823. condition += ` AND a.is_join_edb = 0 `
  824. //超管账号可以查看分类下的所有频度数据
  825. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  826. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  827. if err != nil {
  828. br.Msg = "获取分类数据失败"
  829. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  830. return
  831. }
  832. num := len(classifyIdList)
  833. if num > 0 {
  834. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  835. pars = append(pars, classifyIdList)
  836. }
  837. }
  838. if req.ListAll {
  839. // 关键词
  840. if req.Keyword != "" {
  841. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  842. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  843. }
  844. // 所属分类
  845. lenFrequency := len(req.FrequencyList)
  846. if lenFrequency > 0 {
  847. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  848. pars = append(pars, req.FrequencyList)
  849. }
  850. // 所属分类
  851. lenClassify := len(req.ClassifyIdList)
  852. if lenClassify > 0 {
  853. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  854. pars = append(pars, req.ClassifyIdList)
  855. }
  856. // 所属用户
  857. lenUser := len(req.UserIdList)
  858. if lenUser > 0 {
  859. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  860. pars = append(pars, req.UserIdList)
  861. }
  862. lenTradeList := len(req.TradeCodeList)
  863. // 指标
  864. if lenTradeList > 0 {
  865. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  866. pars = append(pars, req.TradeCodeList)
  867. }
  868. } else {
  869. lenTradeList := len(req.TradeCodeList)
  870. if lenTradeList <= 0 {
  871. br.Msg = "请选择指标"
  872. br.ErrMsg = "请选择指标"
  873. return
  874. }
  875. // 指标
  876. if lenTradeList > 0 {
  877. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  878. pars = append(pars, req.TradeCodeList)
  879. }
  880. }
  881. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  882. if err != nil {
  883. br.Msg = "获取失败"
  884. br.ErrMsg = "获取失败,Err:" + err.Error()
  885. return
  886. }
  887. if len(list) > codeMax {
  888. br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
  889. return
  890. }
  891. br.Data = list
  892. br.Msg = "校验成功"
  893. br.Ret = 200
  894. br.Success = true
  895. }
  896. // NameCheck
  897. // @Title 重名校验
  898. // @Description 批量新增
  899. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  900. // @Success 200 string "操作成功"
  901. // @router /target/edb/batch/add/name_check [post]
  902. func (c *ManualEdbController) NameCheck() {
  903. br := new(models.BaseResponse).Init()
  904. defer func() {
  905. if br.ErrMsg == "" {
  906. br.IsSendEmail = false
  907. }
  908. c.Data["json"] = br
  909. c.ServeJSON()
  910. }()
  911. sysUser := c.SysUser
  912. if sysUser == nil {
  913. br.Msg = "请登录"
  914. br.ErrMsg = "请登录,SysUser Is Empty"
  915. br.Ret = 408
  916. return
  917. }
  918. var req []*data_manage.AddEdbInfoReq
  919. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  920. br.Msg = "参数解析异常!"
  921. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  922. return
  923. }
  924. if len(req) == 0 {
  925. br.Msg = "请选择指标"
  926. return
  927. }
  928. type NameCheckResult struct {
  929. EdbCode string
  930. EdbName string
  931. Exist bool
  932. }
  933. indexNames := make([]string, 0)
  934. resp := make([]*NameCheckResult, 0)
  935. for _, v := range req {
  936. v.EdbCode = strings.TrimSpace(v.EdbCode)
  937. if v.EdbCode == "" {
  938. br.Msg = "指标ID不可为空"
  939. return
  940. }
  941. v.EdbName = strings.TrimSpace(v.EdbName)
  942. if v.EdbName == "" {
  943. br.Msg = "请输入指标名称"
  944. return
  945. }
  946. indexNames = append(indexNames, v.EdbName)
  947. resp = append(resp, &NameCheckResult{
  948. EdbCode: v.EdbCode,
  949. EdbName: v.EdbName,
  950. })
  951. }
  952. // 重名校验
  953. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  954. if e != nil {
  955. br.Msg = "操作失败"
  956. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  957. return
  958. }
  959. nameExists := make(map[string]bool)
  960. for _, v := range edbList {
  961. nameExists[v.EdbName] = true
  962. }
  963. if len(nameExists) > 0 {
  964. for _, v := range resp {
  965. v.Exist = nameExists[v.EdbName]
  966. }
  967. }
  968. br.Data = resp
  969. br.Msg = "操作成功"
  970. br.Ret = 200
  971. br.Success = true
  972. }
  973. // BatchAdd2Edb
  974. // @Title 批量新增到指标库
  975. // @Description 批量新增到指标库
  976. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  977. // @Success 200 string "操作成功"
  978. // @router /target/edb/batch/add [post]
  979. func (c *ManualEdbController) BatchAdd2Edb() {
  980. br := new(models.BaseResponse).Init()
  981. defer func() {
  982. if br.ErrMsg == "" {
  983. br.IsSendEmail = false
  984. }
  985. c.Data["json"] = br
  986. c.ServeJSON()
  987. }()
  988. sysUser := c.SysUser
  989. if sysUser == nil {
  990. br.Msg = "请登录"
  991. br.ErrMsg = "请登录,SysUser Is Empty"
  992. br.Ret = 408
  993. return
  994. }
  995. // 最大批量添加的数量
  996. codeMax := 30
  997. deleteCache := true
  998. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
  999. defer func() {
  1000. if deleteCache {
  1001. _ = utils.Rc.Delete(cacheKey)
  1002. }
  1003. }()
  1004. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1005. deleteCache = false
  1006. br.Msg = "系统处理中,请稍后重试!"
  1007. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  1008. return
  1009. }
  1010. var req []*data_manage.AddEdbInfoReq
  1011. if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
  1012. br.Msg = "参数解析异常!"
  1013. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1014. return
  1015. }
  1016. if len(req) == 0 {
  1017. br.Msg = "请选择指标"
  1018. return
  1019. }
  1020. if len(req) > codeMax {
  1021. br.Msg = "批量添加指标数量不得超过30个"
  1022. return
  1023. }
  1024. indexNames := make([]string, 0)
  1025. for _, v := range req {
  1026. v.EdbCode = strings.TrimSpace(v.EdbCode)
  1027. if v.EdbCode == "" {
  1028. br.Msg = "指标ID不可为空"
  1029. return
  1030. }
  1031. v.EdbName = strings.TrimSpace(v.EdbName)
  1032. if v.EdbName == "" {
  1033. br.Msg = "请输入指标名称"
  1034. return
  1035. }
  1036. indexNames = append(indexNames, v.EdbName)
  1037. v.Frequency = strings.TrimSpace(v.Frequency)
  1038. if v.Frequency == "" {
  1039. br.Msg = "请选择频度"
  1040. return
  1041. }
  1042. v.Unit = strings.TrimSpace(v.Unit)
  1043. if v.Unit == "" {
  1044. br.Msg = "请输入单位"
  1045. return
  1046. }
  1047. if v.ClassifyId <= 0 {
  1048. br.Msg = "请选择分类"
  1049. return
  1050. }
  1051. }
  1052. // 限定同一时间最多批量新增30个指标
  1053. for _, v := range req {
  1054. var r data.ManualIndexSource2EdbReq
  1055. r.EdbCode = v.EdbCode
  1056. r.EdbName = v.EdbName
  1057. r.Frequency = v.Frequency
  1058. r.Unit = v.Unit
  1059. r.ClassifyId = v.ClassifyId
  1060. r.AdminId = sysUser.AdminId
  1061. r.AdminRealName = sysUser.RealName
  1062. edbInfo, e, errMsg, skip := data.ManualIndexSource2Edb(r, c.Lang)
  1063. if e != nil {
  1064. br.Msg = "操作失败"
  1065. if errMsg != "" {
  1066. br.Msg = errMsg
  1067. }
  1068. br.ErrMsg = e.Error()
  1069. return
  1070. }
  1071. if skip {
  1072. continue
  1073. }
  1074. // 试用平台更新用户累计新增指标数
  1075. if utils.BusinessCode == utils.BusinessCodeSandbox {
  1076. go func() {
  1077. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  1078. if e != nil {
  1079. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  1080. utils.FileLog.Info(tips)
  1081. return
  1082. }
  1083. if adminItem.DepartmentName != "ETA试用客户" {
  1084. return
  1085. }
  1086. var ur etaTrialService.EtaTrialUserReq
  1087. ur.Mobile = adminItem.Mobile
  1088. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  1089. }()
  1090. }
  1091. // 新增操作日志
  1092. {
  1093. edbLog := new(data_manage.EdbInfoLog)
  1094. edbLog.EdbInfoId = edbInfo.EdbInfoId
  1095. edbLog.SourceName = edbInfo.SourceName
  1096. edbLog.Source = edbInfo.Source
  1097. edbLog.EdbCode = edbInfo.EdbCode
  1098. edbLog.EdbName = edbInfo.EdbName
  1099. edbLog.ClassifyId = edbInfo.ClassifyId
  1100. edbLog.SysUserId = sysUser.AdminId
  1101. edbLog.SysUserRealName = sysUser.RealName
  1102. edbLog.CreateTime = time.Now()
  1103. edbLog.Content = string(c.Ctx.Input.RequestBody)
  1104. edbLog.Status = "新增指标"
  1105. edbLog.Method = c.Ctx.Input.URI()
  1106. go data_manage.AddEdbInfoLog(edbLog)
  1107. }
  1108. }
  1109. br.Msg = "操作成功"
  1110. br.Ret = 200
  1111. br.Success = true
  1112. br.IsAddLog = true
  1113. }
  1114. // BatchDelEdbCheck
  1115. // @Title 新增校验
  1116. // @Description 新增校验
  1117. // @Param request body data_manage.BatchManualEdbReq true "type json string"
  1118. // @Success 200 string "操作成功"
  1119. // @router /target/edb/batch/del/check [post]
  1120. func (c *ManualEdbController) BatchDelEdbCheck() {
  1121. br := new(models.BaseResponse).Init()
  1122. defer func() {
  1123. if br.ErrMsg == "" {
  1124. br.IsSendEmail = false
  1125. }
  1126. c.Data["json"] = br
  1127. c.ServeJSON()
  1128. }()
  1129. sysUser := c.SysUser
  1130. if sysUser == nil {
  1131. br.Msg = "请登录"
  1132. br.ErrMsg = "请登录,SysUser Is Empty"
  1133. br.Ret = 408
  1134. return
  1135. }
  1136. var req models.BatchManualEdbReq
  1137. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1138. req.Keyword = strings.TrimSpace(req.Keyword)
  1139. var condition string
  1140. var pars []interface{}
  1141. // 就是为了找出已经加入指标库的指标
  1142. condition += ` AND a.is_join_edb = ? `
  1143. pars = append(pars, 1)
  1144. //超管账号可以查看分类下的所有频度数据
  1145. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1146. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  1147. if err != nil {
  1148. br.Msg = "获取分类数据失败"
  1149. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  1150. return
  1151. }
  1152. num := len(classifyIdList)
  1153. if num > 0 {
  1154. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1155. pars = append(pars, classifyIdList)
  1156. }
  1157. }
  1158. if req.ListAll {
  1159. // 关键词
  1160. if req.Keyword != "" {
  1161. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1162. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  1163. }
  1164. // 所属分类
  1165. lenFrequency := len(req.FrequencyList)
  1166. if lenFrequency > 0 {
  1167. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  1168. pars = append(pars, req.FrequencyList)
  1169. }
  1170. // 所属分类
  1171. lenClassify := len(req.ClassifyIdList)
  1172. if lenClassify > 0 {
  1173. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  1174. pars = append(pars, req.ClassifyIdList)
  1175. }
  1176. // 所属用户
  1177. lenUser := len(req.UserIdList)
  1178. if lenUser > 0 {
  1179. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  1180. pars = append(pars, req.UserIdList)
  1181. }
  1182. lenTradeList := len(req.TradeCodeList)
  1183. // 指标
  1184. if lenTradeList > 0 {
  1185. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1186. pars = append(pars, req.TradeCodeList)
  1187. }
  1188. } else {
  1189. lenTradeList := len(req.TradeCodeList)
  1190. if lenTradeList <= 0 {
  1191. br.Msg = "请选择指标"
  1192. br.ErrMsg = "请选择指标"
  1193. return
  1194. }
  1195. // 指标
  1196. if lenTradeList > 0 {
  1197. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1198. pars = append(pars, req.TradeCodeList)
  1199. }
  1200. }
  1201. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  1202. if err != nil {
  1203. br.Msg = "获取失败"
  1204. br.ErrMsg = "获取失败,Err:" + err.Error()
  1205. return
  1206. }
  1207. br.Data = list
  1208. br.Msg = "校验成功"
  1209. br.Ret = 200
  1210. br.Success = true
  1211. }
  1212. // BatchDel
  1213. // @Title 批量删除
  1214. // @Description 批量新增
  1215. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  1216. // @Success 200 string "操作成功"
  1217. // @router /target/edb/batch/del [post]
  1218. func (c *ManualEdbController) BatchDel() {
  1219. br := new(models.BaseResponse).Init()
  1220. defer func() {
  1221. if br.ErrMsg == "" {
  1222. br.IsSendEmail = false
  1223. }
  1224. c.Data["json"] = br
  1225. c.ServeJSON()
  1226. }()
  1227. sysUser := c.SysUser
  1228. if sysUser == nil {
  1229. br.Msg = "请登录"
  1230. br.ErrMsg = "请登录,SysUser Is Empty"
  1231. br.Ret = 408
  1232. return
  1233. }
  1234. deleteCache := true
  1235. cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
  1236. defer func() {
  1237. if deleteCache {
  1238. _ = utils.Rc.Delete(cacheKey)
  1239. }
  1240. }()
  1241. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1242. deleteCache = false
  1243. br.Msg = "系统处理中,请稍后重试!"
  1244. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  1245. return
  1246. }
  1247. var req models.BatchManualEdbReq
  1248. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1249. req.Keyword = strings.TrimSpace(req.Keyword)
  1250. var condition string
  1251. var pars []interface{}
  1252. // 就是为了找出已经加入指标库的指标
  1253. condition += ` AND a.is_join_edb = ? `
  1254. pars = append(pars, 0)
  1255. //超管账号可以查看分类下的所有频度数据
  1256. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1257. classifyIdList, err := data.GetUserManualClassifyIdList(sysUser.AdminId)
  1258. if err != nil {
  1259. br.Msg = "获取分类数据失败"
  1260. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  1261. return
  1262. }
  1263. num := len(classifyIdList)
  1264. if num > 0 {
  1265. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1266. pars = append(pars, classifyIdList)
  1267. }
  1268. }
  1269. if req.ListAll {
  1270. // 关键词
  1271. if req.Keyword != "" {
  1272. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1273. pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
  1274. }
  1275. // 所属分类
  1276. lenFrequency := len(req.FrequencyList)
  1277. if lenFrequency > 0 {
  1278. condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
  1279. pars = append(pars, req.FrequencyList)
  1280. }
  1281. // 所属分类
  1282. lenClassify := len(req.ClassifyIdList)
  1283. if lenClassify > 0 {
  1284. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
  1285. pars = append(pars, req.ClassifyIdList)
  1286. }
  1287. // 所属用户
  1288. lenUser := len(req.UserIdList)
  1289. if lenUser > 0 {
  1290. condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
  1291. pars = append(pars, req.UserIdList)
  1292. }
  1293. lenTradeList := len(req.TradeCodeList)
  1294. // 指标
  1295. if lenTradeList > 0 {
  1296. condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1297. pars = append(pars, req.TradeCodeList)
  1298. }
  1299. } else {
  1300. lenTradeList := len(req.TradeCodeList)
  1301. if lenTradeList <= 0 {
  1302. br.Msg = "请选择指标"
  1303. br.ErrMsg = "请选择指标"
  1304. return
  1305. }
  1306. // 指标
  1307. if lenTradeList > 0 {
  1308. condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
  1309. pars = append(pars, req.TradeCodeList)
  1310. }
  1311. }
  1312. list, err := models.GetEdbInfoList(condition, pars, 0, 0)
  1313. if err != nil {
  1314. br.Msg = "删除失败"
  1315. br.ErrMsg = "删除失败,Err:" + err.Error()
  1316. return
  1317. }
  1318. // TODO 二次校验指标是否已经加到指标库
  1319. {
  1320. codeList := make([]string, 0)
  1321. // 限定同一时间最多批量新增30个指标
  1322. for _, v := range list {
  1323. codeList = append(codeList, v.TradeCode)
  1324. }
  1325. err = models.DelManualIndexByCodeList(codeList)
  1326. if err != nil {
  1327. br.Msg = "删除失败"
  1328. br.ErrMsg = "删除失败,Err:" + err.Error()
  1329. return
  1330. }
  1331. }
  1332. br.Msg = "删除成功"
  1333. br.Ret = 200
  1334. br.Success = true
  1335. br.IsAddLog = true
  1336. }
  1337. // ImportData
  1338. // @Title 导入数据
  1339. // @Description 导入数据
  1340. // @Param EntryFile query file true "文件"
  1341. // @Success 200 Ret=200 录入成功
  1342. // @router /import/data [post]
  1343. func (c *ManualEdbController) ImportData() {
  1344. br := new(models.BaseResponse).Init()
  1345. defer func() {
  1346. c.Data["json"] = br
  1347. c.ServeJSON()
  1348. }()
  1349. sysUser := c.SysUser
  1350. if sysUser == nil {
  1351. br.Msg = "请重新登录"
  1352. return
  1353. }
  1354. file, _, err := c.GetFile("EntryFile")
  1355. if err != nil {
  1356. br.Msg = "获取文件失败"
  1357. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  1358. return
  1359. }
  1360. path := "./static/数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1361. defer file.Close()
  1362. err = c.SaveToFile("EntryFile", path)
  1363. if err != nil {
  1364. br.Msg = "文件保存失败"
  1365. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1366. return
  1367. }
  1368. if utils.RunMode == "debug" {
  1369. defer os.Remove(path)
  1370. }
  1371. successCount, failCount, indexCount, err, errMsg := data.ImportManualData(path, sysUser)
  1372. if err != nil {
  1373. br.Msg = errMsg
  1374. br.ErrMsg = err.Error()
  1375. return
  1376. }
  1377. resp := models.EdbdataImportResp{
  1378. SuccessCount: successCount,
  1379. FailCount: failCount,
  1380. IndexCount: indexCount,
  1381. }
  1382. if failCount > 0 {
  1383. if successCount == 0 {
  1384. resp.Status = -1
  1385. resp.Msg = "导入失败"
  1386. } else {
  1387. resp.Status = 1
  1388. resp.Msg = "存在部分导入失败"
  1389. }
  1390. } else {
  1391. resp.Status = 0
  1392. }
  1393. br.Msg = "导入成功"
  1394. br.Ret = 200
  1395. br.Success = true
  1396. br.Data = resp
  1397. }
  1398. // RecordList
  1399. // @Title 获取指标操作记录列表
  1400. // @Description 获取指标操作记录列表
  1401. // @Param TradeCode query string false "指标编码"
  1402. // @Success 200 {object} models.EdbinfoOpRecordListResp
  1403. // @router /target/edb/op/record/list [get]
  1404. func (c *ManualEdbController) RecordList() {
  1405. br := new(models.BaseResponse).Init()
  1406. defer func() {
  1407. c.Data["json"] = br
  1408. c.ServeJSON()
  1409. }()
  1410. sysUser := c.SysUser
  1411. if sysUser == nil {
  1412. br.Msg = "请重新登录"
  1413. return
  1414. }
  1415. pageSize, _ := c.GetInt("PageSize")
  1416. currentIndex, _ := c.GetInt("CurrentIndex")
  1417. var startSize int
  1418. if pageSize <= 0 {
  1419. pageSize = utils.PageSize20
  1420. }
  1421. if currentIndex <= 0 {
  1422. currentIndex = 1
  1423. }
  1424. startSize = utils.StartIndex(currentIndex, pageSize)
  1425. // 频度
  1426. tradeCode := c.GetString("TradeCode")
  1427. if tradeCode == `` {
  1428. br.Msg = `请选择指标`
  1429. br.ErrMsg = `请选择指标`
  1430. br.IsSendEmail = false
  1431. return
  1432. }
  1433. total, list, err := models.GetEdbinfoOpRecordPageList(tradeCode, startSize, pageSize)
  1434. if err != nil {
  1435. br.Msg = "获取失败"
  1436. br.ErrMsg = "获取失败,Err:" + err.Error()
  1437. return
  1438. }
  1439. resp := models.EdbinfoOpRecordListResp{
  1440. List: list,
  1441. Paging: paging.GetPaging(currentIndex, pageSize, total),
  1442. }
  1443. br.Ret = 200
  1444. br.Success = true
  1445. br.Msg = "获取成功"
  1446. br.Data = resp
  1447. }