manual.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. package data
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/system"
  7. "eta_gn/eta_api/services/alarm_msg"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "github.com/shopspring/decimal"
  11. "github.com/tealeg/xlsx"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func GetManualSysUser(keyWord string) (list []*data_manage.ManualSysUser, err error) {
  17. //departmentId := 1
  18. list = make([]*data_manage.ManualSysUser, 0)
  19. departmentItems, err := system.GetSysDepartmentAll()
  20. if err != nil {
  21. return list, err
  22. }
  23. for _, dv := range departmentItems {
  24. department := new(data_manage.ManualSysUser)
  25. department.ItemId = dv.DepartmentId * 10000
  26. department.ItemName = dv.DepartmentName
  27. fmt.Println(department.ItemId, department.ItemName)
  28. //GetSysuserList
  29. var condition string
  30. var pars []interface{}
  31. if keyWord != "" {
  32. condition += ` AND (real_name LIKE ? OR admin_name LIKE ? OR mobile LIKE ? ) `
  33. pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
  34. }
  35. sysUsers, err := system.GetSysUserItems(condition, pars)
  36. if err != nil {
  37. return list, err
  38. }
  39. groups, err := system.GetSysGroupByDepartmentId(dv.DepartmentId)
  40. if err != nil {
  41. return list, err
  42. }
  43. dg := make([]*data_manage.ManualSysUser, 0)
  44. for _, v := range groups {
  45. group := new(data_manage.ManualSysUser)
  46. group.ItemId = v.DepartmentId * 100000
  47. group.ItemName = v.GroupName
  48. for _, sv := range sysUsers {
  49. user := new(data_manage.ManualSysUser)
  50. user.ItemId = sv.AdminId
  51. user.ItemName = sv.RealName
  52. if sv.GroupId == v.GroupId {
  53. group.Children = append(group.Children, user)
  54. }
  55. }
  56. if len(group.Children) > 0 {
  57. dg = append(dg, group)
  58. }
  59. }
  60. if len(groups) <= 0 {
  61. group := new(data_manage.ManualSysUser)
  62. group.ItemId = dv.DepartmentId * 100000
  63. group.ItemName = "无分组"
  64. for _, sv := range sysUsers {
  65. user := new(data_manage.ManualSysUser)
  66. user.ItemId = sv.AdminId
  67. user.ItemName = sv.RealName
  68. if sv.DepartmentId == dv.DepartmentId && sv.GroupId == 0 {
  69. group.Children = append(group.Children, user)
  70. }
  71. }
  72. if len(group.Children) > 0 {
  73. dg = append(dg, group)
  74. }
  75. }
  76. if len(dg) > 0 {
  77. department.Children = dg
  78. list = append(list, department)
  79. }
  80. }
  81. return
  82. }
  83. type ManualIndexSource2EdbReq struct {
  84. EdbCode string
  85. EdbName string
  86. Frequency string
  87. Unit string
  88. ClassifyId int
  89. AdminId int
  90. AdminRealName string
  91. }
  92. // ManualIndexSource2Edb
  93. // @Description: 新增手工数据源到指标库
  94. // @author: Roc
  95. // @datetime 2024-07-26 13:23:19
  96. // @param req ManualIndexSource2EdbReq
  97. // @param lang string
  98. // @return edb *data_manage.EdbInfo
  99. // @return err error
  100. // @return errMsg string
  101. // @return skip bool
  102. func ManualIndexSource2Edb(req ManualIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  103. if req.EdbCode == "" {
  104. err = fmt.Errorf("指标ID为空")
  105. return
  106. }
  107. defer func() {
  108. if err != nil {
  109. tips := fmt.Sprintf("ManualIndexSource2Edb新增失败, Err: %s", err.Error())
  110. fmt.Println(tips)
  111. utils.FileLog.Info(tips)
  112. }
  113. }()
  114. source := utils.DATA_SOURCE_MANUAL
  115. // 是否已有指标数据
  116. dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
  117. if e != nil {
  118. err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
  119. return
  120. }
  121. // 新增指标数据
  122. if len(dataList) == 0 {
  123. res, e := AddEdbData(source, req.EdbCode, req.Frequency)
  124. if e != nil {
  125. err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
  126. return
  127. }
  128. if res == nil {
  129. err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
  130. return
  131. }
  132. if res.Ret != 200 {
  133. err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
  134. return
  135. }
  136. }
  137. // 是否新增过指标
  138. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  139. if e != nil && !utils.IsErrNoRow(e) {
  140. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  141. return
  142. }
  143. if exist != nil {
  144. skip = true
  145. return
  146. }
  147. // 开始结束时间
  148. var startDate, endDate string
  149. minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
  150. if e != nil && !utils.IsErrNoRow(e) {
  151. err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
  152. return
  153. }
  154. if minMax != nil {
  155. startDate = minMax.MinDate
  156. endDate = minMax.MaxDate
  157. }
  158. // 新增指标库
  159. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  160. if e != nil {
  161. errMsg = msg
  162. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  163. return
  164. }
  165. edb = edbInfo
  166. // 新增es
  167. go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  168. return
  169. }
  170. // ImportManualData
  171. // @Description: 数据导入
  172. // @author: Roc
  173. // @datetime 2024-08-01 11:27:21
  174. // @param path string
  175. // @param sysUser *system.Admin
  176. // @return successCount int
  177. // @return failCount int
  178. // @return err error
  179. // @return errMsg string
  180. func ImportManualData(path string, sysUser *system.Admin) (successCount, failCount int, err error, errMsg string) {
  181. // 错误信息
  182. errMsgList := make([]string, 0)
  183. // 操作记录
  184. recordMap := make(map[string]string)
  185. defer func() {
  186. recordList := make([]*models.EdbinfoOpRecord, 0)
  187. for tradeCode, remark := range recordMap {
  188. recordList = append(recordList, &models.EdbinfoOpRecord{
  189. TradeCode: tradeCode,
  190. Remark: remark,
  191. UserId: sysUser.AdminId,
  192. UserName: sysUser.RealName,
  193. CreateTime: time.Now(),
  194. })
  195. // 修改最大最小值
  196. go ModifyManualEdbMaxMinDate(tradeCode)
  197. }
  198. if len(recordList) > 0 {
  199. go func() {
  200. obj := models.EdbinfoOpRecord{}
  201. _ = obj.MulCreate(recordList)
  202. }()
  203. }
  204. // 错误信息记录
  205. if len(errMsgList) > 0 {
  206. utils.FileLog.Info("导入失败, errMsgList: %v", strings.Join(errMsgList, "\n"))
  207. }
  208. }()
  209. errMsg = `导入失败`
  210. xlFile, err := xlsx.OpenFile(path)
  211. if err != nil {
  212. fmt.Println(err.Error())
  213. return
  214. }
  215. if len(xlFile.Sheets) <= 0 {
  216. errMsg = "导入模板异常"
  217. err = errors.New(errMsg)
  218. return
  219. }
  220. //导入失败数据
  221. failDataList := make([]*models.EdbdataImportFail, 0)
  222. var indexDataList []ImportManualIndexData
  223. // 指标名称列表
  224. indexNameList := make([]string, 0)
  225. // 模板校验,然后处理成标准化格式
  226. for _, sheet := range xlFile.Sheets {
  227. var tmpIndexDataList []ImportManualIndexData
  228. var tmpFailDataList []*models.EdbdataImportFail
  229. rowList := sheet.Rows
  230. if len(rowList) <= 0 {
  231. errMsg = sheet.Name + "页异常"
  232. err = errors.New(errMsg)
  233. return
  234. }
  235. templateType := 1 // 模板类型
  236. minCellNum := 6 // 模板最小列数
  237. headerCell := rowList[0].Cells
  238. // 确定模板
  239. for _, v := range headerCell {
  240. if v.String() == "导入模板2/Import Template 2" {
  241. templateType = 2
  242. minCellNum = 2
  243. break
  244. }
  245. }
  246. // 如果小于最少列数,则报错
  247. if len(headerCell) < minCellNum {
  248. errMsg = sheet.Name + "页模板异常"
  249. err = errors.New(errMsg)
  250. return
  251. }
  252. switch templateType {
  253. case 2:
  254. // 模板2需要走对应的取数逻辑
  255. tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplate2(sheet, sysUser.AdminId)
  256. default:
  257. // 模板1需要走对应的取数逻辑
  258. tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplate1(sheet, sysUser.AdminId)
  259. }
  260. indexDataList = append(indexDataList, tmpIndexDataList...)
  261. failDataList = append(failDataList, tmpFailDataList...)
  262. }
  263. indexDataListMap := make(map[string]ImportManualIndexData)
  264. for _, v := range indexDataList {
  265. indexData, ok := indexDataListMap[v.IndexName]
  266. if !ok {
  267. // 没有就赋值
  268. indexDataListMap[v.IndexName] = v
  269. indexNameList = append(indexNameList, v.IndexName)
  270. continue
  271. }
  272. indexData.Unit = v.Unit
  273. indexData.ClassName = v.ClassName
  274. indexData.Frequency = v.Frequency
  275. indexData.DataMap = v.DataMap
  276. }
  277. if len(indexNameList) <= 0 {
  278. return
  279. }
  280. //超管账号可以查看分类下的所有频度数据
  281. userId := sysUser.AdminId
  282. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  283. userId = 0
  284. }
  285. //获取账户所拥有权限的分类id集合
  286. classifyNameStrList, edbDataClassifyMap, err := GetEdbClassifyNameListByAdminId(int64(userId))
  287. if err != nil {
  288. errMsg = "获取分类数据失败"
  289. return
  290. }
  291. //指标map
  292. targetMap := make(map[string]*models.Edbinfo)
  293. defer func() {
  294. for _, target := range targetMap {
  295. //结束后,清除掉对应的缓存
  296. key := "import:edbinfo:data:" + target.TradeCode
  297. utils.Rc.Delete(key)
  298. //将该指标的code加入到 “手工数据导入后刷新” 缓存
  299. if utils.Re == nil {
  300. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, target.TradeCode)
  301. if err != nil {
  302. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  303. }
  304. }
  305. }
  306. }()
  307. // 所有的指标数据map
  308. edbCodeDataMap := make(map[string]map[string]string)
  309. {
  310. // 获取指标信息
  311. manualIndexList, tmpErr := models.GetEdbinfoListBySecNameList(indexNameList)
  312. if tmpErr != nil {
  313. err = tmpErr
  314. errMsg = "获取指标信息失败"
  315. return
  316. }
  317. tradeCodeList := make([]string, 0)
  318. for _, v := range manualIndexList {
  319. targetMap[v.SecName] = v
  320. tradeCodeList = append(tradeCodeList, v.TradeCode)
  321. recordMap[v.TradeCode] = "更新数据"
  322. }
  323. // 获取明细数据
  324. dataList, tmpErr := models.GetTargetsDataListByCodeList(tradeCodeList)
  325. if tmpErr != nil {
  326. err = tmpErr
  327. errMsg = "获取指标明细数据失败"
  328. return
  329. }
  330. for _, tmpData := range dataList {
  331. _, ok := edbCodeDataMap[tmpData.TradeCode]
  332. if !ok {
  333. edbCodeDataMap[tmpData.TradeCode] = make(map[string]string)
  334. }
  335. edbCodeDataMap[tmpData.TradeCode][tmpData.Dt] = tmpData.Close
  336. }
  337. }
  338. addDataList := make([]*models.Edbdata, 0)
  339. for _, v := range indexDataListMap {
  340. if len(v.DataMap) <= 0 {
  341. continue
  342. }
  343. var tmpDate, tmpValue string
  344. for date, val := range v.DataMap {
  345. tmpDate = date
  346. tmpValue = val
  347. break
  348. }
  349. if !strings.Contains(strings.Join(classifyNameStrList, ","), v.ClassName) {
  350. failItem := &models.EdbdataImportFail{
  351. ClassifyName: v.ClassName,
  352. CreateDate: tmpDate,
  353. SecName: v.IndexName,
  354. Close: tmpValue,
  355. Remark: "没有该品种分类权限",
  356. SysUserId: fmt.Sprint(sysUser.AdminId),
  357. Frequency: v.Frequency,
  358. Unit: v.Unit,
  359. }
  360. failDataList = append(failDataList, failItem)
  361. continue
  362. }
  363. //获取指标分类信息
  364. classify, ok := edbDataClassifyMap[v.ClassName]
  365. if !ok {
  366. failItem := &models.EdbdataImportFail{
  367. ClassifyName: v.ClassName,
  368. CreateDate: tmpDate,
  369. SecName: v.IndexName,
  370. Close: tmpValue,
  371. Remark: "指标分类不存在",
  372. SysUserId: fmt.Sprint(sysUser.AdminId),
  373. Frequency: v.Frequency,
  374. Unit: v.Unit,
  375. }
  376. failDataList = append(failDataList, failItem)
  377. continue
  378. }
  379. target, ok := targetMap[v.IndexName]
  380. if !ok {
  381. if v.Frequency == "" {
  382. failItem := &models.EdbdataImportFail{
  383. ClassifyName: v.ClassName,
  384. CreateDate: tmpDate,
  385. SecName: v.IndexName,
  386. Close: tmpValue,
  387. Remark: "新增指标失败,频度字段为空",
  388. SysUserId: fmt.Sprint(sysUser.AdminId),
  389. Frequency: v.Frequency,
  390. Unit: v.Unit,
  391. }
  392. failDataList = append(failDataList, failItem)
  393. continue
  394. }
  395. if v.Unit == "" {
  396. failItem := &models.EdbdataImportFail{
  397. ClassifyName: v.ClassName,
  398. CreateDate: tmpDate,
  399. SecName: v.IndexName,
  400. Close: tmpValue,
  401. Remark: "新增指标失败,单位字段为空",
  402. SysUserId: fmt.Sprint(sysUser.AdminId),
  403. Frequency: v.Frequency,
  404. Unit: v.Unit,
  405. }
  406. failDataList = append(failDataList, failItem)
  407. continue
  408. }
  409. tmpErr := AddEdbInfo(v.IndexName, v.Unit, v.Frequency, "", sysUser.Mobile, classify.ClassifyId, sysUser.AdminId, sysUser.RealName)
  410. if tmpErr != nil {
  411. fmt.Println("line 158")
  412. failItem := &models.EdbdataImportFail{
  413. ClassifyName: v.ClassName,
  414. CreateDate: tmpDate,
  415. SecName: v.IndexName,
  416. Close: tmpValue,
  417. Remark: "新增指标失败",
  418. SysUserId: fmt.Sprint(sysUser.AdminId),
  419. Frequency: v.Frequency,
  420. Unit: v.Unit,
  421. }
  422. failDataList = append(failDataList, failItem)
  423. continue
  424. }
  425. tmpTarget, tmpErr := models.GetTargetBySecName(v.IndexName)
  426. target = tmpTarget
  427. targetMap[v.IndexName] = target
  428. recordMap[target.TradeCode] = "创建指标"
  429. }
  430. if target == nil {
  431. fmt.Println("指标不存在")
  432. failItem := &models.EdbdataImportFail{
  433. ClassifyName: v.ClassName,
  434. CreateDate: tmpDate,
  435. SecName: v.IndexName,
  436. Close: tmpValue,
  437. Remark: "指标不存在",
  438. SysUserId: fmt.Sprint(sysUser.AdminId),
  439. Frequency: v.Frequency,
  440. Unit: v.Unit,
  441. }
  442. failDataList = append(failDataList, failItem)
  443. continue
  444. }
  445. //设置10分钟缓存,不允许其他地方删除
  446. key := "import:edbinfo:data:" + target.TradeCode
  447. utils.Rc.SetNX(key, 1, time.Second*600)
  448. //更新指标信息
  449. {
  450. updateCols := make([]string, 0)
  451. //更新指标分类
  452. if target.ClassifyId <= 0 && classify.ClassifyId > 0 {
  453. target.ClassifyId = classify.ClassifyId
  454. updateCols = append(updateCols, "ClassifyId")
  455. }
  456. if target.Frequency != v.Frequency {
  457. target.Frequency = v.Frequency
  458. target.NoticeTime = ""
  459. updateCols = append(updateCols, "Frequency", "NoticeTime")
  460. }
  461. if target.Unit != v.Unit {
  462. target.Unit = v.Unit
  463. updateCols = append(updateCols, "Unit")
  464. }
  465. if len(updateCols) > 0 {
  466. _ = target.Update(updateCols)
  467. }
  468. }
  469. {
  470. // 判断指标数据列表是否已经存在
  471. tmpDataMap, ok := edbCodeDataMap[target.TradeCode]
  472. if !ok {
  473. tmpDataMap = make(map[string]string)
  474. edbCodeDataMap[target.TradeCode] = tmpDataMap
  475. }
  476. for createDate, closeVal := range v.DataMap {
  477. //判断数据是否已经存在
  478. tmpVal, ok := tmpDataMap[createDate]
  479. //数据已存在,进行更新操作
  480. if ok {
  481. if tmpVal != closeVal {
  482. edbCodeDataMap[target.TradeCode][createDate] = closeVal
  483. tmpErr := models.ModifyTargetsDataByImport(target.TradeCode, createDate, closeVal)
  484. if tmpErr != nil {
  485. errMsgList = append(errMsgList, fmt.Sprintf("%s修改数据失败,日期:%s,值:%v,Err:%s", target.TradeCode, createDate, closeVal, tmpErr.Error()))
  486. }
  487. }
  488. } else { //数据不存在,进行新增操作
  489. if target.TradeCode != "" && createDate != "" && closeVal != "" {
  490. addDataList = append(addDataList, &models.Edbdata{
  491. TradeCode: target.TradeCode,
  492. Dt: createDate,
  493. Close: closeVal,
  494. ModifyTime: time.Now(),
  495. })
  496. edbCodeDataMap[target.TradeCode][createDate] = closeVal
  497. }
  498. }
  499. successCount++
  500. }
  501. }
  502. }
  503. // 批量添加明细数据
  504. if len(addDataList) > 0 {
  505. tmpErr := models.OnlyMultiAddEdbdata(addDataList)
  506. if tmpErr != nil {
  507. fmt.Println("line 221")
  508. errMsgList = append(errMsgList, fmt.Sprintf("批量添加明细数据失败,Err:%s", tmpErr.Error()))
  509. }
  510. }
  511. // 失败数量
  512. failCount = len(failDataList)
  513. //fmt.Println("failDataList:", len(failDataList))
  514. if failCount > 0 {
  515. //先删除导入失败记录
  516. _ = models.DelEdbDataImportFail(sysUser.AdminId)
  517. // 批量添加导入失败记录
  518. err = models.MultiAddEdbdataImportFail(failDataList)
  519. if err != nil {
  520. go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
  521. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 新增失败记录失败:Err:"+err.Error(), utils.EmailSendToUsers)
  522. }
  523. {
  524. //错误信息字符串切片,最后作为发送邮件通知使用
  525. failContents := make([]string, 0)
  526. for _, v := range failDataList {
  527. failContents = append(failContents, fmt.Sprint(v.SecName, "导入失败:", v.Remark))
  528. }
  529. utils.FileLog.Info("导入数据 存在部分数据导入失败:" + strings.Join(failContents, ";"))
  530. //导入失败的话,最后统一邮件提醒就好啦,不需要每次都去提醒
  531. go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), 3)
  532. }
  533. }
  534. return
  535. }
  536. // ImportManualIndexData
  537. // @Description: excel模板后的内容
  538. type ImportManualIndexData struct {
  539. IndexName string `description:"指标名称"`
  540. Unit string `description:"单位"`
  541. ClassName string `description:"所属品种"`
  542. Frequency string `description:"频度"`
  543. DataMap map[string]string `description:"时间数据"`
  544. }
  545. // getDataByTemplate1
  546. // @Description: 根据模板1获取数据
  547. // @author: Roc
  548. // @datetime 2024-07-24 16:17:45
  549. // @param sheet *xlsx.Sheet
  550. // @return indexDataList []ImportManualIndexData
  551. // @return err error
  552. // @return errMsg string
  553. func getDataByTemplate1(sheet *xlsx.Sheet, sysUserId int) (indexDataList []ImportManualIndexData, failDataList []*models.EdbdataImportFail, err error, errMsg string) {
  554. fmt.Println("sheet name: ", sheet.Name)
  555. indexDataList = make([]ImportManualIndexData, 0)
  556. indexDataListMap := make(map[string]ImportManualIndexData, 0)
  557. failDataList = make([]*models.EdbdataImportFail, 0)
  558. //遍历行读取
  559. maxRow := sheet.MaxRow
  560. fmt.Println("maxRow:", maxRow)
  561. // 表头信息
  562. if maxRow <= 2 {
  563. errMsg = "模板异常1"
  564. err = errors.New(errMsg)
  565. return
  566. }
  567. // 表头校验
  568. {
  569. headerRow := sheet.Row(1)
  570. cells := headerRow.Cells
  571. if len(cells) < 6 {
  572. errMsg = "导入文件异常,请下载最新导入模板文件"
  573. err = errors.New(errMsg)
  574. return
  575. }
  576. templateFail := false
  577. if cells[0].Value != "品种/Variety" {
  578. templateFail = true
  579. }
  580. if cells[1].Value != "指标名称/Indicator Name" {
  581. templateFail = true
  582. }
  583. if cells[2].Value != "指标日期/Indicator Date" {
  584. templateFail = true
  585. }
  586. if cells[3].Value != "值/Value" {
  587. templateFail = true
  588. }
  589. if cells[4].Value != "频度/Frequency" {
  590. templateFail = true
  591. }
  592. if cells[5].Value != "单位/Unit" {
  593. templateFail = true
  594. }
  595. if templateFail {
  596. errMsg = "导入文件异常,请下载最新导入模板文件"
  597. err = errors.New(errMsg)
  598. return
  599. }
  600. }
  601. for i := 2; i < maxRow; i++ {
  602. row := sheet.Row(i)
  603. cells := row.Cells
  604. lenCell := len(cells)
  605. // 过滤空白行
  606. if lenCell <= 0 {
  607. continue
  608. }
  609. if lenCell < 6 {
  610. if cells[0].Value == `` {
  611. continue
  612. }
  613. errMsg = "导入文件异常,请下载最新导入模板文件"
  614. err = errors.New(errMsg)
  615. return
  616. }
  617. classifyName := strings.TrimSpace(cells[0].Value) //分类
  618. if classifyName == "" { //过滤空白行
  619. continue
  620. }
  621. // 指标名称
  622. cell1 := cells[1].Value
  623. indexName := strings.TrimSpace(cell1)
  624. if indexName == "" { //过滤空白行
  625. continue
  626. }
  627. //createDate := utils.ConvertToFormatDay(cell1) //录入日期
  628. createDate := cells[2].Value //指标日期
  629. frequency := strings.TrimSpace(cells[4].String()) //频度
  630. unit := strings.TrimSpace(cells[5].String()) //单位
  631. closeVal := cells[3].Value //值
  632. if strings.Contains(closeVal, "#N/A") {
  633. continue
  634. }
  635. currDate, tmpErr := getExcelDate(createDate)
  636. if tmpErr != nil {
  637. failDataList = append(failDataList, &models.EdbdataImportFail{
  638. //Id: 0,
  639. ClassifyName: classifyName,
  640. CreateDate: createDate,
  641. SecName: indexName,
  642. Close: closeVal,
  643. Remark: "日期格式异常",
  644. SysUserId: strconv.Itoa(sysUserId),
  645. Frequency: frequency,
  646. Unit: unit,
  647. })
  648. continue
  649. }
  650. closeValFloat, tmpErr := cells[3].Float() //值
  651. if tmpErr != nil {
  652. failDataList = append(failDataList, &models.EdbdataImportFail{
  653. //Id: 0,
  654. ClassifyName: classifyName,
  655. CreateDate: currDate,
  656. SecName: indexName,
  657. Close: cells[3].Value,
  658. Remark: "值类型异常",
  659. SysUserId: strconv.Itoa(sysUserId),
  660. Frequency: frequency,
  661. Unit: unit,
  662. })
  663. continue
  664. }
  665. newDecimal := decimal.NewFromFloat(closeValFloat)
  666. newDecimal.Round(4)
  667. closeVal = newDecimal.String()
  668. if strings.Contains(closeVal, "#N/A") {
  669. continue
  670. }
  671. _, ok := indexDataListMap[indexName]
  672. if !ok {
  673. indexDataListMap[indexName] = ImportManualIndexData{
  674. IndexName: indexName,
  675. Unit: unit,
  676. ClassName: classifyName,
  677. Frequency: frequency,
  678. DataMap: make(map[string]string),
  679. }
  680. }
  681. indexDataListMap[indexName].DataMap[currDate] = closeVal
  682. }
  683. for _, v := range indexDataListMap {
  684. indexDataList = append(indexDataList, v)
  685. }
  686. return
  687. }
  688. // getDataByTemplate2
  689. // @Description: 根据模板2获取数据
  690. // @author: Roc
  691. // @datetime 2024-07-24 16:17:56
  692. // @param sheet *xlsx.Sheet
  693. // @return indexDataList []ImportManualIndexData
  694. // @return err error
  695. // @return errMsg string
  696. func getDataByTemplate2(sheet *xlsx.Sheet, sysUserId int) (indexDataList []ImportManualIndexData, failDataList []*models.EdbdataImportFail, err error, errMsg string) {
  697. fmt.Println("sheet name: ", sheet.Name)
  698. indexDataList = make([]ImportManualIndexData, 0)
  699. failDataList = make([]*models.EdbdataImportFail, 0)
  700. //遍历行读取
  701. maxRow := sheet.MaxRow
  702. fmt.Println("maxRow:", maxRow)
  703. varietyList := make([]string, 0)
  704. indexNameList := make([]string, 0)
  705. unitList := make([]string, 0)
  706. frequencyList := make([]string, 0)
  707. // make(map[指标下标]map[日期]值)
  708. indexDateValueMap := make(map[int]map[string]string)
  709. // 表头信息
  710. if maxRow <= 4 {
  711. errMsg = "模板异常1"
  712. err = errors.New(errMsg)
  713. return
  714. }
  715. // 表头处理
  716. for i := 1; i <= 4; i++ {
  717. row := sheet.Row(i)
  718. cells := row.Cells
  719. switch i {
  720. case 1:
  721. for k, v := range cells {
  722. if k == 0 {
  723. continue
  724. }
  725. varietyList = append(varietyList, strings.TrimSpace(v.String()))
  726. }
  727. case 2:
  728. for k, v := range cells {
  729. if k == 0 {
  730. continue
  731. }
  732. indexNameList = append(indexNameList, strings.TrimSpace(v.String()))
  733. }
  734. case 3:
  735. for k, v := range cells {
  736. if k == 0 {
  737. continue
  738. }
  739. unitList = append(unitList, strings.TrimSpace(v.String()))
  740. }
  741. case 4:
  742. for k, v := range cells {
  743. if k == 0 {
  744. continue
  745. }
  746. frequencyList = append(frequencyList, strings.TrimSpace(v.String()))
  747. }
  748. }
  749. }
  750. maxNameIndex := len(indexNameList) - 1
  751. maxUnitIndex := len(unitList) - 1
  752. maxClassNameIndex := len(varietyList) - 1
  753. maxFrequencyIndex := len(frequencyList) - 1
  754. // 数据处理
  755. for i := 5; i < maxRow; i++ {
  756. row := sheet.Row(i)
  757. cells := row.Cells
  758. // 当前日期
  759. var currDate string
  760. // 日期是否异常,异常的话,就跳过,进入下一行数据处理
  761. var dateErr bool
  762. // 数据处理
  763. for k, v := range cells {
  764. if k == 0 {
  765. tmpCurrDate, tmpErr := getExcelDate(v.Value)
  766. if tmpErr != nil {
  767. dateErr = true
  768. }
  769. currDate = tmpCurrDate
  770. continue
  771. }
  772. key := k - 1
  773. // 日期异常,所以不处理该行了
  774. if dateErr {
  775. var indexName, unit, classifyName, frequency string
  776. if key <= maxNameIndex {
  777. indexName = indexNameList[key]
  778. }
  779. if key <= maxUnitIndex {
  780. unit = unitList[key]
  781. }
  782. if key <= maxClassNameIndex {
  783. classifyName = varietyList[key]
  784. }
  785. if key <= maxFrequencyIndex {
  786. frequency = frequencyList[key]
  787. }
  788. failDataList = append(failDataList, &models.EdbdataImportFail{
  789. //Id: 0,
  790. ClassifyName: classifyName,
  791. CreateDate: currDate,
  792. SecName: indexName,
  793. Close: v.Value,
  794. Remark: "日期格式异常",
  795. SysUserId: strconv.Itoa(sysUserId),
  796. Frequency: frequency,
  797. Unit: unit,
  798. })
  799. continue
  800. }
  801. _, ok := indexDateValueMap[key]
  802. if !ok {
  803. indexDateValueMap[key] = make(map[string]string)
  804. }
  805. closeVal := v.Value //值
  806. // 没有数据,说明是空串
  807. if strings.Contains(closeVal, "#N/A") {
  808. indexDateValueMap[key][currDate] = ""
  809. continue
  810. }
  811. closeValFloat, tmpErr := v.Float() //值
  812. if tmpErr != nil {
  813. indexDateValueMap[key][currDate] = ""
  814. continue
  815. }
  816. newDecimal := decimal.NewFromFloat(closeValFloat)
  817. newDecimal.Round(4)
  818. closeVal = newDecimal.String()
  819. if strings.Contains(closeVal, "#N/A") {
  820. indexDateValueMap[key][currDate] = ""
  821. continue
  822. }
  823. indexDateValueMap[key][currDate] = closeVal
  824. }
  825. }
  826. for i, indexName := range indexNameList {
  827. var unit, classifyName, frequency string
  828. if i <= maxUnitIndex {
  829. unit = unitList[i]
  830. }
  831. if i <= maxClassNameIndex {
  832. classifyName = varietyList[i]
  833. }
  834. if i <= maxFrequencyIndex {
  835. frequency = frequencyList[i]
  836. }
  837. indexData := ImportManualIndexData{
  838. IndexName: indexName,
  839. Unit: unit,
  840. ClassName: classifyName,
  841. Frequency: frequency,
  842. DataMap: indexDateValueMap[i],
  843. }
  844. indexDataList = append(indexDataList, indexData)
  845. }
  846. return
  847. }
  848. // getExcelDate
  849. // @Description: 获取excel的日期
  850. // @author: Roc
  851. // @datetime 2024-07-23 17:26:12
  852. // @param createDate string
  853. // @return newCreateDate string
  854. // @return err error
  855. func getExcelDate(createDate string) (newCreateDate string, err error) {
  856. if strings.Contains(createDate, "-") {
  857. //如果是带有 - 的普通日期格式文本
  858. _, err = time.Parse("2006-1-2", createDate)
  859. if err == nil {
  860. newCreateDate = createDate
  861. }
  862. } else if strings.Contains(createDate, "/") {
  863. //如果是带有 / 的普通日期格式文本
  864. createDateTime, timeErr := time.Parse("2006/1/2", createDate)
  865. if timeErr != nil {
  866. err = timeErr
  867. } else {
  868. newCreateDate = createDateTime.Format("2006-01-02")
  869. }
  870. } else {
  871. //可能是excel的日期格式
  872. _, tmpErr := strconv.Atoi(createDate)
  873. if tmpErr != nil {
  874. err = tmpErr
  875. } else {
  876. newCreateDate = utils.ConvertToFormatDay(createDate) //录入日期
  877. }
  878. }
  879. return
  880. }
  881. // ModifyManualEdbMaxMinDate
  882. // @Description: 修改手动录入的edb数据的最大最小日期
  883. // @author: Roc
  884. // @datetime 2024-08-01 15:34:31
  885. // @param tradeCode string
  886. func ModifyManualEdbMaxMinDate(tradeCode string) {
  887. // 获取最大最小日期
  888. item, err := models.GetEdbdataMaxMinDate(tradeCode)
  889. if err != nil {
  890. return
  891. }
  892. // 最新值
  893. latestValue, err := models.GetEdbdataLatestValue(tradeCode)
  894. if err != nil {
  895. return
  896. }
  897. // 修改指标的最大最小日期和最新值
  898. err = models.ModifyEdbinfoMaxMinDate(tradeCode, item.MinDate, item.MaxDate, latestValue)
  899. return
  900. }
  901. // GetUserManualClassifyIdList
  902. // @Description: 获取用户手动录入的分类id列表
  903. // @author: Roc
  904. // @datetime 2024-08-02 15:09:11
  905. // @param userId int
  906. // @return classifyIdList []int
  907. // @return err error
  908. func GetUserManualClassifyIdList(userId int) (classifyIdList []int, err error) {
  909. classifyIdList = make([]int, 0)
  910. // 获取有用权限的分类
  911. classifyList, err := models.GetEdbdataClassify(int64(userId))
  912. if err != nil {
  913. return
  914. }
  915. if len(classifyList) > 0 {
  916. for _, v := range classifyList {
  917. classifyIdList = append(classifyIdList, v.ClassifyId)
  918. if v.Child != nil && len(v.Child) > 0 {
  919. for _, v2 := range v.Child {
  920. classifyIdList = append(classifyIdList, v2.ClassifyId)
  921. }
  922. }
  923. }
  924. }
  925. return
  926. }