base_from_ccf.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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=?,modify_time=NOW() WHERE index_code=? `
  209. _, err = o.Raw(sql, item.MinDate, item.MaxDate, 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. return
  286. }
  287. // BaseFromCCFClassify CCF化纤数据分类表
  288. type BaseFromCCFClassify struct {
  289. ClassifyId int `orm:"column(classify_id);pk"`
  290. ClassifyName string `description:"分类名称"`
  291. ParentId int `description:"父级id"`
  292. SysUserId int `description:"创建人id"`
  293. SysUserRealName string `description:"创建人姓名"`
  294. Level int `description:"层级"`
  295. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  296. ModifyTime time.Time `description:"修改时间"`
  297. CreateTime time.Time `description:"创建时间"`
  298. }
  299. func (m *BaseFromCCFClassify) Add() (lastId int64, err error) {
  300. o := orm.NewOrm()
  301. lastId, err = o.Insert(m)
  302. return
  303. }
  304. func (m *BaseFromCCFClassify) Update(updateCols []string) (err error) {
  305. o := orm.NewOrm()
  306. _, err = o.Update(m, updateCols...)
  307. return
  308. }
  309. func (m *BaseFromCCFClassify) GetByClassifyName(classifyName string) (item *BaseFromCCFClassify, err error) {
  310. o := orm.NewOrm()
  311. sql := ` SELECT * FROM base_from_ccf_classify WHERE classify_name=? `
  312. err = o.Raw(sql, classifyName).QueryRow(&item)
  313. return
  314. }
  315. func (m *BaseFromCCFClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromCCFClassify, err error) {
  316. o := orm.NewOrm()
  317. fields := strings.Join(fieldArr, ",")
  318. if len(fieldArr) == 0 {
  319. fields = `*`
  320. }
  321. order := `ORDER BY create_time DESC`
  322. if orderRule != "" {
  323. order = ` ORDER BY ` + orderRule
  324. }
  325. sql := fmt.Sprintf(`SELECT %s FROM base_from_ccf_classify WHERE 1=1 %s %s`, fields, condition, order)
  326. _, err = o.Raw(sql, pars).QueryRows(&items)
  327. return
  328. }
  329. // BaseFromCCFClassifyItem CCF化纤数据分类信息
  330. type BaseFromCCFClassifyItem struct {
  331. ClassifyId int `description:"分类ID"`
  332. ClassifyName string `description:"分类名称"`
  333. ParentId int `description:"父级id"`
  334. Level int `description:"层级"`
  335. Sort int `description:"排序字段"`
  336. CreateTime string `description:"创建时间"`
  337. ModifyTime string `description:"修改时间"`
  338. Child []*BaseFromCCFClassifyItem `description:"子分类"`
  339. }
  340. func (m *BaseFromCCFClassify) Format2Item(origin *BaseFromCCFClassify) (item *BaseFromCCFClassifyItem) {
  341. if origin == nil {
  342. return
  343. }
  344. item = new(BaseFromCCFClassifyItem)
  345. item.ClassifyId = origin.ClassifyId
  346. item.ClassifyName = origin.ClassifyName
  347. item.ParentId = origin.ParentId
  348. item.Level = origin.Level
  349. item.Sort = origin.Sort
  350. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  351. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  352. return
  353. }
  354. // MultiUpdateBaseFromCCFDataValue 批量更新CCF化纤指标数据
  355. func MultiUpdateBaseFromCCFDataValue(items []*BaseFromCCFData) (err error) {
  356. if len(items) == 0 {
  357. return
  358. }
  359. o := orm.NewOrm()
  360. sql := `UPDATE base_from_ccf_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ? LIMIT 1`
  361. p, err := o.Raw(sql).Prepare()
  362. if err != nil {
  363. return
  364. }
  365. defer func() {
  366. _ = p.Close()
  367. }()
  368. for _, v := range items {
  369. if v.IndexCode == "" || v.DataTime == "" {
  370. continue
  371. }
  372. _, err = p.Exec(v.Value, v.IndexCode, v.DataTime)
  373. if err != nil {
  374. return
  375. }
  376. }
  377. return
  378. }