base_from_fenwei.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. // BaseFromFenwei 涌溢咨询
  11. type BaseFromFenwei struct{}
  12. type BaseFromFenweiData struct {
  13. FenweiDataId int `orm:"column(fenwei_data_id);pk"`
  14. FenweiIndexId 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 *BaseFromFenweiData) TableName() string {
  23. return "base_from_fenwei_data"
  24. }
  25. func GetBaseFromFenweiDataByCondition(condition string, pars []interface{}) (list []*BaseFromFenweiData, err error) {
  26. o := orm.NewOrm()
  27. sql := `SELECT * FROM base_from_fenwei_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 BaseFromFenwei) 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. fenweiBaseDataAll, err := GetBaseFromFenweiDataByCondition(condition, pars)
  44. if err != nil && err.Error() != utils.ErrNoRow() {
  45. return
  46. }
  47. var isAdd bool
  48. addSql := ` INSERT INTO edb_data_fenwei(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 fenweiBaseDataAll {
  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 刷新汾渭数据指标数据
  75. func (obj BaseFromFenwei) 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. fenweiDataList, err := GetBaseFromFenweiDataByCondition(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_fenwei(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  127. var isAdd bool
  128. for _, v := range fenweiDataList {
  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 BaseFromFenwei) GetSource() int {
  173. return utils.DATA_SOURCE_FENWEI
  174. }
  175. // GetSourceName 获取来源名称
  176. func (obj BaseFromFenwei) GetSourceName() string {
  177. return utils.DATA_SOURCE_NAME_FENWEI
  178. }
  179. type BaseFromFenweiIndex struct {
  180. FenweiIndexId int64 `orm:"column(fenwei_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 *BaseFromFenweiIndex) TableName() string {
  194. return "base_from_fenwei_index"
  195. }
  196. func (m *BaseFromFenweiIndex) Add() (lastId int64, err error) {
  197. o := orm.NewOrm()
  198. lastId, err = o.Insert(m)
  199. return
  200. }
  201. func (m *BaseFromFenweiIndex) Update(updateCols []string) (err error) {
  202. o := orm.NewOrm()
  203. _, err = o.Update(m, updateCols...)
  204. return
  205. }
  206. func (m *BaseFromFenweiIndex) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  207. o := orm.NewOrm()
  208. sql := ` UPDATE base_from_fenwei_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 *BaseFromFenweiIndex) GetByIndexCode(indexCode string) (item *BaseFromFenweiIndex, err error) {
  213. o := orm.NewOrm()
  214. sql := ` SELECT * FROM base_from_fenwei_index WHERE index_code=? `
  215. err = o.Raw(sql, indexCode).QueryRow(&item)
  216. return
  217. }
  218. func (m *BaseFromFenweiIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromFenweiIndex, 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_fenwei_index WHERE 1=1 %s %s`, fields, condition, order)
  229. _, err = o.Raw(sql, pars).QueryRows(&items)
  230. return
  231. }
  232. type BaseFromFenweiIndexList struct {
  233. FenweiIndexId int64 `orm:"column(fenwei_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 (y *BaseFromFenweiData) GetByIndexCode(indexCode string) (list []*BaseFromFenweiData, err error) {
  246. o := orm.NewOrm()
  247. sql := ` SELECT * FROM base_from_fenwei_data WHERE index_code=? `
  248. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  249. return
  250. }
  251. func (y *BaseFromFenweiData) AddMulti(item []*BaseFromFenweiData) (err error) {
  252. o := orm.NewOrm()
  253. _, err = o.InsertMulti(1, item)
  254. return
  255. }
  256. // HandleFenweiExcelData 汾渭数据的excel数据
  257. type HandleFenweiExcelData struct {
  258. //ClassifyName string `description:"指标目录"`
  259. //ClassifySort int `description:"指标目录排序号"`
  260. IndexName string `description:"指标名称"`
  261. IndexCode string `description:"指标编码"`
  262. Unit string `description:"单位"`
  263. Sort int `description:"排序号"`
  264. Frequency string `description:"频度"`
  265. ClassifyId int `description:"分类ID"`
  266. ExcelDataMap map[string]string
  267. }
  268. type HandleFenweiExcelDataReq struct {
  269. List []*HandleFenweiExcelData
  270. TerminalCode string `description:"编码"`
  271. }
  272. func (y *BaseFromFenweiData) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  273. o := orm.NewOrm()
  274. 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=? `
  275. err = o.Raw(sql, indexCode).QueryRow(&item)
  276. return
  277. }
  278. // BaseFromFenweiClassify 汾渭数据分类表
  279. type BaseFromFenweiClassify struct {
  280. ClassifyId int `orm:"column(classify_id);pk"`
  281. ClassifyName 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 *BaseFromFenweiClassify) Add() (lastId int64, err error) {
  291. o := orm.NewOrm()
  292. lastId, err = o.Insert(y)
  293. return
  294. }
  295. func (y *BaseFromFenweiClassify) Update(updateCols []string) (err error) {
  296. o := orm.NewOrm()
  297. _, err = o.Update(y, updateCols...)
  298. return
  299. }
  300. func (y *BaseFromFenweiClassify) GetByClassifyName(classifyName string) (item *BaseFromFenweiClassify, err error) {
  301. o := orm.NewOrm()
  302. sql := ` SELECT * FROM base_from_fenwei_classify WHERE classify_name=? `
  303. err = o.Raw(sql, classifyName).QueryRow(&item)
  304. return
  305. }
  306. func (y *BaseFromFenweiClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromFenweiClassify, err error) {
  307. o := orm.NewOrm()
  308. fields := strings.Join(fieldArr, ",")
  309. if len(fieldArr) == 0 {
  310. fields = `*`
  311. }
  312. order := `ORDER BY create_time DESC`
  313. if orderRule != "" {
  314. order = ` ORDER BY ` + orderRule
  315. }
  316. sql := fmt.Sprintf(`SELECT %s FROM base_from_fenwei_classify WHERE 1=1 %s %s`, fields, condition, order)
  317. _, err = o.Raw(sql, pars).QueryRows(&items)
  318. return
  319. }
  320. // BaseFromFenweiClassifyItem 汾渭数据分类信息
  321. type BaseFromFenweiClassifyItem struct {
  322. ClassifyId int `description:"分类ID"`
  323. ClassifyName string `description:"分类名称"`
  324. ParentId int `description:"父级id"`
  325. Level int `description:"层级"`
  326. Sort int `description:"排序字段"`
  327. CreateTime string `description:"创建时间"`
  328. ModifyTime string `description:"修改时间"`
  329. Child []*BaseFromFenweiClassifyItem `description:"子分类"`
  330. }
  331. func (y *BaseFromFenweiClassify) Format2Item(origin *BaseFromFenweiClassify) (item *BaseFromFenweiClassifyItem) {
  332. if origin == nil {
  333. return
  334. }
  335. item = new(BaseFromFenweiClassifyItem)
  336. item.ClassifyId = origin.ClassifyId
  337. item.ClassifyName = origin.ClassifyName
  338. item.ParentId = origin.ParentId
  339. item.Level = origin.Level
  340. item.Sort = origin.Sort
  341. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  342. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  343. return
  344. }
  345. // MultiUpdateBaseFromFenweiDataValue 批量更新汾渭指标数据
  346. func MultiUpdateBaseFromFenweiDataValue(items []*BaseFromFenweiData) (err error) {
  347. if len(items) == 0 {
  348. return
  349. }
  350. o := orm.NewOrm()
  351. sql := `UPDATE base_from_fenwei_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ? LIMIT 1`
  352. p, err := o.Raw(sql).Prepare()
  353. if err != nil {
  354. return
  355. }
  356. defer func() {
  357. _ = p.Close()
  358. }()
  359. for _, v := range items {
  360. if v.IndexCode == "" || v.DataTime == "" {
  361. continue
  362. }
  363. _, err = p.Exec(v.Value, v.IndexCode, v.DataTime)
  364. if err != nil {
  365. return
  366. }
  367. }
  368. return
  369. }