base_from_manual.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. //弘则手工数据
  11. type ManualEdbdata struct {
  12. TradeCode string `orm:"column(TRADE_CODE);pk" description:"指标编码"`
  13. Dt string `orm:"column(DT)" description:"日期"`
  14. Close string `orm:"column(CLOSE)" description:"值"`
  15. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  16. }
  17. func GetEdbdataManualByCondition(condition string, pars []interface{}) (item []*ManualEdbdata, err error) {
  18. sql := ` SELECT * FROM edbdata WHERE 1=1 `
  19. if condition != "" {
  20. sql += condition
  21. }
  22. sql += ` ORDER BY DT DESC `
  23. o := orm.NewOrmUsingDB("edb")
  24. _, err = o.Raw(sql, pars).QueryRows(&item)
  25. return
  26. }
  27. // 新增弘则手工指标数据
  28. func AddEdbDataFromManual(edbCode string) (err error) {
  29. o := orm.NewOrm()
  30. var condition string
  31. var pars []interface{}
  32. if edbCode != "" {
  33. condition += " AND TRADE_CODE=? "
  34. pars = append(pars, edbCode)
  35. }
  36. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  37. if err != nil {
  38. return
  39. }
  40. dataLen := len(manualDataList)
  41. if dataLen > 0 {
  42. var isAdd bool
  43. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  44. for i := 0; i < dataLen; i++ {
  45. item := manualDataList[i]
  46. eDate := item.Dt
  47. sValue := item.Close
  48. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  49. if err != nil {
  50. return err
  51. }
  52. timestamp := dataTime.UnixNano() / 1e6
  53. timeStr := fmt.Sprintf("%d", timestamp)
  54. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  55. isAdd = true
  56. }
  57. if isAdd {
  58. addSql = strings.TrimRight(addSql, ",")
  59. _, err = o.Raw(addSql).Exec()
  60. if err != nil {
  61. return
  62. }
  63. }
  64. }
  65. return
  66. }
  67. // 刷新手工指标数据
  68. func RefreshEdbDataFromManual(edbInfoId int, edbCode, startDate string) (err error) {
  69. source := utils.DATA_SOURCE_MANUAL
  70. o := orm.NewOrm()
  71. if err != nil {
  72. return
  73. }
  74. edbInfoIdStr := strconv.Itoa(edbInfoId)
  75. //计算数据
  76. var condition string
  77. var pars []interface{}
  78. if edbCode != "" {
  79. condition += " AND TRADE_CODE=? "
  80. pars = append(pars, edbCode)
  81. }
  82. if startDate != "" {
  83. condition += " AND DT>=? "
  84. pars = append(pars, startDate)
  85. }
  86. manualDataList, err := GetEdbdataManualByCondition(condition, pars)
  87. if err != nil {
  88. return
  89. }
  90. // 真实数据的最大日期 , 插入规则配置的日期
  91. var realDataMaxDate, edbDataInsertConfigDate time.Time
  92. var edbDataInsertConfig *EdbDataInsertConfig
  93. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  94. edbDataInsertConfigDateStr := ``
  95. {
  96. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  97. if err != nil && err.Error() != utils.ErrNoRow() {
  98. return
  99. }
  100. if edbDataInsertConfig != nil {
  101. edbDataInsertConfigDate = edbDataInsertConfig.Date
  102. edbDataInsertConfigDateStr = edbDataInsertConfig.Date.Format(utils.FormatDate)
  103. }
  104. }
  105. var existCondition string
  106. var existPars []interface{}
  107. existCondition += " AND edb_info_id=? "
  108. existPars = append(existPars, edbInfoId)
  109. if startDate != "" {
  110. existCondition += " AND data_time>=? "
  111. existPars = append(existPars, startDate)
  112. }
  113. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  114. if err != nil {
  115. return err
  116. }
  117. existMap := make(map[string]*EdbInfoSearchData)
  118. for _, v := range existList {
  119. existMap[v.DataTime] = v
  120. }
  121. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  122. var isAdd bool
  123. manualMap := make(map[string]*ManualEdbdata)
  124. //fmt.Println("manualDataList:", len(manualDataList))
  125. for _, v := range manualDataList {
  126. item := v
  127. eDate := item.Dt
  128. sValue := item.Close
  129. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  130. if err != nil {
  131. return err
  132. }
  133. //fmt.Println("Item:", item.Dt, item.Close, item.TradeCode, item.ModifyTime)
  134. if findItem, ok := existMap[v.Dt]; !ok {
  135. timestamp := dataTime.UnixNano() / 1e6
  136. timeStr := fmt.Sprintf("%d", timestamp)
  137. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  138. isAdd = true
  139. } else {
  140. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Close {
  141. err = ModifyEdbDataById(source, findItem.EdbDataId, item.Close)
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. }
  147. manualMap[v.Dt] = v
  148. // 下面代码主要目的是处理掉手动插入的数据判断
  149. {
  150. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  151. realDataMaxDate = dataTime
  152. }
  153. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  154. isFindConfigDateRealData = true
  155. }
  156. }
  157. }
  158. for _, v := range existList {
  159. if _, ok := manualMap[v.DataTime]; !ok {
  160. // 正常来讲,如果来源数据移除了该日期的数据,也需要删除ETA指标的值
  161. // 但是由于引入手动插入最新值,那么需要判断该日期值,是否等于,如果等于,则不删除该日期值
  162. if edbDataInsertConfigDateStr == `` || edbDataInsertConfigDateStr != v.DataTime {
  163. DeleteEdbDataById(utils.DATA_SOURCE_MANUAL, v.EdbDataId)
  164. }
  165. }
  166. }
  167. if isAdd {
  168. addSql = strings.TrimRight(addSql, ",")
  169. _, err = o.Raw(addSql).Exec()
  170. if err != nil {
  171. fmt.Println("RefreshAllEdbDataByManual add Err", err.Error())
  172. return
  173. }
  174. }
  175. // 处理手工数据补充的配置
  176. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  177. return
  178. }
  179. // HandleConfigInsertEdbData 处理手工数据补充的配置
  180. func HandleConfigInsertEdbData(realDataMaxDate time.Time, edbDataInsertConfig *EdbDataInsertConfig, edbInfoId, source int, existMap map[string]*EdbInfoSearchData, isFindConfigDateRealData bool) {
  181. if edbDataInsertConfig == nil {
  182. return
  183. }
  184. var err error
  185. defer func() {
  186. if err != nil {
  187. fmt.Println("处理手工数据补充的配置失败,err:", err)
  188. }
  189. }()
  190. edbDataInsertConfigDate := edbDataInsertConfig.Date // 配置的日期
  191. // 如果存在真实数据的最大日期 && 存在配置插入数据的最大日期 && 真实数据的最大日期 晚于/等于 配置插入数据的最大日期
  192. if realDataMaxDate.After(edbDataInsertConfigDate) || realDataMaxDate.Equal(edbDataInsertConfigDate) {
  193. go DeleteEdbDataInsertConfigByEdbId(edbInfoId)
  194. edbDataInsertConfigDateStr := edbDataInsertConfigDate.Format(utils.FormatDate)
  195. // 如果没有找到找到配置日期的实际数据,那么就直接删除
  196. if item, ok := existMap[edbDataInsertConfigDateStr]; ok && !isFindConfigDateRealData {
  197. DeleteEdbDataById(source, item.EdbDataId)
  198. }
  199. } else {
  200. o := orm.NewOrm()
  201. edbDataInsertConfig.RealDate = realDataMaxDate
  202. _, err = o.Update(edbDataInsertConfig, "RealDate")
  203. }
  204. return
  205. }