base_from_usda_fas.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // BaseFromUsdaFas 美国农业部
  11. type BaseFromUsdaFas struct{}
  12. type BaseFromUsdaFasData struct {
  13. BaseFromUsdaFasDataId int `orm:"column(base_from_usda_fas_data_id);pk"`
  14. BaseFromUsdaFasIndexId int
  15. IndexCode string
  16. DataTime string
  17. Value string
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. DataTimestamp int64
  21. }
  22. func GetBaseFromUsdaFasDataByCondition(condition string, pars []interface{}) (list []*BaseFromUsdaFasData, err error) {
  23. o := orm.NewOrm()
  24. sql := `SELECT * FROM base_from_usda_fas_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 BaseFromUsdaFas) 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. UsdaFasBaseDataAll, err := GetBaseFromUsdaFasDataByCondition(condition, pars)
  41. if err != nil && err.Error() != utils.ErrNoRow() {
  42. return
  43. }
  44. var isAdd bool
  45. addSql := ` INSERT INTO edb_data_usda_fas(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 UsdaFasBaseDataAll {
  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 BaseFromUsdaFas) 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. UsdaFasDataList, err := GetBaseFromUsdaFasDataByCondition(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_usda_fas(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  124. var isAdd bool
  125. for _, v := range UsdaFasDataList {
  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 BaseFromUsdaFas) GetSource() int {
  170. return utils.DATA_SOURCE_USDA_FAS
  171. }
  172. // GetSourceName 获取来源名称
  173. func (obj BaseFromUsdaFas) GetSourceName() string {
  174. return utils.DATA_SOURCE_NAME_USDA_FAS
  175. }
  176. type BaseFromUsdaFasIndex struct {
  177. BaseFromUsdaFasIndexId int64 `orm:"column(base_from_usda_fas_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. Country string `description:"国家"`
  190. Commodity string `description:"属性"`
  191. CreateTime time.Time
  192. ModifyTime time.Time
  193. }
  194. type BaseFromUsdaFasIndexList struct {
  195. BaseFromUsdaFasIndexId int64 `orm:"column(base_from_usda_fas_index_id);pk"`
  196. IndexCode string
  197. IndexName string
  198. Frequency string
  199. Unit string
  200. Sort int
  201. ClassifyId int64
  202. StartDate string
  203. EndDate string
  204. TerminalCode string
  205. CreateTime string
  206. ModifyTime string
  207. }
  208. func (y *BaseFromUsdaFasData) GetByIndexCode(indexCode string) (list []*BaseFromUsdaFasData, err error) {
  209. o := orm.NewOrm()
  210. sql := ` SELECT * FROM base_from_usda_fas_data WHERE index_code=? `
  211. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  212. return
  213. }
  214. func (y *BaseFromUsdaFasData) AddMulti(item []*BaseFromUsdaFasData) (err error) {
  215. o := orm.NewOrm()
  216. _, err = o.InsertMulti(1, item)
  217. return
  218. }
  219. // Update 修改
  220. func (y *BaseFromUsdaFasData) Update(updateCols []string) (err error) {
  221. o := orm.NewOrm()
  222. _, err = o.Update(y, updateCols...)
  223. return
  224. }
  225. // HandleUsdaFasExcelData 涌益咨询的excel数据
  226. type HandleUsdaFasExcelData struct {
  227. ClassifyName string `description:"指标目录"`
  228. ParentClassifyName string `description:"父级指标目录"`
  229. ClassifySort int `description:"指标目录排序号"`
  230. IndexName string `description:"指标名称"`
  231. IndexCode string `description:"指标编码"`
  232. Unit string `description:"单位"`
  233. Sort int `description:"排序号"`
  234. Frequency string `description:"频度"`
  235. Country string `description:"国家"`
  236. Commodity string `description:"属性"`
  237. ExcelDataMap map[string]string
  238. }
  239. type HandleUsdaFasExcelDataReq struct {
  240. List []*HandleUsdaFasExcelData
  241. TerminalCode string `description:"编码"`
  242. }
  243. func (y *BaseFromUsdaFasData) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  244. o := orm.NewOrm()
  245. 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_usda_fas_data WHERE index_code=? `
  246. err = o.Raw(sql, indexCode).QueryRow(&item)
  247. var latest_value float64
  248. sql = ` SELECT value AS latest_value FROM %s WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
  249. sql = fmt.Sprintf(sql, "base_from_usda_fas_data")
  250. err = o.Raw(sql, indexCode).QueryRow(&latest_value)
  251. item.LatestValue = latest_value
  252. return
  253. }
  254. func (y *BaseFromUsdaFasIndex) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  255. o := orm.NewOrm()
  256. sql := ` UPDATE base_from_usda_fas_index SET start_date=?,end_date=?, end_value=?, modify_time=NOW() WHERE index_code=? `
  257. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  258. return
  259. }
  260. func (y *BaseFromUsdaFasIndex) GetByIndexCode(indexCode string) (item *BaseFromUsdaFasIndex, err error) {
  261. o := orm.NewOrm()
  262. sql := ` SELECT * FROM base_from_usda_fas_index WHERE index_code=? `
  263. err = o.Raw(sql, indexCode).QueryRow(&item)
  264. return
  265. }
  266. func (y *BaseFromUsdaFasIndex) Add() (lastId int64, err error) {
  267. o := orm.NewOrm()
  268. lastId, err = o.Insert(y)
  269. return
  270. }
  271. // Update 修改
  272. func (y *BaseFromUsdaFasIndex) Update(updateCols []string) (err error) {
  273. o := orm.NewOrm()
  274. _, err = o.Update(y, updateCols...)
  275. return
  276. }
  277. // BaseFromUsdaFasClassify UsdaFas原始数据分类表
  278. type BaseFromUsdaFasClassify struct {
  279. ClassifyId int64 `orm:"column(classify_id);pk"`
  280. ClassifyName string `description:"分类名称"`
  281. ClassifyNameEn string `description:"分类名称"`
  282. ParentId int `description:"父级id"`
  283. SysUserId int `description:"创建人id"`
  284. SysUserRealName string `description:"创建人姓名"`
  285. Level int `description:"层级"`
  286. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  287. ModifyTime time.Time `description:"修改时间"`
  288. CreateTime time.Time `description:"创建时间"`
  289. }
  290. func (y *BaseFromUsdaFasClassify) Add() (lastId int64, err error) {
  291. o := orm.NewOrm()
  292. lastId, err = o.Insert(y)
  293. return
  294. }
  295. // Update 修改
  296. func (y *BaseFromUsdaFasClassify) Update(updateCols []string) (err error) {
  297. o := orm.NewOrm()
  298. _, err = o.Update(y, updateCols...)
  299. return
  300. }
  301. func (y *BaseFromUsdaFasClassify) GetByClassifyName(classifyName string) (item *BaseFromUsdaFasClassify, err error) {
  302. o := orm.NewOrm()
  303. sql := ` SELECT * FROM base_from_usda_fas_classify WHERE classify_name=? `
  304. err = o.Raw(sql, classifyName).QueryRow(&item)
  305. return
  306. }
  307. func (y *BaseFromUsdaFasClassify) GetParentClassify() (items []*BaseFromUsdaFasClassify, err error) {
  308. o := orm.NewOrm()
  309. sql := ` SELECT * FROM base_from_usda_fas_classify WHERE parent_id=0 `
  310. _, err = o.Raw(sql).QueryRows(&items)
  311. return
  312. }