作者 tangxvhui

关于总后台数据同步

package controllers
import (
"encoding/json"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/services/platform"
)
type PlatformController struct {
BaseController
}
func (c PlatformController) UpdateData() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param platform.CommonProtocol
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
m, err := platform.NewPlatformAction(param.Module)
if err != nil {
msg = protocol.NewReturnResponse(nil, err)
return
}
err = m.DoAction(param.Action, []byte(param.Data))
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
... ... @@ -110,8 +110,9 @@ func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) {
if x, ok := eRR.(CustomErrParse); ok {
return x.ParseToMessage()
}
return NewMessage("1")
msg = NewMessage("1")
msg.Errmsg = eRR.Error()
return
}
//BadRequestParam 控制层响应返回
... ...
package routers
import (
"oppmg/controllers"
"oppmg/middleware"
"github.com/astaxie/beego"
)
//从公司管理平台同步数据用
func init() {
//路由
nsPlatform := beego.NewNamespace("/platform",
beego.NSBefore(middleware.LogRequestData),
beego.NSRouter("/update", &controllers.PlatformController{}, "post:UpdateData"),
)
beego.AddNamespace(nsPlatform)
}
... ...
... ... @@ -6,6 +6,8 @@ import (
"fmt"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"time"
"github.com/astaxie/beego/orm"
... ... @@ -71,7 +73,8 @@ func AddEmployeeData(data []ModuleEmployee) error {
err error
usercompanydata []*models.UserCompany
userdepartmentData []*models.UserDepartment
userpositionmentData []*models.UserPosition
userpositionData []*models.UserPosition
nowTime = time.Now()
)
companydata, err = models.GetCompanyByUCenter(data[0].Companyid)
if err != nil {
... ... @@ -112,24 +115,188 @@ func AddEmployeeData(data []ModuleEmployee) error {
NickName: v.Nickname,
}
usercompanydata = append(usercompanydata, uc)
for i := range v.DepartmentId {
d := &models.UserDepartment{
CompanyId: companydata.Id,
UserCompanyId: v.Userid,
DepartmentId: v.DepartmentId[i],
CreateTime: nowTime,
}
userdepartmentData = append(userdepartmentData, d)
}
for i := range v.Position {
p := &models.UserPosition{
CompanyId: companydata.Id,
UserCompanyId: v.Userid,
PositionId: v.Position[i],
CreateAt: nowTime,
}
userpositionData = append(userpositionData, p)
}
}
o := orm.NewOrm()
o.Begin()
_, err = o.InsertMulti(20, &usercompanydata)
if err != nil {
log.Error("批量插入user_company数据失败,err:%s", err)
e := fmt.Errorf("插入user_companny数据失败,err:%s", err)
log.Error(e.Error())
o.Rollback()
return err
return e
}
//添加部门职位
_, err = o.InsertMulti(20, &userpositionData)
if err != nil {
e := fmt.Errorf("插入user_position数据失败,err:%s", err)
log.Error(e.Error())
o.Rollback()
return e
}
_, err = o.InsertMulti(20, &userdepartmentData)
if err != nil {
e := fmt.Errorf("批量插入user_department数据失败,err:%s", err)
log.Error(e.Error())
o.Rollback()
return e
}
o.Commit()
return nil
}
// UpdateEmployeeData ...
func UpdateEmployeeData(data []ModuleEmployee) error {
func UpdateEmployeeData(data ModuleEmployee) error {
var (
err error
ucompany *models.UserCompany
companyInfo *models.Company
userdata *models.User
)
userdata, err = models.GetUserByUCenter(data.UCenterId)
if err != nil {
e := fmt.Errorf("获取用户数据失败:%s", err)
log.Error(e.Error())
return e
}
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)
if err != nil {
e := fmt.Errorf("获取用户和公司的关联数据失败:%s", err)
log.Error(e.Error())
return e
}
o := orm.NewOrm()
o.Begin()
//更新用户数据
userdata.NickName = data.Nickname
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)
if err != nil {
o.Rollback()
log.Error("UpdateUserCompanyById err:%s", err)
return errors.New("更新用户数据失败")
}
//添加部门
err = editUserDepart(data.Userid, companyInfo.Id, data.DepartmentId, 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)
if err != nil {
o.Rollback()
log.Error("editUserDepart err:%s", err)
return protocol.NewErrWithMessage("1")
}
o.Commit()
return nil
}
func editUserDepart(usercompanyid int64, companyid int64, departids []int64, o orm.Ormer) error {
var (
oldDepartIds []int64
err error
delDepartIds []int64
addDepartIds []int64
)
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 (?,?,?,?,?)`
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)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
}
nowTime := time.Now().String()
for _, v := range addDepartIds {
err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
}
return nil
}
func editUserPosition(usercompanyid int64, companyid int64, positionids []int64, o orm.Ormer) error {
var (
oldPositionIds []int64
err error
delPositionIds []int64
addPositionIds []int64
)
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)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
}
nowTime := time.Now().String()
for _, v := range addPositionIds {
err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
if err != nil {
e := fmt.Errorf("Execute SQL err:%s", err)
log.Error(e.Error())
return e
}
}
return nil
}
... ...