base_from_ccf.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // BaseFromCCF CCF化纤信息
  11. type BaseFromCCF struct{}
  12. type BaseFromCCFData struct {
  13. BaseFromCcfDataId int `orm:"column(base_from_ccf_data_id);pk"`
  14. BaseFromCcfIndexId int
  15. IndexCode string
  16. DataTime string
  17. Value string
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. DataTimestamp int64
  21. }
  22. func (m *BaseFromCCFData) TableName() string {
  23. return "base_from_ccf_data"
  24. }
  25. func GetBaseFromCCFDataByCondition(condition string, pars []interface{}) (list []*BaseFromCCFData, err error) {
  26. o := orm.NewOrm()
  27. sql := `SELECT * FROM base_from_ccf_data WHERE 1=1 `
  28. if condition != "" {
  29. sql += condition
  30. }
  31. _, err = o.Raw(sql, pars).QueryRows(&list)
  32. return
  33. }
  34. // Add 添加
  35. func (obj BaseFromCCF) Add(edbCode string) (err error) {
  36. o := orm.NewOrm()
  37. var condition string
  38. var pars []interface{}
  39. if edbCode != "" {
  40. condition += " AND index_code = ? "
  41. pars = append(pars, edbCode)
  42. }
  43. ccfBaseDataAll, err := GetBaseFromCCFDataByCondition(condition, pars)
  44. if err != nil && err.Error() != utils.ErrNoRow() {
  45. return
  46. }
  47. var isAdd bool
  48. addSql := ` INSERT INTO edb_data_ccf(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  49. existMap := make(map[string]string)
  50. for _, sv := range ccfBaseDataAll {
  51. eDate := sv.DataTime
  52. dataTime, err := time.Parse(utils.FormatDate, eDate)
  53. if err != nil {
  54. return err
  55. }
  56. timestamp := dataTime.UnixNano() / 1e6
  57. timeStr := fmt.Sprintf("%d", timestamp)
  58. if _, ok := existMap[eDate]; !ok {
  59. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  60. isAdd = true
  61. }
  62. existMap[eDate] = sv.Value
  63. }
  64. if isAdd {
  65. addSql = strings.TrimRight(addSql, ",")
  66. utils.FileLog.Info("addSql:" + addSql)
  67. _, err = o.Raw(addSql).Exec()
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. return
  73. }
  74. // Refresh 刷新CCF化纤指标数据
  75. func (obj BaseFromCCF) Refresh(edbInfoId int, edbCode, startDate string) (err error) {
  76. source := obj.GetSource()
  77. o := orm.NewOrm()
  78. if err != nil {
  79. return
  80. }
  81. edbInfoIdStr := strconv.Itoa(edbInfoId)
  82. //计算数据
  83. var condition string
  84. var pars []interface{}
  85. if edbCode != "" {
  86. condition += " AND index_code=? "
  87. pars = append(pars, edbCode)
  88. }
  89. if startDate != "" {
  90. condition += " AND data_time>=? "
  91. pars = append(pars, startDate)
  92. }
  93. ccfDataList, err := GetBaseFromCCFDataByCondition(condition, pars)
  94. if err != nil {
  95. return
  96. }
  97. // 真实数据的最大日期 , 插入规则配置的日期
  98. var realDataMaxDate, edbDataInsertConfigDate time.Time
  99. var edbDataInsertConfig *EdbDataInsertConfig
  100. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  101. {
  102. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  103. if err != nil && err.Error() != utils.ErrNoRow() {
  104. return
  105. }
  106. if edbDataInsertConfig != nil {
  107. edbDataInsertConfigDate = edbDataInsertConfig.Date
  108. }
  109. }
  110. var existCondition string
  111. var existPars []interface{}
  112. existCondition += " AND edb_info_id=? "
  113. existPars = append(existPars, edbInfoId)
  114. if startDate != "" {
  115. existCondition += " AND data_time>=? "
  116. existPars = append(existPars, startDate)
  117. }
  118. existList, err := GetEdbDataByCondition(source, 0, existCondition, existPars)
  119. if err != nil {
  120. return err
  121. }
  122. existMap := make(map[string]*EdbInfoSearchData)
  123. for _, v := range existList {
  124. existMap[v.DataTime] = v
  125. }
  126. addSql := ` INSERT INTO edb_data_ccf(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  127. var isAdd bool
  128. for _, v := range ccfDataList {
  129. item := v
  130. eDate := item.DataTime
  131. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  132. if err != nil {
  133. return err
  134. }
  135. if findItem, ok := existMap[v.DataTime]; !ok {
  136. sValue := item.Value
  137. timestamp := dataTime.UnixNano() / 1e6
  138. timeStr := fmt.Sprintf("%d", timestamp)
  139. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  140. isAdd = true
  141. } else {
  142. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  143. err = ModifyEdbDataById(source, 0, findItem.EdbDataId, item.Value)
  144. if err != nil {
  145. return err
  146. }
  147. }
  148. }
  149. // 下面代码主要目的是处理掉手动插入的数据判断
  150. {
  151. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  152. realDataMaxDate = dataTime
  153. }
  154. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  155. isFindConfigDateRealData = true
  156. }
  157. }
  158. }
  159. // 处理手工数据补充的配置
  160. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, 0, existMap, isFindConfigDateRealData)
  161. if isAdd {
  162. addSql = strings.TrimRight(addSql, ",")
  163. _, err = o.Raw(addSql).Exec()
  164. if err != nil {
  165. fmt.Println("RefreshEdbDataFromBaiinfo add Err", err.Error())
  166. return
  167. }
  168. }
  169. return
  170. }
  171. // GetSource 获取来源编码id
  172. func (obj BaseFromCCF) GetSource() int {
  173. return utils.DATA_SOURCE_CCF
  174. }
  175. // GetSourceName 获取来源名称
  176. func (obj BaseFromCCF) GetSourceName() string {
  177. return utils.DATA_SOURCE_NAME_CCF
  178. }
  179. type BaseFromCCFIndex struct {
  180. BaseFromCcfIndexId int64 `orm:"column(base_from_ccf_index_id);pk"`
  181. IndexCode string
  182. IndexName string
  183. Frequency string
  184. Unit string
  185. ClassifyId int
  186. StartDate string
  187. EndDate string
  188. Sort int
  189. TerminalCode string
  190. CreateTime time.Time
  191. ModifyTime time.Time
  192. }
  193. func (m *BaseFromCCFIndex) TableName() string {
  194. return "base_from_ccf_index"
  195. }
  196. func (m *BaseFromCCFIndex) Add() (lastId int64, err error) {
  197. o := orm.NewOrm()
  198. lastId, err = o.Insert(m)
  199. return
  200. }
  201. func (m *BaseFromCCFIndex) Update(updateCols []string) (err error) {
  202. o := orm.NewOrm()
  203. _, err = o.Update(m, updateCols...)
  204. return
  205. }
  206. func (m *BaseFromCCFIndex) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  207. o := orm.NewOrm()
  208. sql := ` UPDATE base_from_ccf_index SET start_date=?,end_date=?,latest_value=?,modify_time=NOW() WHERE index_code=? `
  209. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  210. return
  211. }
  212. func (m *BaseFromCCFIndex) GetByIndexCode(indexCode string) (item *BaseFromCCFIndex, err error) {
  213. o := orm.NewOrm()
  214. sql := ` SELECT * FROM base_from_ccf_index WHERE index_code=? `
  215. err = o.Raw(sql, indexCode).QueryRow(&item)
  216. return
  217. }
  218. func (m *BaseFromCCFIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromCCFIndex, err error) {
  219. o := orm.NewOrm()
  220. fields := strings.Join(fieldArr, ",")
  221. if len(fieldArr) == 0 {
  222. fields = `*`
  223. }
  224. order := `ORDER BY create_time DESC`
  225. if orderRule != "" {
  226. order = ` ORDER BY ` + orderRule
  227. }
  228. sql := fmt.Sprintf(`SELECT %s FROM base_from_ccf_index WHERE 1=1 %s %s`, fields, condition, order)
  229. _, err = o.Raw(sql, pars).QueryRows(&items)
  230. return
  231. }
  232. type BaseFromCCFIndexList struct {
  233. CcfIndexId int64 `orm:"column(ccf_index_id);pk"`
  234. IndexCode string
  235. IndexName string
  236. Frequency string
  237. Unit string
  238. ClassifyId int
  239. StartDate string
  240. EndDate string
  241. TerminalCode string
  242. CreateTime string
  243. ModifyTime string
  244. }
  245. func (m *BaseFromCCFData) GetByIndexCode(indexCode string) (list []*BaseFromCCFData, err error) {
  246. o := orm.NewOrm()
  247. sql := ` SELECT * FROM base_from_ccf_data WHERE index_code=? `
  248. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  249. return
  250. }
  251. func (m *BaseFromCCFData) AddMulti(item []*BaseFromCCFData) (err error) {
  252. o := orm.NewOrm()
  253. _, err = o.InsertMulti(100, item)
  254. return
  255. }
  256. // HandleCCFEdbData CCF化纤的指标数据
  257. type HandleCCFEdbData struct {
  258. IndexName string `description:"指标名称"`
  259. IndexCode string `description:"指标编码"`
  260. ClassifyId int `description:"分类ID"`
  261. Unit string `description:"单位"`
  262. Sort int `description:"排序"`
  263. Frequency string `description:"频度"`
  264. TerminalCode string `description:"终端编码"`
  265. DateData map[string]string `description:"日期数据"`
  266. }
  267. type HandleCCFEdbDataReq struct {
  268. List []*HandleCCFEdbData
  269. TerminalCode string `description:"编码"`
  270. }
  271. type HandleCCFTableData struct {
  272. ClassifyId int `description:"分类ID"`
  273. FromPage string `description:"表格来源"`
  274. TableDate time.Time `description:"表格日期"`
  275. TableContent string `description:"表格HTML"`
  276. }
  277. type HandleCCFStockTableReq struct {
  278. Table *HandleCCFTableData
  279. TerminalCode string `description:"编码"`
  280. }
  281. func (m *BaseFromCCFData) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  282. o := orm.NewOrm()
  283. 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_ccf_data WHERE index_code=? `
  284. err = o.Raw(sql, indexCode).QueryRow(&item)
  285. var latest_value float64
  286. sql = ` SELECT value AS latest_value FROM base_from_ccf_data WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
  287. err = o.Raw(sql, indexCode).QueryRow(&latest_value)
  288. item.LatestValue = latest_value
  289. return
  290. }
  291. // BaseFromCCFClassify CCF化纤数据分类表
  292. type BaseFromCCFClassify struct {
  293. ClassifyId int `orm:"column(classify_id);pk"`
  294. ClassifyName string `description:"分类名称"`
  295. ParentId int `description:"父级id"`
  296. SysUserId int `description:"创建人id"`
  297. SysUserRealName string `description:"创建人姓名"`
  298. Level int `description:"层级"`
  299. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  300. ModifyTime time.Time `description:"修改时间"`
  301. CreateTime time.Time `description:"创建时间"`
  302. }
  303. func (m *BaseFromCCFClassify) Add() (lastId int64, err error) {
  304. o := orm.NewOrm()
  305. lastId, err = o.Insert(m)
  306. return
  307. }
  308. func (m *BaseFromCCFClassify) Update(updateCols []string) (err error) {
  309. o := orm.NewOrm()
  310. _, err = o.Update(m, updateCols...)
  311. return
  312. }
  313. func (m *BaseFromCCFClassify) GetByClassifyName(classifyName string) (item *BaseFromCCFClassify, err error) {
  314. o := orm.NewOrm()
  315. sql := ` SELECT * FROM base_from_ccf_classify WHERE classify_name=? `
  316. err = o.Raw(sql, classifyName).QueryRow(&item)
  317. return
  318. }
  319. func (m *BaseFromCCFClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromCCFClassify, err error) {
  320. o := orm.NewOrm()
  321. fields := strings.Join(fieldArr, ",")
  322. if len(fieldArr) == 0 {
  323. fields = `*`
  324. }
  325. order := `ORDER BY create_time DESC`
  326. if orderRule != "" {
  327. order = ` ORDER BY ` + orderRule
  328. }
  329. sql := fmt.Sprintf(`SELECT %s FROM base_from_ccf_classify WHERE 1=1 %s %s`, fields, condition, order)
  330. _, err = o.Raw(sql, pars).QueryRows(&items)
  331. return
  332. }
  333. // BaseFromCCFClassifyItem CCF化纤数据分类信息
  334. type BaseFromCCFClassifyItem struct {
  335. ClassifyId int `description:"分类ID"`
  336. ClassifyName string `description:"分类名称"`
  337. ParentId int `description:"父级id"`
  338. Level int `description:"层级"`
  339. Sort int `description:"排序字段"`
  340. CreateTime string `description:"创建时间"`
  341. ModifyTime string `description:"修改时间"`
  342. Child []*BaseFromCCFClassifyItem `description:"子分类"`
  343. }
  344. func (m *BaseFromCCFClassify) Format2Item(origin *BaseFromCCFClassify) (item *BaseFromCCFClassifyItem) {
  345. if origin == nil {
  346. return
  347. }
  348. item = new(BaseFromCCFClassifyItem)
  349. item.ClassifyId = origin.ClassifyId
  350. item.ClassifyName = origin.ClassifyName
  351. item.ParentId = origin.ParentId
  352. item.Level = origin.Level
  353. item.Sort = origin.Sort
  354. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  355. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  356. return
  357. }
  358. // MultiUpdateBaseFromCCFDataValue 批量更新CCF化纤指标数据
  359. func MultiUpdateBaseFromCCFDataValue(items []*BaseFromCCFData) (err error) {
  360. if len(items) == 0 {
  361. return
  362. }
  363. o := orm.NewOrm()
  364. sql := `UPDATE base_from_ccf_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ? LIMIT 1`
  365. p, err := o.Raw(sql).Prepare()
  366. if err != nil {
  367. return
  368. }
  369. defer func() {
  370. _ = p.Close()
  371. }()
  372. for _, v := range items {
  373. if v.IndexCode == "" || v.DataTime == "" {
  374. continue
  375. }
  376. _, err = p.Exec(v.Value, v.IndexCode, v.DataTime)
  377. if err != nil {
  378. return
  379. }
  380. }
  381. return
  382. }