base_from_fenwei.go 14 KB

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