manual.go 29 KB

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