12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145 |
- package data
- import (
- "errors"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services/alarm_msg"
- "eta/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
- }
- func GetManualEdbClassifyListByAdminId(adminId int64) (classifyIdList []int, err error) {
- var list []*models.EdbdataClassify
- if adminId <= 0 {
- list, err = models.GetAllChildManualEdbClassify()
- } else {
- userClassifyList, _ := models.GetManualUserClassify(int(adminId))
- var userClassifyIdList []int
- for _, v := range userClassifyList {
- userClassifyIdList = append(userClassifyIdList, v.ClassifyId)
- }
- list, err = models.GetChildManualEdbClassifyByIdList(userClassifyIdList)
- }
- if err != nil {
- return
- }
- for _, classify := range list {
- classifyIdList = append(classifyIdList, classify.ClassifyId)
- }
- 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 && e.Error() != utils.ErrNoRow() {
- 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 && e.Error() != utils.ErrNoRow() {
- 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, indexCount 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 v.IndexName == `` {
- continue
- }
- 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
- }
-
- indexCount++
- 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)
-
- {
- 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
- }
- 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) {
-
- indexDataList = make([]ImportManualIndexData, 0)
- failDataList = make([]*models.EdbdataImportFail, 0)
-
- maxRow := sheet.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
- var emptyDate bool
-
- for k, v := range cells {
- if k == 0 {
-
- if v.Value == `` {
- emptyDate = true
- continue
- }
- tmpCurrDate, tmpErr := getExcelDate(v.Value)
- if tmpErr != nil {
- dateErr = true
- }
- currDate = tmpCurrDate
- continue
- }
- key := k - 1
-
- if emptyDate {
- continue
- }
-
- 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
- }
|