edb_data_gl.go 8.6 KB

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