manual_edb.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "github.com/shopspring/decimal"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // EdbDetail
  15. // @Title 指标列表
  16. // @Description 指标列表
  17. // @Param TradeCode query string true "指标编码"
  18. // @Success 200 {object} models.TargetDetailResp
  19. // @router /target/edb/detail [get]
  20. func (this *TargetController) EdbDetail() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. sysUser := this.SysUser
  27. if sysUser == nil {
  28. br.Msg = "请重新登录"
  29. return
  30. }
  31. // 指标编码
  32. tradeCode := this.GetString("TradeCode")
  33. if tradeCode == `` {
  34. br.Msg = "请输入指标编码"
  35. br.IsSendEmail = false
  36. return
  37. }
  38. manualEdbInfo, err := models.GetTargetByTradeCode(tradeCode)
  39. if err != nil {
  40. br.Msg = "获取指标失败"
  41. br.ErrMsg = "获取指标失败,err:" + err.Error()
  42. return
  43. }
  44. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  45. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  46. //获取账户所拥有权限的分类id集合
  47. count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
  48. if err != nil {
  49. br.Msg = "获取分类数据失败"
  50. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  51. return
  52. }
  53. if count <= 0 {
  54. br.Msg = "无权访问"
  55. br.ErrMsg = "无权访问"
  56. br.IsSendEmail = false
  57. return
  58. }
  59. }
  60. classifyList := make([]*models.EdbdataClassify, 0)
  61. {
  62. classify, err := models.GetManualClassifyByClassifyId(manualEdbInfo.ClassifyId)
  63. if err != nil {
  64. br.Msg = "获取分类信息失败"
  65. br.ErrMsg = "获取分类信息失败,err:" + err.Error()
  66. return
  67. }
  68. parentClassify, err := models.GetManualClassifyByClassifyId(classify.ParentId)
  69. if err != nil {
  70. br.Msg = "获取分类信息失败"
  71. br.ErrMsg = "获取父级分类信息失败,err:" + err.Error()
  72. return
  73. }
  74. classifyList = []*models.EdbdataClassify{
  75. parentClassify, classify,
  76. }
  77. }
  78. // 明细数据
  79. {
  80. dataList, err := models.GetEdbDataListByCode(manualEdbInfo.TradeCode)
  81. if err != nil {
  82. br.Msg = "获取明细数据失败"
  83. br.ErrMsg = "获取明细数据失败,err:" + err.Error()
  84. return
  85. }
  86. manualEdbInfo.DataList = dataList
  87. }
  88. resp := models.TargetDetailResp{
  89. Detail: manualEdbInfo,
  90. ClassifyList: classifyList,
  91. }
  92. br.Ret = 200
  93. br.Success = true
  94. br.Msg = "获取成功"
  95. //br.Data = resp
  96. br.Data = resp
  97. }
  98. // ClassifyEdbList
  99. // @Title 分类指标列表
  100. // @Description 指标列表
  101. // @Param ClassifyId query string true "分类id"
  102. // @Success 200 {object} models.TargetDetailResp
  103. // @router /target/classify/edb/list [get]
  104. func (this *TargetController) ClassifyEdbList() {
  105. br := new(models.BaseResponse).Init()
  106. defer func() {
  107. this.Data["json"] = br
  108. this.ServeJSON()
  109. }()
  110. sysUser := this.SysUser
  111. if sysUser == nil {
  112. br.Msg = "请登录"
  113. br.Ret = 408
  114. return
  115. }
  116. resp := new(models.EdbdataClassifyResp)
  117. classifyId, _ := this.GetInt("ClassifyId") //分类
  118. if classifyId <= 0 {
  119. br.Msg = "请传入分类"
  120. br.ErrMsg = "请传入分类"
  121. br.IsSendEmail = false
  122. }
  123. var condition string
  124. var pars []interface{}
  125. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  126. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  127. //获取账户所拥有权限的分类id集合
  128. count, err := models.GetCountManualUserClassify(sysUser.AdminId, classifyId)
  129. if err != nil {
  130. br.Msg = "获取分类数据失败"
  131. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  132. return
  133. }
  134. if count <= 0 {
  135. br.Msg = "无权访问"
  136. br.ErrMsg = "无权访问"
  137. br.IsSendEmail = false
  138. return
  139. }
  140. }
  141. list := make([]*models.EdbdataClassifyList, 0)
  142. condition += ` AND a.classify_id = ? `
  143. pars = append(pars, classifyId)
  144. tmpList, err := models.GetEdbInfoList(condition, pars, 0, 0)
  145. if err != nil {
  146. br.Msg = "获取失败"
  147. br.ErrMsg = "获取失败,Err:" + err.Error()
  148. return
  149. }
  150. for _, v := range tmpList {
  151. list = append(list, &models.EdbdataClassifyList{
  152. ClassifyId: 0,
  153. ClassifyName: v.SecName,
  154. ParentId: v.ClassifyId,
  155. Child: nil,
  156. TradeCode: v.TradeCode,
  157. UniqueCode: utils.MD5(v.TradeCode),
  158. })
  159. }
  160. resp.List = list
  161. br.Ret = 200
  162. br.Success = true
  163. br.Msg = "获取成功"
  164. br.Data = resp
  165. }
  166. // EdbSearch
  167. // @Title 指标搜索
  168. // @Description 指标搜索
  169. // @Param Keyword query string false "关键字搜索"
  170. // @Success 200 {object} models.TargetItemListResp
  171. // @router /target/edb/search [get]
  172. func (this *TargetController) EdbSearch() {
  173. br := new(models.BaseResponse).Init()
  174. defer func() {
  175. this.Data["json"] = br
  176. this.ServeJSON()
  177. }()
  178. sysUser := this.SysUser
  179. if sysUser == nil {
  180. br.Msg = "请重新登录"
  181. return
  182. }
  183. pageSize, _ := this.GetInt("PageSize")
  184. currentIndex, _ := this.GetInt("CurrentIndex")
  185. var startSize int
  186. if pageSize <= 0 {
  187. pageSize = utils.PageSize20
  188. }
  189. if currentIndex <= 0 {
  190. currentIndex = 1
  191. }
  192. startSize = utils.StartIndex(currentIndex, pageSize)
  193. //关键字
  194. keyword := this.GetString("Keyword")
  195. var condition string
  196. var pars []interface{}
  197. userId := sysUser.AdminId
  198. //超管账号可以查看分类下的所有频度数据
  199. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  200. classifyList, err := models.GetEdbdataClassify(int64(userId))
  201. if err != nil {
  202. br.Msg = "获取分类数据失败"
  203. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  204. return
  205. }
  206. num := len(classifyList)
  207. if num > 0 {
  208. classifyIdList := make([]int, 0)
  209. for _, v := range classifyList {
  210. classifyIdList = append(classifyIdList, v.ClassifyId)
  211. }
  212. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  213. pars = append(pars, classifyIdList)
  214. }
  215. }
  216. if keyword != "" {
  217. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  218. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  219. }
  220. total, err := models.GetCountEdbInfoList(condition, pars)
  221. if err != nil {
  222. br.Msg = "获取失败"
  223. br.ErrMsg = "获取失败,Err:" + err.Error()
  224. return
  225. }
  226. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  227. if err != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = "获取失败,Err:" + err.Error()
  230. return
  231. }
  232. for _, v := range list {
  233. v.UniqueCode = utils.MD5(v.TradeCode)
  234. }
  235. resp := models.EdbListResp{
  236. List: list,
  237. Paging: paging.GetPaging(currentIndex, pageSize, total),
  238. }
  239. br.Ret = 200
  240. br.Success = true
  241. br.Msg = "获取成功"
  242. br.Data = resp
  243. }
  244. // EdbList
  245. // @Title 获取研究员指标
  246. // @Description 获取研究员指标
  247. // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
  248. // @Param Keyword query string false "关键字搜索"
  249. // @Param TradeCode query string false "指标唯一编码"
  250. // @Param ClassifyId query int false "分类id"
  251. // @Param EdbShowType query string true "指标展示类型:0,1,2; 0:不管有没有数据,指标都展示出来, 1:只展示有数据的指标, 2:只展示没有数据的指标"
  252. // @Success 200 {object} models.TargetItemListResp
  253. // @router /target/edb/list [get]
  254. func (this *TargetController) EdbList() {
  255. br := new(models.BaseResponse).Init()
  256. defer func() {
  257. this.Data["json"] = br
  258. this.ServeJSON()
  259. }()
  260. sysUser := this.SysUser
  261. if sysUser == nil {
  262. br.Msg = "请重新登录"
  263. return
  264. }
  265. pageSize, _ := this.GetInt("PageSize")
  266. currentIndex, _ := this.GetInt("CurrentIndex")
  267. var startSize int
  268. if pageSize <= 0 {
  269. pageSize = utils.PageSize20
  270. }
  271. if currentIndex <= 0 {
  272. currentIndex = 1
  273. }
  274. startSize = utils.StartIndex(currentIndex, pageSize)
  275. //关键字
  276. keyword := this.GetString("Keyword")
  277. //关键字
  278. classifyId, _ := this.GetInt("ClassifyId", 0)
  279. var condition string
  280. var pars []interface{}
  281. userId := sysUser.AdminId
  282. //超管账号可以查看分类下的所有频度数据
  283. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  284. classifyList, err := models.GetEdbdataClassify(int64(userId))
  285. if err != nil {
  286. br.Msg = "获取分类数据失败"
  287. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  288. return
  289. }
  290. num := len(classifyList)
  291. if num > 0 {
  292. classifyIdList := make([]int, 0)
  293. for _, v := range classifyList {
  294. classifyIdList = append(classifyIdList, v.ClassifyId)
  295. }
  296. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  297. pars = append(pars, classifyIdList)
  298. }
  299. }
  300. // 关键词
  301. if keyword != "" {
  302. condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  303. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  304. }
  305. // 所属分类
  306. if classifyId > 0 {
  307. condition += ` AND a.classify_id = ? `
  308. pars = append(pars, classifyId)
  309. }
  310. total, err := models.GetCountEdbInfoList(condition, pars)
  311. if err != nil {
  312. br.Msg = "获取失败"
  313. br.ErrMsg = "获取失败,Err:" + err.Error()
  314. return
  315. }
  316. list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
  317. if err != nil {
  318. br.Msg = "获取失败"
  319. br.ErrMsg = "获取失败,Err:" + err.Error()
  320. return
  321. }
  322. resp := models.EdbListResp{
  323. List: list,
  324. Paging: paging.GetPaging(currentIndex, pageSize, total),
  325. }
  326. br.Ret = 200
  327. br.Success = true
  328. br.Msg = "获取成功"
  329. br.Data = resp
  330. }
  331. // EditExcelData
  332. // @Title 根据excel的样式去编辑指标
  333. // @Description 根据excel的样式去编辑指标
  334. // @Param request body models.ManualEdbExcelStyleEditReq true "type json string"
  335. // @Success 200
  336. // @router /target/edb/excel_style/edit [post]
  337. func (this *TargetController) EditExcelData() {
  338. br := new(models.BaseResponse).Init()
  339. var err error
  340. errs := make([]string, 0)
  341. defer func() {
  342. if len(errs) > 0 {
  343. utils.FileLog.Info("编辑EXCEL数据 新增或修改数据失败,Err:" + strings.Join(errs, "\n"))
  344. }
  345. this.Data["json"] = br
  346. this.ServeJSON()
  347. }()
  348. sysUser := this.SysUser
  349. if sysUser == nil {
  350. br.Msg = "请重新登录"
  351. return
  352. }
  353. var req data_manage.ManualEdbExcelStyleEditReq
  354. err = json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  355. if err != nil {
  356. br.Msg = "参数解析异常!"
  357. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  358. return
  359. }
  360. if req.TradeCode == `` {
  361. br.Msg = "指标编码异常!"
  362. br.ErrMsg = "指标编码异常!"
  363. return
  364. }
  365. if req.Unit == `` {
  366. br.Msg = "单位填写异常!"
  367. br.ErrMsg = "单位填写异常!"
  368. return
  369. }
  370. if req.Frequency == `` {
  371. br.Msg = "频度填写异常!"
  372. br.ErrMsg = "频度填写异常!"
  373. return
  374. }
  375. if req.ClassifyId <= 0 {
  376. br.Msg = "品种填写异常!"
  377. br.ErrMsg = "品种填写异常!"
  378. return
  379. }
  380. // 获取指标信息
  381. manualEdbInfo, err := models.GetEdbinfoByTradeCode(req.TradeCode)
  382. if err != nil {
  383. br.Msg = `找不到该指标`
  384. br.Msg = `找不到该指标,ERR:` + err.Error()
  385. return
  386. }
  387. // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
  388. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  389. //获取账户所拥有权限的分类id集合
  390. count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
  391. if err != nil {
  392. br.Msg = "获取分类数据失败"
  393. br.ErrMsg = "获取分类数据失败,err:" + err.Error()
  394. return
  395. }
  396. if count <= 0 {
  397. br.Msg = "无权访问"
  398. br.ErrMsg = "无权访问"
  399. br.IsSendEmail = false
  400. return
  401. }
  402. }
  403. dateValueMap := make(map[string]string)
  404. //取到所有数据
  405. for i := range req.Data {
  406. // 过滤第一行
  407. if i == 0 {
  408. continue
  409. }
  410. if req.Data[i] == nil {
  411. continue
  412. }
  413. tmpData := req.Data[i]
  414. // 少于两列,说明数据是异常的,得过滤
  415. if len(tmpData) < 2 {
  416. continue
  417. }
  418. var tmpDate string
  419. //先拿下日期列表
  420. {
  421. dateMap := tmpData[0].(map[string]interface{})
  422. _, ok := dateMap["m"]
  423. if !ok {
  424. continue
  425. }
  426. tmpDate = dateMap["m"].(string)
  427. }
  428. // 数据处理
  429. {
  430. if tmpData[1] == nil {
  431. continue
  432. }
  433. valueMap := tmpData[1].(map[string]interface{})
  434. value, _ := valueMap["m"]
  435. if value == nil {
  436. continue
  437. }
  438. dateValueMap[tmpDate] = valueMap["m"].(string)
  439. // 09-27 千位分隔符时用 "m" 取字符串存数据库会把逗号当小数点,现在换用 "v" 直接取数字再转为字符串,看看会不会有问题
  440. if ct, ok := valueMap["ct"].(map[string]interface{}); ok {
  441. fa, _ := ct["fa"]
  442. if fa == "#,##0.000" {
  443. value = valueMap["v"]
  444. floatValue := valueMap["v"].(float64)
  445. dateValueMap[tmpDate] = strconv.FormatFloat(floatValue, 'f', -1, 64)
  446. }
  447. }
  448. }
  449. }
  450. //操作指标,新增指标及数据等
  451. {
  452. secName := strings.TrimSpace(req.Name)
  453. frequency := strings.TrimSpace(req.Frequency)
  454. unit := strings.TrimSpace(req.Unit)
  455. // 指标基础信息变更
  456. {
  457. updateCols := make([]string, 0)
  458. if manualEdbInfo.SecName != secName {
  459. updateCols = append(updateCols, "SecName")
  460. manualEdbInfo.SecName = secName
  461. }
  462. if manualEdbInfo.ClassifyId != req.ClassifyId {
  463. updateCols = append(updateCols, "ClassifyId")
  464. manualEdbInfo.ClassifyId = req.ClassifyId
  465. }
  466. if manualEdbInfo.Frequency != frequency {
  467. updateCols = append(updateCols, "Frequency")
  468. manualEdbInfo.Frequency = frequency
  469. }
  470. if manualEdbInfo.Unit != unit {
  471. updateCols = append(updateCols, "Unit")
  472. manualEdbInfo.Unit = unit
  473. }
  474. if len(updateCols) > 0 {
  475. err = manualEdbInfo.Update(updateCols)
  476. if err != nil {
  477. br.Msg = `指标保存失败`
  478. br.Msg = `指标保存失败,ERR:` + err.Error()
  479. return
  480. }
  481. }
  482. }
  483. // 当前已经存在的指标明细数据
  484. targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
  485. if tmpErr != nil {
  486. err = tmpErr
  487. }
  488. existDataMap := make(map[string]string)
  489. deleteDataMap := make(map[string]string)
  490. for _, tmpData := range targetDataList {
  491. existDataMap[tmpData.Dt] = tmpData.Close
  492. deleteDataMap[tmpData.Dt] = tmpData.Dt
  493. }
  494. addDataList := make([]*models.Edbdata, 0)
  495. for createDate, closeVal := range dateValueMap {
  496. if createDate == "" || closeVal == "" {
  497. continue
  498. }
  499. //判断数据是否已经存在
  500. tmpVal, ok3 := existDataMap[createDate]
  501. // 不存在,那么后面进行插入
  502. if !ok3 {
  503. addDataList = append(addDataList, &models.Edbdata{
  504. TradeCode: manualEdbInfo.TradeCode,
  505. Dt: createDate,
  506. Close: closeVal,
  507. ModifyTime: time.Now(),
  508. })
  509. continue
  510. }
  511. delete(deleteDataMap, createDate)
  512. // 库里面的数据
  513. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  514. if tmpErr != nil {
  515. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  516. err = tmpErr
  517. continue
  518. }
  519. // 用户填写的数据
  520. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  521. if tmpErr != nil {
  522. fmt.Println("closeVal Parse err:", tmpErr.Error())
  523. err = tmpErr
  524. continue
  525. }
  526. if !tmpValDecimal.Equal(closeValDecimal) {
  527. fmt.Println("更新数值")
  528. err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
  529. if err != nil {
  530. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  531. errs = append(errs, err.Error())
  532. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  533. }
  534. }
  535. }
  536. // 新增指标
  537. if len(addDataList) > 0 {
  538. err = models.OnlyMultiAddEdbdata(addDataList)
  539. if err != nil {
  540. fmt.Println("MultiAddEdbdata err:", err.Error())
  541. errs = append(errs, err.Error())
  542. }
  543. }
  544. // 删除不需要的日期数据
  545. num := len(deleteDataMap)
  546. if num > 0 {
  547. deleteDateList := make([]string, 0)
  548. for date := range deleteDataMap {
  549. deleteDateList = append(deleteDateList, date)
  550. }
  551. err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
  552. if err != nil {
  553. fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
  554. errs = append(errs, err.Error())
  555. }
  556. }
  557. }
  558. br.Ret = 200
  559. br.Success = true
  560. br.Msg = "保存成功"
  561. }