zwxi 2 months ago
parent
commit
567c87787e

+ 136 - 0
controllers/base_from_hisugar.go

@@ -0,0 +1,136 @@
+package controllers
+
+import (
+	"encoding/json"
+	"eta/eta_index_lib/logic"
+	"eta/eta_index_lib/models"
+	"eta/eta_index_lib/utils"
+	"strconv"
+	"time"
+)
+
+// HisugarController 泛糖科技
+type HisugarController struct {
+	BaseAuthController
+}
+
+// Add
+// @Title 新增泛糖科技指标接口
+// @Description 新增泛糖科技指标接口
+// @Success 200 {object} models.AddEdbInfoReq
+// @router /add [post]
+func (this *HisugarController) Add() {
+	br := new(models.BaseResponse).Init()
+	var cacheKey string
+	defer func() {
+		if br.ErrMsg == "" {
+			br.IsSendEmail = false
+		}
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	var req models.AddEdbInfoReq
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	if req.EdbCode == "" {
+		br.Msg = "请输入指标编码!"
+		br.ErrMsg = "请输入指标编码,指标编码为空"
+		return
+	}
+
+	source := utils.DATA_SOURCE_HISUGAR
+	cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
+	if !utils.Rc.IsExist(cacheKey) {
+		utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
+		err = models.AddEdbDataFromOilchem(req.EdbCode)
+		if err != nil {
+			br.Msg = "获取指标信息失败!"
+			br.ErrMsg = "获取指标信息失败 AddEdbDataFromCoal,Err:" + err.Error()
+			return
+		}
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "获取成功"
+	} else {
+		br.Ret = 501
+		br.Success = true
+		br.Msg = "系统处理中,请稍后重试"
+	}
+}
+
+// Refresh
+// @Title 刷新泛糖科技指标接口
+// @Description 刷新泛糖科技指标接口
+// @Success 200 {object} models.RefreshEdbInfoReq
+// @router /refresh [post]
+func (this *HisugarController) Refresh() {
+	br := new(models.BaseResponse).Init()
+	var cacheKey string
+	defer func() {
+		utils.Rc.Delete(cacheKey)
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	source := utils.DATA_SOURCE_HISUGAR
+	var req models.RefreshEdbInfoReq
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	if req.EdbCode == "" {
+		br.Msg = "请输入指标编码!"
+		br.ErrMsg = "请输入指标编码,指标编码为空"
+		return
+	}
+	if req.EdbInfoId <= 0 {
+		br.Msg = "请输入指标ID!"
+		br.ErrMsg = "请输入指标ID"
+		return
+	}
+
+	cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
+	if utils.Rc.IsExist(cacheKey) {
+		br.Ret = 501
+		br.Success = true
+		br.Msg = "系统处理中,请稍后重试"
+		return
+	}
+
+	utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
+	// 获取指标详情
+	edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
+	if err != nil {
+		br.Msg = "指标不存在!"
+		br.ErrMsg = "指标不存在"
+		return
+	}
+
+	err = models.RefreshEdbDataFromOilchem(req.EdbInfoId, req.EdbCode, req.StartDate)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		br.Msg = "刷新指标信息失败!"
+		br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromCoal,Err:" + err.Error()
+		return
+	}
+	// 更新指标最大最小值
+	err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
+	if err != nil {
+		br.Msg = errMsg
+		br.ErrMsg = err.Error()
+		return
+	}
+
+	// 更新ES
+	go logic.UpdateEs(edbInfo.EdbInfoId)
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+}
+
+

+ 278 - 0
models/base_from_hisugar.go

@@ -0,0 +1,278 @@
+package models
+
+import (
+	"eta/eta_index_lib/utils"
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"strconv"
+	"strings"
+	"time"
+)
+
+type BaseFromHisugarIndex struct {
+	BaseFromHisugarIndexId int       // 主键ID
+	IndexCode              string    // 指标编码
+	IndexName              string    // 指标名称
+	ClassifyId             uint      // 分类ID
+	Unit                   string    // 单位
+	Frequency              string    // 频度
+	Describe               string    // 指标描述
+	Sort                   int       // 排序
+	CreateTime             time.Time // 创建时间
+	ModifyTime             time.Time // 修改时间
+}
+
+type BaseFromHisugarData struct {
+	BaseFromHisugarDataId  int    // 数据表ID
+	BaseFromHisugarIndexId int    // 指标ID
+	IndexCode              string // 指标编码
+	DataTime               string
+	Value                  string
+	CreateTime             time.Time // 创建时间
+	ModifyTime             time.Time // 修改时间
+}
+
+
+//添加数据
+func AddBaseFromHisugarIndexMuti(items []*BaseFromHisugarIndex) (err error) {
+	o := orm.NewOrm()
+	_, err = o.InsertMulti(500, items)
+	return
+}
+func AddBaseFromHisugarIndex(item *BaseFromHisugarIndex) (lastId int64, err error) {
+	o := orm.NewOrm()
+	lastId, err = o.Insert(item)
+	return
+}
+
+func AddBaseFromHisugarData(item *BaseFromHisugarData) (lastId int64, err error) {
+	o := orm.NewOrm()
+	lastId, err = o.Insert(item)
+	return
+}
+
+func AddEdbDataFromHisugar(edbCode string) (err error) {
+	o := orm.NewOrm()
+
+	hisugarBaseDataAll, err := GetHisugarDataByCode(edbCode)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		return
+	}
+
+	var isAdd bool
+	addSql := ` INSERT INTO edb_data_hisugar(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
+	existMap := make(map[string]string)
+	for _, sv := range hisugarBaseDataAll {
+		eDate := sv.DataTime
+		var timeStr string
+		var dataTime time.Time
+		var sDataTime string
+		var timestamp int64
+
+		sDataTime = eDate
+		dataTime, err = time.ParseInLocation(utils.FormatDate, eDate, time.Local)
+		if err != nil {
+			fmt.Println("time.Parse Err:" + eDate)
+			return err
+		}
+		timestamp = dataTime.UnixNano() / 1e6
+		timeStr = fmt.Sprintf("%d", timestamp)
+
+		value := strings.Replace(sv.Value, "%", "", -1)
+		if _, ok := existMap[sDataTime]; !ok {
+			addSql += GetAddSql("0", edbCode, sDataTime, timeStr, value)
+			fmt.Println("edbCode:", edbCode)
+			fmt.Println("sDataTime:", sDataTime)
+			fmt.Println("timeStr:", timeStr)
+			fmt.Println("value:", value)
+			isAdd = true
+		}
+		existMap[eDate] = value
+	}
+	if isAdd {
+		addSql = strings.TrimRight(addSql, ",")
+		utils.FileLog.Info("addSql:" + addSql)
+		_, err = o.Raw(addSql).Exec()
+		if err != nil {
+			return err
+		}
+	}
+	return
+}
+
+// GetHisugarDataByCode
+func GetHisugarDataByCode(indexCode string) (items []*BaseFromHisugarData, err error) {
+	o := orm.NewOrm()
+	sql := "SELECT * FROM base_from_hisugar_data WHERE index_code=? "
+	_, err = o.Raw(sql, indexCode).QueryRows(&items)
+	return
+}
+
+
+// RefreshEdbDataFromHisugar 刷新隆众资讯
+func RefreshEdbDataFromHisugar(edbInfoId int, edbCode, startDate string) (err error) {
+	source := utils.DATA_SOURCE_OILCHEM
+	subSource := utils.DATA_SUB_SOURCE_EDB
+
+	o := orm.NewOrm()
+	if err != nil {
+		return
+	}
+	edbInfoIdStr := strconv.Itoa(edbInfoId)
+	//计算数据
+	var condition string
+	var pars []interface{}
+
+	if edbCode != "" {
+		condition += " AND index_code=? "
+		pars = append(pars, edbCode)
+	}
+
+	if startDate != "" {
+		condition += " AND data_time>=? "
+		pars = append(pars, startDate)
+	}
+
+	hisugarDataList, err := GetBaseFromHisugarDataByCondition(condition, pars)
+	if err != nil {
+		return
+	}
+
+	// 真实数据的最大日期  , 插入规则配置的日期
+	var realDataMaxDate, edbDataInsertConfigDate time.Time
+	var edbDataInsertConfig *EdbDataInsertConfig
+	var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
+	{
+		edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			return
+		}
+		if edbDataInsertConfig != nil {
+			edbDataInsertConfigDate = edbDataInsertConfig.Date
+		}
+	}
+
+	var existCondition string
+	var existPars []interface{}
+
+	existCondition += " AND edb_info_id=? "
+	existPars = append(existPars, edbInfoId)
+	if startDate != "" {
+		existCondition += " AND data_time>=? "
+		existPars = append(existPars, startDate)
+	}
+
+	existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
+	if err != nil {
+		return err
+	}
+	existMap := make(map[string]*EdbInfoSearchData)
+	for _, v := range existList {
+		existMap[v.DataTime] = v
+	}
+	addSql := ` INSERT INTO edb_data_hisugar(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
+	var isAdd bool
+	for _, v := range hisugarDataList {
+		item := v
+
+		eDate := item.DataTime
+		dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
+		if err != nil {
+			return err
+		}
+		if findItem, ok := existMap[v.DataTime]; !ok {
+			sValue := item.Value
+			timestamp := dataTime.UnixNano() / 1e6
+			timeStr := fmt.Sprintf("%d", timestamp)
+
+			addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
+			isAdd = true
+		} else {
+			if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
+				err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Value)
+				if err != nil {
+					return err
+				}
+			}
+		}
+
+		// 下面代码主要目的是处理掉手动插入的数据判断
+		{
+			if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
+				realDataMaxDate = dataTime
+			}
+			if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
+				isFindConfigDateRealData = true
+			}
+		}
+	}
+
+	// 处理手工数据补充的配置
+	HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
+
+	if isAdd {
+		addSql = strings.TrimRight(addSql, ",")
+		_, err = o.Raw(addSql).Exec()
+		if err != nil {
+			fmt.Println("RefreshEdbDataFromSci add Err", err.Error())
+			return
+		}
+	}
+	return
+}
+
+func GetBaseFromHisugarDataByCondition(condition string, pars []interface{}) (list []*BaseFromHisugarData, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM base_from_hisugar_data WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	_, err = o.Raw(sql, pars).QueryRows(&list)
+	return
+}
+
+
+type BaseFromHisugarIndexReq struct {
+	BaseFromHisugarIndexId int       // 主键ID
+	IndexCode              string    // 指标编码
+	IndexName              string    // 指标名称
+	ClassifyId             uint      // 分类ID
+	Unit                   string    // 单位
+	Source                 string    // 数据来源
+	Frequency              string    // 频度
+	Describe               string    // 指标描述
+	DataTime               string    // 数据日期
+	Value                  string   // 数据值
+	Sort                   int       // 排序
+	CreateTime             time.Time // 创建时间
+	ModifyTime             time.Time // 修改时间
+	IndexNameStr           string    // 指标名称字符串
+	MarketName             string    // 市场名称
+}
+
+
+type HandleHisugarEdbDataReq struct {
+	List []*BaseFromHisugarIndexReq
+}
+
+func GetBaseFromHisugarIndex() (list []*BaseFromHisugarIndex, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM base_from_hisugar_index group by index_name `
+	_, err = o.Raw(sql).QueryRows(&list)
+	return
+}
+
+func GetBaseFromHisugarData(indexCode, dataTime string) (item *BaseFromHisugarData, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM base_from_hisugar_data where index_code=? And data_time = ? `
+	err = o.Raw(sql, indexCode, dataTime).QueryRow(&item)
+	return
+}
+
+// UpdateBaseFromSci99Data
+func UpdateBaseFromHisugarData(value , indexCode, dataTime string) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE base_from_sci99_data SET value=?,modify_time=NOW() WHERE index_code = ? AND data_time = ? `
+	_, err = o.Raw(sql, value, indexCode, dataTime).Exec()
+	return
+}

+ 18 - 0
routers/commentsRouter.go

@@ -718,6 +718,24 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_index_lib/controllers:HisugarController"] = append(beego.GlobalControllerRouter["eta/eta_index_lib/controllers:HisugarController"],
+        beego.ControllerComments{
+            Method: "Add",
+            Router: `/add`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_index_lib/controllers:HisugarController"] = append(beego.GlobalControllerRouter["eta/eta_index_lib/controllers:HisugarController"],
+        beego.ControllerComments{
+            Method: "Refresh",
+            Router: `/refresh`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_index_lib/controllers:IcpiController"] = append(beego.GlobalControllerRouter["eta/eta_index_lib/controllers:IcpiController"],
         beego.ControllerComments{
             Method: "Add",

+ 5 - 0
routers/router.go

@@ -292,6 +292,11 @@ func init() {
 				&controllers.OilchemController{},
 			),
 		),
+		beego.NSNamespace("/hisugar",
+			beego.NSInclude(
+				&controllers.HisugarController{},
+			),
+		),
 	)
 	beego.AddNamespace(ns)
 }

+ 184 - 0
services/base_from_hisugar.go

@@ -0,0 +1,184 @@
+package services
+
+//var IndexCodeMap = make(map[string]string)
+//var IndexMap = make(map[string]*models.BaseFromOilchemIndex)
+//func HandleOilchemIndex(list []*models.BaseFromOilchemIndexReq) (err error) {
+//	allCode, e := models.GetBaseFromOilchemIndex()
+//	if e != nil {
+//		err = e
+//		fmt.Println("select Code err:", err)
+//		utils.FileLog.Info("GetBaseFromOilchemIndex err:", err)
+//		return
+//	}
+//
+//	for _, item := range allCode {
+//		IndexCodeMap[item.IndexName] = item.IndexCode
+//		IndexMap[item.IndexName] = item
+//	}
+//
+//	for _, v := range list {
+//		indexCode, needAdd := OilchemIndexCodeGenerator(v.IndexName, v.IndexNameStr, v.MarketName)
+//		if needAdd {
+//			item := models.BaseFromOilchemIndex{
+//				IndexCode:              indexCode,
+//				IndexName:              v.IndexName,
+//				ClassifyId:             v.ClassifyId,
+//				Unit:                   v.Unit,
+//				Frequency:              v.Frequency,
+//				Describe:               v.Describe,
+//				Sort:                   v.Sort,
+//				CreateTime:             v.CreateTime,
+//				ModifyTime:             v.ModifyTime,
+//			}
+//			id, e := models.AddBaseFromOilchemIndex(&item)
+//			if e != nil {
+//				err = e
+//				fmt.Println("AddBaseFromOilchemIndexMuti err:", err)
+//				utils.FileLog.Info("AddBaseFromOilchemIndexMuti err:", err)
+//				return
+//			}
+//			item.BaseFromOilchemIndexId = int(id)
+//			IndexMap[item.IndexName] = &item
+//		}
+//
+//
+//		//获取指标数据信息
+//		data, e := models.GetBaseFromOilchemData(indexCode, v.DataTime)
+//		if e != nil && e.Error() != utils.ErrNoRow() {
+//			err = e
+//			fmt.Println("select err:", err)
+//			utils.FileLog.Info("GetBaseFromTradeSci99IndexAll err:", err)
+//		}
+//
+//		if data != nil {
+//			if data.Value != v.Value {
+//				// 更新
+//				fmt.Println("更新指标:", indexCode+v.DataTime)
+//				utils.FileLog.Info("更新指标:", indexCode+v.DataTime)
+//				e = models.UpdateBaseFromOilchemData(v.Value, indexCode, v.DataTime)
+//				if e != nil {
+//					err = e
+//					fmt.Println("Error update into database:", err)
+//					utils.FileLog.Info("Error update into database:", err)
+//					return
+//				}
+//			}
+//		} else {
+//			// 新增
+//			dataItem := models.BaseFromOilchemData{
+//				BaseFromOilchemIndexId: IndexMap[v.IndexName].BaseFromOilchemIndexId,
+//				IndexCode:              indexCode,
+//				DataTime:               v.DataTime,
+//				Value:                  v.Value,
+//				CreateTime:             v.CreateTime,
+//				ModifyTime:             v.ModifyTime,
+//			}
+//			fmt.Println("新增数据:", indexCode+v.DataTime)
+//			utils.FileLog.Info("新增数据:", indexCode+v.DataTime)
+//
+//			_,e = models.AddBaseFromOilchemData(&dataItem)
+//			if e != nil {
+//				err = e
+//				fmt.Println("Error inserting into database:", err)
+//				utils.FileLog.Info("Error inserting into database:", err)
+//				return
+//			}
+//		}
+//	}
+//
+//	return
+//}
+//
+//
+//func OilchemIndexCodeGenerator(indexName, indexCodeStr, marketSampleName string) (indexCode string, needAdd bool) {
+//	strResult := ""
+//	indexCode, _ = IndexCodeMap[indexName]
+//	if indexCode == "" {
+//		//首字母
+//		a := pinyin.NewArgs()
+//		a.Fallback = func(r rune, a pinyin.Args) []string {
+//			return []string{string(r)}
+//		}
+//		indexCodeStr = strings.Replace(indexCodeStr, "(", "", -1)
+//		indexCodeStr = strings.Replace(indexCodeStr, ")", "", -1)
+//		rows := pinyin.Pinyin(indexCodeStr, a)
+//		for i := 0; i < len(rows); i++ {
+//			//strResult += rows[i][0]
+//			if len(rows[i]) != 0 {
+//				str := rows[i][0]
+//				pi := str[0:1]
+//				strResult += pi
+//			}
+//		}
+//
+//		// 处理括号内名称
+//		if marketSampleName != "" {
+//			if province, ok := ProvinceMap[marketSampleName]; ok {
+//				strResult += province
+//			} else {
+//				a := pinyin.NewArgs()
+//				rows := pinyin.LazyPinyin(marketSampleName, a)
+//				for i := 0; i < len(rows); i++ {
+//					strResult += rows[i]
+//				}
+//				if len(rows) == 0 {
+//					strResult += marketSampleName
+//				}
+//			}
+//		}
+//
+//
+//		// 去除特殊符号
+//		strResult = strings.Replace(strResult, " ", "", -1)
+//		strResult = strings.Replace(strResult, "-", "", -1)
+//		strResult = strings.Replace(strResult, "/", "", -1)
+//		strResult = strings.Replace(strResult, "#", "", -1)
+//		strResult = strings.Replace(strResult, ":", "", -1)
+//		strResult = strings.Replace(strResult, "(", "", -1)
+//		strResult = strings.Replace(strResult, ")", "", -1)
+//
+//
+//		needAdd = true
+//		strResult = "lzzxw" + strResult
+//		indexCode = strings.Replace(strResult, " ", "", -1)
+//		IndexCodeMap[indexName] = indexCode
+//	}
+//	return
+//}
+//
+//var ProvinceMap = map[string]string{
+//	"上海":  "shanghai",
+//	"云南":  "yunnan",
+//	"内蒙古": "innermongolia",
+//	"北京":  "beijing",
+//	"台湾":  "taiwan",
+//	"吉林":  "jilin",
+//	"四川":  "sichuan",
+//	"天津":  "tianjin",
+//	"宁夏":  "ningxia",
+//	"安徽":  "anhui",
+//	"山东":  "shandong",
+//	"山西":  "shanxi",
+//	"广东":  "guangdong",
+//	"广西":  "guangxi",
+//	"新疆":  "xinjiang",
+//	"江苏":  "jiangsu",
+//	"江西":  "jiangxi",
+//	"河北":  "hebei",
+//	"河南":  "henan",
+//	"浙江":  "zhejiang",
+//	"海南":  "hainan",
+//	"湖北":  "hubei",
+//	"湖南":  "hunan",
+//	"澳门":  "macao",
+//	"甘肃":  "gansu",
+//	"福建":  "fujian",
+//	"西藏":  "tibet",
+//	"贵州":  "guizhou",
+//	"辽宁":  "liaoning",
+//	"重庆":  "chongqing",
+//	"陕西":  "shaanxi",
+//	"青海":  "qinhai",
+//	"香港":  "hongkong",
+//	"黑龙江": "heilongjiang",
+//}

+ 3 - 1
utils/constants.go

@@ -110,7 +110,8 @@ const (
 	DATA_SOURCE_SCI99                                = 85 // 卓创资讯
 	DATA_SOURCE_CCF                                  = 86 // CCF化纤信息
 	DATA_SOURCE_SCI_HQ                               = 88 // 卓创红期->88
-	DATA_SOURCE_OILCHEM       = 89 // 隆众资讯 -> 89
+	DATA_SOURCE_OILCHEM                              = 89 // 隆众资讯 -> 89
+	DATA_SOURCE_HISUGAR                              = 91 // 泛糖科技 -> 91
 )
 
 // 指标来源的中文展示
@@ -199,6 +200,7 @@ const (
 	DATA_SOURCE_NAME_CCF                                  = `CCF`  // CCF化纤信息
 	DATA_SOURCE_NAME_SCI_HQ                               = `卓创红期` // 卓创红期
 	DATA_SOURCE_NAME_OILCHEM                              = `隆众资讯` // 隆众资讯 -> 89
+	DATA_SOURCE_NAME_HISUGAR                              = `泛糖科技` // 泛糖科技 -> 91
 )
 
 // 基础数据初始化日期