manual.go 23 KB

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