chart_framework.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // ChartFramework 图库框架表
  10. type ChartFramework struct {
  11. ChartFrameworkId int `orm:"column(chart_framework_id);pk"`
  12. FrameworkCode string `description:"框架唯一编码"`
  13. FrameworkName string `description:"框架名称"`
  14. FrameworkImg string `description:"框架图片"`
  15. FrameworkContent string `description:"框架内容"`
  16. IsPublic int `description:"是否公开:0-私有;1-公开"`
  17. PublicTime time.Time `description:"公开时间"`
  18. Sort int `description:"排序"`
  19. AdminId int `description:"创建人ID"`
  20. AdminName string `description:"创建人姓名"`
  21. CreateTime time.Time `description:"创建时间"`
  22. ModifyTime time.Time `description:"更新时间"`
  23. }
  24. func (m *ChartFramework) TableName() string {
  25. return "chart_framework"
  26. }
  27. func (m *ChartFramework) PrimaryId() string {
  28. return ChartFrameworkColumns.ChartFrameworkId
  29. }
  30. var ChartFrameworkColumns = struct {
  31. ChartFrameworkId string
  32. FrameworkCode string
  33. FrameworkName string
  34. FrameworkImg string
  35. FrameworkContent string
  36. IsPublic string
  37. PublicTime string
  38. Sort string
  39. AdminId string
  40. AdminName string
  41. CreateTime string
  42. ModifyTime string
  43. }{
  44. ChartFrameworkId: "chart_framework_id",
  45. FrameworkCode: "framework_code",
  46. FrameworkName: "framework_name",
  47. FrameworkImg: "framework_img",
  48. FrameworkContent: "framework_content",
  49. IsPublic: "is_public",
  50. PublicTime: "public_time",
  51. Sort: "sort",
  52. AdminId: "admin_id",
  53. AdminName: "admin_name",
  54. CreateTime: "create_time",
  55. ModifyTime: "modify_time",
  56. }
  57. func (m *ChartFramework) Create() (err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. id, err := o.Insert(m)
  60. if err != nil {
  61. return
  62. }
  63. m.ChartFrameworkId = int(id)
  64. return
  65. }
  66. func (m *ChartFramework) CreateMulti(items []*ChartFramework) (err error) {
  67. if len(items) == 0 {
  68. return
  69. }
  70. o := orm.NewOrmUsingDB("data")
  71. _, err = o.InsertMulti(len(items), items)
  72. return
  73. }
  74. func (m *ChartFramework) Update(cols []string) (err error) {
  75. o := orm.NewOrmUsingDB("data")
  76. _, err = o.Update(m, cols...)
  77. return
  78. }
  79. func (m *ChartFramework) Del() (err error) {
  80. o := orm.NewOrmUsingDB("data")
  81. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  82. _, err = o.Raw(sql, m.ChartFrameworkId).Exec()
  83. return
  84. }
  85. func (m *ChartFramework) GetItemById(id int) (item *ChartFramework, err error) {
  86. o := orm.NewOrmUsingDB("data")
  87. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  88. err = o.Raw(sql, id).QueryRow(&item)
  89. return
  90. }
  91. func (m *ChartFramework) GetItemByCondition(condition string, pars []interface{}) (item *ChartFramework, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  94. err = o.Raw(sql, pars).QueryRow(&item)
  95. return
  96. }
  97. func (m *ChartFramework) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  98. o := orm.NewOrmUsingDB("data")
  99. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  100. err = o.Raw(sql, pars).QueryRow(&count)
  101. return
  102. }
  103. func (m *ChartFramework) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ChartFramework, err error) {
  104. o := orm.NewOrmUsingDB("data")
  105. fields := strings.Join(fieldArr, ",")
  106. if len(fieldArr) == 0 {
  107. fields = `*`
  108. }
  109. order := `ORDER BY create_time DESC`
  110. if orderRule != "" {
  111. order = ` ORDER BY ` + orderRule
  112. }
  113. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  114. _, err = o.Raw(sql, pars).QueryRows(&items)
  115. return
  116. }
  117. func (m *ChartFramework) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ChartFramework, err error) {
  118. o := orm.NewOrmUsingDB("data")
  119. fields := strings.Join(fieldArr, ",")
  120. if len(fieldArr) == 0 {
  121. fields = `*`
  122. }
  123. order := `ORDER BY create_time DESC`
  124. if orderRule != "" {
  125. order = ` ORDER BY ` + orderRule
  126. }
  127. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  128. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  129. return
  130. }
  131. func (m *ChartFramework) CreateFrameworkAndNodes(item *ChartFramework, nodes []*ChartFrameworkNode) (err error) {
  132. o := orm.NewOrmUsingDB("data")
  133. tx, e := o.Begin()
  134. if e != nil {
  135. err = fmt.Errorf("orm begin err: %s", e.Error())
  136. return
  137. }
  138. defer func() {
  139. if err != nil {
  140. _ = tx.Rollback()
  141. return
  142. }
  143. _ = tx.Commit()
  144. }()
  145. id, e := tx.Insert(item)
  146. if e != nil {
  147. err = fmt.Errorf("insert framework err: %s", e.Error())
  148. return
  149. }
  150. newId := int(id)
  151. item.ChartFrameworkId = newId
  152. if len(nodes) > 0 {
  153. for _, n := range nodes {
  154. n.ChartFrameworkId = newId
  155. }
  156. _, e = tx.InsertMulti(len(nodes), nodes)
  157. if e != nil {
  158. err = fmt.Errorf("insert multi nodes err: %s", e.Error())
  159. return
  160. }
  161. }
  162. return
  163. }
  164. func (m *ChartFramework) EditFrameworkAndNodes(item *ChartFramework, updateCols []string, nodes []*ChartFrameworkNode) (err error) {
  165. o := orm.NewOrmUsingDB("data")
  166. tx, e := o.Begin()
  167. if e != nil {
  168. err = fmt.Errorf("orm begin err: %s", e.Error())
  169. return
  170. }
  171. defer func() {
  172. if err != nil {
  173. _ = tx.Rollback()
  174. return
  175. }
  176. _ = tx.Commit()
  177. }()
  178. _, e = tx.Update(item, updateCols...)
  179. if e != nil {
  180. err = fmt.Errorf("framework update err: %s", e.Error())
  181. return
  182. }
  183. sql := `DELETE FROM chart_framework_node WHERE chart_framework_id = ?`
  184. _, e = tx.Raw(sql, item.ChartFrameworkId).Exec()
  185. if e != nil {
  186. err = fmt.Errorf("clear nodes err: %s", e.Error())
  187. return
  188. }
  189. if len(nodes) > 0 {
  190. _, e = tx.InsertMulti(len(nodes), nodes)
  191. if e != nil {
  192. err = fmt.Errorf("insert multi nodes err: %s", e.Error())
  193. return
  194. }
  195. }
  196. return
  197. }
  198. func (m *ChartFramework) RemoveFrameworkAndNodes(frameworkId int) (err error) {
  199. o := orm.NewOrmUsingDB("data")
  200. tx, e := o.Begin()
  201. if e != nil {
  202. err = fmt.Errorf("orm begin err: %s", e.Error())
  203. return
  204. }
  205. defer func() {
  206. if err != nil {
  207. _ = tx.Rollback()
  208. return
  209. }
  210. _ = tx.Commit()
  211. }()
  212. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  213. _, e = tx.Raw(sql, frameworkId).Exec()
  214. if e != nil {
  215. err = fmt.Errorf("delete framework err: %s", e.Error())
  216. return
  217. }
  218. sql = `DELETE FROM chart_framework_node WHERE chart_framework_id = ?`
  219. _, e = tx.Raw(sql, frameworkId).Exec()
  220. if e != nil {
  221. err = fmt.Errorf("clear nodes err: %s", e.Error())
  222. return
  223. }
  224. return
  225. }
  226. // ChartFrameworkAddReq 图库框架新增请求体
  227. type ChartFrameworkAddReq struct {
  228. FrameworkName string `description:"框架名称"`
  229. FrameworkImg string `description:"框架图片"`
  230. FrameworkContent string `description:"框架内容"`
  231. Nodes []ChartFrameworkNodeReq `description:"框架节点"`
  232. }
  233. // ChartFrameworkNodeReq 图库框架节点请求体
  234. type ChartFrameworkNodeReq struct {
  235. MyChartClassifyId int `description:"我的图表分类ID"`
  236. NodeId string `description:"节点ID"`
  237. NodeName string `description:"节点名称"`
  238. }
  239. // ChartFrameworkEditReq 图库框架编辑请求体
  240. type ChartFrameworkEditReq struct {
  241. ChartFrameworkId int `description:"图库框架ID"`
  242. ChartFrameworkAddReq
  243. }
  244. // ChartFrameworkRemoveReq 图库框架编辑请求体
  245. type ChartFrameworkRemoveReq struct {
  246. ChartFrameworkId int `description:"图库框架ID"`
  247. }
  248. // ChartFrameworkRenameReq 图库框架重命名请求体
  249. type ChartFrameworkRenameReq struct {
  250. ChartFrameworkId int `description:"图库框架ID"`
  251. FrameworkName string `description:"框架名称"`
  252. }
  253. // ChartFrameworkEditPublicReq 图库框架编辑公开请求体
  254. type ChartFrameworkEditPublicReq struct {
  255. ChartFrameworkId int `description:"图库框架ID"`
  256. IsPublic int `description:"0-隐藏公开; 1-公开"`
  257. }
  258. // ChartFrameworkMoveReq 图库框架移动排序请求体
  259. type ChartFrameworkMoveReq struct {
  260. ChartFrameworkId int `description:"图库框架ID"`
  261. PrevChartFrameworkId int `description:"上一个框架ID"`
  262. NextChartFrameworkId int `description:"下一个框架ID"`
  263. }
  264. // UpdateChartFrameworkSort 更新我的图库框架排序
  265. func UpdateChartFrameworkSort(adminId, frameworkId, current int, updates string) (err error) {
  266. o := orm.NewOrmUsingDB("data")
  267. sql := fmt.Sprintf(`UPDATE chart_framework SET sort = %s WHERE admin_id = ? and sort > ?`, updates)
  268. if frameworkId > 0 {
  269. sql += ` OR (chart_framework_id > ` + fmt.Sprint(frameworkId) + ` AND sort = ` + fmt.Sprint(current) + `)`
  270. }
  271. _, err = o.Raw(sql, adminId, current).Exec()
  272. return
  273. }
  274. // GetFirstChartFramework 获取我的图库框架排首位的数据
  275. func GetFirstChartFramework(adminId int) (item *ChartFramework, err error) {
  276. o := orm.NewOrmUsingDB("data")
  277. sql := `SELECT * FROM chart_framework WHERE admin_id = ? ORDER BY sort ASC,chart_framework_id ASC LIMIT 1`
  278. err = o.Raw(sql, adminId).QueryRow(&item)
  279. return
  280. }
  281. // ChartFrameworkItem 图库框架表信息
  282. type ChartFrameworkItem struct {
  283. ChartFrameworkId int `description:"框架ID"`
  284. FrameworkCode string `description:"框架唯一编码"`
  285. FrameworkName string `description:"框架名称"`
  286. FrameworkImg string `description:"框架图片"`
  287. FrameworkContent string `description:"框架内容"`
  288. IsPublic int `description:"是否公开:0-私有;1-公开"`
  289. PublicTime string `description:"公开时间"`
  290. Sort int `description:"排序"`
  291. AdminId int `description:"创建人ID"`
  292. AdminName string `description:"创建人姓名"`
  293. CreateTime string `description:"创建时间"`
  294. ModifyTime string `description:"更新时间"`
  295. Nodes []*ChartFrameworkNodeItem `description:"框架节点"`
  296. }
  297. // FormatChartFramework2Item 格式化框架信息
  298. func FormatChartFramework2Item(origin *ChartFramework, nodes []*ChartFrameworkNodeItem) (item *ChartFrameworkItem) {
  299. if origin == nil {
  300. return
  301. }
  302. item = new(ChartFrameworkItem)
  303. item.ChartFrameworkId = origin.ChartFrameworkId
  304. item.FrameworkCode = origin.FrameworkCode
  305. item.FrameworkName = origin.FrameworkName
  306. item.FrameworkImg = origin.FrameworkImg
  307. item.FrameworkContent = origin.FrameworkContent
  308. item.IsPublic = origin.IsPublic
  309. item.PublicTime = utils.TimeTransferString(utils.FormatDateTime, origin.PublicTime)
  310. item.Sort = origin.Sort
  311. item.AdminId = origin.AdminId
  312. item.AdminName = origin.AdminName
  313. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  314. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  315. item.Nodes = nodes
  316. return
  317. }
  318. // ChartFrameworkPublicMenuItem 公开框架目录
  319. type ChartFrameworkPublicMenuItem struct {
  320. AdminId int `description:"创建人ID"`
  321. MenuName string `description:"目录名称"`
  322. Frameworks []*ChartFrameworkItem `description:"框架列表"`
  323. }