chart_framework.go 10 KB

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