|
|
@@ -0,0 +1,206 @@
|
|
|
+package repositories
|
|
|
+
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "time"
|
|
|
+ "xorm.io/xorm"
|
|
|
+ "xps/datamodels"
|
|
|
+ "xps/viewmodels"
|
|
|
+)
|
|
|
+
|
|
|
+type DocDirRepo struct {
|
|
|
+ engine *xorm.Engine
|
|
|
+}
|
|
|
+
|
|
|
+func NewDocDirRepo(engine *xorm.Engine) *DocDirRepo {
|
|
|
+ return &DocDirRepo{
|
|
|
+ engine: engine,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) {
|
|
|
+ limit := m["limit"].(int)
|
|
|
+ page := m["page"].(int)
|
|
|
+
|
|
|
+ sqlSelect := ` SELECT A.oc_id,
|
|
|
+ A.dir_id,
|
|
|
+ A.dir_title,
|
|
|
+ A.dir_desc,
|
|
|
+ A.parent_id,
|
|
|
+ A.is_fixed,
|
|
|
+ A.node_left,
|
|
|
+ A.node_right,
|
|
|
+ A.node_level,
|
|
|
+ A.is_leaf `
|
|
|
+ sqlFrom := ` FROM doc_dir AS A `
|
|
|
+ sqlWhere := ` WHERE A.deleted_flag = 0 `
|
|
|
+ if ocId, ok := m["ocId"]; ok {
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
|
|
|
+ }
|
|
|
+ if dirId, ok := m["dirId"]; ok {
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.dir_id = %d ", dirId)
|
|
|
+ }
|
|
|
+ if keywords, ok := m["keywords"]; ok {
|
|
|
+ sqlWhere += " AND A.dir_title like '%" + fmt.Sprintf("%s", keywords) + "%' "
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlCount := ` SELECT COUNT(*) `
|
|
|
+ total, err1 := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodels.DocDir))
|
|
|
+ if err1 != nil {
|
|
|
+ return nil, err1
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlOrderBy := " ORDER BY A.oc_id, A.dir_id "
|
|
|
+ pageStart := (page - 1) * limit
|
|
|
+ sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
|
|
|
+
|
|
|
+ datalist := make([]datamodels.DocDir, 0)
|
|
|
+ err2 := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy + sqlLimit).Find(&datalist)
|
|
|
+ if err2 != nil {
|
|
|
+ return nil, err2
|
|
|
+ }
|
|
|
+
|
|
|
+ pageResult := &viewmodels.PageResult{}
|
|
|
+ pageResult.Data = datalist
|
|
|
+ pageResult.Total = total
|
|
|
+ pageResult.PageSize = limit
|
|
|
+ pageResult.Page = page
|
|
|
+ return pageResult, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) GetList(m map[string]interface{}) ([]viewmodels.DocDirInfo, error) {
|
|
|
+ sqlSelect := ` SELECT A.oc_id,
|
|
|
+ A.dir_id,
|
|
|
+ A.dir_title,
|
|
|
+ A.dir_desc,
|
|
|
+ A.parent_id,
|
|
|
+ A.is_fixed,
|
|
|
+ A.node_left,
|
|
|
+ A.node_right,
|
|
|
+ A.node_level,
|
|
|
+ A.is_leaf `
|
|
|
+ sqlFrom := ` FROM doc_dir AS A `
|
|
|
+ sqlWhere := ` WHERE A.deleted_flag = 0 `
|
|
|
+ if ocId, ok := m["ocId"]; ok {
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
|
|
|
+ }
|
|
|
+ if dirId, ok := m["dirId"]; ok {
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.dir_id = %d ", dirId)
|
|
|
+ }
|
|
|
+ nodeLeft, ok1 := m["nodeLeft"]
|
|
|
+ nodeRight, ok2 := m["nodeRight"]
|
|
|
+ if ok1 && ok2 {
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.node_left > %d", nodeLeft)
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.node_right < %d", nodeRight)
|
|
|
+ }
|
|
|
+ if keywords, ok := m["keywords"]; ok {
|
|
|
+ sqlWhere += " AND A.dir_title like '%" + fmt.Sprintf("%s", keywords) + "%' "
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlOrderBy := " ORDER BY A.oc_id, A.dir_id "
|
|
|
+ datalist := make([]viewmodels.DocDirInfo, 0)
|
|
|
+ err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ } else {
|
|
|
+ return datalist, nil
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) GetById(ocId, artCatId int64) (*viewmodels.DocDirInfo, error) {
|
|
|
+ sqlSelect := ` SELECT A.oc_id,
|
|
|
+ A.dir_id,
|
|
|
+ A.dir_title,
|
|
|
+ A.dir_desc,
|
|
|
+ A.parent_id,
|
|
|
+ A.is_fixed,
|
|
|
+ A.node_left,
|
|
|
+ A.node_right,
|
|
|
+ A.node_level,
|
|
|
+ A.is_leaf `
|
|
|
+ sqlFrom := ` FROM doc_dir AS A `
|
|
|
+ sqlWhere := ` WHERE A.deleted_flag = 0 `
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_id = %d ", ocId)
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.dir_id = %d ", artCatId)
|
|
|
+ data := &viewmodels.DocDirInfo{}
|
|
|
+ has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
|
|
|
+ if !has || err != nil {
|
|
|
+ return nil, err
|
|
|
+ } else {
|
|
|
+ return data, nil
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) Create(data *datamodels.DocDir) error {
|
|
|
+ data.CreatedAt = time.Now()
|
|
|
+ _, err := d.engine.Insert(data)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) Update(data *datamodels.DocDir) error {
|
|
|
+ ocId := data.OcId
|
|
|
+ dirId := data.DirId
|
|
|
+
|
|
|
+ data.UpdatedAt = time.Now()
|
|
|
+ _, err := d.engine.
|
|
|
+ Where("oc_id = ?", ocId).
|
|
|
+ Where("dir_id = ?", dirId).
|
|
|
+ Update(data)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) Delete(m map[string]interface{}) error {
|
|
|
+ ocId := m["ocId"].(int64)
|
|
|
+ artCatId := m["artCatId"].(int64)
|
|
|
+ deletedBy := m["deletedBy"].(int64)
|
|
|
+
|
|
|
+ data := &datamodels.DocDir{}
|
|
|
+ data.DeletedFlag = 1
|
|
|
+ data.DeletedBy = deletedBy
|
|
|
+ data.DeletedAt = time.Now()
|
|
|
+ _, err := d.engine.
|
|
|
+ Where("oc_id = ?", ocId).
|
|
|
+ Where("dir_id = ?", artCatId).
|
|
|
+ Update(data)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) CountByParent(ocId, parentId int64)(int64, error ){
|
|
|
+ docDir := &datamodels.DocDir{}
|
|
|
+ return d.engine.Where("oc_id = %d", ocId).
|
|
|
+ Where("parent_id = %d", parentId).
|
|
|
+ Where("deleted_flag = %d", 0).
|
|
|
+ Count(docDir)
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) IncrementLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
|
|
|
+ sql := fmt.Sprintf("UPDATE doc_dir SET node_left = node_left + %d WHERE oc_id =%d AND node_left >= %d AND deleted_flag = 0" ,steps ,ocId ,nodeLeft)
|
|
|
+ _, err :=d.engine.Exec(sql)
|
|
|
+
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) IncrementRight(ocId int64 ,steps int64 ,nodeRight int64) error {
|
|
|
+ sql := fmt.Sprintf("UPDATE doc_dir SET node_right = node_right + %d WHERE oc_id =%d AND node_right >= %d AND deleted_flag = 0" ,steps ,ocId ,nodeRight)
|
|
|
+ _, err :=d.engine.Exec(sql)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) DecrementLeft(ocId int64 ,steps int64 ,nodeLeft int64) error {
|
|
|
+ sql := fmt.Sprintf("UPDATE doc_dir SET node_left = node_left - %d WHERE oc_id =%d AND node_left > %d AND deleted_flag = 0" ,steps ,ocId ,nodeLeft)
|
|
|
+ _, err :=d.engine.Exec(sql)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) DecrementRight(ocId int64 ,steps int64 ,nodeRight int64) error {
|
|
|
+ sql := fmt.Sprintf("UPDATE doc_dir SET node_right = node_right - %d WHERE oc_id =%d AND node_right > %d AND deleted_flag = 0" ,steps ,ocId ,nodeRight)
|
|
|
+ _, err :=d.engine.Exec(sql)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (d *DocDirRepo) BatchDelete(ocId ,nodeLeft ,nodeRight int64) error {
|
|
|
+ sql := fmt.Sprintf("DELETE FROM doc_dir WHERE oc_id = %d AND node_left >= %d AND node_right <= %d " ,ocId ,nodeLeft ,nodeRight)
|
|
|
+ _ ,err := d.engine.Exec(sql)
|
|
|
+ return err
|
|
|
+}
|