manual_edb.go 38 KB

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