chart_framework.go 11 KB

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