...
|
...
|
@@ -13,12 +13,12 @@ import ( |
|
|
|
|
|
// ModuleDeparmentData 主管理平台发送过来的数据
|
|
|
type ModuleDeparmentData struct {
|
|
|
Id int64 `json:"id"` //id
|
|
|
Name string `json:"name"` //部门名称
|
|
|
ParentId int64 `json:"parent_id"` //父级id
|
|
|
CompanyId int64 `json:"company_id"`
|
|
|
Relation string `json:"relation"`
|
|
|
Manages []int64 `json:"manages"`
|
|
|
Id int64 `json:"id"` //id
|
|
|
Name string `json:"name"` //部门名称
|
|
|
ParentId int64 `json:"parent_id"` //父级id
|
|
|
CompanyId int64 `json:"company_id"`
|
|
|
Path string `json:"path"`
|
|
|
Level int `json:"level"`
|
|
|
}
|
|
|
|
|
|
var _ PlatformAction = ModuleDeparmentData{}
|
...
|
...
|
@@ -28,7 +28,7 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error { |
|
|
switch code {
|
|
|
case "edit":
|
|
|
var (
|
|
|
data []ModuleDeparmentData
|
|
|
data ModuleDeparmentData
|
|
|
err error
|
|
|
)
|
|
|
err = json.Unmarshal(jsondata, &data)
|
...
|
...
|
@@ -38,7 +38,7 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error { |
|
|
return UpdateDepartmentData(data)
|
|
|
case "add":
|
|
|
var (
|
|
|
data []ModuleDeparmentData
|
|
|
data ModuleDeparmentData
|
|
|
err error
|
|
|
)
|
|
|
err = json.Unmarshal(jsondata, &data)
|
...
|
...
|
@@ -68,80 +68,64 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error { |
|
|
//同步 部门数据
|
|
|
|
|
|
//UpdateDepartmentData ....
|
|
|
func UpdateDepartmentData(data []ModuleDeparmentData) error {
|
|
|
func UpdateDepartmentData(data ModuleDeparmentData) error {
|
|
|
var (
|
|
|
departmentData *models.Department
|
|
|
err error
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
for _, v := range data {
|
|
|
departmentData, err = models.GetDepartmentById(v.Id)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("获取部门数据失败,err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return errors.New("获取部门数据失败")
|
|
|
}
|
|
|
departmentData.Name = v.Name
|
|
|
departmentData.ParentId = v.ParentId
|
|
|
departmentData.Relation = v.Relation
|
|
|
if bt, err := json.Marshal(v.Manages); err == nil {
|
|
|
departmentData.Manages = string(bt)
|
|
|
}
|
|
|
err = models.UpdateDepartmentById(departmentData, []string{"Name", "Manage", "ParentId", "Relation"}, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
e := fmt.Errorf("更新部门数据失败,err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return errors.New("更新部门数据失败")
|
|
|
}
|
|
|
|
|
|
departmentData, err = models.GetDepartmentById(data.Id)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("获取部门数据失败,err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return errors.New("获取部门数据失败")
|
|
|
}
|
|
|
departmentData.Name = data.Name
|
|
|
departmentData.ParentId = data.ParentId
|
|
|
departmentData.Relation = data.Path
|
|
|
departmentData.Level = data.Level
|
|
|
|
|
|
err = models.UpdateDepartmentById(departmentData, []string{"Name", "Manage", "ParentId", "Relation"})
|
|
|
if err != nil {
|
|
|
|
|
|
e := fmt.Errorf("更新部门数据失败,err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
return errors.New("更新部门数据失败")
|
|
|
}
|
|
|
o.Commit()
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
//AddDepartmentData ...
|
|
|
func AddDepartmentData(data []ModuleDeparmentData) error {
|
|
|
if len(data) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
func AddDepartmentData(data ModuleDeparmentData) error {
|
|
|
var (
|
|
|
companyinfo *models.Company
|
|
|
err error
|
|
|
)
|
|
|
companyinfo, err = models.GetCompanyByUCenter(data[0].CompanyId)
|
|
|
companyinfo, err = models.GetCompanyByUCenter(data.CompanyId)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("获取公司数据失败,err:%s", err)
|
|
|
e := fmt.Errorf("获取公司数据失败,id=%d,err:%s", data.CompanyId, err)
|
|
|
log.Error(e.Error())
|
|
|
return errors.New("获取公司数据失败")
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
var isRollback bool
|
|
|
for _, v := range data {
|
|
|
departmentData := &models.Department{
|
|
|
Id: v.Id,
|
|
|
CompanyId: companyinfo.Id,
|
|
|
Name: v.Name,
|
|
|
ParentId: v.ParentId,
|
|
|
Manages: "[]",
|
|
|
Relation: v.Relation, //TODO 格式转化
|
|
|
}
|
|
|
if bt, err := json.Marshal(v.Manages); err == nil {
|
|
|
departmentData.Manages = string(bt)
|
|
|
}
|
|
|
_, err = models.AddDepartment(departmentData, o)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("存储部门数据失败,err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
isRollback = true
|
|
|
}
|
|
|
|
|
|
departmentData := &models.Department{
|
|
|
Id: data.Id,
|
|
|
CompanyId: companyinfo.Id,
|
|
|
Name: data.Name,
|
|
|
ParentId: data.ParentId,
|
|
|
Manages: "[]",
|
|
|
Relation: data.Path, //TODO 格式转化
|
|
|
Level: data.Level,
|
|
|
}
|
|
|
if isRollback {
|
|
|
o.Rollback()
|
|
|
return errors.New("存储部门数据失败")
|
|
|
|
|
|
_, err = models.AddDepartment(departmentData)
|
|
|
if err != nil {
|
|
|
e := fmt.Errorf("存储部门数据失败,err:%s", err)
|
|
|
log.Error(e.Error())
|
|
|
|
|
|
}
|
|
|
o.Commit()
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
...
|
...
|
|