正在显示
4 个修改的文件
包含
124 行增加
和
107 行删除
| @@ -2,11 +2,8 @@ package models | @@ -2,11 +2,8 @@ package models | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | - "errors" | ||
| 6 | "fmt" | 5 | "fmt" |
| 7 | "oppmg/common/log" | 6 | "oppmg/common/log" |
| 8 | - "reflect" | ||
| 9 | - "strings" | ||
| 10 | "time" | 7 | "time" |
| 11 | 8 | ||
| 12 | "github.com/astaxie/beego/orm" | 9 | "github.com/astaxie/beego/orm" |
| @@ -42,6 +39,11 @@ func (t *Department) GetManagesIds() []int64 { | @@ -42,6 +39,11 @@ func (t *Department) GetManagesIds() []int64 { | ||
| 42 | return r | 39 | return r |
| 43 | } | 40 | } |
| 44 | 41 | ||
| 42 | +func (t *Department) ParseManagesIds(v []int64) string { | ||
| 43 | + bt, _ := json.Marshal(v) | ||
| 44 | + return string(bt) | ||
| 45 | +} | ||
| 46 | + | ||
| 45 | // AddDepartment insert a new Department into database and returns | 47 | // AddDepartment insert a new Department into database and returns |
| 46 | // last inserted Id on success. | 48 | // last inserted Id on success. |
| 47 | func AddDepartment(m *Department) (id int64, err error) { | 49 | func AddDepartment(m *Department) (id int64, err error) { |
| @@ -61,93 +63,20 @@ func GetDepartmentById(id int64) (v *Department, err error) { | @@ -61,93 +63,20 @@ func GetDepartmentById(id int64) (v *Department, err error) { | ||
| 61 | return nil, err | 63 | return nil, err |
| 62 | } | 64 | } |
| 63 | 65 | ||
| 64 | -// GetAllDepartment retrieves all Department matches certain condition. Returns empty list if | ||
| 65 | -// no records exist | ||
| 66 | -func GetAllDepartment(query map[string]string, fields []string, sortby []string, order []string, | ||
| 67 | - offset int64, limit int64) (ml []interface{}, err error) { | ||
| 68 | - o := orm.NewOrm() | ||
| 69 | - qs := o.QueryTable(new(Department)) | ||
| 70 | - // query k=v | ||
| 71 | - for k, v := range query { | ||
| 72 | - // rewrite dot-notation to Object__Attribute | ||
| 73 | - k = strings.Replace(k, ".", "__", -1) | ||
| 74 | - if strings.Contains(k, "isnull") { | ||
| 75 | - qs = qs.Filter(k, (v == "true" || v == "1")) | ||
| 76 | - } else { | ||
| 77 | - qs = qs.Filter(k, v) | ||
| 78 | - } | ||
| 79 | - } | ||
| 80 | - // order by: | ||
| 81 | - var sortFields []string | ||
| 82 | - if len(sortby) != 0 { | ||
| 83 | - if len(sortby) == len(order) { | ||
| 84 | - // 1) for each sort field, there is an associated order | ||
| 85 | - for i, v := range sortby { | ||
| 86 | - orderby := "" | ||
| 87 | - if order[i] == "desc" { | ||
| 88 | - orderby = "-" + v | ||
| 89 | - } else if order[i] == "asc" { | ||
| 90 | - orderby = v | ||
| 91 | - } else { | ||
| 92 | - return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
| 93 | - } | ||
| 94 | - sortFields = append(sortFields, orderby) | ||
| 95 | - } | ||
| 96 | - qs = qs.OrderBy(sortFields...) | ||
| 97 | - } else if len(sortby) != len(order) && len(order) == 1 { | ||
| 98 | - // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
| 99 | - for _, v := range sortby { | ||
| 100 | - orderby := "" | ||
| 101 | - if order[0] == "desc" { | ||
| 102 | - orderby = "-" + v | ||
| 103 | - } else if order[0] == "asc" { | ||
| 104 | - orderby = v | ||
| 105 | - } else { | ||
| 106 | - return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
| 107 | - } | ||
| 108 | - sortFields = append(sortFields, orderby) | ||
| 109 | - } | ||
| 110 | - } else if len(sortby) != len(order) && len(order) != 1 { | ||
| 111 | - return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
| 112 | - } | ||
| 113 | - } else { | ||
| 114 | - if len(order) != 0 { | ||
| 115 | - return nil, errors.New("Error: unused 'order' fields") | ||
| 116 | - } | ||
| 117 | - } | ||
| 118 | - | ||
| 119 | - var l []Department | ||
| 120 | - qs = qs.OrderBy(sortFields...) | ||
| 121 | - if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
| 122 | - if len(fields) == 0 { | ||
| 123 | - for _, v := range l { | ||
| 124 | - ml = append(ml, v) | ||
| 125 | - } | ||
| 126 | - } else { | ||
| 127 | - // trim unused fields | ||
| 128 | - for _, v := range l { | ||
| 129 | - m := make(map[string]interface{}) | ||
| 130 | - val := reflect.ValueOf(v) | ||
| 131 | - for _, fname := range fields { | ||
| 132 | - m[fname] = val.FieldByName(fname).Interface() | ||
| 133 | - } | ||
| 134 | - ml = append(ml, m) | ||
| 135 | - } | ||
| 136 | - } | ||
| 137 | - return ml, nil | ||
| 138 | - } | ||
| 139 | - return nil, err | ||
| 140 | -} | ||
| 141 | - | ||
| 142 | // UpdateDepartment updates Department by Id and returns error if | 66 | // UpdateDepartment updates Department by Id and returns error if |
| 143 | // the record to be updated doesn't exist | 67 | // the record to be updated doesn't exist |
| 144 | -func UpdateDepartmentById(m *Department) (err error) { | ||
| 145 | - o := orm.NewOrm() | 68 | +func UpdateDepartmentById(m *Department, col []string, om ...orm.Ormer) (err error) { |
| 69 | + var o orm.Ormer | ||
| 70 | + if len(om) > 0 { | ||
| 71 | + o = om[0] | ||
| 72 | + } else { | ||
| 73 | + o = orm.NewOrm() | ||
| 74 | + } | ||
| 146 | v := Department{Id: m.Id} | 75 | v := Department{Id: m.Id} |
| 147 | // ascertain id exists in the database | 76 | // ascertain id exists in the database |
| 148 | if err = o.Read(&v); err == nil { | 77 | if err = o.Read(&v); err == nil { |
| 149 | var num int64 | 78 | var num int64 |
| 150 | - if num, err = o.Update(m); err == nil { | 79 | + if num, err = o.Update(m, col...); err == nil { |
| 151 | fmt.Println("Number of records updated in database:", num) | 80 | fmt.Println("Number of records updated in database:", num) |
| 152 | } | 81 | } |
| 153 | } | 82 | } |
| @@ -168,3 +97,25 @@ func DeleteDepartment(id int64) (err error) { | @@ -168,3 +97,25 @@ func DeleteDepartment(id int64) (err error) { | ||
| 168 | } | 97 | } |
| 169 | return | 98 | return |
| 170 | } | 99 | } |
| 100 | + | ||
| 101 | +func GetDepartmentSubsetByRelation(relation string, om ...orm.Ormer) ([]Department, error) { | ||
| 102 | + dataSql := `SELECT company_id,parent_id,name,create_at,relation,member,managers | ||
| 103 | + FROM department | ||
| 104 | + WHERE relation LIKE ? AND delete_at = 0` | ||
| 105 | + var ( | ||
| 106 | + o orm.Ormer | ||
| 107 | + result []Department | ||
| 108 | + err error | ||
| 109 | + ) | ||
| 110 | + like := relation + "%" | ||
| 111 | + if len(om) == 0 { | ||
| 112 | + o = orm.NewOrm() | ||
| 113 | + } else { | ||
| 114 | + o = om[0] | ||
| 115 | + } | ||
| 116 | + _, err = o.Raw(dataSql, like).QueryRows(&result) | ||
| 117 | + if err != nil { | ||
| 118 | + return nil, err | ||
| 119 | + } | ||
| 120 | + return result, nil | ||
| 121 | +} |
| @@ -10,6 +10,8 @@ import ( | @@ -10,6 +10,8 @@ import ( | ||
| 10 | type Role struct { | 10 | type Role struct { |
| 11 | Id int `orm:"column(id);auto"` | 11 | Id int `orm:"column(id);auto"` |
| 12 | Name string `orm:"column(name);size(30)"` | 12 | Name string `orm:"column(name);size(30)"` |
| 13 | + Pid int `orm:"column(pid)"` | ||
| 14 | + Types int8 `orm:"column(types)"` | ||
| 13 | CompanyId int `orm:"column(company_id)"` | 15 | CompanyId int `orm:"column(company_id)"` |
| 14 | Descript string `orm:"column(descript)"` | 16 | Descript string `orm:"column(descript)"` |
| 15 | CreateAt time.Time `orm:"column(create_at);type(timestamp)"` | 17 | CreateAt time.Time `orm:"column(create_at);type(timestamp)"` |
| @@ -6,6 +6,8 @@ import ( | @@ -6,6 +6,8 @@ import ( | ||
| 6 | "oppmg/models" | 6 | "oppmg/models" |
| 7 | "oppmg/protocol" | 7 | "oppmg/protocol" |
| 8 | "oppmg/utils" | 8 | "oppmg/utils" |
| 9 | + "strings" | ||
| 10 | + "time" | ||
| 9 | 11 | ||
| 10 | "github.com/astaxie/beego/orm" | 12 | "github.com/astaxie/beego/orm" |
| 11 | ) | 13 | ) |
| @@ -17,57 +19,105 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart | @@ -17,57 +19,105 @@ func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepart | ||
| 17 | 19 | ||
| 18 | } | 20 | } |
| 19 | 21 | ||
| 20 | -func DepartmentEdit(param protocol.RequestDepartmentEdit) (protocol.ResponseDepartmentInfo, error) { | 22 | +func DepartmentEdit(param protocol.RequestDepartmentEdit) error { |
| 21 | var ( | 23 | var ( |
| 22 | - depart *models.Department | 24 | + departUpdate *models.Department |
| 23 | err error | 25 | err error |
| 24 | - result protocol.ResponseDepartmentInfo | ||
| 25 | ) | 26 | ) |
| 26 | - result = protocol.ResponseDepartmentInfo{ | ||
| 27 | - ID: param.ID, | ||
| 28 | - Name: param.Name, | ||
| 29 | - ParantID: param.ParantID, | ||
| 30 | - } | ||
| 31 | - depart, err = models.GetDepartmentById(param.ID) | 27 | + |
| 28 | + departUpdate, err = models.GetDepartmentById(param.ID) | ||
| 32 | if err != nil { | 29 | if err != nil { |
| 33 | e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ID, err) | 30 | e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ID, err) |
| 34 | log.Error(e.Error()) | 31 | log.Error(e.Error()) |
| 35 | - return result, protocol.NewErrWithMessage("1", e) | 32 | + return protocol.NewErrWithMessage("1", e) |
| 36 | } | 33 | } |
| 37 | - if depart.CompanyId != param.CompanyID { | ||
| 38 | - e := fmt.Errorf("depart.CompanyId(%d) !=param.CompanyID(%d)", depart.CompanyId, param.CompanyID) | 34 | + if departUpdate.CompanyId != param.CompanyID { |
| 35 | + e := fmt.Errorf("depart.CompanyId(%d) !=param.CompanyID(%d)", departUpdate.CompanyId, param.CompanyID) | ||
| 39 | log.Error(e.Error()) | 36 | log.Error(e.Error()) |
| 40 | - return result, protocol.NewErrWithMessage("1", e) | 37 | + return protocol.NewErrWithMessage("1", e) |
| 41 | } | 38 | } |
| 42 | //确认部门主管变更情况 | 39 | //确认部门主管变更情况 |
| 43 | var ( | 40 | var ( |
| 44 | oldmanage []int64 | 41 | oldmanage []int64 |
| 45 | ) | 42 | ) |
| 46 | - oldmanage = depart.GetManagesIds() | 43 | + oldmanage = departUpdate.GetManagesIds() |
| 47 | diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage) | 44 | diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage) |
| 48 | for i := range diffmanage { | 45 | for i := range diffmanage { |
| 49 | _, err = models.GetUserCompanyBy(diffmanage[i], param.CompanyID) | 46 | _, err = models.GetUserCompanyBy(diffmanage[i], param.CompanyID) |
| 50 | if err != nil { | 47 | if err != nil { |
| 51 | e := fmt.Errorf("GetUserCompanyBy(%d,%d) err:%s", diffmanage[i], param.CompanyID, err) | 48 | e := fmt.Errorf("GetUserCompanyBy(%d,%d) err:%s", diffmanage[i], param.CompanyID, err) |
| 52 | log.Error(e.Error()) | 49 | log.Error(e.Error()) |
| 53 | - return result, protocol.NewErrWithMessage("1", e) | 50 | + return protocol.NewErrWithMessage("1", e) |
| 54 | } | 51 | } |
| 55 | } | 52 | } |
| 56 | //处理部门上级发生变化的情况 | 53 | //处理部门上级发生变化的情况 |
| 57 | - if depart.ParentId != param.ParantID { | ||
| 58 | - //oldRelation := strings.Split(depart.Relation, "") | 54 | + var ( |
| 55 | + newparent *models.Department | ||
| 56 | + ) | ||
| 57 | + if departUpdate.ParentId != param.ParantID { | ||
| 58 | + newparent, err = models.GetDepartmentById(param.ParantID) | ||
| 59 | + if err != nil { | ||
| 60 | + e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ParantID, err) | ||
| 61 | + log.Error(e.Error()) | ||
| 62 | + return protocol.NewErrWithMessage("1", e) | ||
| 59 | } | 63 | } |
| 60 | - //更新部门数据 | ||
| 61 | - departmentInfoUpdate(depart, ¶m) | ||
| 62 | - return result, nil | 64 | + } |
| 65 | + departUpdate.Manages = departUpdate.ParseManagesIds(param.Managers) | ||
| 66 | + departUpdate.Name = param.Name | ||
| 67 | + departUpdate.UpdateAt = time.Now() | ||
| 68 | + err = models.UpdateDepartmentById(departUpdate, []string{"Manages", "Name", "UpdateAt"}) | ||
| 69 | + if err != nil { | ||
| 70 | + e := fmt.Errorf("UpdateDepartmentById err:%s", err) | ||
| 71 | + log.Error(e.Error()) | ||
| 72 | + return protocol.NewErrWithMessage("1", e) | ||
| 73 | + } | ||
| 74 | + //更新部门关系数据 | ||
| 75 | + err = departmentRelationUpdate(departUpdate, newparent) | ||
| 76 | + if err != nil { | ||
| 77 | + e := fmt.Errorf(" departmentRelationUpdate err:%s", err) | ||
| 78 | + log.Error(e.Error()) | ||
| 79 | + return protocol.NewErrWithMessage("1", e) | ||
| 80 | + } | ||
| 81 | + return nil | ||
| 63 | } | 82 | } |
| 64 | 83 | ||
| 65 | //DepartmentParentChange 处理部门上级发生变化的情况 | 84 | //DepartmentParentChange 处理部门上级发生变化的情况 |
| 66 | -func departmentInfoUpdate(old *models.Department, new *protocol.RequestDepartmentEdit) error { | ||
| 67 | - | 85 | +func departmentRelationUpdate(old *models.Department, newparent *models.Department) error { |
| 86 | + if newparent == nil { | ||
| 87 | + return nil | ||
| 88 | + } | ||
| 89 | + const ( | ||
| 90 | + //获取某个部门的下级部门 select ... for update | ||
| 91 | + dataSql0 string = `SELECT id,relation FROM department WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE` | ||
| 92 | + dataSql2 string = `update department set relation=? where id=?` | ||
| 93 | + ) | ||
| 94 | + var ( | ||
| 95 | + departSubset []models.Department //子级部门 | ||
| 96 | + err error | ||
| 97 | + oldrelation string = old.Relation | ||
| 98 | + relationLike string = oldrelation + "%" | ||
| 99 | + newRelation string = fmt.Sprintf("%s/%d", newparent.Relation, old.Id) | ||
| 100 | + ) | ||
| 68 | o := orm.NewOrm() | 101 | o := orm.NewOrm() |
| 69 | o.Begin() | 102 | o.Begin() |
| 70 | - | 103 | + err = utils.ExcuteQueryAllWithOrmer(o, &departSubset, dataSql0, relationLike) |
| 104 | + if err != nil { | ||
| 105 | + o.Rollback() | ||
| 106 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 107 | + log.Error(e.Error()) | ||
| 108 | + return protocol.NewErrWithMessage("1", e) | ||
| 109 | + } | ||
| 110 | + for i := range departSubset { | ||
| 111 | + s := strings.TrimPrefix(departSubset[i].Relation, oldrelation) | ||
| 112 | + departSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s)) | ||
| 113 | + err := utils.ExcuteSQLWithOrmer(o, dataSql2, departSubset[i].Relation, departSubset[i].Id) | ||
| 114 | + if err != nil { | ||
| 115 | + o.Rollback() | ||
| 116 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 117 | + log.Error(e.Error()) | ||
| 118 | + return protocol.NewErrWithMessage("1", e) | ||
| 119 | + } | ||
| 120 | + } | ||
| 71 | o.Commit() | 121 | o.Commit() |
| 72 | return nil | 122 | return nil |
| 73 | } | 123 | } |
| @@ -56,6 +56,20 @@ func ExcuteQueryAllWithOrmer(o orm.Ormer, result interface{}, sqlstr string, par | @@ -56,6 +56,20 @@ func ExcuteQueryAllWithOrmer(o orm.Ormer, result interface{}, sqlstr string, par | ||
| 56 | return nil | 56 | return nil |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | +func ExcuteSQLWithOrmer(o orm.Ormer, sqlstr string, param ...interface{}) error { | ||
| 60 | + PrintLogSql(sqlstr, param...) | ||
| 61 | + var ( | ||
| 62 | + err error | ||
| 63 | + ) | ||
| 64 | + r, err := o.Raw(sqlstr, param...).Exec() | ||
| 65 | + if err != nil { | ||
| 66 | + return err | ||
| 67 | + } | ||
| 68 | + num, _ := r.RowsAffected() | ||
| 69 | + log.Debug("RowsAffected:%d", num) | ||
| 70 | + return nil | ||
| 71 | +} | ||
| 72 | + | ||
| 59 | type QueryDataByPage struct { | 73 | type QueryDataByPage struct { |
| 60 | CountSql string | 74 | CountSql string |
| 61 | DataSql string | 75 | DataSql string |
-
请 注册 或 登录 后发表评论