chart_framework.go 11 KB

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