123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- package data_manage
- import (
- "eta_gn/eta_api/global"
- "eta_gn/eta_api/utils"
- "fmt"
- "strings"
- "time"
- )
- // ChartFramework 图库框架表
- type ChartFramework struct {
- ChartFrameworkId int `orm:"column(chart_framework_id);pk" gorm:"primaryKey" `
- FrameworkCode string `description:"框架唯一编码"`
- FrameworkName string `description:"框架名称"`
- FrameworkImg string `description:"框架图片"`
- FrameworkContent string `description:"框架内容"`
- IsPublic int `description:"是否公开:0-私有;1-公开"`
- PublicTime time.Time `description:"公开时间"`
- Sort int `description:"排序"`
- AdminId int `description:"创建人ID"`
- AdminName string `description:"创建人姓名"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"更新时间"`
- }
- func (m *ChartFramework) TableName() string {
- return "chart_framework"
- }
- func (m *ChartFramework) PrimaryId() string {
- return ChartFrameworkColumns.ChartFrameworkId
- }
- var ChartFrameworkColumns = struct {
- ChartFrameworkId string
- FrameworkCode string
- FrameworkName string
- FrameworkImg string
- FrameworkContent string
- IsPublic string
- PublicTime string
- Sort string
- AdminId string
- AdminName string
- CreateTime string
- ModifyTime string
- }{
- ChartFrameworkId: "chart_framework_id",
- FrameworkCode: "framework_code",
- FrameworkName: "framework_name",
- FrameworkImg: "framework_img",
- FrameworkContent: "framework_content",
- IsPublic: "is_public",
- PublicTime: "public_time",
- Sort: "sort",
- AdminId: "admin_id",
- AdminName: "admin_name",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- func (m *ChartFramework) Create() (err error) {
- err = global.DmSQL["data"].Create(m).Error
- return
- }
- func (m *ChartFramework) CreateMulti(items []*ChartFramework) (err error) {
- if len(items) == 0 {
- return
- }
- err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
- return
- }
- func (m *ChartFramework) Update(cols []string) (err error) {
- o := global.DmSQL["data"]
- err = o.Select(cols).Updates(m).Error
- return
- }
- func (m *ChartFramework) Del() (err error) {
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
- err = global.DmSQL["data"].Exec(sql, m.ChartFrameworkId).Error
- return
- }
- func (m *ChartFramework) GetItemById(id int) (item *ChartFramework, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
- err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
- return
- }
- func (m *ChartFramework) GetItemByCondition(condition string, pars []interface{}) (item *ChartFramework, err error) {
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
- err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
- return
- }
- func (m *ChartFramework) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
- return
- }
- func (m *ChartFramework) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ChartFramework, err error) {
- o := global.DmSQL["data"]
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
- err = o.Raw(sql, pars...).Scan(&items).Error
- return
- }
- func (m *ChartFramework) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ChartFramework, err error) {
- o := global.DmSQL["data"]
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
- pars = append(pars, startSize)
- pars = append(pars, pageSize)
- err = o.Raw(sql, pars...).Scan(&items).Error
- return
- }
- func (m *ChartFramework) CreateFrameworkAndNodes(item *ChartFramework, nodes []*ChartFrameworkNode) (err error) {
- tx := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- e := tx.Create(item).Error
- if e != nil {
- err = fmt.Errorf("insert framework err: %s", e.Error())
- return
- }
- if len(nodes) > 0 {
- for _, n := range nodes {
- n.ChartFrameworkId = item.ChartFrameworkId
- }
- e = tx.CreateInBatches(nodes, utils.MultiAddNum).Error
- if e != nil {
- err = fmt.Errorf("insert multi nodes err: %s", e.Error())
- return
- }
- }
- return
- }
- func (m *ChartFramework) EditFrameworkAndNodes(item *ChartFramework, updateCols []string, nodes []*ChartFrameworkNode) (err error) {
- tx := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- e := tx.Select(updateCols).Updates(item).Error
- if e != nil {
- err = fmt.Errorf("framework update err: %s", e.Error())
- return
- }
- sql := `DELETE FROM chart_framework_node WHERE chart_framework_id = ?`
- e = tx.Exec(sql, item.ChartFrameworkId).Error
- if e != nil {
- err = fmt.Errorf("clear nodes err: %s", e.Error())
- return
- }
- if len(nodes) > 0 {
- e = tx.CreateInBatches(nodes, utils.MultiAddNum).Error
- if e != nil {
- err = fmt.Errorf("insert multi nodes err: %s", e.Error())
- return
- }
- }
- return
- }
- func (m *ChartFramework) RemoveFrameworkAndNodes(frameworkId int) (err error) {
- tx := global.DmSQL["data"].Begin()
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- }()
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
- e := tx.Exec(sql, frameworkId).Error
- if e != nil {
- err = fmt.Errorf("delete framework err: %s", e.Error())
- return
- }
- sql = `DELETE FROM chart_framework_node WHERE chart_framework_id = ?`
- e = tx.Exec(sql, frameworkId).Error
- if e != nil {
- err = fmt.Errorf("clear nodes err: %s", e.Error())
- return
- }
- return
- }
- // ChartFrameworkAddReq 图库框架新增请求体
- type ChartFrameworkAddReq struct {
- FrameworkName string `description:"框架名称"`
- FrameworkImg string `description:"框架图片"`
- FrameworkContent string `description:"框架内容"`
- Nodes []ChartFrameworkNodeReq `description:"框架节点"`
- }
- // ChartFrameworkNodeReq 图库框架节点请求体
- type ChartFrameworkNodeReq struct {
- MyChartClassifyId int `description:"我的图表分类ID"`
- NodeId string `description:"节点ID"`
- NodeName string `description:"节点名称"`
- }
- // ChartFrameworkEditReq 图库框架编辑请求体
- type ChartFrameworkEditReq struct {
- ChartFrameworkId int `description:"图库框架ID"`
- ChartFrameworkAddReq
- }
- // ChartFrameworkRemoveReq 图库框架编辑请求体
- type ChartFrameworkRemoveReq struct {
- ChartFrameworkId int `description:"图库框架ID"`
- }
- // ChartFrameworkRenameReq 图库框架重命名请求体
- type ChartFrameworkRenameReq struct {
- ChartFrameworkId int `description:"图库框架ID"`
- FrameworkName string `description:"框架名称"`
- }
- // ChartFrameworkEditPublicReq 图库框架编辑公开请求体
- type ChartFrameworkEditPublicReq struct {
- ChartFrameworkId int `description:"图库框架ID"`
- IsPublic int `description:"0-隐藏公开; 1-公开"`
- }
- // ChartFrameworkMoveReq 图库框架移动排序请求体
- type ChartFrameworkMoveReq struct {
- ChartFrameworkId int `description:"图库框架ID"`
- PrevChartFrameworkId int `description:"上一个框架ID"`
- NextChartFrameworkId int `description:"下一个框架ID"`
- }
- // UpdateChartFrameworkSort 更新我的图库框架排序
- func UpdateChartFrameworkSort(adminId, frameworkId, current int, updates string) (err error) {
- o := global.DmSQL["data"]
- sql := fmt.Sprintf(`UPDATE chart_framework SET sort = %s WHERE admin_id = ? and sort > ?`, updates)
- if frameworkId > 0 {
- sql += ` OR (chart_framework_id > ` + fmt.Sprint(frameworkId) + ` AND sort = ` + fmt.Sprint(current) + `)`
- }
- err = o.Exec(sql, adminId, current).Error
- return
- }
- // GetFirstChartFramework 获取我的图库框架排首位的数据
- func GetFirstChartFramework(adminId int) (item *ChartFramework, err error) {
- o := global.DmSQL["data"]
- sql := `SELECT * FROM chart_framework WHERE admin_id = ? ORDER BY sort ASC,chart_framework_id ASC LIMIT 1`
- err = o.Raw(sql, adminId).First(&item).Error
- return
- }
- // ChartFrameworkItem 图库框架表信息
- type ChartFrameworkItem struct {
- ChartFrameworkId int `description:"框架ID"`
- FrameworkCode string `description:"框架唯一编码"`
- FrameworkName string `description:"框架名称"`
- FrameworkImg string `description:"框架图片"`
- FrameworkContent string `description:"框架内容"`
- IsPublic int `description:"是否公开:0-私有;1-公开"`
- PublicTime string `description:"公开时间"`
- Sort int `description:"排序"`
- AdminId int `description:"创建人ID"`
- AdminName string `description:"创建人姓名"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"更新时间"`
- Nodes []*ChartFrameworkNodeItem `description:"框架节点"`
- }
- // FormatChartFramework2Item 格式化框架信息
- func FormatChartFramework2Item(origin *ChartFramework, nodes []*ChartFrameworkNodeItem) (item *ChartFrameworkItem) {
- if origin == nil {
- return
- }
- item = new(ChartFrameworkItem)
- item.ChartFrameworkId = origin.ChartFrameworkId
- item.FrameworkCode = origin.FrameworkCode
- item.FrameworkName = origin.FrameworkName
- item.FrameworkImg = origin.FrameworkImg
- item.FrameworkContent = origin.FrameworkContent
- item.IsPublic = origin.IsPublic
- item.PublicTime = utils.TimeTransferString(utils.FormatDateTime, origin.PublicTime)
- item.Sort = origin.Sort
- item.AdminId = origin.AdminId
- item.AdminName = origin.AdminName
- item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
- item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
- item.Nodes = nodes
- return
- }
- // ChartFrameworkPublicMenuItem 公开框架目录
- type ChartFrameworkPublicMenuItem struct {
- AdminId int `description:"创建人ID"`
- MenuName string `description:"目录名称"`
- Frameworks []*ChartFrameworkItem `description:"框架列表"`
- }
|