edb_data_manual.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package data_manage
  2. import (
  3. "fmt"
  4. "hongze/hongze_chart_lib/utils"
  5. "github.com/rdlucklib/rdluck_tools/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddEdbDataManualBySql(sqlStr string) (err error) {
  11. o := orm.NewOrm()
  12. o.Using("data")
  13. _, err = o.Raw(sqlStr).Exec()
  14. return
  15. }
  16. func GetEdbDataManualMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
  17. o := orm.NewOrm()
  18. o.Using("data")
  19. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_manual WHERE edb_code=? `
  20. err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
  21. return
  22. }
  23. type ManualEdbdata struct {
  24. TradeCode string `orm:"column(TRADE_CODE);pk" description:"指标编码"`
  25. Dt string `orm:"column(DT)" description:"日期"`
  26. Close string `orm:"column(CLOSE)" description:"值"`
  27. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  28. }
  29. func GetEdbdataManualByTradeCode(condition string, pars []interface{}) (item []*ManualEdbdata, err error) {
  30. sql := ` SELECT * FROM edbdata WHERE 1=1 `
  31. o := orm.NewOrm()
  32. o.Using("edb")
  33. if condition != "" {
  34. sql += condition
  35. }
  36. sql += ` ORDER BY DT DESC `
  37. _, err = o.Raw(sql, pars).QueryRows(&item)
  38. return
  39. }
  40. func GetEdbDataManualByCode(edbCode string, size int) (items []*EdbInfoSearchData, err error) {
  41. o := orm.NewOrm()
  42. o.Using("data")
  43. sql := ` SELECT * FROM edb_data_manual WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  44. _, err = o.Raw(sql, edbCode, size).QueryRows(&items)
  45. return
  46. }
  47. func GetEdbDataByManual(edbCode, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
  48. o := orm.NewOrm()
  49. o.Using("data")
  50. o.Begin()
  51. defer func() {
  52. if err != nil {
  53. o.Rollback()
  54. } else {
  55. o.Commit()
  56. }
  57. }()
  58. var condition string
  59. var pars []interface{}
  60. if edbCode != "" {
  61. condition += " AND TRADE_CODE=? "
  62. pars = append(pars, edbCode)
  63. }
  64. manualDataList, err := GetEdbdataManualByTradeCode(condition, pars)
  65. if err != nil {
  66. return
  67. }
  68. searchItem = new(EdbInfoSearch)
  69. searchItem.EdbCode = edbCode
  70. dataLen := len(manualDataList)
  71. if dataLen > 0 {
  72. var isAdd bool
  73. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  74. nowStr := time.Now().Format(utils.FormatDateTime)
  75. dataList := make([]*EdbInfoSearchData, 0)
  76. for i := 0; i < dataLen; i++ {
  77. item := manualDataList[i]
  78. eDate := item.Dt
  79. sValue := item.Close
  80. dataTime, err := time.Parse(utils.FormatDate, eDate)
  81. if err != nil {
  82. return nil, err
  83. }
  84. timestamp := dataTime.UnixNano() / 1e6
  85. timeStr := fmt.Sprintf("%d", timestamp)
  86. addSql += "("
  87. addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
  88. "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
  89. addSql += "),"
  90. isAdd = true
  91. }
  92. if isAdd {
  93. addSql = strings.TrimRight(addSql, ",")
  94. err = AddEdbDataManualBySql(addSql)
  95. if err != nil {
  96. utils.FileLogData.Info("AddEdbDataThsBySql Err:%s", err.Error())
  97. return
  98. }
  99. }
  100. size := utils.EDB_DATA_LIMIT
  101. dataList, err := GetEdbDataManualByCode(edbCode, size)
  102. if err != nil {
  103. utils.FileLogData.Info("GetEdbDataThsByCode Err:%s", err.Error())
  104. return searchItem, err
  105. }
  106. minDate, maxDate, err := GetEdbDataManualMaxOrMinDate(edbCode)
  107. if err != nil {
  108. return searchItem, err
  109. }
  110. searchItem.DataList = dataList
  111. searchItem.StartDate = minDate
  112. searchItem.EndDate = maxDate
  113. }
  114. if searchItem.DataList == nil {
  115. searchItem.DataList = make([]*EdbInfoSearchData, 0)
  116. }
  117. return
  118. }
  119. func GetEdbDataManualByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  120. o := orm.NewOrm()
  121. o.Using("data")
  122. sql := ` SELECT COUNT(1) AS count FROM edb_data_manual WHERE edb_code=? AND data_time=? `
  123. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  124. return
  125. }
  126. func ModifyEdbDataManual(edbInfoId int64, dataTime, value string) (err error) {
  127. o := orm.NewOrm()
  128. o.Using("data")
  129. sql := ` UPDATE edb_data_manual SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  130. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  131. return
  132. }
  133. //刷新手工指标数据
  134. func RefreshEdbDataByManual(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  135. o := orm.NewOrm()
  136. o.Using("data")
  137. o.Begin()
  138. defer func() {
  139. if err != nil {
  140. o.Rollback()
  141. } else {
  142. o.Commit()
  143. }
  144. }()
  145. if err != nil {
  146. return
  147. }
  148. edbInfoIdStr := strconv.Itoa(edbInfoId)
  149. //计算数据
  150. var condition string
  151. var pars []interface{}
  152. if edbCode != "" {
  153. condition += " AND TRADE_CODE=? "
  154. pars = append(pars, edbCode)
  155. }
  156. if startDate != "" {
  157. condition += " AND DT>=? "
  158. pars = append(pars, startDate)
  159. }
  160. if endDate != "" {
  161. condition += " AND DT<=? "
  162. pars = append(pars, endDate)
  163. }
  164. manualDataList, err := GetEdbdataManualByTradeCode(condition, pars)
  165. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  166. var isAdd bool
  167. for _, v := range manualDataList {
  168. item := v
  169. count, err := GetEdbDataManualByCodeAndDate(v.TradeCode, v.Dt)
  170. if err != nil && err.Error() != utils.ErrNoRow() {
  171. return err
  172. }
  173. if count <= 0 {
  174. nowStr := time.Now().Format(utils.FormatDateTime)
  175. eDate := item.Dt
  176. sValue := item.Close
  177. dataTime, err := time.Parse(utils.FormatDate, eDate)
  178. if err != nil {
  179. return err
  180. }
  181. timestamp := dataTime.UnixNano() / 1e6
  182. timeStr := fmt.Sprintf("%d", timestamp)
  183. addSql += "("
  184. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
  185. "," + "'" + nowStr + "'" + "," + "1" + "," + "'" + timeStr + "'"
  186. addSql += "),"
  187. isAdd = true
  188. } else {
  189. err = ModifyEdbDataManual(int64(edbInfoId), v.Dt, v.Close)
  190. if err != nil {
  191. return err
  192. }
  193. }
  194. }
  195. if isAdd {
  196. addSql = strings.TrimRight(addSql, ",")
  197. _, err = o.Raw(addSql).Exec()
  198. if err != nil {
  199. return err
  200. }
  201. }
  202. return
  203. }
  204. //刷新手工指标数据
  205. func RefreshAllEdbDataByManual(edbInfoId, source int, edbCode string) (err error) {
  206. o := orm.NewOrm()
  207. o.Using("data")
  208. o.Begin()
  209. defer func() {
  210. if err != nil {
  211. o.Rollback()
  212. } else {
  213. o.Commit()
  214. }
  215. }()
  216. if err != nil {
  217. return
  218. }
  219. edbInfoIdStr := strconv.Itoa(edbInfoId)
  220. //计算数据
  221. var condition string
  222. var pars []interface{}
  223. if edbCode != "" {
  224. condition += " AND TRADE_CODE=? "
  225. pars = append(pars, edbCode)
  226. }
  227. manualDataList, err := GetEdbdataManualByTradeCode(condition, pars)
  228. //获取指标所有数据
  229. existDataList := make([]*EdbDataBase, 0)
  230. dataTableName := GetEdbDataTableName(source)
  231. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  232. sql = fmt.Sprintf(sql, dataTableName)
  233. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  234. if err != nil {
  235. return err
  236. }
  237. existDataMap := make(map[string]*EdbDataBase)
  238. for _, v := range existDataList {
  239. existDataMap[v.DataTime] = v
  240. }
  241. addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  242. var isAdd bool
  243. manualMap := make(map[string]*ManualEdbdata)
  244. for _, v := range manualDataList {
  245. item := v
  246. if findItem, ok := existDataMap[v.Dt]; !ok {
  247. eDate := item.Dt
  248. sValue := item.Close
  249. dataTime, err := time.Parse(utils.FormatDate, eDate)
  250. if err != nil {
  251. return err
  252. }
  253. timestamp := dataTime.UnixNano() / 1e6
  254. timeStr := fmt.Sprintf("%d", timestamp)
  255. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  256. isAdd = true
  257. } else {
  258. if findItem.Value != item.Close {
  259. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  260. sql = fmt.Sprintf(sql, dataTableName)
  261. _, err = o.Raw(sql, item.Close, edbInfoId, item.Dt).Exec()
  262. if err != nil {
  263. return err
  264. }
  265. }
  266. }
  267. manualMap[v.Dt] = v
  268. }
  269. for _, v := range existDataList {
  270. if _, ok := manualMap[v.DataTime]; !ok {
  271. go DeleteEdbDataByIdAndSource(v.EdbDataId, utils.DATA_SOURCE_MANUAL)
  272. }
  273. }
  274. if isAdd {
  275. addSql = strings.TrimRight(addSql, ",")
  276. _, err = o.Raw(addSql).Exec()
  277. if err != nil {
  278. fmt.Println("RefreshAllEdbDataByManual add Err", err.Error())
  279. return
  280. }
  281. }
  282. return
  283. }
  284. func DeleteEdbDataByIdAndSource(edbDataId, source int) (err error) {
  285. sql := ` DELETE FROM %s WHERE edb_data_id=? `
  286. tableName := GetEdbDataTableName(source)
  287. sql = fmt.Sprintf(sql, tableName)
  288. o := orm.NewOrm()
  289. o.Using("data")
  290. _, err = o.Raw(sql, edbDataId).Exec()
  291. return
  292. }