base_from_fenwei.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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=?,modify_time=NOW() WHERE index_code=? `
  210. _, err = o.Raw(sql, item.MinDate, item.MaxDate, 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. return
  293. }
  294. // BaseFromFenweiClassify 汾渭数据分类表
  295. type BaseFromFenweiClassify struct {
  296. ClassifyId int `orm:"column(classify_id);pk"`
  297. ClassifyName string `description:"分类名称"`
  298. ParentId int `description:"父级id"`
  299. SysUserId int `description:"创建人id"`
  300. SysUserRealName string `description:"创建人姓名"`
  301. Level int `description:"层级"`
  302. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  303. ModifyTime time.Time `description:"修改时间"`
  304. CreateTime time.Time `description:"创建时间"`
  305. }
  306. func (y *BaseFromFenweiClassify) Add() (lastId int64, err error) {
  307. o := orm.NewOrm()
  308. lastId, err = o.Insert(y)
  309. return
  310. }
  311. func (y *BaseFromFenweiClassify) Update(updateCols []string) (err error) {
  312. o := orm.NewOrm()
  313. _, err = o.Update(y, updateCols...)
  314. return
  315. }
  316. func (y *BaseFromFenweiClassify) GetByClassifyName(classifyName string) (item []*BaseFromFenweiClassify, err error) {
  317. o := orm.NewOrm()
  318. sql := ` SELECT * FROM base_from_fenwei_classify WHERE classify_name=? `
  319. _, err = o.Raw(sql, classifyName).QueryRows(&item)
  320. if errors.Is(err, orm.ErrNoRows) {
  321. return nil, nil
  322. }
  323. return
  324. }
  325. func (y *BaseFromFenweiClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromFenweiClassify, err error) {
  326. o := orm.NewOrm()
  327. fields := strings.Join(fieldArr, ",")
  328. if len(fieldArr) == 0 {
  329. fields = `*`
  330. }
  331. order := `ORDER BY create_time DESC`
  332. if orderRule != "" {
  333. order = ` ORDER BY ` + orderRule
  334. }
  335. sql := fmt.Sprintf(`SELECT %s FROM base_from_fenwei_classify WHERE 1=1 %s %s`, fields, condition, order)
  336. _, err = o.Raw(sql, pars).QueryRows(&items)
  337. return
  338. }
  339. // BaseFromFenweiClassifyItem 汾渭数据分类信息
  340. type BaseFromFenweiClassifyItem struct {
  341. ClassifyId int `description:"分类ID"`
  342. ClassifyName string `description:"分类名称"`
  343. ParentId int `description:"父级id"`
  344. Level int `description:"层级"`
  345. Sort int `description:"排序字段"`
  346. CreateTime string `description:"创建时间"`
  347. ModifyTime string `description:"修改时间"`
  348. Child []*BaseFromFenweiClassifyItem `description:"子分类"`
  349. }
  350. func (y *BaseFromFenweiClassify) Format2Item(origin *BaseFromFenweiClassify) (item *BaseFromFenweiClassifyItem) {
  351. if origin == nil {
  352. return
  353. }
  354. item = new(BaseFromFenweiClassifyItem)
  355. item.ClassifyId = origin.ClassifyId
  356. item.ClassifyName = origin.ClassifyName
  357. item.ParentId = origin.ParentId
  358. item.Level = origin.Level
  359. item.Sort = origin.Sort
  360. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  361. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  362. return
  363. }
  364. // MultiUpdateBaseFromFenweiDataValue 批量更新汾渭指标数据
  365. func MultiUpdateBaseFromFenweiDataValue(items []*BaseFromFenweiData) (err error) {
  366. if len(items) == 0 {
  367. return
  368. }
  369. o := orm.NewOrm()
  370. sql := `UPDATE base_from_fenwei_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ? LIMIT 1`
  371. p, err := o.Raw(sql).Prepare()
  372. if err != nil {
  373. return
  374. }
  375. defer func() {
  376. _ = p.Close()
  377. }()
  378. for _, v := range items {
  379. if v.IndexCode == "" || v.DataTime == "" {
  380. continue
  381. }
  382. _, err = p.Exec(v.Value, v.IndexCode, v.DataTime)
  383. if err != nil {
  384. return
  385. }
  386. }
  387. return
  388. }
  389. // GetBaseFromFenWeiIndexByIndexName 根据指标名称查询指标
  390. func GetBaseFromFenWeiIndexByIndexName(indexName string) (item *BaseFromFenweiIndex, err error) {
  391. o := orm.NewOrm()
  392. sql := ` SELECT * FROM base_from_fenwei_index WHERE index_name=? `
  393. err = o.Raw(sql, indexName).QueryRow(&item)
  394. if errors.Is(err, orm.ErrNoRows) {
  395. return nil, nil
  396. }
  397. return
  398. }
  399. // GetBaseFromFenweiDataByIndexCodeAndDataTime 根据指标编码和dataTime查询指标数据
  400. func GetBaseFromFenweiDataByIndexCodeAndDataTime(indexCode, dataTime string) (item *BaseFromFenweiData, err error) {
  401. o := orm.NewOrm()
  402. sql := ` SELECT * FROM base_from_fenwei_data WHERE index_code=? AND data_time=? `
  403. err = o.Raw(sql, indexCode, dataTime).QueryRow(&item)
  404. if errors.Is(err, orm.ErrNoRows) {
  405. return nil, nil
  406. }
  407. return
  408. }
  409. // BatchAddBaseFromFenWeiData 批量插入指标数据
  410. func BatchAddBaseFromFenWeiData(items []*BaseFromFenweiData) (err error) {
  411. if len(items) == 0 {
  412. return
  413. }
  414. o := orm.NewOrm()
  415. _, err = o.InsertMulti(1, items)
  416. return
  417. }
  418. // AddBaseFromFenWeiClassify 新增汾渭分类
  419. func AddBaseFromFenWeiClassify(item *BaseFromFenweiClassify) (classifyId int64, err error) {
  420. o := orm.NewOrm()
  421. classifyId, err = o.Insert(item)
  422. return
  423. }
  424. // GetBaseFromFenWeiIndexByIndexCode 跟据指标编码查询指标信息
  425. func GetBaseFromFenWeiIndexByIndexCode(indexCode string) (item *BaseFromFenweiIndex, err error) {
  426. o := orm.NewOrm()
  427. sql := ` SELECT * FROM base_from_fenwei_index WHERE index_code=? `
  428. err = o.Raw(sql, indexCode).QueryRow(&item)
  429. if errors.Is(err, orm.ErrNoRows) {
  430. return nil, nil
  431. }
  432. return
  433. }