|
@@ -0,0 +1,346 @@
|
|
|
|
+package models
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "bytes"
|
|
|
|
+ sql2 "database/sql"
|
|
|
|
+ "eta/eta_index_lib/global"
|
|
|
|
+ "eta/eta_index_lib/utils"
|
|
|
|
+ "fmt"
|
|
|
|
+ "strconv"
|
|
|
|
+ "strings"
|
|
|
|
+ "time"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// BaseFromPurang 普兰金融
|
|
|
|
+type BaseFromPurang struct{}
|
|
|
|
+
|
|
|
|
+type BaseFromPurangData struct {
|
|
|
|
+ BaseFromPurangDataId int `gorm:"column:base_from_purang_data_id;primaryKey"`
|
|
|
|
+ BaseFromPurangIndexId int
|
|
|
|
+ IndexCode string
|
|
|
|
+ DataTime time.Time
|
|
|
|
+ Value string
|
|
|
|
+ CreateTime time.Time
|
|
|
|
+ ModifyTime time.Time
|
|
|
|
+ DataTimestamp int64
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetBaseFromPurangDataByCondition(condition string, pars []interface{}) (list []*BaseFromPurangData, err error) {
|
|
|
|
+ sql := `SELECT * FROM base_from_purang_data WHERE 1=1 `
|
|
|
|
+ if condition != "" {
|
|
|
|
+ sql += condition
|
|
|
|
+ }
|
|
|
|
+ err = global.DEFAULT_DB.Raw(sql, pars...).Find(&list).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Add 添加
|
|
|
|
+func (obj BaseFromPurang) Add(edbCode string) (err error) {
|
|
|
|
+ var condition string
|
|
|
|
+ var pars []interface{}
|
|
|
|
+ if edbCode != "" {
|
|
|
|
+ condition += " AND index_code=? "
|
|
|
|
+ pars = append(pars, edbCode)
|
|
|
|
+ }
|
|
|
|
+ purangBaseDataAll, err := GetBaseFromPurangDataByCondition(condition, pars)
|
|
|
|
+ if err != nil && !utils.IsErrNoRow(err) {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ var isAdd bool
|
|
|
|
+ addSql := ` INSERT INTO edb_data_purang(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
|
+ length := len(purangBaseDataAll)
|
|
|
|
+ existMap := make(map[string]struct{}, length)
|
|
|
|
+ nowStr := time.Now().Format(utils.FormatDateTime)
|
|
|
|
+ var buffer bytes.Buffer
|
|
|
|
+ buffer.WriteString(addSql)
|
|
|
|
+ for i := 0; i < length; i++ {
|
|
|
|
+ sv := purangBaseDataAll[i]
|
|
|
|
+ eDate := sv.DataTime.Format(utils.FormatDate)
|
|
|
|
+ dataTime, err := time.Parse(utils.FormatDate, eDate)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ timestamp := dataTime.UnixNano() / 1e6
|
|
|
|
+ timeStr := fmt.Sprintf("%d", timestamp)
|
|
|
|
+ if _, ok := existMap[eDate]; !ok {
|
|
|
|
+ sqlTmp := GetAddSqlV2("0", edbCode, eDate, timeStr, sv.Value, nowStr)
|
|
|
|
+ buffer.WriteString(sqlTmp)
|
|
|
|
+ isAdd = true
|
|
|
|
+ }
|
|
|
|
+ existMap[eDate] = struct{}{}
|
|
|
|
+ }
|
|
|
|
+ if isAdd {
|
|
|
|
+ addSql = buffer.String()
|
|
|
|
+ addSql = strings.TrimRight(addSql, ",")
|
|
|
|
+ utils.FileLog.Info("addSql:" + addSql)
|
|
|
|
+ err = global.DEFAULT_DB.Exec(addSql).Error
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Refresh 刷新普兰金融指标数据
|
|
|
|
+func (obj BaseFromPurang) Refresh(edbInfoId int, edbCode, startDate string) (err error) {
|
|
|
|
+ source := obj.GetSource()
|
|
|
|
+ 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)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ PurangDataList, err := GetBaseFromPurangDataByCondition(condition, pars)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 真实数据的最大日期 , 插入规则配置的日期
|
|
|
|
+ var realDataMaxDate, edbDataInsertConfigDate time.Time
|
|
|
|
+ var edbDataInsertConfig *EdbDataInsertConfig
|
|
|
|
+ var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
|
|
|
|
+ {
|
|
|
|
+ edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
|
|
|
|
+ if err != nil && !utils.IsErrNoRow(err) {
|
|
|
|
+ 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, 0, 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_purang_risk(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
|
+ var isAdd bool
|
|
|
|
+ for _, v := range PurangDataList {
|
|
|
|
+ item := v
|
|
|
|
+ eDate := item.DataTime.Format(utils.FormatDate)
|
|
|
|
+ dataTime := item.DataTime
|
|
|
|
+ if findItem, ok := existMap[eDate]; !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, 0, 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, 0, existMap, isFindConfigDateRealData)
|
|
|
|
+
|
|
|
|
+ if isAdd {
|
|
|
|
+ addSql = strings.TrimRight(addSql, ",")
|
|
|
|
+ err = global.DEFAULT_DB.Exec(addSql).Error
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println("RefreshEdbDataFromBaiinfo add Err", err.Error())
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetSource 获取来源编码id
|
|
|
|
+func (obj BaseFromPurang) GetSource() int {
|
|
|
|
+ return utils.DATA_SOURCE_PuRang
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetSourceName 获取来源名称
|
|
|
|
+func (obj BaseFromPurang) GetSourceName() string {
|
|
|
|
+ return utils.DATA_SOURCE_NAME_PuRang
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type BaseFromPurangIndex struct {
|
|
|
|
+ BaseFromPurangIndexId int64 `gorm:"column:base_from_purang_index_id;primaryKey"`
|
|
|
|
+ IndexCode string
|
|
|
|
+ IndexName string
|
|
|
|
+ Frequency string
|
|
|
|
+ Unit string
|
|
|
|
+ StartDate time.Time
|
|
|
|
+ EndDate time.Time
|
|
|
|
+ ClassifyId int64
|
|
|
|
+ Sort int
|
|
|
|
+ BaseFileName string
|
|
|
|
+ RenameFileName string
|
|
|
|
+ TerminalCode string
|
|
|
|
+ CreateTime time.Time
|
|
|
|
+ ModifyTime time.Time
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type BaseFromPurangIndexList struct {
|
|
|
|
+ BaseFromPurangIndexId int64 `gorm:"column:base_from_purang_index_id;primaryKey"`
|
|
|
|
+ IndexCode string
|
|
|
|
+ IndexName string
|
|
|
|
+ Frequency string
|
|
|
|
+ Unit string
|
|
|
|
+ Sort int
|
|
|
|
+ ClassifyId int64
|
|
|
|
+ StartDate time.Time
|
|
|
|
+ EndDate time.Time
|
|
|
|
+ TerminalCode string
|
|
|
|
+ CreateTime time.Time
|
|
|
|
+ ModifyTime time.Time
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangData) GetByIndexCode(indexCode string) (list []*BaseFromPurangData, err error) {
|
|
|
|
+ sql := ` SELECT * FROM base_from_purang_data WHERE index_code=? `
|
|
|
|
+ err = global.DEFAULT_DB.Raw(sql, indexCode).Find(&list).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangData) AddMulti(item []*BaseFromPurangData) (err error) {
|
|
|
|
+ err = global.DEFAULT_DB.CreateInBatches(&item, 1).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Update 修改
|
|
|
|
+func (y *BaseFromPurangData) Update(updateCols []string) (err error) {
|
|
|
|
+ err = global.DEFAULT_DB.Model(&y).Select(updateCols).Updates(&y).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// HandlePurangExcelData 涌益咨询的excel数据
|
|
|
|
+type HandlePurangExcelData struct {
|
|
|
|
+ ClassifyName string `description:"指标目录"`
|
|
|
|
+ ParentClassifyName string `description:"父级指标目录"`
|
|
|
|
+ ClassifySort int `description:"指标目录排序号"`
|
|
|
|
+ IndexName string `description:"指标名称"`
|
|
|
|
+ IndexCode string `description:"指标编码"`
|
|
|
|
+ Unit string `description:"单位"`
|
|
|
|
+ Sort int `description:"排序号"`
|
|
|
|
+ Frequency string `description:"频度"`
|
|
|
|
+ ExcelDataMap map[string]string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type HandlePurangExcelDataReq struct {
|
|
|
|
+ List []*HandlePurangExcelData
|
|
|
|
+ TerminalCode string `description:"编码"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangData) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
|
|
|
|
+ sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_purang_data WHERE index_code=? `
|
|
|
|
+ err = global.DEFAULT_DB.Raw(sql, indexCode).First(&item).Error
|
|
|
|
+ sql = ` SELECT value AS latest_value FROM %s WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
|
|
|
|
+ sql = fmt.Sprintf(sql, "base_from_purang_data")
|
|
|
|
+ var latestValueNull sql2.NullFloat64
|
|
|
|
+ err = global.DEFAULT_DB.Raw(sql, indexCode).Scan(&latestValueNull).Error
|
|
|
|
+ if err == nil && latestValueNull.Valid {
|
|
|
|
+ item.LatestValue = latestValueNull.Float64
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangIndex) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
|
|
|
|
+ sql := ` UPDATE base_from_purang_index SET start_date=?,end_date=?, end_value=?, modify_time=NOW() WHERE index_code=? `
|
|
|
|
+ err = global.DEFAULT_DB.Exec(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangIndex) GetByIndexCode(indexCode string) (item *BaseFromPurangIndex, err error) {
|
|
|
|
+ sql := ` SELECT * FROM base_from_purang_index WHERE index_code=? `
|
|
|
|
+ err = global.DEFAULT_DB.Raw(sql, indexCode).First(&item).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangIndex) Add() (lastId int64, err error) {
|
|
|
|
+ err = global.DEFAULT_DB.Create(&y).Error
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ lastId = y.BaseFromPurangIndexId
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Update 修改
|
|
|
|
+func (y *BaseFromPurangIndex) Update(updateCols []string) (err error) {
|
|
|
|
+ err = global.DEFAULT_DB.Model(&y).Select(updateCols).Updates(&y).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// BaseFromPurangClassify Purang原始数据分类表
|
|
|
|
+type BaseFromPurangClassify struct {
|
|
|
|
+ ClassifyId int64 `gorm:"column:classify_id;primaryKey"`
|
|
|
|
+ ClassifyName string `description:"分类名称"`
|
|
|
|
+ ClassifyNameEn string `description:"分类名称"`
|
|
|
|
+ ParentId int `description:"父级id"`
|
|
|
|
+ SysUserId int `description:"创建人id"`
|
|
|
|
+ SysUserRealName string `description:"创建人姓名"`
|
|
|
|
+ Level int `description:"层级"`
|
|
|
|
+ Sort int `description:"排序字段,越小越靠前,默认值:10"`
|
|
|
|
+ ModifyTime time.Time `description:"修改时间"`
|
|
|
|
+ CreateTime time.Time `description:"创建时间"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangClassify) Add() (lastId int64, err error) {
|
|
|
|
+ err = global.DEFAULT_DB.Create(&y).Error
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ lastId = int64(y.ClassifyId)
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Update 修改
|
|
|
|
+func (y *BaseFromPurangClassify) Update(updateCols []string) (err error) {
|
|
|
|
+ err = global.DEFAULT_DB.Model(&y).Select(updateCols).Updates(&y).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangClassify) GetByClassifyName(classifyName string) (item *BaseFromPurangClassify, err error) {
|
|
|
|
+ sql := ` SELECT * FROM base_from_purang_classify WHERE classify_name=? `
|
|
|
|
+ err = global.DEFAULT_DB.Raw(sql, classifyName).First(&item).Error
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (y *BaseFromPurangClassify) GetParentClassify() (items []*BaseFromPurangClassify, err error) {
|
|
|
|
+ sql := ` SELECT * FROM base_from_purang_classify WHERE parent_id=0 `
|
|
|
|
+ err = global.DEFAULT_DB.Raw(sql).Find(&items).Error
|
|
|
|
+ return
|
|
|
|
+}
|