edb_data_coal.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/rdlucklib/rdluck_tools/orm"
  5. "hongze/hongze_chart_lib/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddEdbDataCoalBySql(sqlStr string) (err error) {
  11. o := orm.NewOrm()
  12. o.Using("data")
  13. _, err = o.Raw(sqlStr).Exec()
  14. return
  15. }
  16. func GetEdbDataCoalByEdbCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  17. o := orm.NewOrm()
  18. o.Using("data")
  19. sql := ` SELECT * FROM edb_data_coal WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  20. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  21. return
  22. }
  23. func GetEdbDataCoalMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
  24. o := orm.NewOrm()
  25. o.Using("data")
  26. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_coal WHERE edb_code=? `
  27. err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
  28. return
  29. }
  30. type coalSurveyData struct {
  31. DataTime string `orm:"column(data_time)" description:"日期"`
  32. InputValue string `orm:"column(input_value)" description:"值"`
  33. }
  34. func GetCoalSurveyDataByTradeCode(condition string, pars []interface{}) (item []*coalSurveyData, err error) {
  35. sql := ` SELECT a.* FROM longzhong_survey_data AS a
  36. INNER JOIN longzhong_survey_product AS b ON a.survey_product_id=b.survey_product_id
  37. WHERE 1=1 `
  38. o := orm.NewOrm()
  39. o.Using("edb")
  40. if condition != "" {
  41. sql += condition
  42. }
  43. sql += ` ORDER BY a.data_time DESC `
  44. _, err = o.Raw(sql, pars).QueryRows(&item)
  45. return
  46. }
  47. type BaseFromCoalmineIndex struct {
  48. BaseFromCoalmineCompanyIndexId int
  49. IndexName string `description:"持买单量指标名称"`
  50. IndexCode string `description:"持买单量指标编码"`
  51. DealValue string `description:"成交量"`
  52. DataTime string `description:"数据日期"`
  53. Source string `description:"来源"`
  54. Province string `description:"省份"`
  55. City string `description:"城市"`
  56. GroupName string `description:"集团名称"`
  57. Unit string `description:"单位"`
  58. Frequency string `description:"频率"`
  59. CreateTime string `description:"插入时间"`
  60. ModifyTime string `description:"修改时间"`
  61. }
  62. func GetBaseFromCoalIndexByCode(suffix, indexCode string) (items []*BaseFromCoalmineIndex, err error) {
  63. o := orm.NewOrm()
  64. o.Using("data")
  65. sql := `SELECT * FROM base_from_coalmine_%s WHERE index_code=? `
  66. sql = fmt.Sprintf(sql, suffix)
  67. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  68. return
  69. }
  70. func GetEdbDataCoalByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  71. o := orm.NewOrm()
  72. o.Using("data")
  73. sql := ` SELECT * FROM edb_data_coal WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  74. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  75. return
  76. }
  77. func GetEdbDataByCoal(edbCode, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
  78. o := orm.NewOrm()
  79. o.Using("data")
  80. o.Begin()
  81. defer func() {
  82. if err != nil {
  83. o.Rollback()
  84. } else {
  85. o.Commit()
  86. }
  87. }()
  88. var condition string
  89. var pars []interface{}
  90. if edbCode != "" {
  91. condition += " AND b.coal_code=? "
  92. pars = append(pars, edbCode)
  93. }
  94. coalDataList, err := GetCoalSurveyDataByTradeCode(condition, pars)
  95. if err != nil {
  96. return
  97. }
  98. searchItem = new(EdbInfoSearch)
  99. searchItem.EdbCode = edbCode
  100. dataLen := len(coalDataList)
  101. if dataLen > 0 {
  102. var isAdd bool
  103. addSql := ` INSERT INTO edb_data_coal(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  104. nowStr := time.Now().Format(utils.FormatDateTime)
  105. dataList := make([]*EdbInfoSearchData, 0)
  106. for i := 0; i < dataLen; i++ {
  107. item := coalDataList[i]
  108. eDate := item.DataTime
  109. sValue := item.InputValue
  110. dataTime, err := time.Parse(utils.FormatDate, eDate)
  111. if err != nil {
  112. return nil, err
  113. }
  114. timestamp := dataTime.UnixNano() / 1e6
  115. timeStr := fmt.Sprintf("%d", timestamp)
  116. addSql += "("
  117. addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
  118. "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
  119. addSql += "),"
  120. isAdd = true
  121. }
  122. if isAdd {
  123. addSql = strings.TrimRight(addSql, ",")
  124. err = AddEdbDataCoalBySql(addSql)
  125. if err != nil {
  126. utils.FileLogData.Info("AddEdbDataCoalBySql Err:%s", err.Error())
  127. return
  128. }
  129. }
  130. dataList, err := GetEdbDataCoalByCode(edbCode)
  131. if err != nil {
  132. utils.FileLogData.Info("GetEdbDataCoalByCode Err:%s", err.Error())
  133. return searchItem, err
  134. }
  135. minDate, maxDate, err := GetEdbDataCoalMaxOrMinDate(edbCode)
  136. if err != nil {
  137. return searchItem, err
  138. }
  139. searchItem.DataList = dataList
  140. searchItem.StartDate = minDate
  141. searchItem.EndDate = maxDate
  142. }
  143. if searchItem.DataList == nil {
  144. searchItem.DataList = make([]*EdbInfoSearchData, 0)
  145. }
  146. return
  147. }
  148. func ModifyEdbDataCoalStatus(edbInfoId int64, edbCode string) (err error) {
  149. o := orm.NewOrm()
  150. o.Using("data")
  151. sql := ` UPDATE edb_data_coal SET edb_info_id=?, status=1,modify_time=NOW() WHERE edb_code=? `
  152. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  153. return
  154. }
  155. func GetEdbDataCoalByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  156. o := orm.NewOrm()
  157. o.Using("data")
  158. sql := ` SELECT COUNT(1) AS count FROM edb_data_coal WHERE edb_code=? AND data_time=? `
  159. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  160. return
  161. }
  162. func ModifyEdbDataCoal(edbInfoId int64, dataTime, value string) (err error) {
  163. o := orm.NewOrm()
  164. o.Using("data")
  165. sql := ` UPDATE edb_data_coal SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  166. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  167. return
  168. }
  169. //刷新煤炭指标数据
  170. func RefreshEdbDataByCoal(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  171. o := orm.NewOrm()
  172. o.Using("data")
  173. o.Begin()
  174. defer func() {
  175. if err != nil {
  176. o.Rollback()
  177. } else {
  178. o.Commit()
  179. }
  180. }()
  181. if err != nil {
  182. return
  183. }
  184. edbInfoIdStr := strconv.Itoa(edbInfoId)
  185. //计算数据
  186. var condition string
  187. var pars []interface{}
  188. if edbCode != "" {
  189. condition += " AND b.coal_code=? "
  190. pars = append(pars, edbCode)
  191. }
  192. if startDate != "" {
  193. condition += " AND a.data_time>=? "
  194. pars = append(pars, startDate)
  195. }
  196. if endDate != "" {
  197. condition += " AND a.data_time<=? "
  198. pars = append(pars, endDate)
  199. }
  200. coalDataList, err := GetCoalSurveyDataByTradeCode(condition, pars)
  201. addSql := ` INSERT INTO edb_data_coal(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  202. var isAdd bool
  203. for _, v := range coalDataList {
  204. item := v
  205. count, err := GetEdbDataCoalByCodeAndDate(edbCode, v.DataTime)
  206. if err != nil && err.Error() != utils.ErrNoRow() {
  207. return err
  208. }
  209. if count <= 0 {
  210. nowStr := time.Now().Format(utils.FormatDateTime)
  211. eDate := item.DataTime
  212. sValue := item.InputValue
  213. dataTime, err := time.Parse(utils.FormatDate, eDate)
  214. if err != nil {
  215. return err
  216. }
  217. timestamp := dataTime.UnixNano() / 1e6
  218. timeStr := fmt.Sprintf("%d", timestamp)
  219. addSql += "("
  220. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
  221. "," + "'" + nowStr + "'" + "," + "1" + "," + "'" + timeStr + "'"
  222. addSql += "),"
  223. isAdd = true
  224. } else {
  225. err = ModifyEdbDataCoal(int64(edbInfoId), v.DataTime, v.InputValue)
  226. if err != nil {
  227. return err
  228. }
  229. }
  230. }
  231. if isAdd {
  232. addSql = strings.TrimRight(addSql, ",")
  233. _, err = o.Raw(addSql).Exec()
  234. if err != nil {
  235. return err
  236. }
  237. }
  238. return
  239. }
  240. //全部刷新隆众数据
  241. func RefreshAllEdbDataByCoal(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
  242. o := orm.NewOrm()
  243. o.Using("data")
  244. o.Begin()
  245. defer func() {
  246. if err != nil {
  247. o.Rollback()
  248. } else {
  249. o.Commit()
  250. }
  251. }()
  252. if err != nil {
  253. return
  254. }
  255. edbInfoIdStr := strconv.Itoa(edbInfoId)
  256. //计算数据
  257. var condition string
  258. var pars []interface{}
  259. var suffix string
  260. if strings.Contains(edbCode, "jsm") {
  261. suffix = "jsm_index"
  262. } else if strings.Contains(edbCode, "company") {
  263. suffix = "company_index"
  264. } else if strings.Contains(edbCode, "firm") {
  265. suffix = "firm_index"
  266. } else if strings.Contains(edbCode, "coastal") {
  267. suffix = "coastal_index"
  268. } else if strings.Contains(edbCode, "inland") {
  269. suffix = "inland_index"
  270. }
  271. if edbCode != "" {
  272. condition += " AND b.index_code=? "
  273. pars = append(pars, edbCode)
  274. }
  275. if startDate != "" {
  276. condition += " AND a.data_time>=? "
  277. pars = append(pars, startDate)
  278. }
  279. if endDate != "" {
  280. condition += " AND a.data_time<=? "
  281. pars = append(pars, endDate)
  282. }
  283. coalBaseDataAll, err := GetBaseFromCoalIndexByCode(suffix, edbCode)
  284. if err != nil && err.Error() != utils.ErrNoRow() {
  285. return
  286. }
  287. //获取指标所有数据
  288. dataList := make([]*EdbDataBase, 0)
  289. dataTableName := GetEdbDataTableName(source)
  290. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  291. sql = fmt.Sprintf(sql, dataTableName)
  292. _, err = o.Raw(sql, edbInfoId).QueryRows(&dataList)
  293. if err != nil {
  294. return err
  295. }
  296. dataMap := make(map[string]string)
  297. for _, v := range dataList {
  298. dataMap[v.DataTime] = v.Value
  299. }
  300. addSql := ` INSERT INTO edb_data_coal(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  301. var isAdd bool
  302. for _, v := range coalBaseDataAll {
  303. item := v
  304. eDate := item.DataTime
  305. sValue := item.DealValue
  306. dataTime, err := time.Parse(utils.FormatDate, eDate)
  307. if err != nil {
  308. return err
  309. }
  310. timestamp := dataTime.UnixNano() / 1e6
  311. timeStr := fmt.Sprintf("%d", timestamp)
  312. saveValue := sValue
  313. if existVal, ok := dataMap[eDate]; !ok {
  314. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  315. isAdd = true
  316. } else {
  317. if existVal != saveValue {
  318. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  319. sql = fmt.Sprintf(sql, dataTableName)
  320. _, err = o.Raw(sql, sValue, edbInfoId, eDate).Exec()
  321. if err != nil {
  322. return err
  323. }
  324. }
  325. }
  326. }
  327. if isAdd {
  328. addSql = strings.TrimRight(addSql, ",")
  329. _, err = o.Raw(addSql).Exec()
  330. if err != nil {
  331. return err
  332. }
  333. }
  334. return
  335. }