|
@@ -0,0 +1,149 @@
|
|
|
|
|
+package repositories
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "time"
|
|
|
|
|
+ "xorm.io/xorm"
|
|
|
|
|
+ "xps/datamodels"
|
|
|
|
|
+ "xps/viewmodels"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+type OcCatRepo struct {
|
|
|
|
|
+ engine *xorm.Engine
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func NewOcCatRepo(engine *xorm.Engine) *OcCatRepo {
|
|
|
|
|
+ return &OcCatRepo{
|
|
|
|
|
+ engine: engine,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *OcCatRepo) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) {
|
|
|
|
|
+ limit := m["limit"].(int)
|
|
|
|
|
+ page := m["page"].(int)
|
|
|
|
|
+
|
|
|
|
|
+ sqlSelect := ` SELECT A.* `
|
|
|
|
|
+ sqlFrom := ` FROM oc_cat AS A `
|
|
|
|
|
+ sqlWhere := ` WHERE A.deleted_flag = 0 `
|
|
|
|
|
+
|
|
|
|
|
+ if ocTypeId, ok := m["ocTypeId"]; ok {
|
|
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_type_id = %d ", ocTypeId)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ocCatId, ok := m["ocCatId"]; ok {
|
|
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_cat_id = %d ", ocCatId)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if keywords, ok := m["keywords"]; ok {
|
|
|
|
|
+ sqlWhere += " AND A.oc_cat_title like '%" + fmt.Sprintf("%s", keywords) + "%' "
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sqlCount := ` SELECT COUNT(*) `
|
|
|
|
|
+ total, err1 := d.engine.SQL(sqlCount + sqlFrom + sqlWhere).Count(new(datamodels.OcCat))
|
|
|
|
|
+ if err1 != nil {
|
|
|
|
|
+ return nil, err1
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sqlOrderBy := " ORDER BY A.oc_type_id, A.oc_cat_id "
|
|
|
|
|
+ pageStart := (page - 1) * limit
|
|
|
|
|
+ sqlLimit := fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, pageStart)
|
|
|
|
|
+
|
|
|
|
|
+ datalist := make([]datamodels.OcCat, 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 *OcCatRepo) GetList(m map[string]interface{}) ([]viewmodels.OcCatInfo, error) {
|
|
|
|
|
+ sqlSelect := ` SELECT A.* `
|
|
|
|
|
+ sqlFrom := ` FROM oc_cat AS A `
|
|
|
|
|
+ sqlWhere := ` WHERE A.deleted_flag = 0 `
|
|
|
|
|
+
|
|
|
|
|
+ if ocTypeId, ok := m["ocTypeId"]; ok {
|
|
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_type_id = %d ", ocTypeId)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ocCatId, ok := m["ocCatId"]; ok {
|
|
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_cat_id = %d ", ocCatId)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if keywords, ok := m["keywords"]; ok {
|
|
|
|
|
+ sqlWhere += ` AND A.oc_cat_title like ` + "'%" + fmt.Sprintf("%s", keywords) + "%'"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sqlOrderBy := " ORDER BY A.oc_type_id, A.oc_cat_id "
|
|
|
|
|
+ datalist := make([]viewmodels.OcCatInfo, 0)
|
|
|
|
|
+ err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return datalist, nil
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *OcCatRepo) GetById(ocTypeId, ocCatId int64) (*viewmodels.OcCatInfo, error) {
|
|
|
|
|
+ sqlSelect := ` SELECT A.* `
|
|
|
|
|
+ sqlFrom := ` FROM oc_cat AS A `
|
|
|
|
|
+ sqlWhere := ` WHERE A.deleted_flag = 0 `
|
|
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_type_id = %d ", ocTypeId)
|
|
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_cat_id = %d ", ocCatId)
|
|
|
|
|
+ data := &viewmodels.OcCatInfo{}
|
|
|
|
|
+ has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
|
|
|
|
|
+ if !has || err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return data, nil
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *OcCatRepo) GetListById(ocTypeId int64) ([]viewmodels.OcCatInfo, error) {
|
|
|
|
|
+ sqlSelect := ` SELECT A.* `
|
|
|
|
|
+ sqlFrom := ` FROM oc_cat AS A `
|
|
|
|
|
+ sqlWhere := ` WHERE A.deleted_flag = 0 `
|
|
|
|
|
+ sqlWhere += fmt.Sprintf(" AND A.oc_type_id = %d ", ocTypeId)
|
|
|
|
|
+ sqlOrderBy := " ORDER BY A.oc_type_id, A.oc_cat_id "
|
|
|
|
|
+ datalist := make([]viewmodels.OcCatInfo, 0)
|
|
|
|
|
+ err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere + sqlOrderBy).Find(&datalist)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return datalist, nil
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+func (d *OcCatRepo) Create(data *datamodels.OcCat) error {
|
|
|
|
|
+ data.CreatedAt = time.Now()
|
|
|
|
|
+ _, err := d.engine.Insert(data)
|
|
|
|
|
+ return err
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *OcCatRepo) Update(data *datamodels.OcCat) error {
|
|
|
|
|
+ ocCatId := data.OcCatId
|
|
|
|
|
+
|
|
|
|
|
+ data.UpdatedAt = time.Now()
|
|
|
|
|
+ _, err := d.engine.Where("oc_cat_id = ?", ocCatId).Update(data)
|
|
|
|
|
+ return err
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (d *OcCatRepo) Delete(m map[string]interface{}) error {
|
|
|
|
|
+ ocCatId := m["ocCatId"].(int64)
|
|
|
|
|
+ deletedBy := m["deletedBy"].(int64)
|
|
|
|
|
+
|
|
|
|
|
+ data := &datamodels.OcCat{}
|
|
|
|
|
+ data.DeletedFlag = 1
|
|
|
|
|
+ data.DeletedBy = deletedBy
|
|
|
|
|
+ data.DeletedAt = time.Now()
|
|
|
|
|
+ _, err := d.engine.
|
|
|
|
|
+ Where("oc_cat_id = ?", ocCatId).
|
|
|
|
|
+ Update(data)
|
|
|
|
|
+ return err
|
|
|
|
|
+}
|