base_from_national_statistics_index.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/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, limitNum int) (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. limit := ``
  86. if limitNum > 0 {
  87. limit += fmt.Sprintf(` LIMIT %d`, limitNum)
  88. }
  89. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s %s`, fields, m.TableName(), condition, order, limit)
  90. _, err = o.Raw(sql, pars).QueryRows(&items)
  91. return
  92. }
  93. // SaveNationalStatisticsIndexAndDataReq 保存指标和数据请求体
  94. type SaveNationalStatisticsIndexAndDataReq struct {
  95. Index *BaseFromNationalStatisticsIndex `description:"指标信息"`
  96. IndexExist bool `description:"指标是否存在"`
  97. DataList []*BaseFromNationalStatisticsData `description:"新增的指标数据"`
  98. }
  99. // SaveNationalStatisticsIndexAndData 保存指标和值
  100. func SaveNationalStatisticsIndexAndData(req *SaveNationalStatisticsIndexAndDataReq) (err error) {
  101. if req.Index == nil {
  102. return
  103. }
  104. o := orm.NewOrmUsingDB("data")
  105. tx, err := o.Begin()
  106. if err != nil {
  107. return
  108. }
  109. defer func() {
  110. if err != nil {
  111. _ = tx.Rollback()
  112. return
  113. }
  114. _ = tx.Commit()
  115. }()
  116. if !req.IndexExist {
  117. id, e := tx.Insert(req.Index)
  118. if e != nil {
  119. return e
  120. }
  121. req.Index.BaseFromNationalStatisticsIndexId = int(id)
  122. }
  123. indexId := req.Index.BaseFromNationalStatisticsIndexId
  124. if req.DataList != nil && len(req.DataList) > 0 {
  125. for _, d := range req.DataList {
  126. d.BaseFromNationalStatisticsIndexId = indexId
  127. }
  128. _, e := tx.InsertMulti(len(req.DataList), req.DataList)
  129. if e != nil {
  130. return e
  131. }
  132. }
  133. return
  134. }
  135. // BatchSaveNationalStatisticsIndexAndData 批量保存指标和值
  136. func BatchSaveNationalStatisticsIndexAndData(indexArr []*BaseFromNationalStatisticsIndex, indexDataMap map[string][]*BaseFromNationalStatisticsData) (err error) {
  137. o := orm.NewOrmUsingDB("data")
  138. tx, err := o.Begin()
  139. if err != nil {
  140. return
  141. }
  142. defer func() {
  143. if err != nil {
  144. _ = tx.Rollback()
  145. return
  146. }
  147. _ = tx.Commit()
  148. }()
  149. // 指标
  150. for _, v := range indexArr {
  151. id, e := tx.Insert(v)
  152. if e != nil {
  153. return e
  154. }
  155. indexId := int(id)
  156. // 数据
  157. dataList := indexDataMap[v.IndexCode]
  158. if dataList != nil && len(dataList) > 0 {
  159. for _, d := range dataList {
  160. d.BaseFromNationalStatisticsIndexId = indexId
  161. }
  162. _, e = tx.InsertMulti(len(dataList), dataList)
  163. if e != nil {
  164. return e
  165. }
  166. }
  167. }
  168. return
  169. }
  170. // UpdateNationalStatisticsIndexStartEndDate 更新指标开始结束日期
  171. func UpdateNationalStatisticsIndexStartEndDate() (err error) {
  172. o := orm.NewOrmUsingDB("data")
  173. sql := `UPDATE base_from_national_statistics_index AS a
  174. JOIN (
  175. SELECT
  176. index_code,
  177. MIN(data_time) AS min_time,
  178. MAX(data_time) AS max_time
  179. FROM
  180. base_from_national_statistics_data
  181. GROUP BY
  182. index_code
  183. ) AS b ON a.index_code = b.index_code
  184. SET a.start_date = b.min_time, a.end_date = b.max_time`
  185. _, err = o.Raw(sql).Exec()
  186. return
  187. }
  188. type BaseFromNationalStatisticsIndexItem struct {
  189. IndexId int `description:"指标ID"`
  190. ClassifyId int `description:"指标分类ID"`
  191. Dbcode string `description:"dbcode"`
  192. IndexCode string `description:"指标编码"`
  193. IndexName string `description:"指标名称"`
  194. Unit string `description:"单位"`
  195. Frequency string `description:"频度"`
  196. Reg string `description:"地区"`
  197. StartDate string `description:"开始日期"`
  198. EndDate string `description:"结束日期"`
  199. CreateTime string `description:"创建时间"`
  200. ModifyTime string `description:"更新时间"`
  201. ChildrenIndexIds []int `description:"有地区维度的指标-子指标ID集合-用于前端定位"`
  202. }
  203. type BaseFromNationalStatisticsIndexListResp struct {
  204. List []*BaseFromNationalStatisticsIndexItem `description:"指标列表"`
  205. RegList []string `description:"地区列表"`
  206. }
  207. func FormatBaseFromNationalStatisticsIndex2Item(v *BaseFromNationalStatisticsIndex) (item *BaseFromNationalStatisticsIndexItem) {
  208. item = new(BaseFromNationalStatisticsIndexItem)
  209. item.IndexId = v.BaseFromNationalStatisticsIndexId
  210. item.ClassifyId = v.BaseFromNationalStatisticsClassifyId
  211. item.Dbcode = v.Dbcode
  212. item.IndexCode = v.IndexCode
  213. item.IndexName = v.IndexName
  214. item.Unit = v.Unit
  215. item.Frequency = v.Frequency
  216. item.Reg = v.Reg
  217. item.StartDate = v.StartDate.Format(utils.FormatDate)
  218. item.EndDate = v.EndDate.Format(utils.FormatDate)
  219. item.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  220. item.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  221. return
  222. }
  223. type BaseFromNationalStatisticsIndexDetailItem struct {
  224. BaseFromNationalStatisticsIndexItem
  225. DataList []*BaseFromNationalStatisticsIndexDetailData `description:"数据列表"`
  226. }
  227. type BaseFromNationalStatisticsIndexDetailData struct {
  228. DataTime string `description:"数据日期"`
  229. Value float64 `description:"数据值"`
  230. }
  231. func GetBaseFromNationalStatisticsIndexByIndexCode(indexCode string) (item *BaseFromNationalStatisticsIndex, err error) {
  232. o := orm.NewOrmUsingDB("data")
  233. sql := `SELECT * FROM base_from_national_statistics_index WHERE index_code = ? LIMIT 1`
  234. err = o.Raw(sql, indexCode).QueryRow(&item)
  235. return
  236. }