edb_collect.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type EdbCollect struct {
  12. EdbCollectId int `gorm:"primaryKey;autoIncrement;column:edb_collect_id;type:int(10) unsigned;not null"`
  13. EdbCollectClassifyId int `gorm:"index:idx_classify_id;column:edb_collect_classify_id;type:int(10) unsigned;not null;default:0"` // 指标收藏分类ID
  14. EdbInfoId int `gorm:"column:edb_info_id;type:int(10) unsigned;not null;default:0"` // 指标ID
  15. EdbCode string `gorm:"column:edb_code;type:varchar(255);not null;default:''"` // 指标编码
  16. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人ID
  17. SysRealName string `gorm:"column:sys_real_name;type:int(10) unsigned;not null;default:0"` // 创建人姓名
  18. Sort int `gorm:"column:sort;type:int(10);default:0"` // 排序
  19. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  20. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
  21. }
  22. func (m *EdbCollect) TableName() string {
  23. return "edb_collect"
  24. }
  25. type EdbCollectCols struct {
  26. PrimaryId string
  27. EdbCollectClassifyId string
  28. EdbInfoId string
  29. EdbCode string
  30. SysUserId string
  31. SysRealName string
  32. Sort string
  33. CreateTime string
  34. ModifyTime string
  35. }
  36. func (m *EdbCollect) Cols() EdbCollectCols {
  37. return EdbCollectCols{
  38. PrimaryId: "edb_collect_id",
  39. EdbCollectClassifyId: "edb_collect_classify_id",
  40. EdbInfoId: "edb_info_id",
  41. EdbCode: "edb_code",
  42. SysUserId: "sys_user_id",
  43. SysRealName: "sys_real_name",
  44. Sort: "sort",
  45. CreateTime: "create_time",
  46. ModifyTime: "modify_time",
  47. }
  48. }
  49. func (m *EdbCollect) Create() (err error) {
  50. err = global.DmSQL["data"].Create(m).Error
  51. return
  52. }
  53. func (m *EdbCollect) CreateMulti(items []*EdbCollect) (err error) {
  54. if len(items) == 0 {
  55. return
  56. }
  57. err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
  58. return
  59. }
  60. func (m *EdbCollect) Update(cols []string) (err error) {
  61. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  62. return
  63. }
  64. func (m *EdbCollect) Remove() (err error) {
  65. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  66. err = global.DmSQL["data"].Exec(sql, m.EdbCollectId).Error
  67. return
  68. }
  69. func (m *EdbCollect) MultiRemove(ids []int) (err error) {
  70. if len(ids) == 0 {
  71. return
  72. }
  73. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  74. err = global.DmSQL["data"].Exec(sql, ids).Error
  75. return
  76. }
  77. func (m *EdbCollect) RemoveByCondition(condition string, pars []interface{}) (err error) {
  78. if condition == "" {
  79. return
  80. }
  81. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  82. err = global.DmSQL["data"].Exec(sql, pars...).Error
  83. return
  84. }
  85. func (m *EdbCollect) RemoveAndCreateMulti(delCondition string, delPars []interface{}, items []*EdbCollect) (err error) {
  86. to := global.DmSQL["data"].Begin()
  87. defer func() {
  88. if err != nil {
  89. _ = to.Rollback()
  90. } else {
  91. _ = to.Commit()
  92. }
  93. }()
  94. {
  95. if delCondition == "" {
  96. return
  97. }
  98. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), delCondition)
  99. err = to.Exec(sql, delPars...).Error
  100. if err != nil {
  101. return
  102. }
  103. }
  104. if len(items) == 0 {
  105. return
  106. }
  107. err = to.CreateInBatches(items, utils.MultiAddNum).Error
  108. return
  109. }
  110. func (m *EdbCollect) GetItemById(id int) (item *EdbCollect, err error) {
  111. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  112. err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
  113. return
  114. }
  115. func (m *EdbCollect) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbCollect, err error) {
  116. order := ``
  117. if orderRule != "" {
  118. order = ` ORDER BY ` + orderRule
  119. }
  120. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  121. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  122. return
  123. }
  124. func (m *EdbCollect) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  125. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  126. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  127. return
  128. }
  129. func (m *EdbCollect) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbCollect, err error) {
  130. fields := strings.Join(fieldArr, ",")
  131. if len(fieldArr) == 0 {
  132. fields = `*`
  133. }
  134. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  135. if orderRule != "" {
  136. order = ` ORDER BY ` + orderRule
  137. }
  138. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  139. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  140. return
  141. }
  142. func (m *EdbCollect) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbCollect, err error) {
  143. fields := strings.Join(fieldArr, ",")
  144. if len(fieldArr) == 0 {
  145. fields = `*`
  146. }
  147. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  148. if orderRule != "" {
  149. order = ` ORDER BY ` + orderRule
  150. }
  151. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  152. pars = append(pars, startSize, pageSize)
  153. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  154. return
  155. }
  156. func GetCollectEdbInfoByClassifyId(classifyId int) (items []*EdbInfo, err error) {
  157. sql := `SELECT b.* FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE a.edb_collect_classify_id = ? ORDER BY a.sort ASC`
  158. err = global.DmSQL["data"].Raw(sql, classifyId).Find(&items).Error
  159. return
  160. }
  161. type EdbCollectReq struct {
  162. ClassifyId int `description:"分类ID"`
  163. ClassifyIdList []int `description:"分类ID列表"`
  164. EdbInfoId int `description:"指标ID"`
  165. }
  166. func GetCollectEdbInfoCount(condition string, pars []interface{}) (total int64, err error) {
  167. sql := fmt.Sprintf(`SELECT COUNT(1) AS ct FROM (
  168. SELECT b.edb_info_id FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  169. WHERE 1=1 %s
  170. GROUP BY b.edb_info_id
  171. ) AS sub`, condition)
  172. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&total).Error
  173. return
  174. }
  175. func GetCollectEdbInfoPageList(condition string, pars []interface{}, startSize, pageSize int) (list []*CollectEdbInfoQuery, err error) {
  176. sql := fmt.Sprintf(`SELECT b."edb_info_id",
  177. WM_CONCAT(DISTINCT a."edb_collect_classify_id") AS "collect_classify_id",
  178. MAX(a.create_time) as collect_time,
  179. MAX(b."edb_code") AS "edb_code",
  180. MAX(b."edb_name") "edb_name",
  181. MAX(b."edb_info_type") "edb_info_type",
  182. MAX(b."edb_type") "edb_type",
  183. MAX(b."source") "source",
  184. MAX(b."source_name") "source_name",
  185. MAX(b."frequency") "frequency",
  186. MAX(b."unit") "unit",
  187. MAX(b."classify_id") "classify_id",
  188. MAX(b."create_time") "create_time",
  189. MAX(b."unique_code") "unique_code",
  190. MAX(b."chart_image") "chart_image",
  191. MAX(b."modify_time") "modify_time",
  192. MAX(a."sort") AS "sort"
  193. FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  194. WHERE 1=1 %s
  195. GROUP BY b.edb_info_id
  196. ORDER BY collect_time DESC LIMIT ?,?`, condition)
  197. pars = append(pars, startSize, pageSize)
  198. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  199. return
  200. }
  201. type CollectEdbInfoQuery struct {
  202. EdbInfo
  203. CollectClassifyIdStr string `gorm:"column:collect_classify_id" description:"收藏分类ID"`
  204. CollectTime time.Time `gorm:"column:collect_time" description:"收藏时间"`
  205. }
  206. type CollectEdbInfoItem struct {
  207. EdbInfoId int `description:"指标ID"`
  208. EdbInfoType int `description:"指标类型:0-普通指标; 1-预测指标"`
  209. EdbType int `description:"指标类型:1-基础指标; 2-计算指标"`
  210. Source int `description:"来源ID"`
  211. SourceName string `description:"来源名称"`
  212. EdbCode string `description:"指标编码"`
  213. EdbName string `description:"指标名称"`
  214. Frequency string `description:"频率"`
  215. Unit string `description:"单位"`
  216. UniqueCode string `description:"唯一编码"`
  217. ChartImage string `description:"图表图片"`
  218. ClassifyId int `description:"指标分类ID"`
  219. CollectClassifyIdList []int `description:"收藏分类ID列表"`
  220. CollectTime string `description:"收藏时间"`
  221. CreateTime string `description:"创建时间"`
  222. Sort int `description:"排序"`
  223. }
  224. func FormatEdbInfo2CollectItem(origin *CollectEdbInfoQuery) (item *CollectEdbInfoItem) {
  225. item = new(CollectEdbInfoItem)
  226. item.EdbInfoId = origin.EdbInfoId
  227. item.EdbInfoType = origin.EdbInfoType
  228. item.EdbType = origin.EdbType
  229. item.Source = origin.Source
  230. item.SourceName = origin.SourceName
  231. item.EdbCode = origin.EdbCode
  232. item.EdbName = origin.EdbName
  233. item.Frequency = origin.Frequency
  234. item.Unit = origin.Unit
  235. item.UniqueCode = origin.UniqueCode
  236. item.ChartImage = origin.ChartImage
  237. item.ClassifyId = origin.ClassifyId
  238. collectClassifyIdList := make([]int, 0)
  239. if origin.CollectClassifyIdStr != `` {
  240. collectClassifyIdStrList := strings.Split(origin.CollectClassifyIdStr, ",")
  241. for _, v := range collectClassifyIdStrList {
  242. collectClassifyId, tmpErr := strconv.Atoi(v)
  243. if tmpErr == nil {
  244. collectClassifyIdList = append(collectClassifyIdList, collectClassifyId)
  245. }
  246. }
  247. }
  248. item.CollectClassifyIdList = collectClassifyIdList
  249. item.CollectTime = origin.CollectTime.Format(utils.FormatDateTime)
  250. item.CreateTime = origin.CreateTime.Format(utils.FormatDateTime)
  251. item.Sort = origin.Sort
  252. return
  253. }
  254. type CollectEdbInfoListResp struct {
  255. Paging *paging.PagingItem
  256. List []*CollectEdbInfoItem
  257. }
  258. type EdbCollectMoveReq struct {
  259. EdbInfoId int `description:"收藏的指标ID(分类下指标ID唯一)"`
  260. PrevEdbInfoId int `description:"前一个收藏的指标ID"`
  261. NextEdbInfoId int `description:"后一个收藏的指标ID"`
  262. ClassifyId int `description:"当前分类ID"`
  263. }
  264. func GetEdbCollectSort(adminId, classifyId, sort int) (item *EdbCollect, err error) {
  265. sql := ` SELECT * FROM edb_collect WHERE 1=1 AND sys_user_id = ? AND edb_collect_classify_id = ? `
  266. if sort == 1 {
  267. sql += ` ORDER BY sort DESC, edb_collect_id ASC LIMIT 1 `
  268. } else {
  269. sql += ` ORDER BY sort ASC, edb_collect_id DESC LIMIT 1 `
  270. }
  271. err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error
  272. return
  273. }
  274. func GetEdbCollectByEdbInfoId(adminId, edbInfoId, classifyId int) (item *EdbCollect, err error) {
  275. sql := `SELECT * FROM edb_collect WHERE sys_user_id = ? AND edb_info_id = ? AND edb_collect_classify_id=? `
  276. err = global.DmSQL["data"].Raw(sql, adminId, edbInfoId, classifyId).First(&item).Error
  277. return
  278. }
  279. func UpdateEdbCollectSortByClassifyId(classifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
  280. sql := ` update edb_collect set sort = ` + updateSort + ` WHERE edb_collect_classify_id = ? `
  281. if prevMyChartClassifyMappingId > 0 {
  282. sql += ` AND ( sort > ? or ( edb_collect_id < ? and sort=? )) `
  283. }
  284. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Error
  285. return
  286. }
  287. func UpdateEdbCollectMove(sort, adminId, edbInfoId, myChartClassifyId int) (err error) {
  288. sql := ` UPDATE edb_collect SET sort = ?,modify_time=NOW() WHERE sys_user_id=? AND edb_info_id=? AND edb_collect_classify_id=? `
  289. err = global.DmSQL["data"].Exec(sql, sort, adminId, edbInfoId, myChartClassifyId).Error
  290. return
  291. }