base_from_gpr_risk.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package models
  2. import (
  3. "bytes"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/beego/beego/v2/client/orm"
  10. )
  11. // BaseFromGprRisk 美国农业部
  12. type BaseFromGprRisk struct{}
  13. type BaseFromGprRiskData struct {
  14. BaseFromGprRiskDataId int `orm:"column(base_from_gpr_risk_data_id);pk"`
  15. BaseFromGprRiskIndexId int
  16. IndexCode string
  17. DataTime string
  18. Value string
  19. CreateTime time.Time
  20. ModifyTime time.Time
  21. DataTimestamp int64
  22. }
  23. func GetBaseFromGprRiskDataByCondition(condition string, pars []interface{}) (list []*BaseFromGprRiskData, err error) {
  24. o := orm.NewOrm()
  25. sql := `SELECT * FROM base_from_gpr_risk_data WHERE 1=1 `
  26. if condition != "" {
  27. sql += condition
  28. }
  29. _, err = o.Raw(sql, pars).QueryRows(&list)
  30. return
  31. }
  32. // Add 添加
  33. func (obj BaseFromGprRisk) Add(edbCode string) (err error) {
  34. o := orm.NewOrm()
  35. var condition string
  36. var pars []interface{}
  37. if edbCode != "" {
  38. condition += " AND index_code=? "
  39. pars = append(pars, edbCode)
  40. }
  41. gprRiskBaseDataAll, err := GetBaseFromGprRiskDataByCondition(condition, pars)
  42. if err != nil && err.Error() != utils.ErrNoRow() {
  43. return
  44. }
  45. var isAdd bool
  46. //fmt.Println("开始时间", time.Now())
  47. addSql := ` INSERT INTO edb_data_gpr_risk(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  48. length := len(gprRiskBaseDataAll)
  49. existMap := make(map[string]struct{}, length)
  50. nowStr := time.Now().Format(utils.FormatDateTime)
  51. var buffer bytes.Buffer
  52. buffer.WriteString(addSql)
  53. for i := 0; i < length; i++ {
  54. sv := gprRiskBaseDataAll[i]
  55. eDate := sv.DataTime
  56. dataTime, err := time.Parse(utils.FormatDate, eDate)
  57. if err != nil {
  58. return err
  59. }
  60. timestamp := dataTime.UnixNano() / 1e6
  61. timeStr := fmt.Sprintf("%d", timestamp)
  62. if _, ok := existMap[eDate]; !ok {
  63. sqlTmp := GetAddSqlV2("0", edbCode, eDate, timeStr, sv.Value, nowStr)
  64. buffer.WriteString(sqlTmp)
  65. isAdd = true
  66. }
  67. existMap[eDate] = struct{}{}
  68. }
  69. //fmt.Println("结束时间", time.Now())
  70. if isAdd {
  71. addSql = buffer.String()
  72. addSql = strings.TrimRight(addSql, ",")
  73. utils.FileLog.Info("addSql:" + addSql)
  74. _, err = o.Raw(addSql).Exec()
  75. if err != nil {
  76. return err
  77. }
  78. }
  79. return
  80. }
  81. // Refresh 刷新涌益咨询指标数据
  82. func (obj BaseFromGprRisk) Refresh(edbInfoId int, edbCode, startDate string) (err error) {
  83. source := obj.GetSource()
  84. o := orm.NewOrm()
  85. if err != nil {
  86. return
  87. }
  88. edbInfoIdStr := strconv.Itoa(edbInfoId)
  89. //计算数据
  90. var condition string
  91. var pars []interface{}
  92. if edbCode != "" {
  93. condition += " AND index_code=? "
  94. pars = append(pars, edbCode)
  95. }
  96. if startDate != "" {
  97. condition += " AND data_time>=? "
  98. pars = append(pars, startDate)
  99. }
  100. GprRiskDataList, err := GetBaseFromGprRiskDataByCondition(condition, pars)
  101. if err != nil {
  102. return
  103. }
  104. // 真实数据的最大日期 , 插入规则配置的日期
  105. var realDataMaxDate, edbDataInsertConfigDate time.Time
  106. var edbDataInsertConfig *EdbDataInsertConfig
  107. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  108. {
  109. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  110. if err != nil && err.Error() != utils.ErrNoRow() {
  111. return
  112. }
  113. if edbDataInsertConfig != nil {
  114. edbDataInsertConfigDate = edbDataInsertConfig.Date
  115. }
  116. }
  117. var existCondition string
  118. var existPars []interface{}
  119. existCondition += " AND edb_info_id=? "
  120. existPars = append(existPars, edbInfoId)
  121. if startDate != "" {
  122. existCondition += " AND data_time>=? "
  123. existPars = append(existPars, startDate)
  124. }
  125. existList, err := GetEdbDataByCondition(source, 0, existCondition, existPars)
  126. if err != nil {
  127. return err
  128. }
  129. existMap := make(map[string]*EdbInfoSearchData)
  130. for _, v := range existList {
  131. existMap[v.DataTime] = v
  132. }
  133. addSql := ` INSERT INTO edb_data_gpr_risk(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  134. var isAdd bool
  135. for _, v := range GprRiskDataList {
  136. item := v
  137. eDate := item.DataTime
  138. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  139. if err != nil {
  140. return err
  141. }
  142. if findItem, ok := existMap[v.DataTime]; !ok {
  143. sValue := item.Value
  144. timestamp := dataTime.UnixNano() / 1e6
  145. timeStr := fmt.Sprintf("%d", timestamp)
  146. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  147. isAdd = true
  148. } else {
  149. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  150. err = ModifyEdbDataById(source, 0, findItem.EdbDataId, item.Value)
  151. if err != nil {
  152. return err
  153. }
  154. }
  155. }
  156. // 下面代码主要目的是处理掉手动插入的数据判断
  157. {
  158. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  159. realDataMaxDate = dataTime
  160. }
  161. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  162. isFindConfigDateRealData = true
  163. }
  164. }
  165. }
  166. // 处理手工数据补充的配置
  167. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, 0, existMap, isFindConfigDateRealData)
  168. if isAdd {
  169. addSql = strings.TrimRight(addSql, ",")
  170. _, err = o.Raw(addSql).Exec()
  171. if err != nil {
  172. fmt.Println("RefreshEdbDataFromBaiinfo add Err", err.Error())
  173. return
  174. }
  175. }
  176. return
  177. }
  178. // GetSource 获取来源编码id
  179. func (obj BaseFromGprRisk) GetSource() int {
  180. return utils.DATA_SOURCE_GPR_RISK
  181. }
  182. // GetSourceName 获取来源名称
  183. func (obj BaseFromGprRisk) GetSourceName() string {
  184. return utils.DATA_SOURCE_NAME_GPR_RISK
  185. }
  186. type BaseFromGprRiskIndex struct {
  187. BaseFromGprRiskIndexId int64 `orm:"column(base_from_gpr_risk_index_id);pk"`
  188. IndexCode string
  189. IndexName string
  190. Frequency string
  191. Unit string
  192. StartDate string
  193. EndDate string
  194. ClassifyId int64
  195. Sort int
  196. BaseFileName string
  197. RenameFileName string
  198. TerminalCode string
  199. CreateTime time.Time
  200. ModifyTime time.Time
  201. }
  202. type BaseFromGprRiskIndexList struct {
  203. BaseFromGprRiskIndexId int64 `orm:"column(base_from_gpr_risk_index_id);pk"`
  204. IndexCode string
  205. IndexName string
  206. Frequency string
  207. Unit string
  208. Sort int
  209. ClassifyId int64
  210. StartDate string
  211. EndDate string
  212. TerminalCode string
  213. CreateTime string
  214. ModifyTime string
  215. }
  216. func (y *BaseFromGprRiskData) GetByIndexCode(indexCode string) (list []*BaseFromGprRiskData, err error) {
  217. o := orm.NewOrm()
  218. sql := ` SELECT * FROM base_from_gpr_risk_data WHERE index_code=? `
  219. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  220. return
  221. }
  222. func (y *BaseFromGprRiskData) AddMulti(item []*BaseFromGprRiskData) (err error) {
  223. o := orm.NewOrm()
  224. _, err = o.InsertMulti(1, item)
  225. return
  226. }
  227. // Update 修改
  228. func (y *BaseFromGprRiskData) Update(updateCols []string) (err error) {
  229. o := orm.NewOrm()
  230. _, err = o.Update(y, updateCols...)
  231. return
  232. }
  233. // HandleGprRiskExcelData 涌益咨询的excel数据
  234. type HandleGprRiskExcelData struct {
  235. ClassifyName string `description:"指标目录"`
  236. ParentClassifyName string `description:"父级指标目录"`
  237. ClassifySort int `description:"指标目录排序号"`
  238. IndexName string `description:"指标名称"`
  239. IndexCode string `description:"指标编码"`
  240. Unit string `description:"单位"`
  241. Sort int `description:"排序号"`
  242. Frequency string `description:"频度"`
  243. ExcelDataMap map[string]string
  244. }
  245. type HandleGprRiskExcelDataReq struct {
  246. List []*HandleGprRiskExcelData
  247. TerminalCode string `description:"编码"`
  248. }
  249. func (y *BaseFromGprRiskData) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  250. o := orm.NewOrm()
  251. 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_gpr_risk_data WHERE index_code=? `
  252. err = o.Raw(sql, indexCode).QueryRow(&item)
  253. var latest_value float64
  254. sql = ` SELECT value AS latest_value FROM %s WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
  255. sql = fmt.Sprintf(sql, "base_from_gpr_risk_data")
  256. err = o.Raw(sql, indexCode).QueryRow(&latest_value)
  257. item.LatestValue = latest_value
  258. return
  259. }
  260. func (y *BaseFromGprRiskIndex) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  261. o := orm.NewOrm()
  262. sql := ` UPDATE base_from_gpr_risk_index SET start_date=?,end_date=?, end_value=?, modify_time=NOW() WHERE index_code=? `
  263. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  264. return
  265. }
  266. func (y *BaseFromGprRiskIndex) GetByIndexCode(indexCode string) (item *BaseFromGprRiskIndex, err error) {
  267. o := orm.NewOrm()
  268. sql := ` SELECT * FROM base_from_gpr_risk_index WHERE index_code=? `
  269. err = o.Raw(sql, indexCode).QueryRow(&item)
  270. return
  271. }
  272. func (y *BaseFromGprRiskIndex) Add() (lastId int64, err error) {
  273. o := orm.NewOrm()
  274. lastId, err = o.Insert(y)
  275. return
  276. }
  277. // Update 修改
  278. func (y *BaseFromGprRiskIndex) Update(updateCols []string) (err error) {
  279. o := orm.NewOrm()
  280. _, err = o.Update(y, updateCols...)
  281. return
  282. }
  283. // BaseFromGprRiskClassify GprRisk原始数据分类表
  284. type BaseFromGprRiskClassify struct {
  285. ClassifyId int64 `orm:"column(classify_id);pk"`
  286. ClassifyName string `description:"分类名称"`
  287. ClassifyNameEn string `description:"分类名称"`
  288. ParentId int `description:"父级id"`
  289. SysUserId int `description:"创建人id"`
  290. SysUserRealName string `description:"创建人姓名"`
  291. Level int `description:"层级"`
  292. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  293. ModifyTime time.Time `description:"修改时间"`
  294. CreateTime time.Time `description:"创建时间"`
  295. }
  296. func (y *BaseFromGprRiskClassify) Add() (lastId int64, err error) {
  297. o := orm.NewOrm()
  298. lastId, err = o.Insert(y)
  299. return
  300. }
  301. // Update 修改
  302. func (y *BaseFromGprRiskClassify) Update(updateCols []string) (err error) {
  303. o := orm.NewOrm()
  304. _, err = o.Update(y, updateCols...)
  305. return
  306. }
  307. func (y *BaseFromGprRiskClassify) GetByClassifyName(classifyName string) (item *BaseFromGprRiskClassify, err error) {
  308. o := orm.NewOrm()
  309. sql := ` SELECT * FROM base_from_gpr_risk_classify WHERE classify_name=? `
  310. err = o.Raw(sql, classifyName).QueryRow(&item)
  311. return
  312. }
  313. func (y *BaseFromGprRiskClassify) GetParentClassify() (items []*BaseFromGprRiskClassify, err error) {
  314. o := orm.NewOrm()
  315. sql := ` SELECT * FROM base_from_gpr_risk_classify WHERE parent_id=0 `
  316. _, err = o.Raw(sql).QueryRows(&items)
  317. return
  318. }