作者 tangxvhui

关于总后台数据同步

  1 +package controllers
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "oppmg/common/log"
  6 + "oppmg/protocol"
  7 + "oppmg/services/platform"
  8 +)
  9 +
  10 +type PlatformController struct {
  11 + BaseController
  12 +}
  13 +
  14 +func (c PlatformController) UpdateData() {
  15 + var msg *protocol.ResponseMessage
  16 + defer func() {
  17 + c.ResposeJson(msg)
  18 + }()
  19 + var param platform.CommonProtocol
  20 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  21 + log.Error("json 解析失败 err:%s", err)
  22 + msg = protocol.BadRequestParam("1")
  23 + return
  24 + }
  25 + m, err := platform.NewPlatformAction(param.Module)
  26 + if err != nil {
  27 + msg = protocol.NewReturnResponse(nil, err)
  28 + return
  29 + }
  30 + err = m.DoAction(param.Action, []byte(param.Data))
  31 + msg = protocol.NewReturnResponse(nil, err)
  32 + return
  33 +}
@@ -110,8 +110,9 @@ func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) { @@ -110,8 +110,9 @@ func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) {
110 if x, ok := eRR.(CustomErrParse); ok { 110 if x, ok := eRR.(CustomErrParse); ok {
111 return x.ParseToMessage() 111 return x.ParseToMessage()
112 } 112 }
113 -  
114 - return NewMessage("1") 113 + msg = NewMessage("1")
  114 + msg.Errmsg = eRR.Error()
  115 + return
115 } 116 }
116 117
117 //BadRequestParam 控制层响应返回 118 //BadRequestParam 控制层响应返回
1 package routers 1 package routers
2 2
  3 +import (
  4 + "oppmg/controllers"
  5 + "oppmg/middleware"
  6 +
  7 + "github.com/astaxie/beego"
  8 +)
  9 +
