作者 tangxvhui

与企业后台对接相关

... ... @@ -26,13 +26,17 @@ func (c PlatformController) UpdateData() {
log.Error("json 解析失败 err:%s", err)
return
}
// m, err := platform.NewPlatformAction(param.Module)
// if err != nil {
// msg = protocol.NewReturnResponse(nil, err)
// return
// }
// err = m.DoAction(param.Action, []byte(param.Data))
m, err := platform.NewPlatformAction(param.Module)
if err != nil {
log.Error("同步数据发生错误,err%s", err)
return
}
bt, err := json.Marshal(param.Data)
if err != nil {
log.Error("解析param.data数据失败")
}
err = m.DoAction(param.Action, bt)
log.Error("同步数据发生错误,module=%s,action=%s,err%s", param.Module, param.Action, err)
return
}
... ...
... ... @@ -19,6 +19,22 @@ type UserCompany struct {
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
Enable int8 `orm:"column(enable)"`
DeleteAt time.Time `orm:"column(delete_at)"`
OpenId int64 `orm:"column(open_id)" description:"统一用户中心uid"`
Sex int8 `orm:"column(sex)" description:"性别:0保密 1男 2女"`
JobNum string `orm:"column(job_num);size(255)" description:"员工工号"`
Phone string `orm:"column(phone);size(11)" description:"用户手机,登录手机号"`
PrivatePhone string `orm:"column(private_phone);size(11)" description:"私人手机"`
Email string `orm:"column(email);size(255)" description:"邮箱"`
ExtensionNum string `orm:"column(extension_num);size(255)" description:"分机号"`
EntryTime time.Time `orm:"column(entry_time);type(date);null" description:"入职时间"`
Workspace string `orm:"column(workspace);size(255)" description:"工作地点"`
IsBusiness int8 `orm:"column(is_business)" description:"是否开启业务状态 1是 0否"`
//Status int8 `orm:"column(status)" description:"状态 1正常 2禁用"`
Avatar string `orm:"column(avatar);size(255)" description:"头像"`
Remarks string `orm:"column(remarks);size(255)" description:"备注"`
AdminType int8 `orm:"column(admin_type)" description:"1普通用户 2主管理员"`
ChargeStatus int8 `orm:"column(charge_status)" description:"是否为当前公司主管 1 是2 否"`
ExtraText string `orm:"column(extra_text);null" description:"自定义参数数据"`
}
func (t *UserCompany) TableName() string {
... ...
... ... @@ -8,8 +8,7 @@ import (
)
type UserPosition struct {
Id int64 `orm:"column(id)" description:"唯一键值"`
//UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
Id int64 `orm:"column(id)" description:"唯一键值"`
PositionId int64 `orm:"column(position_id)" description:"表position.id 职位编号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
... ...
... ... @@ -116,6 +116,7 @@ func initCompany(centerCompany protocol.CenterCompanyInfo, admininfo *models.Use
}
} else if err == orm.ErrNoRows {
newCompany = &models.Company{
Id: centerCompany.CompanyId,
Name: centerCompany.CompanyName,
AdminId: admininfo.Id,
UserCenterId: centerCompany.CompanyId,
... ...
... ... @@ -8,9 +8,9 @@ type PlatformAction interface {
//从主管理平台接收数据数据并处理数据
type CommonProtocol struct {
Module string `json:"module"` //模块
Action string `json:"action"` //动作
Data string `json:"data"` //json数据
Module string `json:"module"` //模块
Action string `json:"action"` //动作
Data interface{} `json:"data"` //json数据
}
var actionmap = map[string]PlatformAction{
... ...
... ... @@ -12,11 +12,11 @@ import (
// ModuleCompanytData 主管理平台发送过来的数据
type ModuleCompanytData struct {
Id int64 `json:"id"` //id
Name string `json:"name"` //公司名称名称
AdminId int64 `json:"admin_id"` //主管理员id
Logo string `json:"logo"`
Charge []int64 `json:"charge"`
Id int64 `json:"id"` //id
Name string `json:"name"` //公司名称名称
//AdminId int64 `json:"admin_id"` //主管理员id
Logo string `json:"logo"`
Charge []int64 `json:"charge"`
}
var _ PlatformAction = ModuleCompanytData{}
... ... @@ -89,15 +89,21 @@ func SetCompanyCharge(data ModuleCompanytData) error {
}
func UpdateCompanyData(data ModuleCompanytData) error {
// var (
// companyData *models.Company
// err error
// )
// companyData, err = models.GetCompanyByUCenter(data.Id)
// if err != nil {
// log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)
// return errors.New("获取企业数据失败")
// }
// companyData.Logo =
var (
companyData *models.Company
err error
)
companyData, err = models.GetCompanyByUCenter(data.Id)
if err != nil {
log.Error("获取企业数据失败,user_center_id:%d ,err:%s", data.Id, err)
return errors.New("获取企业数据失败")
}
companyData.Logo = data.Logo
companyData.Name = data.Name
err = models.UpdateCompanyById(companyData, []string{"Logo", "Name"})
if err != nil {
log.Error("更新公司数据失败:%s", err)
return errors.New("更新公司数据失败")
}
return nil
}
... ...
... ... @@ -7,6 +7,7 @@ import (
"oppmg/common/log"
"oppmg/models"
"oppmg/utils"
"sort"
"strings"
"time"
... ... @@ -24,6 +25,12 @@ type ModuleDeparmentData struct {
Charge []int64 `json:"charge"` //部门主管
}
type SortDepartmentByLevel []ModuleDeparmentData
func (a SortDepartmentByLevel) Len() int { return len(a) }
func (a SortDepartmentByLevel) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortDepartmentByLevel) Less(i, j int) bool { return a[i].Level < a[j].Level }
var _ PlatformAction = ModuleDeparmentData{}
//DoAction PlatformAction 的接口实现
... ... @@ -48,7 +55,8 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return AddDepartmentData(data)
datas := []ModuleDeparmentData{data}
return AddDepartmentData(datas)
case "batchDelete":
var (
err error
... ... @@ -64,6 +72,16 @@ func (m ModuleDeparmentData) DoAction(code string, jsondata []byte) error {
return fmt.Errorf("参数错误")
}
return DeleteDepartmentData(ids.Ids)
case "import":
var (
data []ModuleDeparmentData
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return AddDepartmentData(data)
default:
return errors.New("action not found")
}
... ... @@ -156,8 +174,8 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode
//重建关系树
s := strings.TrimLeft(departmentSubset[i].Relation, oldRelation)
departmentSubset[i].Relation = fmt.Sprintf("%s%s", newRelation, s)
s1 := strings.Split(departmentSubset[i].Relation, "/")
departmentSubset[i].Level = len(s1) - 1
s1 := strings.Split(departmentSubset[i].Relation, ",")
departmentSubset[i].Level = len(s1) - 2
}
//修改部门及子级部门的relation
... ... @@ -174,50 +192,63 @@ func departmentRelationUpdate(departmentUpdate models.Department, newparent mode
}
//AddDepartmentData ...
func AddDepartmentData(data ModuleDeparmentData) error {
func AddDepartmentData(data []ModuleDeparmentData) error {
if len(data) == 0 {
return nil
}
sort.Sort(SortDepartmentByLevel(data))
var (
companyinfo *models.Company
err error
err error
companyData *models.Company
topDepartment *models.Department
)
companyinfo, err = models.GetCompanyByUCenter(data.CompanyId)
companyData, err = models.GetCompanyByUCenter(data[0].CompanyId)
if err != nil {
e := fmt.Errorf("获取公司数据失败,id=%d,err:%s", data.CompanyId, err)
log.Error(e.Error())
log.Error("获取公司数据失败,user_center_id=%d,err:%s", data[0].CompanyId, err)
return errors.New("获取公司数据失败")
}
departmentData := &models.Department{
Id: data.Id,
CompanyId: companyinfo.Id,
Name: data.Name,
Manages: "[]",
Level: data.Level,
topDepartment, err = models.GetTopDepartmentByCompany(companyData.Id)
if err != nil {
log.Error("获取公司一级部门数据失败;company_id=%d,err:%s", companyData.Id, err)
return errors.New("获取公司一级部门数据失败")
}
var (
parentDepart *models.Department
)
if data.ParentId > 0 {
parentDepart, err = models.GetDepartmentByBusinessId(data.ParentId)
if err != nil {
log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", data.ParentId, err)
return errors.New("获取父级职位数据失败")
o := orm.NewOrm()
o.Begin()
for _, v := range data {
departmentData := &models.Department{
Id: v.Id,
CompanyId: companyData.Id,
Name: v.Name,
Manages: "[]",
}
departmentData.ParentId = parentDepart.Id
}
if data.ParentId == 0 {
parentDepart, err = models.GetTopDepartmentByCompany(companyinfo.Id)
if v.ParentId > 0 {
parentDepart := &models.Department{}
err = o.QueryTable(&models.Department{}).
Filter("business_department_id", v.ParentId).
Filter("delete_at", 0).
One(v)
if err != nil {
log.Error("获取父级职位数据失败,business_admin_id=%d,err:%s", v.ParentId, err)
return errors.New("获取父级职位数据失败")
}
departmentData.ParentId = parentDepart.Id
departmentData.SetRelation(parentDepart)
}
if v.ParentId == 0 {
departmentData.ParentId = topDepartment.Id
departmentData.SetRelation(topDepartment)
}
s := strings.Split(departmentData.Relation, ",")
departmentData.Level = len(s) - 2
_, err = models.AddDepartment(departmentData, o)
if err != nil {
log.Error("获取公司一级部门数据失败:company_id=%d,err:%s", companyinfo.Id, err)
return errors.New("获取公司一级部门数据失败")
e := fmt.Errorf("存储部门数据失败,err:%s", err)
o.Rollback()
log.Error(e.Error())
return errors.New("存储部门数据失败")
}
departmentData.ParentId = parentDepart.Id
}
departmentData.SetRelation(parentDepart)
_, err = models.AddDepartment(departmentData)
if err != nil {
e := fmt.Errorf("存储部门数据失败,err:%s", err)
log.Error(e.Error())
}
o.Commit()
return nil
}
... ...
... ... @@ -15,16 +15,60 @@ import (
//同步 人员数据
//--------------------
type UserDepartData struct {
Id int64 `json:"id"`
CompanyId int64 `json:"company_id"`
DepartmentId int64 `json:"department_id"`
UserId int64 `json:"user_id"`
}
type UserPositionData struct {
Id int64 `json:"id"`
CompanyId int64 `json:"company_id"`
PositionId int64 `json:"position_id"`
UserId int64 `json:"user_id"`
}
//--------------------
type ModuleEmployee struct {
Userid int64 `json:"user_id"`
UCenterId int64 `json:"ucenter_id"` //同一用户中心的用户id
Phone string `json:"phone"`
Nickname string `json:"nick_name"`
Companyid int64 `json:"company_id"` //同意用户中心的company_id
DepartmentId []int64 `json:"department_id"`
Position []int64 `json:"position"`
Id int64 `json:"id"` //用户的id,对应本地的user_company_id
OpenId int64 `json:"open_id"` //同一用户中心的用户id
Phone string `json:"phone"`
Name string `json:"name"`
Sex int8 `json:"sex"`
JobNum string `json:"job_num"`
PrivatePhone string `json:"private_phone"` //私人手机
CompanyId int64 `json:"company_id"` //总后台的company_id
Email string `json:"email"`
ExtensionNum string `json:"extension_num"` //分机号
EntryTime string `json:"entry_time"` //入职时间
WorkSpace string `json:"work_space"`
IsBusiness int8 `json:"is_business"`
Status int8 `json:"status"`
Avatar string `json:"avatar"`
ExtraText string `json:"extra_text"`
Remarks string `json:"remarks"`
AdminType int8 `json:"admin_type"`
ChargeStatus int8 `json:"charge_status"`
UserDepartments []UserDepartData `json:"user_departments"`
UserPositions []UserPositionData `json:"user_positions"`
}
type DeleteUserData struct {
CompanyId int64 `json:"company_id"`
Ids []int64 `json:"ids"`
}
type ForbidAllowUserData struct {
CompanyId int64 `json:"company_id"`
Ids []int64 `json:"ids"`
Status int8 `json:"status"`
}
type RemoveUserDepartmentData struct {
CompanyId int64 `json:"company_id"`
UserIds []int64 `json:"user_ids"`
DepartmentIds []int64 `json:"department_ids"`
}
var _ PlatformAction = ModuleEmployee{}
... ... @@ -33,34 +77,73 @@ var _ PlatformAction = ModuleEmployee{}
func (m ModuleEmployee) DoAction(code string, jsondata []byte) error {
switch code {
case "edit":
// return UpdateDepartmentData(m)
var (
data ModuleEmployee
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return UpdateEmployeeData(data)
case "add":
var (
data []ModuleEmployee
data ModuleEmployee
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return AddEmployeeData(data)
case "delete":
datas := []ModuleEmployee{data}
return AddEmployeeData(datas)
case "batchDelete":
var (
err error
ids []int64
err error
data DeleteUserData
)
err = json.Unmarshal(jsondata, &ids)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
if len(ids) == 0 {
return fmt.Errorf("参数错误")
return DeleteEmployeeData(data)
case "batchForbid":
//批量禁用、启用
var (
err error
data ForbidAllowUserData
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return ForbidAllowEmployeeData(data)
case "batchRemove":
//移动通讯录用户部门
var (
err error
data RemoveUserDepartmentData
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return DeleteEmployeeData(ids)
return RemoveUserDepartment(data)
case "import":
//导入用户
var (
data []ModuleEmployee
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return AddEmployeeData(data)
default:
return errors.New("action not found")
}
return nil
// return nil
}
// AddEmployeeData ...
... ... @@ -76,9 +159,10 @@ func AddEmployeeData(data []ModuleEmployee) error {
userpositionData []*models.UserPosition
nowTime = time.Now()
)
companydata, err = models.GetCompanyByUCenter(data[0].Companyid)
companydata, err = models.GetCompanyByUCenter(data[0].CompanyId)
if err != nil {
log.Error("GetCompanyByUCenter获取公司数据失败,ucenterId=%d,err:%s", data[0].Companyid, err)
log.Error("GetCompanyByUCenter获取公司数据失败,ucenterId=%d,err:%s", data[0].CompanyId, err)
return errors.New("获取公司数据失败")
}
... ... @@ -86,7 +170,7 @@ func AddEmployeeData(data []ModuleEmployee) error {
var (
userdata *models.User
)
userdata, err = models.GetUserByUCenter(v.UCenterId)
userdata, err = models.GetUserByUCenter(v.OpenId)
if err != nil && err != orm.ErrNoRows {
log.Error("获取用户数据失败,err:%s", err)
return errors.New("获取用户数据失败")
... ... @@ -94,8 +178,8 @@ func AddEmployeeData(data []ModuleEmployee) error {
if err == orm.ErrNoRows {
//添加用户
userdata = &models.User{
UserCenterId: v.UCenterId,
NickName: v.Nickname,
UserCenterId: v.OpenId,
NickName: v.Name,
Phone: v.Phone,
}
_, err = models.AddUser(userdata)
... ... @@ -104,31 +188,47 @@ func AddEmployeeData(data []ModuleEmployee) error {
return err
}
} else {
userdata.NickName = v.Nickname
userdata.NickName = v.Name
userdata.Phone = v.Phone
models.UpdateUserById(userdata, []string{"NickName", "Phone"})
}
uc := &models.UserCompany{
Id: v.Userid,
CompanyId: companydata.Id,
UserId: userdata.Id,
NickName: v.Nickname,
Id: v.Id,
CompanyId: companydata.Id,
UserId: userdata.Id,
NickName: v.Name,
OpenId: v.OpenId,
Sex: v.Sex,
JobNum: v.JobNum,
Phone: v.Phone,
PrivatePhone: v.PrivatePhone,
Email: v.Email,
ExtensionNum: v.ExtensionNum,
Workspace: v.WorkSpace,
IsBusiness: v.IsBusiness,
Enable: v.Status,
Avatar: v.Avatar,
Remarks: v.Remarks,
AdminType: v.AdminType,
ChargeStatus: v.ChargeStatus,
ExtraText: v.ExtraText,
}
uc.EntryTime, _ = time.ParseInLocation("2006-01-02", v.EntryTime, time.Local)
usercompanydata = append(usercompanydata, uc)
for i := range v.DepartmentId {
for i := range v.UserDepartments {
d := &models.UserDepartment{
CompanyId: companydata.Id,
UserCompanyId: v.Userid,
DepartmentId: v.DepartmentId[i],
UserCompanyId: v.UserDepartments[i].UserId,
DepartmentId: v.UserDepartments[i].DepartmentId,
CreateTime: nowTime,
}
userdepartmentData = append(userdepartmentData, d)
}
for i := range v.Position {
for i := range v.UserPositions {
p := &models.UserPosition{
CompanyId: companydata.Id,
UserCompanyId: v.Userid,
PositionId: v.Position[i],
UserCompanyId: v.UserPositions[i].UserId,
PositionId: v.UserPositions[i].PositionId,
CreateAt: nowTime,
}
userpositionData = append(userpositionData, p)
... ... @@ -143,7 +243,7 @@ func AddEmployeeData(data []ModuleEmployee) error {
o.Rollback()
return e
}
//添加部门职位
//添加用户的职位
_, err = o.InsertMulti(20, &userpositionData)
if err != nil {
e := fmt.Errorf("插入user_position数据失败,err:%s", err)
... ... @@ -151,6 +251,7 @@ func AddEmployeeData(data []ModuleEmployee) error {
o.Rollback()
return e
}
//添加用户的部门
_, err = o.InsertMulti(20, &userdepartmentData)
if err != nil {
e := fmt.Errorf("批量插入user_department数据失败,err:%s", err)
... ... @@ -170,19 +271,19 @@ func UpdateEmployeeData(data ModuleEmployee) error {
companyInfo *models.Company
userdata *models.User
)
userdata, err = models.GetUserByUCenter(data.UCenterId)
userdata, err = models.GetUserByUCenter(data.OpenId)
if err != nil {
e := fmt.Errorf("获取用户数据失败:%s", err)
log.Error(e.Error())
return e
}
companyInfo, err = models.GetCompanyByUCenter(data.Companyid)
companyInfo, err = models.GetCompanyByUCenter(data.CompanyId)
if err != nil {
e := fmt.Errorf("获取公司数据失败:%s", err)
log.Error(e.Error())
return e
}
ucompany, err = models.GetUserCompanyById(data.Userid)
ucompany, err = models.GetUserCompanyById(data.Id)
if err != nil {
e := fmt.Errorf("获取用户和公司的关联数据失败:%s", err)
log.Error(e.Error())
... ... @@ -191,30 +292,52 @@ func UpdateEmployeeData(data ModuleEmployee) error {
o := orm.NewOrm()
o.Begin()
//更新用户数据
userdata.NickName = data.Nickname
userdata.NickName = data.Name
userdata.Phone = data.Phone
err = models.UpdateUserById(userdata, []string{"NickName", "Phone"}, o)
if err != nil {
o.Rollback()
log.Error("UpdateUserById err:%s", err)
return errors.New("更新用户数据失败")
}
ucompany.NickName = data.Nickname
models.UpdateUserCompanyById(ucompany, []string{"NickName"}, o)
ucompany.NickName = data.Name
ucompany.NickName = data.Name
ucompany.OpenId = data.OpenId
ucompany.Sex = data.Sex
ucompany.JobNum = data.JobNum
ucompany.Phone = data.Phone
ucompany.PrivatePhone = data.PrivatePhone
ucompany.Email = data.Email
ucompany.ExtensionNum = data.ExtensionNum
ucompany.Workspace = data.WorkSpace
ucompany.IsBusiness = data.IsBusiness
ucompany.Enable = data.Status
ucompany.Avatar = data.Avatar
ucompany.Remarks = data.Remarks
ucompany.AdminType = data.AdminType
ucompany.ChargeStatus = data.ChargeStatus
ucompany.ExtraText = data.ExtraText
cols := []string{"NickName", "OpenId", "Sex", "JobNum",
"Phone", "PrivatePhone", "Email", "ExtensionNum", "Workspace",
"IsBusiness", "Status", "Avatar", "Remarks", "AdminType", "ChargeStatus",
"ExtraText",
}
err = models.UpdateUserCompanyById(ucompany, cols, o)
if err != nil {
o.Rollback()
log.Error("UpdateUserCompanyById err:%s", err)
return errors.New("更新用户数据失败")
}
//添加部门
err = editUserDepart(data.Userid, companyInfo.Id, data.DepartmentId, o)
err = editUserDepart(data.Id, companyInfo.Id, data.UserDepartments, o)
if err != nil {
o.Rollback()
log.Error("editUserDepart err:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加职位
err = editUserPosition(data.Userid, companyInfo.Id, data.Position, o)
err = editUserPosition(data.Id, companyInfo.Id, data.UserPositions, o)
if err != nil {
o.Rollback()
log.Error("editUserDepart err:%s", err)
... ... @@ -224,89 +347,126 @@ func UpdateEmployeeData(data ModuleEmployee) error {
return nil
}
func editUserDepart(usercompanyid int64, companyid int64, departids []int64, o orm.Ormer) error {
func editUserDepart(usercompanyid int64, companyid int64, userDepart []UserDepartData, o orm.Ormer) error {
var (
oldDepartIds []int64
newDepartIds []int64
err error
delDepartIds []int64
addDepartIds []int64
addDepart []models.UserDepartment
)
dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND user_company_id=?`
dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE user_company_id=? AND department_id=?`
datasql3 := `INSERT INTO user_department (user_company_id,department_id,company_id,enable_status,create_time) VALUES (?,?,?,?,?)`
for i := range userDepart {
newDepartIds = append(newDepartIds, userDepart[i].DepartmentId)
}
dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND user_company_id=? `
err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, usercompanyid)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids)
addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds)
for _, v := range delDepartIds {
err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
delDepartIds = utils.ArrayInt64Diff(oldDepartIds, newDepartIds)
addDepartIds = utils.ArrayInt64Diff(newDepartIds, oldDepartIds)
nowTime := time.Now()
for _, v := range addDepartIds {
m := models.UserDepartment{
UserCompanyId: usercompanyid,
DepartmentId: v,
EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
CompanyId: companyid,
CreateTime: nowTime,
}
addDepart = append(addDepart, m)
}
if len(delDepartIds) > 0 {
_, err = o.QueryTable(&models.UserDepartment{}).
Filter("id__in", delDepartIds).
Update(orm.Params{
"enable_status": models.USER_DEPARTMENT_ENABLE_NO,
})
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
log.Error("删除用户部门失败: 用户id=%d,err:%s", usercompanyid, err)
return errors.New("删除用户部门失败")
}
}
nowTime := time.Now().String()
for _, v := range addDepartIds {
err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
if len(addDepart) > 0 {
_, err = o.InsertMulti(10, addDepart)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
log.Error("添加用户的部门失败 用户id=%d,err:%s", usercompanyid, err)
return errors.New("添加用户的部门失败")
}
}
return nil
}
func editUserPosition(usercompanyid int64, companyid int64, positionids []int64, o orm.Ormer) error {
func editUserPosition(usercompanyid int64, companyid int64, userPosition []UserPositionData, o orm.Ormer) error {
var (
oldPositionIds []int64
newPositionIds []int64
err error
delPositionIds []int64
addPositionIds []int64
addPosition []models.UserPosition
)
for _, v := range userPosition {
newPositionIds = append(newPositionIds, v.PositionId)
}
dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND user_company_id=?`
dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE user_company_id=? AND position_id=?`
datasql3 := `INSERT INTO user_position (user_company_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, usercompanyid)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids)
addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds)
for _, v := range delPositionIds {
err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
delPositionIds = utils.ArrayInt64Diff(oldPositionIds, newPositionIds)
addPositionIds = utils.ArrayInt64Diff(newPositionIds, oldPositionIds)
nowTime := time.Now()
for _, v := range addPositionIds {
m := models.UserPosition{
UserCompanyId: usercompanyid,
PositionId: v,
EnableStatus: models.USER_DEPARTMENT_ENABLE_YES,
CompanyId: companyid,
CreateAt: nowTime,
}
addPosition = append(addPosition, m)
}
if len(delPositionIds) > 0 {
_, err = o.QueryTable(&models.UserPosition{}).
Filter("id__in", delPositionIds).
Update(orm.Params{
"enable_status": models.USER_POSITION_ENABLE_NO,
})
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
log.Error("删除用户职位失败: 用户id=%d,err:%s", usercompanyid, err)
return errors.New("删除用户部门失败")
}
}
nowTime := time.Now().String()
for _, v := range addPositionIds {
err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
if len(addPosition) > 0 {
_, err = o.InsertMulti(10, addPosition)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
log.Error("添加用户的职位失败 用户id=%d,err:%s", usercompanyid, err)
return errors.New("添加用户的部门失败")
}
}
return nil
}
// DeleteEmployeeData ...
func DeleteEmployeeData(ids []int64) error {
func DeleteEmployeeData(data DeleteUserData) error {
companyData, err := models.GetCompanyByUCenter(data.CompanyId)
if err != nil {
log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
return errors.New("获取公司数据失败")
}
o := orm.NewOrm()
_, err := o.QueryTable(&models.UserCompany{}).
Filter("id__in", ids).
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id__in", data.Ids).
Filter("company_id", companyData.Id).
Update(orm.Params{
"delete_at": time.Now().String(),
"delete_at": time.Now().Format("2006-01-02 15:04:05"),
"enable": models.USERCOMPANY_ENABLE_NO,
})
if err != nil {
log.Error("更新user_company数据失败:%s", err)
... ... @@ -314,3 +474,69 @@ func DeleteEmployeeData(ids []int64) error {
}
return nil
}
func ForbidAllowEmployeeData(data ForbidAllowUserData) error {
companyData, err := models.GetCompanyByUCenter(data.CompanyId)
if err != nil {
log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
return errors.New("获取公司数据失败")
}
var status int8
switch data.Status {
case 1:
status = models.USERCOMPANY_ENABLE_YES
case 2:
status = models.USERCOMPANY_ENABLE_NO
}
o := orm.NewOrm()
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id__in", data.Ids).
Filter("company_id", companyData.Id).
Update(orm.Params{
"enable": status,
"update_at": time.Now().Format("2006-01-02 15:04:05"),
})
if err != nil {
log.Error("更新user_company数据失败:%s", err)
return errors.New("删除user_company数据失败")
}
return nil
}
func RemoveUserDepartment(data RemoveUserDepartmentData) error {
var (
err error
companyData *models.Company
)
userDepartmentDatas := make(map[int64][]UserDepartData)
companyData, err = models.GetCompanyByUCenter(data.CompanyId)
if err != nil {
log.Error("h获取公司数据失败,user_center_id=%d,err:%s", data.CompanyId, err)
return errors.New("获取公司数据失败")
}
for _, v := range data.UserIds {
var userDapart []UserDepartData
for _, vv := range data.DepartmentIds {
m := UserDepartData{
CompanyId: companyData.Id,
UserId: v,
DepartmentId: vv,
}
userDapart = append(userDapart, m)
}
userDepartmentDatas[v] = userDapart
}
o := orm.NewOrm()
o.Begin()
for k, v := range userDepartmentDatas {
err = editUserDepart(k, companyData.Id, v, o)
if err != nil {
o.Rollback()
log.Error("更新用户部门失败;user_company_id=%d,err:%s", k, err)
return errors.New("更新用户部门失败")
}
}
o.Commit()
return nil
}
... ...