base_from_national_statistics_index.go 5.9 KB

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