正在显示
6 个修改的文件
包含
274 行增加
和
0 行删除
| @@ -82,6 +82,7 @@ func UpdatePositionById(m *Position, col []string, om ...orm.Ormer) (err error) | @@ -82,6 +82,7 @@ func UpdatePositionById(m *Position, col []string, om ...orm.Ormer) (err error) | ||
| 82 | if err = o.Read(&v); err == nil { | 82 | if err = o.Read(&v); err == nil { |
| 83 | var num int64 | 83 | var num int64 |
| 84 | m.UpdateAt = time.Now() | 84 | m.UpdateAt = time.Now() |
| 85 | + col = append(col, "UpdateAt") | ||
| 85 | if num, err = o.Update(m, col...); err == nil { | 86 | if num, err = o.Update(m, col...); err == nil { |
| 86 | fmt.Println("Number of records updated in database:", num) | 87 | fmt.Println("Number of records updated in database:", num) |
| 87 | } | 88 | } |
services/platform/action.go
0 → 100644
| 1 | +package platform | ||
| 2 | + | ||
| 3 | +import "errors" | ||
| 4 | + | ||
| 5 | +type PlatformAction interface { | ||
| 6 | + DoAction(string) error | ||
| 7 | +} | ||
| 8 | + | ||
| 9 | +//从主管理平台接收数据数据并处理数据 | ||
| 10 | +type CommonProtocol struct { | ||
| 11 | + Module string `json:"module"` //模块 | ||
| 12 | + Action string `json:"action"` //动作 | ||
| 13 | + Data string `json:"data"` //json数据 | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +var actionmap = map[string]PlatformAction{ | ||
| 17 | + "department": ModuleDeparmentData{}, | ||
| 18 | + "position": ModulePositionData{}, | ||
| 19 | + "emplayee": ModuleEmployee{}, | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +func NewPlatformAction(module string) (PlatformAction, error) { | ||
| 23 | + if v, ok := actionmap[module]; ok { | ||
| 24 | + return v, nil | ||
| 25 | + } | ||
| 26 | + return nil, errors.New("module cannot found") | ||
| 27 | +} |
services/platform/module_company.go
0 → 100644
| 1 | +package platform |
services/platform/module_department.go
0 → 100644
| 1 | +package platform | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "errors" | ||
| 5 | +) | ||
| 6 | + | ||
| 7 | +// ModuleDeparmentData 主管理平台发送过来的数据 | ||
| 8 | +type ModuleDeparmentData struct { | ||
| 9 | + Id int64 `json:"id"` //id | ||
| 10 | + Name string `json:"name"` //部门名称 | ||
| 11 | + ParentId int64 `json:"parent_id"` //父级id | ||
| 12 | + CompanyId int64 `json:"company_id"` | ||
| 13 | + Manages []int64 `json:"manages"` | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +var _ PlatformAction = ModuleDeparmentData{} | ||
| 17 | + | ||
| 18 | +func (m ModuleDeparmentData) DoAction(code string) error { | ||
| 19 | + switch code { | ||
| 20 | + case "edit": | ||
| 21 | + return UpdateDepartmentData(m) | ||
| 22 | + case "add": | ||
| 23 | + return AddDepartmentData(m) | ||
| 24 | + case "delete": | ||
| 25 | + return DeleteDepartmentData(m) | ||
| 26 | + default: | ||
| 27 | + return errors.New("action not found") | ||
| 28 | + } | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +//同步 部门数据 | ||
| 32 | +func UpdateDepartmentData(data ModuleDeparmentData) error { | ||
| 33 | + return nil | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +func AddDepartmentData(data ModuleDeparmentData) error { | ||
| 37 | + // var ( | ||
| 38 | + // companyinfo *models.Company | ||
| 39 | + // err error | ||
| 40 | + // ) | ||
| 41 | + // models.GetCompanyByUCenter(data.CompanyId) | ||
| 42 | + return nil | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +func DeleteDepartmentData(data ModuleDeparmentData) error { | ||
| 46 | + return nil | ||
| 47 | +} |
services/platform/module_emplayee.go
0 → 100644
| 1 | +package platform | ||
| 2 | + | ||
| 3 | +//同步 人员数据 | ||
| 4 | + | ||
| 5 | +//-------------------- | ||
| 6 | + | ||
| 7 | +type ModuleEmployee struct { | ||
| 8 | + UCenterId int64 `json:"ucenter_id"` //同一用户中心的用户id | ||
| 9 | + Nickname string `json:"nick_name"` | ||
| 10 | + Companyid int64 `json:"company_id"` // | ||
| 11 | + DepartmentId []int64 `json:"department_id"` | ||
| 12 | + Position []int64 `json:"position"` | ||
| 13 | +} | ||
| 14 | + | ||
| 15 | +var _ PlatformAction = ModuleEmployee{} | ||
| 16 | + | ||
| 17 | +func (m ModuleEmployee) DoAction(code string) error { | ||
| 18 | + return nil | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +func UpdateEmployeeData(data ModuleEmployee) error { | ||
| 22 | + return nil | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +func AddEmployeeData(data ModuleEmployee) error { | ||
| 26 | + return nil | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +func DeleteEmployeeData(data ModuleEmployee) error { | ||
| 30 | + return nil | ||
| 31 | +} |
services/platform/module_position.go
0 → 100644
| 1 | +package platform | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "errors" | ||
| 5 | + "fmt" | ||
| 6 | + "oppmg/common/log" | ||
| 7 | + "oppmg/models" | ||
| 8 | + "oppmg/utils" | ||
| 9 | + "strings" | ||
| 10 | + | ||
| 11 | + "github.com/astaxie/beego/orm" | ||
| 12 | +) | ||
| 13 | + | ||
| 14 | +type ModulePositionData struct { | ||
| 15 | + Id int64 `json:"id"` | ||
| 16 | + Name string `json:"nick_name"` | ||
| 17 | + ParentId int64 `json:"parent_id"` | ||
| 18 | + CompanyId int64 `json:"company_id"` | ||
| 19 | + Status string `json:"status"` | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +var _ PlatformAction = ModulePositionData{} | ||
| 23 | + | ||
| 24 | +func (m ModulePositionData) DoAction(code string) error { | ||
| 25 | + return nil | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +//同步职位数据 | ||
| 29 | +func UpdatePosition(data ModulePositionData) error { | ||
| 30 | + var ( | ||
| 31 | + positioninfo *models.Position | ||
| 32 | + err error | ||
| 33 | + parentPosition *models.Position | ||
| 34 | + companyinfo *models.Company | ||
| 35 | + ) | ||
| 36 | + positioninfo, err = models.GetPositionById(data.Id) | ||
| 37 | + if err != nil { | ||
| 38 | + log.Error("获取职位数据失败:%s", err) | ||
| 39 | + return errors.New("获取职位数据失败") | ||
| 40 | + } | ||
| 41 | + companyinfo, err = models.GetCompanyByUCenter(data.CompanyId) | ||
| 42 | + if err != nil { | ||
| 43 | + log.Error("获取获取公司数据失败;%s", err) | ||
| 44 | + return errors.New("获取获取公司数据失败") | ||
| 45 | + } | ||
| 46 | + //检查上级 | ||
| 47 | + if data.ParentId > 0 { | ||
| 48 | + parentPosition, err = models.GetPositionById(data.ParentId) | ||
| 49 | + if err != nil { | ||
| 50 | + log.Error("获取父级职位数据失败;%s", err) | ||
| 51 | + return errors.New("获取父级职位数据失败;") | ||
| 52 | + } | ||
| 53 | + if parentPosition.CompanyId == companyinfo.Id { | ||
| 54 | + log.Error("父级职位公司不匹配") | ||
| 55 | + return errors.New("父级职位公司不匹配") | ||
| 56 | + } | ||
| 57 | + } | ||
| 58 | + o := orm.NewOrm() | ||
| 59 | + o.Begin() | ||
| 60 | + positioninfo.Name = data.Name | ||
| 61 | + err = models.UpdatePositionById(positioninfo, []string{"Name", "UpdateAt"}, o) | ||
| 62 | + if err != nil { | ||
| 63 | + o.Rollback() | ||
| 64 | + return err | ||
| 65 | + } | ||
| 66 | + err = positionRelationUpdate(positioninfo, parentPosition, o) | ||
| 67 | + if err != nil { | ||
| 68 | + o.Rollback() | ||
| 69 | + return err | ||
| 70 | + } | ||
| 71 | + o.Commit() | ||
| 72 | + return nil | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +//positionRelationUpdate 处理部门上级发生变化的情况 | ||
| 76 | +func positionRelationUpdate(positionUpdate *models.Position, newparent *models.Position, o orm.Ormer) error { | ||
| 77 | + const ( | ||
| 78 | + //获取某个部门的下级部门 锁数据 select ... for update | ||
| 79 | + dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 FOR UPDATE` | ||
| 80 | + //更新关系树 | ||
| 81 | + dataSql2 string = `update position set relation=? where id=?` | ||
| 82 | + //更新departUpdate的parent_id | ||
| 83 | + dataSql3 string = `update position set parent_id=? where id=?` | ||
| 84 | + ) | ||
| 85 | + var ( | ||
| 86 | + positionSubset []models.Position //子级部门 | ||
| 87 | + err error | ||
| 88 | + oldRelation string = positionUpdate.Relation | ||
| 89 | + relationLike string = oldRelation + "%" | ||
| 90 | + newRelation string | ||
| 91 | + ) | ||
| 92 | + if newparent == nil || newparent.Id == 0 { | ||
| 93 | + //修改节点为顶层节点的情况 | ||
| 94 | + newparent = &models.Position{} | ||
| 95 | + newRelation = fmt.Sprintf("%d", positionUpdate.Id) | ||
| 96 | + } else { | ||
| 97 | + newRelation = fmt.Sprintf("%s/%d", newparent.Relation, positionUpdate.Id) | ||
| 98 | + } | ||
| 99 | + //修改部门的parent_id | ||
| 100 | + err = utils.ExecuteSQLWithOrmer(o, dataSql3, newparent.Id, positionUpdate.Id) | ||
| 101 | + if err != nil { | ||
| 102 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 103 | + log.Error(e.Error()) | ||
| 104 | + return errors.New("更新数据失败") | ||
| 105 | + } | ||
| 106 | + //获取部门及子级部门的relation和id | ||
| 107 | + err = utils.ExecuteQueryAllWithOrmer(o, &positionSubset, dataSql0, relationLike) | ||
| 108 | + if err != nil { | ||
| 109 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 110 | + log.Error(e.Error()) | ||
| 111 | + return errors.New("更新数据失败") | ||
| 112 | + } | ||
| 113 | + //修改部门及子级部门的relation | ||
| 114 | + for i := range positionSubset { | ||
| 115 | + //重建关系树 | ||
| 116 | + s := strings.TrimPrefix(positionSubset[i].Relation, oldRelation) | ||
| 117 | + positionSubset[i].Relation = strings.TrimSpace(fmt.Sprintf("%s%s", newRelation, s)) | ||
| 118 | + err = utils.ExecuteSQLWithOrmer(o, dataSql2, positionSubset[i].Relation, positionSubset[i].Id) | ||
| 119 | + if err != nil { | ||
| 120 | + e := fmt.Errorf("EXECUTE SQL err:%s", err) | ||
| 121 | + log.Error(e.Error()) | ||
| 122 | + return errors.New("更新数据失败") | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + return nil | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +func AddPosition(data ModulePositionData) error { | ||
| 129 | + var ( | ||
| 130 | + companyinfo *models.Company | ||
| 131 | + err error | ||
| 132 | + parentPosition *models.Position | ||
| 133 | + ) | ||
| 134 | + companyinfo, err = models.GetCompanyByUCenter(data.CompanyId) | ||
| 135 | + if err != nil { | ||
| 136 | + log.Error("获取公司数据失败:s%", err) | ||
| 137 | + return errors.New("无效公司") | ||
| 138 | + } | ||
| 139 | + if data.ParentId > 0 { | ||
| 140 | + parentPosition, err = models.GetPositionById(data.ParentId) | ||
| 141 | + if err != nil { | ||
| 142 | + log.Error("获取父级职位失败:%s", err) | ||
| 143 | + return errors.New("获取父级职位失败") | ||
| 144 | + } | ||
| 145 | + } else { | ||
| 146 | + parentPosition = &models.Position{} | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + positioninfo := &models.Position{ | ||
| 150 | + Id: data.Id, | ||
| 151 | + Name: data.Name, | ||
| 152 | + ParentId: data.ParentId, | ||
| 153 | + CompanyId: companyinfo.Id, | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + positioninfo.SetRelation(parentPosition) | ||
| 157 | + _, err = models.AddPosition(positioninfo) | ||
| 158 | + if err != nil { | ||
| 159 | + log.Error("添加职位失败:%s", err) | ||
| 160 | + return errors.New("添加职位失败") | ||
| 161 | + } | ||
| 162 | + return nil | ||
| 163 | +} | ||
| 164 | +func DeletePosition(data ModulePositionData) error { | ||
| 165 | + | ||
| 166 | + return nil | ||
| 167 | +} |
-
请 注册 或 登录 后发表评论