package repositories import ( "fmt" "time" "xorm.io/xorm" "xps/datamodel" "xps/viewmodel" ) type GroupRepo struct { engine *xorm.Engine } func NewGroupRepo(engine *xorm.Engine) *GroupRepo { return &GroupRepo{ engine:engine, } } func (d *GroupRepo) GetById(ocId int64 ,groupId int64)(*viewmodel.GroupInfo, error) { sqlSelect := ` SELECT A.oc_id, A.group_id , A.group_name, A.group_cat_id, B.group_cat_title, A.group_code, A.group_select, A.group_path, A.group_desc, A.root_id, A.parent_id, C.group_name as parent_name, A.node_left, A.node_right, A.node_level, A.is_leaf, A.is_fixed ` sqlFrom := ` FROM s_group AS A LEFT JOIN s_group_cat AS B ON (A.oc_id = B.oc_id AND A.group_cat_id = B.group_cat_id) LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.parent_id = C.group_id )` sqlWhere := ` WHERE A.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d" ,ocId) sqlWhere += ` AND A.group_id = ` + fmt.Sprintf("%d" ,groupId) data := &viewmodel.GroupInfo{GroupId: groupId} ok, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id ASC").Get(data) if !ok && err != nil { return nil, err } else { return data, nil } } func (d *GroupRepo) GetList(m map[string]interface{})([]viewmodel.GroupInfo, error) { sqlSelect := ` SELECT A.oc_id, A.group_id , A.group_name, A.group_cat_id, B.group_cat_title, A.group_code, A.group_select, A.group_path, A.group_desc, A.root_id, A.parent_id, C.group_name as parent_name, A.node_left, A.node_right, A.node_level, A.is_leaf, A.is_fixed ` sqlFrom := ` FROM s_group AS A LEFT JOIN s_group_cat AS B ON (A.oc_id = B.oc_id AND A.group_cat_id = B.group_cat_id) LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.parent_id = C.group_id )` ocId := m["ocId"].(int64) sqlWhere := ` WHERE A.deleted_flag = 0 and A.oc_id = ` + fmt.Sprintf("%d" ,ocId) if keywords, ok := m["keywords"]; ok { sqlWhere+= ` AND A.group_name like `+"'%"+ fmt.Sprintf("%s", keywords) +"%'" } datalist := make([]viewmodel.GroupInfo, 0) err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id ASC").Find(&datalist) if err != nil{ return nil, err } return datalist, nil } func (d *GroupRepo) GetAll(ocId int64)([]viewmodel.GroupInfo, error) { sqlSelect := ` SELECT A.oc_id, A.group_id , A.group_name, A.group_cat_id, B.group_cat_title, A.group_code, A.group_select, A.group_path, A.group_desc, A.root_id, A.parent_id, C.group_name as parent_name, A.node_left, A.node_right, A.node_level, A.is_leaf, A.is_fixed ` sqlFrom := ` FROM s_group AS A LEFT JOIN s_group_cat AS B ON (A.oc_id = B.oc_id AND A.group_cat_id = B.group_cat_id) LEFT JOIN s_group AS C ON (A.oc_id = C.oc_id AND A.parent_id = C.group_id )` sqlWhere := ` WHERE A.deleted_flag = 0 and A.oc_id = ` + fmt.Sprintf("%d" ,ocId) datalist := make([]viewmodel.GroupInfo, 0) err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).OrderBy("group_id ASC").Find(&datalist) if err != nil{ return nil, err } return datalist, nil } func (d *GroupRepo) Delete(m map[string]interface{}) error { ocId := m["ocId"].(int64) groupId := m["groupId"].(int64) deletedBy := m["deletedBy"].(int64) group := &datamodel.Group{} group.OcId = ocId group.GroupId = groupId group.DeletedFlag = 1 group.DeletedBy = deletedBy group.DeletedAt = time.Now() _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(group) return err } func (d *GroupRepo) Update(data *datamodel.Group) error{ ocId := data.OcId groupId := data.GroupId _, err := d.engine.Where("oc_id = ?", ocId).Where("group_id =?", groupId).Update(data) return err } func (d *GroupRepo) RefreshLeafStatus(ocId int64 ,groupId int64 ,isLeaf bool) error { var sql string if isLeaf { sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 1 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId) }else { sql = fmt.Sprintf("UPDATE s_group SET is_leaf = 0 WHERE oc_id = %d AND group_id = %d" ,ocId ,groupId) } _ ,err := d.engine.Exec(sql) return err } func (d *GroupRepo) Create(data *datamodel.Group) error { _, err := d.engine.Insert(data) return err } func (d *GroupRepo) IncrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error { sql := fmt.Sprintf("UPDATE s_group 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 *GroupRepo) IncrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error { sql := fmt.Sprintf("UPDATE s_group 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 *GroupRepo) DecrementGroupLeft(ocId int64 ,steps int64 ,nodeLeft int64) error { sql := fmt.Sprintf("UPDATE s_group 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 *GroupRepo) DecrementGroupRight(ocId int64 ,steps int64 ,nodeRight int64) error { sql := fmt.Sprintf("UPDATE s_group 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 *GroupRepo) BatchDelete(ocId ,nodeLeft ,nodeRight int64) error { sql := fmt.Sprintf("DELETE FROM s_group WHERE oc_id = %d AND node_left >= %d AND node_right <= %d " ,ocId ,nodeLeft ,nodeRight) _ ,err := d.engine.Exec(sql) return err } func (d *GroupRepo) CountOfChildren(ocId, groupId int64)(int64, error){ count, err := d.engine.Where("oc_id = ?" ,ocId). Where("group_parent_id = ?" ,groupId). Where("deleted_flag = 0 "). Count(new(datamodel.Group)) if err != nil { return 0, err } return count, nil }