base_from_national_statistics_index.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package models
  2. import (
  3. "eta/eta_crawler/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromNationalStatisticsIndex 国统局-指标
  10. type BaseFromNationalStatisticsIndex struct {
  11. BaseFromNationalStatisticsIndexId int `orm:"column(base_from_national_statistics_index_id);pk"`
  12. BaseFromNationalStatisticsClassifyId int `description:"指标分类ID"`
  13. Dbcode string `description:"dbcode"`
  14. IndexCode string `description:"指标编码"`
  15. IndexName string `description:"指标名称"`
  16. Unit string `description:"单位"`
  17. Frequency string `description:"频度"`
  18. Reg string `description:"地区"`
  19. StartDate time.Time `description:"开始日期"`
  20. EndDate time.Time `description:"结束日期"`
  21. CreateTime time.Time `description:"创建时间"`
  22. ModifyTime time.Time `description:"更新时间"`
  23. }
  24. func (m *BaseFromNationalStatisticsIndex) TableName() string {
  25. return "base_from_national_statistics_index"
  26. }
  27. func (m *BaseFromNationalStatisticsIndex) Create() (err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. id, err := o.Insert(m)
  30. if err != nil {
  31. return
  32. }
  33. m.BaseFromNationalStatisticsIndexId = int(id)
  34. return
  35. }
  36. func (m *BaseFromNationalStatisticsIndex) CreateMulti(items []*BaseFromNationalStatisticsIndex) (err error) {
  37. if len(items) == 0 {
  38. return
  39. }
  40. o := orm.NewOrmUsingDB("data")
  41. _, err = o.InsertMulti(len(items), items)
  42. return
  43. }
  44. func (m *BaseFromNationalStatisticsIndex) Update(cols []string) (err error) {
  45. o := orm.NewOrmUsingDB("data")
  46. _, err = o.Update(m, cols...)
  47. return
  48. }
  49. func (m *BaseFromNationalStatisticsIndex) Del() (err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. sql := `DELETE FROM base_from_national_statistics_index WHERE base_from_national_statistics_index_id = ? LIMIT 1`
  52. _, err = o.Raw(sql, m.BaseFromNationalStatisticsIndexId).Exec()
  53. return
  54. }
  55. func (m *BaseFromNationalStatisticsIndex) GetItemById(id int) (err error) {
  56. o := orm.NewOrmUsingDB("data")
  57. sql := `SELECT * FROM base_from_national_statistics_index WHERE base_from_national_statistics_index_id = ? LIMIT 1`
  58. err = o.Raw(sql, id).QueryRow(&m)
  59. return
  60. }
  61. func (m *BaseFromNationalStatisticsIndex) GetItemByCondition(condition string, pars []interface{}) (err error) {
  62. o := orm.NewOrmUsingDB("data")
  63. sql := `SELECT * FROM base_from_national_statistics_index WHERE 1=1 `
  64. sql += condition
  65. sql += ` LIMIT 1`
  66. err = o.Raw(sql, pars).QueryRow(&m)
  67. return
  68. }
  69. func (m *BaseFromNationalStatisticsIndex) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  70. o := orm.NewOrmUsingDB("data")
  71. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  72. err = o.Raw(sql, pars).QueryRow(&count)
  73. return
  74. }
  75. func (m *BaseFromNationalStatisticsIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromNationalStatisticsIndex, err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. fields := strings.Join(fieldArr, ",")
  78. if len(fieldArr) == 0 {
  79. fields = `*`
  80. }
  81. order := `ORDER BY create_time DESC`
  82. if orderRule != "" {
  83. order = ` ORDER BY ` + orderRule
  84. }
  85. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  86. _, err = o.Raw(sql, pars).QueryRows(&items)
  87. return
  88. }
  89. // SaveNationalStatisticsIndexAndDataReq 保存指标和数据请求体
  90. type SaveNationalStatisticsIndexAndDataReq struct {
  91. //IndexExist bool `description:"指标是否存在"`
  92. Index *BaseFromNationalStatisticsIndex `description:"指标信息"`
  93. DataList []*BaseFromNationalStatisticsData `description:"新增的指标数据"`
  94. }
  95. // SaveNationalStatisticsIndexAndData 保存指标和值
  96. func SaveNationalStatisticsIndexAndData(index *BaseFromNationalStatisticsIndex, data []*BaseFromNationalStatisticsData) (err error) {
  97. if index == nil {
  98. return
  99. }
  100. indexExist := new(BaseFromNationalStatisticsIndex)
  101. indexCond := ` AND index_code = ?`
  102. indexPars := make([]interface{}, 0)
  103. indexPars = append(indexPars, index.IndexCode)
  104. if e := indexExist.GetItemByCondition(indexCond, indexPars); e != nil && e.Error() != utils.ErrNoRow() {
  105. return e
  106. }
  107. dataOB := new(BaseFromNationalStatisticsData)
  108. dataCond := ` AND index_code = ?`
  109. dataPars := make([]interface{}, 0)
  110. dataPars = append(dataPars, index.IndexCode)
  111. dataList, e := dataOB.GetItemsByCondition(dataCond, dataPars, []string{}, "data_time ASC")
  112. if e != nil {
  113. return e
  114. }
  115. dataMap := make(map[string]*BaseFromNationalStatisticsData)
  116. for _, d := range dataList {
  117. dataMap[d.DataTime.Format(utils.FormatDate)] = d
  118. }
  119. o := orm.NewOrmUsingDB("data")
  120. tx, e := o.Begin()
  121. if e != nil {
  122. return e
  123. }
  124. defer func() {
  125. if err != nil {
  126. _ = tx.Rollback()
  127. return
  128. }
  129. _ = tx.Commit()
  130. }()
  131. indexId := indexExist.BaseFromNationalStatisticsIndexId
  132. if indexExist.BaseFromNationalStatisticsIndexId == 0 {
  133. id, e := tx.Insert(index)
  134. if e != nil {
  135. return e
  136. }
  137. indexId = int(id)
  138. }
  139. if indexExist.BaseFromNationalStatisticsIndexId > 0 {
  140. indexExist.ModifyTime = time.Now().Local()
  141. if e = indexExist.Update([]string{"ModifyTime"}); e != nil {
  142. return e
  143. }
  144. }
  145. inserts := make([]*BaseFromNationalStatisticsData, 0)
  146. updateCols := []string{"Value", "ModifyTime"}
  147. for _, d := range data {
  148. k := d.DataTime.Format(utils.FormatDate)
  149. // 更新数据
  150. v := dataMap[k]
  151. if v != nil {
  152. v.Value = d.Value
  153. v.ModifyTime = time.Now().Local()
  154. if _, e = tx.Update(v, updateCols...); e != nil {
  155. err = e
  156. return
  157. }
  158. continue
  159. }
  160. d.BaseFromNationalStatisticsIndexId = indexId
  161. inserts = append(inserts, d)
  162. }
  163. // 批量插入新数据
  164. if len(inserts) > 0 {
  165. _, e = tx.InsertMulti(len(inserts), inserts)
  166. if e != nil {
  167. return e
  168. }
  169. }
  170. return
  171. }
  172. // BatchSaveNationalStatisticsIndexAndData 批量保存指标和值
  173. func BatchSaveNationalStatisticsIndexAndData(indexArr []*BaseFromNationalStatisticsIndex, indexDataMap map[string][]*BaseFromNationalStatisticsData) (err error) {
  174. o := orm.NewOrmUsingDB("data")
  175. tx, err := o.Begin()
  176. if err != nil {
  177. return
  178. }
  179. defer func() {
  180. if err != nil {
  181. _ = tx.Rollback()
  182. return
  183. }
  184. _ = tx.Commit()
  185. }()
  186. // 指标
  187. for _, v := range indexArr {
  188. id, e := tx.Insert(v)
  189. if e != nil {
  190. return e
  191. }
  192. indexId := int(id)
  193. // 数据
  194. dataList := indexDataMap[v.IndexCode]
  195. if dataList != nil && len(dataList) > 0 {
  196. for _, d := range dataList {
  197. d.BaseFromNationalStatisticsIndexId = indexId
  198. }
  199. _, e = tx.InsertMulti(len(dataList), dataList)
  200. if e != nil {
  201. return e
  202. }
  203. }
  204. }
  205. return
  206. }
  207. // UpdateNationalStatisticsIndexStartEndDate 更新指标开始结束日期
  208. func UpdateNationalStatisticsIndexStartEndDate() (err error) {
  209. o := orm.NewOrmUsingDB("data")
  210. sql := `UPDATE base_from_national_statistics_index AS a
  211. JOIN (
  212. SELECT
  213. index_code,
  214. MIN(data_time) AS min_time,
  215. MAX(data_time) AS max_time
  216. FROM
  217. base_from_national_statistics_data
  218. GROUP BY
  219. index_code
  220. ) AS b ON a.index_code = b.index_code
  221. SET a.start_date = b.min_time, a.end_date = b.max_time`
  222. _, err = o.Raw(sql).Exec()
  223. return
  224. }