3 //从公司管理平台同步数据用 10 //从公司管理平台同步数据用
4 func init() { 11 func init() {
5 - //路由 12 + nsPlatform := beego.NewNamespace("/platform",
  13 + beego.NSBefore(middleware.LogRequestData),
  14 + beego.NSRouter("/update", &controllers.PlatformController{}, "post:UpdateData"),
  15 + )
  16 + beego.AddNamespace(nsPlatform)
6 } 17 }
@@ -6,6 +6,8 @@ import ( @@ -6,6 +6,8 @@ import (
6 "fmt" 6 "fmt"
7 "oppmg/common/log" 7 "oppmg/common/log"
8 "oppmg/models" 8 "oppmg/models"
  9 + "oppmg/protocol"
  10 + "oppmg/utils"
9 "time" 11 "time"
10 12
11 "github.com/astaxie/beego/orm" 13 "github.com/astaxie/beego/orm"
@@ -67,11 +69,12 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -67,11 +69,12 @@ func AddEmployeeData(data []ModuleEmployee) error {
67 return nil 69 return nil
68 } 70 }
69 var ( 71 var (
70 - companydata *models.Company  
71 - err error  
72 - usercompanydata []*models.UserCompany  
73 - userdepartmentData []*models.UserDepartment  
74 - userpositionmentData []*models.UserPosition 72 + companydata *models.Company
  73 + err error
  74 + usercompanydata []*models.UserCompany
  75 + userdepartmentData []*models.UserDepartment
  76 + userpositionData []*models.UserPosition
  77 + nowTime = time.Now()
75 ) 78 )
76 companydata, err = models.GetCompanyByUCenter(data[0].Companyid) 79 companydata, err = models.GetCompanyByUCenter(data[0].Companyid)
77 if err != nil { 80 if err != nil {
@@ -112,24 +115,188 @@ func AddEmployeeData(data []ModuleEmployee) error { @@ -112,24 +115,188 @@ func AddEmployeeData(data []ModuleEmployee) error {
112 NickName: v.Nickname, 115 NickName: v.Nickname,
113 } 116 }
114 usercompanydata = append(usercompanydata, uc) 117 usercompanydata = append(usercompanydata, uc)
  118 + for i := range v.DepartmentId {
  119 + d := &models.UserDepartment{
  120 + CompanyId: companydata.Id,
  121 + UserCompanyId: v.Userid,
  122 + DepartmentId: v.DepartmentId[i],
  123 + CreateTime: nowTime,
  124 + }
  125 + userdepartmentData = append(userdepartmentData, d)
  126 + }
  127 + for i := range v.Position {
  128 + p := &models.UserPosition{
  129 + CompanyId: companydata.Id,
  130 + UserCompanyId: v.Userid,
  131 + PositionId: v.Position[i],
  132 + CreateAt: nowTime,
  133 + }
  134 + userpositionData = append(userpositionData, p)
  135 + }
115 } 136 }
116 o := orm.NewOrm() 137 o := orm.NewOrm()
117 o.Begin() 138 o.Begin()
118 _, err = o.InsertMulti(20, &usercompanydata) 139 _, err = o.InsertMulti(20, &usercompanydata)
119 if err != nil { 140 if err != nil {
120 - log.Error("批量插入user_company数据失败,err:%s", err) 141 + e := fmt.Errorf("插入user_companny数据失败,err:%s", err)
  142 + log.Error(e.Error())
121 o.Rollback() 143 o.Rollback()
122 - return err 144 + return e
123 } 145 }
124 //添加部门职位 146 //添加部门职位
125 - 147 + _, err = o.InsertMulti(20, &userpositionData)
  148 + if err != nil {
  149 + e := fmt.Errorf("插入user_position数据失败,err:%s", err)
  150 + log.Error(e.Error())
  151 + o.Rollback()
  152 + return e
  153 + }
  154 + _, err = o.InsertMulti(20, &userdepartmentData)
  155 + if err != nil {
  156 + e := fmt.Errorf("批量插入user_department数据失败,err:%s", err)
  157 + log.Error(e.Error())
  158 + o.Rollback()
  159 + return e
  160 + }
126 o.Commit() 161 o.Commit()
127 return nil 162 return nil
128 } 163 }
129 164
130 // UpdateEmployeeData ... 165 // UpdateEmployeeData ...
131 -func UpdateEmployeeData(data []ModuleEmployee) error { 166 +func UpdateEmployeeData(data ModuleEmployee) error {
  167 + var (
  168 + err error
  169 + ucompany *models.UserCompany
  170 + companyInfo *models.Company
  171 + userdata *models.User
  172 + )
  173 + userdata, err = models.GetUserByUCenter(data.UCenterId)
  174 + if err != nil {
  175 + e := fmt.Errorf("获取用户数据失败:%s", err)
  176 + log.Error(e.Error())
  177 + return e
  178 + }
  179 + companyInfo, err = models.GetCompanyByUCenter(data.Companyid)
  180 + if err != nil {
  181 + e := fmt.Errorf("获取公司数据失败:%s", err)
  182 + log.Error(e.Error())
  183 + return e
  184 + }
  185 + ucompany, err = models.GetUserCompanyById(data.Userid)
  186 + if err != nil {
  187 + e := fmt.Errorf("获取用户和公司的关联数据失败:%s", err)
  188 + log.Error(e.Error())
  189 + return e
  190 + }
  191 + o := orm.NewOrm()
  192 + o.Begin()
  193 + //更新用户数据
  194 + userdata.NickName = data.Nickname
  195 + userdata.Phone = data.Phone
  196 + err = models.UpdateUserById(userdata, []string{"NickName", "Phone"}, o)
  197 + if err != nil {
  198 + o.Rollback()
  199 + log.Error("UpdateUserById err:%s", err)
  200 + return errors.New("更新用户数据失败")
  201 + }
  202 + ucompany.NickName = data.Nickname
  203 + models.UpdateUserCompanyById(ucompany, []string{"NickName"}, o)
  204 + if err != nil {
  205 + o.Rollback()
  206 + log.Error("UpdateUserCompanyById err:%s", err)
  207 + return errors.New("更新用户数据失败")
  208 + }
  209 + //添加部门
  210 + err = editUserDepart(data.Userid, companyInfo.Id, data.DepartmentId, o)
  211 + if err != nil {
  212 + o.Rollback()
  213 + log.Error("editUserDepart err:%s", err)
  214 + return protocol.NewErrWithMessage("1")
  215 + }
  216 + //添加职位
  217 + err = editUserPosition(data.Userid, companyInfo.Id, data.Position, o)
  218 + if err != nil {
  219 + o.Rollback()
  220 + log.Error("editUserDepart err:%s", err)
  221 + return protocol.NewErrWithMessage("1")
  222 + }
  223 + o.Commit()
  224 + return nil
  225 +}
132 226
  227 +func editUserDepart(usercompanyid int64, companyid int64, departids []int64, o orm.Ormer) error {
  228 + var (
  229 + oldDepartIds []int64
  230 + err error
  231 + delDepartIds []int64
  232 + addDepartIds []int64
  233 + )
  234 + dataSql := `SELECT department_id FROM user_department WHERE enable_status=1 AND user_company_id=?`
  235 + dataSql2 := `UPDATE user_department SET enable_status = 2 WHERE user_company_id=? AND department_id=?`
  236 + datasql3 := `INSERT INTO user_department (user_company_id,department_id,company_id,enable_status,create_time) VALUES (?,?,?,?,?)`
  237 + err = utils.ExecuteQueryAllWithOrmer(o, &oldDepartIds, dataSql, usercompanyid)
  238 + if err != nil {
  239 + e := fmt.Errorf("Execute SQL err:%s", err)
  240 + log.Error(e.Error())
  241 + return e
  242 + }
  243 + delDepartIds = utils.ArrayInt64Diff(oldDepartIds, departids)
  244 + addDepartIds = utils.ArrayInt64Diff(departids, oldDepartIds)
  245 + for _, v := range delDepartIds {
  246 + err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
  247 + if err != nil {
  248 + e := fmt.Errorf("Execute SQL err:%s", err)
  249 + log.Error(e.Error())
  250 + return e
  251 + }
  252 + }
  253 + nowTime := time.Now().String()
  254 + for _, v := range addDepartIds {
  255 + err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
  256 + if err != nil {
  257 + e := fmt.Errorf("Execute SQL err:%s", err)
  258 + log.Error(e.Error())
  259 + return e
  260 + }
  261 + }
  262 + return nil
  263 +}
  264 +
  265 +func editUserPosition(usercompanyid int64, companyid int64, positionids []int64, o orm.Ormer) error {
  266 + var (
  267 + oldPositionIds []int64
  268 + err error
  269 + delPositionIds []int64
  270 + addPositionIds []int64
  271 + )
  272 + dataSql := `SELECT position_id FROM user_position WHERE enable_status=1 AND user_company_id=?`
  273 + dataSql2 := `UPDATE user_position SET enable_status = 2 WHERE user_company_id=? AND position_id=?`
  274 + datasql3 := `INSERT INTO user_position (user_company_id,position_id,company_id,enable_status,create_at) VALUES (?,?,?,?,?)`
  275 + err = utils.ExecuteQueryAllWithOrmer(o, &oldPositionIds, dataSql, usercompanyid)
  276 + if err != nil {
  277 + e := fmt.Errorf("Execute SQL err:%s", err)
  278 + log.Error(e.Error())
  279 + return e
  280 + }
  281 + delPositionIds = utils.ArrayInt64Diff(oldPositionIds, positionids)
  282 + addPositionIds = utils.ArrayInt64Diff(positionids, oldPositionIds)
  283 + for _, v := range delPositionIds {
  284 + err = utils.ExecuteSQLWithOrmer(o, dataSql2, usercompanyid, v)
  285 + if err != nil {
  286 + e := fmt.Errorf("Execute SQL err:%s", err)
  287 + log.Error(e.Error())
  288 + return e
  289 + }
  290 + }
  291 + nowTime := time.Now().String()
  292 + for _, v := range addPositionIds {
  293 + err = utils.ExecuteSQLWithOrmer(o, datasql3, usercompanyid, v, companyid, models.USER_POSITION_ENABLE_YES, nowTime)
  294 + if err != nil {
  295 + e := fmt.Errorf("Execute SQL err:%s", err)
  296 + log.Error(e.Error())
  297 + return e
  298 + }
  299 + }
133 return nil 300 return nil
134 } 301 }
135 302