base_from_gpr_risk.go 10 KB

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