base_from_yongyi.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. // BaseFromYongyi 涌溢咨询
  11. type BaseFromYongyi struct{}
  12. type BaseFromYongyiData struct {
  13. YongyiDataId int `orm:"column(yongyi_data_id);pk"`
  14. YongyiIndexId int
  15. IndexCode string
  16. DataTime string
  17. Value string
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. DataTimestamp int64
  21. }
  22. func GetBaseFromYongyiDataByCondition(condition string, pars []interface{}) (list []*BaseFromYongyiData, err error) {
  23. o := orm.NewOrm()
  24. sql := `SELECT * FROM base_from_yongyi_data WHERE 1=1 `
  25. if condition != "" {
  26. sql += condition
  27. }
  28. _, err = o.Raw(sql, pars).QueryRows(&list)
  29. return
  30. }
  31. // Add 添加
  32. func (obj BaseFromYongyi) Add(edbCode string) (err error) {
  33. o := orm.NewOrm()
  34. var condition string
  35. var pars []interface{}
  36. if edbCode != "" {
  37. condition += " AND index_code=? "
  38. pars = append(pars, edbCode)
  39. }
  40. yongyiBaseDataAll, err := GetBaseFromYongyiDataByCondition(condition, pars)
  41. if err != nil && err.Error() != utils.ErrNoRow() {
  42. return
  43. }
  44. var isAdd bool
  45. addSql := ` INSERT INTO edb_data_yongyi(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  46. existMap := make(map[string]string)
  47. for _, sv := range yongyiBaseDataAll {
  48. eDate := sv.DataTime
  49. dataTime, err := time.Parse(utils.FormatDate, eDate)
  50. if err != nil {
  51. return err
  52. }
  53. timestamp := dataTime.UnixNano() / 1e6
  54. timeStr := fmt.Sprintf("%d", timestamp)
  55. if _, ok := existMap[eDate]; !ok {
  56. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  57. isAdd = true
  58. }
  59. existMap[eDate] = sv.Value
  60. }
  61. if isAdd {
  62. addSql = strings.TrimRight(addSql, ",")
  63. utils.FileLog.Info("addSql:" + addSql)
  64. _, err = o.Raw(addSql).Exec()
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. return
  70. }
  71. // Refresh 刷新涌益咨询指标数据
  72. func (obj BaseFromYongyi) Refresh(edbInfoId int, edbCode, startDate string) (err error) {
  73. source := obj.GetSource()
  74. o := orm.NewOrm()
  75. if err != nil {
  76. return
  77. }
  78. edbInfoIdStr := strconv.Itoa(edbInfoId)
  79. //计算数据
  80. var condition string
  81. var pars []interface{}
  82. if edbCode != "" {
  83. condition += " AND index_code=? "
  84. pars = append(pars, edbCode)
  85. }
  86. if startDate != "" {
  87. condition += " AND data_time>=? "
  88. pars = append(pars, startDate)
  89. }
  90. yongyiDataList, err := GetBaseFromYongyiDataByCondition(condition, pars)
  91. if err != nil {
  92. return
  93. }
  94. // 真实数据的最大日期 , 插入规则配置的日期
  95. var realDataMaxDate, edbDataInsertConfigDate time.Time
  96. var edbDataInsertConfig *EdbDataInsertConfig
  97. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  98. {
  99. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  100. if err != nil && err.Error() != utils.ErrNoRow() {
  101. return
  102. }
  103. if edbDataInsertConfig != nil {
  104. edbDataInsertConfigDate = edbDataInsertConfig.Date
  105. }
  106. }
  107. var existCondition string
  108. var existPars []interface{}
  109. existCondition += " AND edb_info_id=? "
  110. existPars = append(existPars, edbInfoId)
  111. if startDate != "" {
  112. existCondition += " AND data_time>=? "
  113. existPars = append(existPars, startDate)
  114. }
  115. existList, err := GetEdbDataByCondition(source, 0, existCondition, existPars)
  116. if err != nil {
  117. return err
  118. }
  119. existMap := make(map[string]*EdbInfoSearchData)
  120. for _, v := range existList {
  121. existMap[v.DataTime] = v
  122. }
  123. addSql := ` INSERT INTO edb_data_yongyi(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  124. var isAdd bool
  125. for _, v := range yongyiDataList {
  126. item := v
  127. eDate := item.DataTime
  128. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  129. if err != nil {
  130. return err
  131. }
  132. if findItem, ok := existMap[v.DataTime]; !ok {
  133. sValue := item.Value
  134. timestamp := dataTime.UnixNano() / 1e6
  135. timeStr := fmt.Sprintf("%d", timestamp)
  136. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  137. isAdd = true
  138. } else {
  139. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  140. err = ModifyEdbDataById(source, 0, findItem.EdbDataId, item.Value)
  141. if err != nil {
  142. return err
  143. }
  144. }
  145. }
  146. // 下面代码主要目的是处理掉手动插入的数据判断
  147. {
  148. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  149. realDataMaxDate = dataTime
  150. }
  151. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  152. isFindConfigDateRealData = true
  153. }
  154. }
  155. }
  156. // 处理手工数据补充的配置
  157. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, 0, existMap, isFindConfigDateRealData)
  158. if isAdd {
  159. addSql = strings.TrimRight(addSql, ",")
  160. _, err = o.Raw(addSql).Exec()
  161. if err != nil {
  162. fmt.Println("RefreshEdbDataFromBaiinfo add Err", err.Error())
  163. return
  164. }
  165. }
  166. return
  167. }
  168. // GetSource 获取来源编码id
  169. func (obj BaseFromYongyi) GetSource() int {
  170. return utils.DATA_SOURCE_YONYI
  171. }
  172. // GetSourceName 获取来源名称
  173. func (obj BaseFromYongyi) GetSourceName() string {
  174. return utils.DATA_SOURCE_NAME_YONYI
  175. }
  176. type BaseFromYongyiIndex struct {
  177. YongyiIndexId int64 `orm:"column(yongyi_index_id);pk"`
  178. IndexCode string
  179. IndexName string
  180. Frequency string
  181. Unit string
  182. ClassifyId int64
  183. StartDate string
  184. EndDate string
  185. Sort int
  186. TerminalCode string
  187. CreateTime time.Time
  188. ModifyTime time.Time
  189. }
  190. type BaseFromYongyiIndexList struct {
  191. YongyiIndexId int64 `orm:"column(yongyi_index_id);pk"`
  192. IndexCode string
  193. IndexName string
  194. Frequency string
  195. Unit string
  196. ClassifyId int64
  197. StartDate string
  198. EndDate string
  199. TerminalCode string
  200. CreateTime string
  201. ModifyTime string
  202. }
  203. func (y *BaseFromYongyiData) GetByIndexCode(indexCode string) (list []*BaseFromYongyiData, err error) {
  204. o := orm.NewOrm()
  205. sql := ` SELECT * FROM base_from_yongyi_data WHERE index_code=? `
  206. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  207. return
  208. }
  209. func (y *BaseFromYongyiData) AddMulti(item []*BaseFromYongyiData) (err error) {
  210. o := orm.NewOrm()
  211. _, err = o.InsertMulti(1, item)
  212. return
  213. }
  214. // Update 修改
  215. func (y *BaseFromYongyiData) Update(updateCols []string) (err error) {
  216. o := orm.NewOrm()
  217. _, err = o.Update(y, updateCols...)
  218. return
  219. }
  220. // HandleYongyiExcelData 涌益咨询的excel数据
  221. type HandleYongyiExcelData struct {
  222. ClassifyName string `description:"指标目录"`
  223. ClassifySort int `description:"指标目录排序号"`
  224. IndexName string `description:"指标名称"`
  225. IndexCode string `description:"指标编码"`
  226. Unit string `description:"单位"`
  227. Sort int `description:"排序号"`
  228. Frequency string `description:"频度"`
  229. ExcelDataMap map[string]string
  230. }
  231. type HandleYongyiExcelDataReq struct {
  232. List []*HandleYongyiExcelData
  233. TerminalCode string `description:"编码"`
  234. }
  235. func (y *BaseFromYongyiData) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  236. o := orm.NewOrm()
  237. 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_yongyi_data WHERE index_code=? `
  238. err = o.Raw(sql, indexCode).QueryRow(&item)
  239. return
  240. }
  241. func (y *BaseFromYongyiIndex) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  242. o := orm.NewOrm()
  243. sql := ` UPDATE base_from_yongyi_index SET start_date=?,end_date=?,modify_time=NOW() WHERE index_code=? `
  244. _, err = o.Raw(sql, item.MinDate, item.MaxDate, indexCode).Exec()
  245. return
  246. }
  247. func (y *BaseFromYongyiIndex) GetByIndexCode(indexCode string) (item *BaseFromYongyiIndex, err error) {
  248. o := orm.NewOrm()
  249. sql := ` SELECT * FROM base_from_yongyi_index WHERE index_code=? `
  250. err = o.Raw(sql, indexCode).QueryRow(&item)
  251. return
  252. }
  253. func (y *BaseFromYongyiIndex) Add() (lastId int64, err error) {
  254. o := orm.NewOrm()
  255. lastId, err = o.Insert(y)
  256. return
  257. }
  258. // Update 修改
  259. func (y *BaseFromYongyiIndex) Update(updateCols []string) (err error) {
  260. o := orm.NewOrm()
  261. _, err = o.Update(y, updateCols...)
  262. return
  263. }
  264. // BaseFromYongyiClassify Yongyi原始数据分类表
  265. type BaseFromYongyiClassify struct {
  266. ClassifyId int64 `orm:"column(classify_id);pk"`
  267. ClassifyName string `description:"分类名称"`
  268. ParentId int `description:"父级id"`
  269. SysUserId int `description:"创建人id"`
  270. SysUserRealName string `description:"创建人姓名"`
  271. Level int `description:"层级"`
  272. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  273. ModifyTime time.Time `description:"修改时间"`
  274. CreateTime time.Time `description:"创建时间"`
  275. }
  276. func (y *BaseFromYongyiClassify) Add() (lastId int64, err error) {
  277. o := orm.NewOrm()
  278. lastId, err = o.Insert(y)
  279. return
  280. }
  281. // Update 修改
  282. func (y *BaseFromYongyiClassify) Update(updateCols []string) (err error) {
  283. o := orm.NewOrm()
  284. _, err = o.Update(y, updateCols...)
  285. return
  286. }
  287. func (y *BaseFromYongyiClassify) GetByClassifyName(classifyName string) (item *BaseFromYongyiClassify, err error) {
  288. o := orm.NewOrm()
  289. sql := ` SELECT * FROM base_from_yongyi_classify WHERE classify_name=? `
  290. err = o.Raw(sql, classifyName).QueryRow(&item)
  291. return
  292. }
  293. func (y *BaseFromYongyiClassify) GetParentClassify() (items []*BaseFromYongyiClassify, err error) {
  294. o := orm.NewOrm()
  295. sql := ` SELECT * FROM base_from_yongyi_classify WHERE classify_name in ("日度", "月度", "周度", "年度", "旬度", "半年度", "年度") and parent_id=0 `
  296. _, err = o.Raw(sql).QueryRows(&items)
  297. return
  298. }