123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904 |
- package data
- import (
- "errors"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/models/data_manage"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/services/alarm_msg"
- "eta_gn/eta_api/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "github.com/tealeg/xlsx"
- "strconv"
- "strings"
- "time"
- )
- func GetManualSysUser(keyWord string) (list []*data_manage.ManualSysUser, err error) {
- list = make([]*data_manage.ManualSysUser, 0)
- departmentItems, err := system.GetSysDepartmentAll()
- if err != nil {
- return list, err
- }
- for _, dv := range departmentItems {
- department := new(data_manage.ManualSysUser)
- department.ItemId = dv.DepartmentId * 10000
- department.ItemName = dv.DepartmentName
- fmt.Println(department.ItemId, department.ItemName)
- var condition string
- var pars []interface{}
- if keyWord != "" {
- condition += ` AND (real_name LIKE ? OR admin_name LIKE ? OR mobile LIKE ? ) `
- pars = utils.GetLikeKeywordPars(pars, keyWord, 3)
- }
- sysUsers, err := system.GetSysUserItems(condition, pars)
- if err != nil {
- return list, err
- }
- groups, err := system.GetSysGroupByDepartmentId(dv.DepartmentId)
- if err != nil {
- return list, err
- }
- dg := make([]*data_manage.ManualSysUser, 0)
- for _, v := range groups {
- group := new(data_manage.ManualSysUser)
- group.ItemId = v.DepartmentId * 100000
- group.ItemName = v.GroupName
- for _, sv := range sysUsers {
- user := new(data_manage.ManualSysUser)
- user.ItemId = sv.AdminId
- user.ItemName = sv.RealName
- if sv.GroupId == v.GroupId {
- group.Children = append(group.Children, user)
- }
- }
- if len(group.Children) > 0 {
- dg = append(dg, group)
- }
- }
- if len(groups) <= 0 {
- group := new(data_manage.ManualSysUser)
- group.ItemId = dv.DepartmentId * 100000
- group.ItemName = "无分组"
- for _, sv := range sysUsers {
- user := new(data_manage.ManualSysUser)
- user.ItemId = sv.AdminId
- user.ItemName = sv.RealName
- if sv.DepartmentId == dv.DepartmentId && sv.GroupId == 0 {
- group.Children = append(group.Children, user)
- }
- }
- if len(group.Children) > 0 {
- dg = append(dg, group)
- }
- }
- if len(dg) > 0 {
- department.Children = dg
- list = append(list, department)
- }
- }
- return
- }
- type ManualIndexSource2EdbReq struct {
- EdbCode string
- EdbName string
- Frequency string
- Unit string
- ClassifyId int
- AdminId int
- AdminRealName string
- }
- func ManualIndexSource2Edb(req ManualIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
- if req.EdbCode == "" {
- err = fmt.Errorf("指标ID为空")
- return
- }
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("ManualIndexSource2Edb新增失败, Err: %s", err.Error())
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- }
- }()
- source := utils.DATA_SOURCE_MANUAL
- dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
- if e != nil {
- err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
- return
- }
- if len(dataList) == 0 {
- res, e := AddEdbData(source, req.EdbCode, req.Frequency)
- if e != nil {
- err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
- return
- }
- if res == nil {
- err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
- return
- }
- if res.Ret != 200 {
- err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
- return
- }
- }
- exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
- if e != nil && !utils.IsErrNoRow(e) {
- err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
- return
- }
- if exist != nil {
- skip = true
- return
- }
- var startDate, endDate string
- minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
- if e != nil && !utils.IsErrNoRow(e) {
- err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
- return
- }
- if minMax != nil {
- startDate = minMax.MinDate
- endDate = minMax.MaxDate
- }
- 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)
- if e != nil {
- errMsg = msg
- err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
- return
- }
- edb = edbInfo
- go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
- return
- }
- func ImportManualData(path string, sysUser *system.Admin) (successCount, failCount int, err error, errMsg string) {
- errMsgList := make([]string, 0)
- recordMap := make(map[string]string)
- defer func() {
- recordList := make([]*models.EdbinfoOpRecord, 0)
- for tradeCode, remark := range recordMap {
- recordList = append(recordList, &models.EdbinfoOpRecord{
- TradeCode: tradeCode,
- Remark: remark,
- UserId: sysUser.AdminId,
- UserName: sysUser.RealName,
- CreateTime: time.Now(),
- })
- go ModifyManualEdbMaxMinDate(tradeCode)
- }
- if len(recordList) > 0 {
- go func() {
- obj := models.EdbinfoOpRecord{}
- _ = obj.MulCreate(recordList)
- }()
- }
- if len(errMsgList) > 0 {
- utils.FileLog.Info("导入失败, errMsgList: %v", strings.Join(errMsgList, "\n"))
- }
- }()
- errMsg = `导入失败`
- xlFile, err := xlsx.OpenFile(path)
- if err != nil {
- fmt.Println(err.Error())
- return
- }
- if len(xlFile.Sheets) <= 0 {
- errMsg = "导入模板异常"
- err = errors.New(errMsg)
- return
- }
- failDataList := make([]*models.EdbdataImportFail, 0)
- var indexDataList []ImportManualIndexData
- indexNameList := make([]string, 0)
- for _, sheet := range xlFile.Sheets {
- var tmpIndexDataList []ImportManualIndexData
- var tmpFailDataList []*models.EdbdataImportFail
- rowList := sheet.Rows
- if len(rowList) <= 0 {
- errMsg = sheet.Name + "页异常"
- err = errors.New(errMsg)
- return
- }
- templateType := 1 // 模板类型
- minCellNum := 6 // 模板最小列数
- headerCell := rowList[0].Cells
- for _, v := range headerCell {
- if v.String() == "导入模板2/Import Template 2" {
- templateType = 2
- minCellNum = 2
- break
- }
- }
- if len(headerCell) < minCellNum {
- errMsg = sheet.Name + "页模板异常"
- err = errors.New(errMsg)
- return
- }
- switch templateType {
- case 2:
- tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplate2(sheet, sysUser.AdminId)
- default:
- tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplate1(sheet, sysUser.AdminId)
- }
- indexDataList = append(indexDataList, tmpIndexDataList...)
- failDataList = append(failDataList, tmpFailDataList...)
- }
- indexDataListMap := make(map[string]ImportManualIndexData)
- for _, v := range indexDataList {
- indexData, ok := indexDataListMap[v.IndexName]
- if !ok {
- indexDataListMap[v.IndexName] = v
- indexNameList = append(indexNameList, v.IndexName)
- continue
- }
- indexData.Unit = v.Unit
- indexData.ClassName = v.ClassName
- indexData.Frequency = v.Frequency
- indexData.DataMap = v.DataMap
- }
- if len(indexNameList) <= 0 {
- return
- }
- userId := sysUser.AdminId
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- userId = 0
- }
- classifyNameStrList, edbDataClassifyMap, err := GetEdbClassifyNameListByAdminId(int64(userId))
- if err != nil {
- errMsg = "获取分类数据失败"
- return
- }
- targetMap := make(map[string]*models.Edbinfo)
- defer func() {
- for _, target := range targetMap {
- key := "import:edbinfo:data:" + target.TradeCode
- utils.Rc.Delete(key)
- if utils.Re == nil {
- err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, target.TradeCode)
- if err != nil {
- fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
- }
- }
- }
- }()
- edbCodeDataMap := make(map[string]map[string]string)
- {
- manualIndexList, tmpErr := models.GetEdbinfoListBySecNameList(indexNameList)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "获取指标信息失败"
- return
- }
- tradeCodeList := make([]string, 0)
- for _, v := range manualIndexList {
- targetMap[v.SecName] = v
- tradeCodeList = append(tradeCodeList, v.TradeCode)
- recordMap[v.TradeCode] = "更新数据"
- }
- dataList, tmpErr := models.GetTargetsDataListByCodeList(tradeCodeList)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "获取指标明细数据失败"
- return
- }
- for _, tmpData := range dataList {
- _, ok := edbCodeDataMap[tmpData.TradeCode]
- if !ok {
- edbCodeDataMap[tmpData.TradeCode] = make(map[string]string)
- }
- edbCodeDataMap[tmpData.TradeCode][tmpData.Dt] = tmpData.Close
- }
- }
- addDataList := make([]*models.Edbdata, 0)
- for _, v := range indexDataListMap {
- if len(v.DataMap) <= 0 {
- continue
- }
- var tmpDate, tmpValue string
- for date, val := range v.DataMap {
- tmpDate = date
- tmpValue = val
- break
- }
- if !strings.Contains(strings.Join(classifyNameStrList, ","), v.ClassName) {
- failItem := &models.EdbdataImportFail{
- ClassifyName: v.ClassName,
- CreateDate: tmpDate,
- SecName: v.IndexName,
- Close: tmpValue,
- Remark: "没有该品种分类权限",
- SysUserId: fmt.Sprint(sysUser.AdminId),
- Frequency: v.Frequency,
- Unit: v.Unit,
- }
- failDataList = append(failDataList, failItem)
- continue
- }
- classify, ok := edbDataClassifyMap[v.ClassName]
- if !ok {
- failItem := &models.EdbdataImportFail{
- ClassifyName: v.ClassName,
- CreateDate: tmpDate,
- SecName: v.IndexName,
- Close: tmpValue,
- Remark: "指标分类不存在",
- SysUserId: fmt.Sprint(sysUser.AdminId),
- Frequency: v.Frequency,
- Unit: v.Unit,
- }
- failDataList = append(failDataList, failItem)
- continue
- }
- target, ok := targetMap[v.IndexName]
- if !ok {
- if v.Frequency == "" {
- failItem := &models.EdbdataImportFail{
- ClassifyName: v.ClassName,
- CreateDate: tmpDate,
- SecName: v.IndexName,
- Close: tmpValue,
- Remark: "新增指标失败,频度字段为空",
- SysUserId: fmt.Sprint(sysUser.AdminId),
- Frequency: v.Frequency,
- Unit: v.Unit,
- }
- failDataList = append(failDataList, failItem)
- continue
- }
- if v.Unit == "" {
- failItem := &models.EdbdataImportFail{
- ClassifyName: v.ClassName,
- CreateDate: tmpDate,
- SecName: v.IndexName,
- Close: tmpValue,
- Remark: "新增指标失败,单位字段为空",
- SysUserId: fmt.Sprint(sysUser.AdminId),
- Frequency: v.Frequency,
- Unit: v.Unit,
- }
- failDataList = append(failDataList, failItem)
- continue
- }
- tmpErr := AddEdbInfo(v.IndexName, v.Unit, v.Frequency, "", sysUser.Mobile, classify.ClassifyId, sysUser.AdminId, sysUser.RealName)
- if tmpErr != nil {
- fmt.Println("line 158")
- failItem := &models.EdbdataImportFail{
- ClassifyName: v.ClassName,
- CreateDate: tmpDate,
- SecName: v.IndexName,
- Close: tmpValue,
- Remark: "新增指标失败",
- SysUserId: fmt.Sprint(sysUser.AdminId),
- Frequency: v.Frequency,
- Unit: v.Unit,
- }
- failDataList = append(failDataList, failItem)
- continue
- }
- tmpTarget, tmpErr := models.GetTargetBySecName(v.IndexName)
- target = tmpTarget
- targetMap[v.IndexName] = target
- recordMap[target.TradeCode] = "创建指标"
- }
- if target == nil {
- fmt.Println("指标不存在")
- failItem := &models.EdbdataImportFail{
- ClassifyName: v.ClassName,
- CreateDate: tmpDate,
- SecName: v.IndexName,
- Close: tmpValue,
- Remark: "指标不存在",
- SysUserId: fmt.Sprint(sysUser.AdminId),
- Frequency: v.Frequency,
- Unit: v.Unit,
- }
- failDataList = append(failDataList, failItem)
- continue
- }
- key := "import:edbinfo:data:" + target.TradeCode
- utils.Rc.SetNX(key, 1, time.Second*600)
- {
- updateCols := make([]string, 0)
- if target.ClassifyId <= 0 && classify.ClassifyId > 0 {
- target.ClassifyId = classify.ClassifyId
- updateCols = append(updateCols, "ClassifyId")
- }
- if target.Frequency != v.Frequency {
- target.Frequency = v.Frequency
- target.NoticeTime = ""
- updateCols = append(updateCols, "Frequency", "NoticeTime")
- }
- if target.Unit != v.Unit {
- target.Unit = v.Unit
- updateCols = append(updateCols, "Unit")
- }
- if len(updateCols) > 0 {
- _ = target.Update(updateCols)
- }
- }
- {
- tmpDataMap, ok := edbCodeDataMap[target.TradeCode]
- if !ok {
- tmpDataMap = make(map[string]string)
- edbCodeDataMap[target.TradeCode] = tmpDataMap
- }
- for createDate, closeVal := range v.DataMap {
- tmpVal, ok := tmpDataMap[createDate]
- if ok {
- if tmpVal != closeVal {
- edbCodeDataMap[target.TradeCode][createDate] = closeVal
- tmpErr := models.ModifyTargetsDataByImport(target.TradeCode, createDate, closeVal)
- if tmpErr != nil {
- errMsgList = append(errMsgList, fmt.Sprintf("%s修改数据失败,日期:%s,值:%v,Err:%s", target.TradeCode, createDate, closeVal, tmpErr.Error()))
- }
- }
- } else { //数据不存在,进行新增操作
- if target.TradeCode != "" && createDate != "" && closeVal != "" {
- addDataList = append(addDataList, &models.Edbdata{
- TradeCode: target.TradeCode,
- Dt: createDate,
- Close: closeVal,
- ModifyTime: time.Now(),
- })
- edbCodeDataMap[target.TradeCode][createDate] = closeVal
- }
- }
- successCount++
- }
- }
- }
- if len(addDataList) > 0 {
- tmpErr := models.OnlyMultiAddEdbdata(addDataList)
- if tmpErr != nil {
- fmt.Println("line 221")
- errMsgList = append(errMsgList, fmt.Sprintf("批量添加明细数据失败,Err:%s", tmpErr.Error()))
- }
- }
- failCount = len(failDataList)
- if failCount > 0 {
- _ = models.DelEdbDataImportFail(sysUser.AdminId)
- err = models.MultiAddEdbdataImportFail(failDataList)
- if err != nil {
- go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
- }
- {
- failContents := make([]string, 0)
- for _, v := range failDataList {
- failContents = append(failContents, fmt.Sprint(v.SecName, "导入失败:", v.Remark))
- }
- utils.FileLog.Info("导入数据 存在部分数据导入失败:" + strings.Join(failContents, ";"))
- go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), 3)
- }
- }
- return
- }
- type ImportManualIndexData struct {
- IndexName string `description:"指标名称"`
- Unit string `description:"单位"`
- ClassName string `description:"所属品种"`
- Frequency string `description:"频度"`
- DataMap map[string]string `description:"时间数据"`
- }
- func getDataByTemplate1(sheet *xlsx.Sheet, sysUserId int) (indexDataList []ImportManualIndexData, failDataList []*models.EdbdataImportFail, err error, errMsg string) {
- fmt.Println("sheet name: ", sheet.Name)
- indexDataList = make([]ImportManualIndexData, 0)
- indexDataListMap := make(map[string]ImportManualIndexData, 0)
- failDataList = make([]*models.EdbdataImportFail, 0)
- maxRow := sheet.MaxRow
- fmt.Println("maxRow:", maxRow)
- if maxRow <= 2 {
- errMsg = "模板异常1"
- err = errors.New(errMsg)
- return
- }
- {
- headerRow := sheet.Row(1)
- cells := headerRow.Cells
- if len(cells) < 6 {
- errMsg = "导入文件异常,请下载最新导入模板文件"
- err = errors.New(errMsg)
- return
- }
- templateFail := false
- if cells[0].Value != "品种/Variety" {
- templateFail = true
- }
- if cells[1].Value != "指标名称/Indicator Name" {
- templateFail = true
- }
- if cells[2].Value != "指标日期/Indicator Date" {
- templateFail = true
- }
- if cells[3].Value != "值/Value" {
- templateFail = true
- }
- if cells[4].Value != "频度/Frequency" {
- templateFail = true
- }
- if cells[5].Value != "单位/Unit" {
- templateFail = true
- }
- if templateFail {
- errMsg = "导入文件异常,请下载最新导入模板文件"
- err = errors.New(errMsg)
- return
- }
- }
- for i := 2; i < maxRow; i++ {
- row := sheet.Row(i)
- cells := row.Cells
- lenCell := len(cells)
- if lenCell <= 0 {
- continue
- }
- if lenCell < 6 {
- if cells[0].Value == `` {
- continue
- }
- errMsg = "导入文件异常,请下载最新导入模板文件"
- err = errors.New(errMsg)
- return
- }
- classifyName := strings.TrimSpace(cells[0].Value) //分类
- if classifyName == "" { //过滤空白行
- continue
- }
- cell1 := cells[1].Value
- indexName := strings.TrimSpace(cell1)
- if indexName == "" { //过滤空白行
- continue
- }
- createDate := cells[2].Value //指标日期
- frequency := strings.TrimSpace(cells[4].String()) //频度
- unit := strings.TrimSpace(cells[5].String()) //单位
- closeVal := cells[3].Value //值
- if strings.Contains(closeVal, "#N/A") {
- continue
- }
- currDate, tmpErr := getExcelDate(createDate)
- if tmpErr != nil {
- failDataList = append(failDataList, &models.EdbdataImportFail{
- ClassifyName: classifyName,
- CreateDate: createDate,
- SecName: indexName,
- Close: closeVal,
- Remark: "日期格式异常",
- SysUserId: strconv.Itoa(sysUserId),
- Frequency: frequency,
- Unit: unit,
- })
- continue
- }
- closeValFloat, tmpErr := cells[3].Float() //值
- if tmpErr != nil {
- failDataList = append(failDataList, &models.EdbdataImportFail{
- ClassifyName: classifyName,
- CreateDate: currDate,
- SecName: indexName,
- Close: cells[3].Value,
- Remark: "值类型异常",
- SysUserId: strconv.Itoa(sysUserId),
- Frequency: frequency,
- Unit: unit,
- })
- continue
- }
- newDecimal := decimal.NewFromFloat(closeValFloat)
- newDecimal.Round(4)
- closeVal = newDecimal.String()
- if strings.Contains(closeVal, "#N/A") {
- continue
- }
- _, ok := indexDataListMap[indexName]
- if !ok {
- indexDataListMap[indexName] = ImportManualIndexData{
- IndexName: indexName,
- Unit: unit,
- ClassName: classifyName,
- Frequency: frequency,
- DataMap: make(map[string]string),
- }
- }
- indexDataListMap[indexName].DataMap[currDate] = closeVal
- }
- for _, v := range indexDataListMap {
- indexDataList = append(indexDataList, v)
- }
- return
- }
- func getDataByTemplate2(sheet *xlsx.Sheet, sysUserId int) (indexDataList []ImportManualIndexData, failDataList []*models.EdbdataImportFail, err error, errMsg string) {
- fmt.Println("sheet name: ", sheet.Name)
- indexDataList = make([]ImportManualIndexData, 0)
- failDataList = make([]*models.EdbdataImportFail, 0)
- maxRow := sheet.MaxRow
- fmt.Println("maxRow:", maxRow)
- varietyList := make([]string, 0)
- indexNameList := make([]string, 0)
- unitList := make([]string, 0)
- frequencyList := make([]string, 0)
- indexDateValueMap := make(map[int]map[string]string)
- if maxRow <= 4 {
- errMsg = "模板异常1"
- err = errors.New(errMsg)
- return
- }
- for i := 1; i <= 4; i++ {
- row := sheet.Row(i)
- cells := row.Cells
- switch i {
- case 1:
- for k, v := range cells {
- if k == 0 {
- continue
- }
- varietyList = append(varietyList, strings.TrimSpace(v.String()))
- }
- case 2:
- for k, v := range cells {
- if k == 0 {
- continue
- }
- indexNameList = append(indexNameList, strings.TrimSpace(v.String()))
- }
- case 3:
- for k, v := range cells {
- if k == 0 {
- continue
- }
- unitList = append(unitList, strings.TrimSpace(v.String()))
- }
- case 4:
- for k, v := range cells {
- if k == 0 {
- continue
- }
- frequencyList = append(frequencyList, strings.TrimSpace(v.String()))
- }
- }
- }
- maxNameIndex := len(indexNameList) - 1
- maxUnitIndex := len(unitList) - 1
- maxClassNameIndex := len(varietyList) - 1
- maxFrequencyIndex := len(frequencyList) - 1
- for i := 5; i < maxRow; i++ {
- row := sheet.Row(i)
- cells := row.Cells
- var currDate string
- var dateErr bool
- for k, v := range cells {
- if k == 0 {
- tmpCurrDate, tmpErr := getExcelDate(v.Value)
- if tmpErr != nil {
- dateErr = true
- }
- currDate = tmpCurrDate
- continue
- }
- key := k - 1
- if dateErr {
- var indexName, unit, classifyName, frequency string
- if key <= maxNameIndex {
- indexName = indexNameList[key]
- }
- if key <= maxUnitIndex {
- unit = unitList[key]
- }
- if key <= maxClassNameIndex {
- classifyName = varietyList[key]
- }
- if key <= maxFrequencyIndex {
- frequency = frequencyList[key]
- }
- failDataList = append(failDataList, &models.EdbdataImportFail{
- ClassifyName: classifyName,
- CreateDate: currDate,
- SecName: indexName,
- Close: v.Value,
- Remark: "日期格式异常",
- SysUserId: strconv.Itoa(sysUserId),
- Frequency: frequency,
- Unit: unit,
- })
- continue
- }
- _, ok := indexDateValueMap[key]
- if !ok {
- indexDateValueMap[key] = make(map[string]string)
- }
- closeVal := v.Value //值
- if strings.Contains(closeVal, "#N/A") {
- indexDateValueMap[key][currDate] = ""
- continue
- }
- closeValFloat, tmpErr := v.Float() //值
- if tmpErr != nil {
- indexDateValueMap[key][currDate] = ""
- continue
- }
- newDecimal := decimal.NewFromFloat(closeValFloat)
- newDecimal.Round(4)
- closeVal = newDecimal.String()
- if strings.Contains(closeVal, "#N/A") {
- indexDateValueMap[key][currDate] = ""
- continue
- }
- indexDateValueMap[key][currDate] = closeVal
- }
- }
- for i, indexName := range indexNameList {
- var unit, classifyName, frequency string
- if i <= maxUnitIndex {
- unit = unitList[i]
- }
- if i <= maxClassNameIndex {
- classifyName = varietyList[i]
- }
- if i <= maxFrequencyIndex {
- frequency = frequencyList[i]
- }
- indexData := ImportManualIndexData{
- IndexName: indexName,
- Unit: unit,
- ClassName: classifyName,
- Frequency: frequency,
- DataMap: indexDateValueMap[i],
- }
- indexDataList = append(indexDataList, indexData)
- }
- return
- }
- func getExcelDate(createDate string) (newCreateDate string, err error) {
- if strings.Contains(createDate, "-") {
- _, err = time.Parse("2006-1-2", createDate)
- if err == nil {
- newCreateDate = createDate
- }
- } else if strings.Contains(createDate, "/") {
- createDateTime, timeErr := time.Parse("2006/1/2", createDate)
- if timeErr != nil {
- err = timeErr
- } else {
- newCreateDate = createDateTime.Format("2006-01-02")
- }
- } else {
- _, tmpErr := strconv.Atoi(createDate)
- if tmpErr != nil {
- err = tmpErr
- } else {
- newCreateDate = utils.ConvertToFormatDay(createDate) //录入日期
- }
- }
- return
- }
- func ModifyManualEdbMaxMinDate(tradeCode string) {
- item, err := models.GetEdbdataMaxMinDate(tradeCode)
- if err != nil {
- return
- }
- latestValue, err := models.GetEdbdataLatestValue(tradeCode)
- if err != nil {
- return
- }
- err = models.ModifyEdbinfoMaxMinDate(tradeCode, item.MinDate, item.MaxDate, latestValue)
- return
- }
- func GetUserManualClassifyIdList(userId int) (classifyIdList []int, err error) {
- classifyIdList = make([]int, 0)
- classifyList, err := models.GetEdbdataClassify(int64(userId))
- if err != nil {
- return
- }
- if len(classifyList) > 0 {
- for _, v := range classifyList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- if v.Child != nil && len(v.Child) > 0 {
- for _, v2 := range v.Child {
- classifyIdList = append(classifyIdList, v2.ClassifyId)
- }
- }
- }
- }
- return
- }
|