base_from_national_statistics_index.go 5.8 KB

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