作者 tangxvhui

同步数据 编辑公司

@@ -18,6 +18,7 @@ type Company struct { @@ -18,6 +18,7 @@ type Company struct {
18 Enable int8 `orm:"column(enable)"` 18 Enable int8 `orm:"column(enable)"`
19 UserCenterId int64 `orm:"column(user_center_id)"` 19 UserCenterId int64 `orm:"column(user_center_id)"`
20 Remark string `orm:"column(remark)"` 20 Remark string `orm:"column(remark)"`
  21 + UserCompanyId int64 `orm:"column(user_company_id)"`
21 } 22 }
22 23
23 func (t *Company) TableName() string { 24 func (t *Company) TableName() string {
@@ -116,20 +116,57 @@ func UpdateCompanyData(data ModuleCompanytData) error { @@ -116,20 +116,57 @@ func UpdateCompanyData(data ModuleCompanytData) error {
116 companyData *models.Company 116 companyData *models.Company
117 err error 117 err error
118 ) 118 )
119 - companyBaseData := data.Company  
120 - companyData, err = models.GetCompanyByUCenter(companyBaseData.AdminCompanyId) 119 + newCompanyData := data.Company
  120 + companyData, err = models.GetCompanyByUCenter(newCompanyData.AdminCompanyId)
121 if err != nil { 121 if err != nil {
122 - log.Error("获取企业数据失败,user_center_id:%d ,err:%s", companyBaseData.AdminCompanyId, err) 122 + log.Error("获取企业数据失败,user_center_id:%d ,err:%s", newCompanyData.AdminCompanyId, err)
123 return errors.New("获取企业数据失败") 123 return errors.New("获取企业数据失败")
124 } 124 }
125 - companyData.Logo = companyBaseData.Logo  
126 - companyData.Name = companyBaseData.Name  
127 - companyData.Remark = companyBaseData.Remarks  
128 - err = models.UpdateCompanyById(companyData, []string{"Logo", "Name"}) 125 + var (
  126 + oldAdminData *models.UserCompany
  127 + )
  128 + oldAdminData, err = models.GetUserCompanyBy(companyData.AdminId, companyData.Id)
  129 + if err != nil {
  130 + log.Error("获取公司主管理员user_company数据失败,company_id=%d,user_id=%d,err:%s", companyData.Id, companyData.AdminId, err)
  131 + return errors.New("获取公司主管理员数据失败")
  132 + }
  133 + o := orm.NewOrm()
  134 + o.Begin()
  135 + if data.User.Id != oldAdminData.Id {
  136 + //更新公司主管理员
  137 + oldAdminData.ChargeStatus = models.USERCOMPANY_CHARGE_NO
  138 + err = models.UpdateUserCompanyById(oldAdminData, []string{"ChargeStatus"}, o)
  139 + if err != nil {
  140 + o.Rollback()
  141 + log.Error("去除旧的主管理员失败:user_company_id=%d,err:%s", oldAdminData.Id, err)
  142 + return errors.New("去除旧的更主管理员失败")
  143 + }
  144 + var newAdminData *models.UserCompany
  145 + newAdminData, err = models.GetUserCompanyById(data.User.Id)
129 if err != nil { 146 if err != nil {
  147 + o.Rollback()
  148 + log.Error("获取新的主管理员失败,user_company_id=%d,err:%s", data.User.Id, err)
  149 + return errors.New("获取新的主管理员")
  150 + }
  151 + newAdminData.ChargeStatus = models.USERCOMPANY_CHARGE_YES
  152 + err = models.UpdateUserCompanyById(newAdminData, []string{"ChargeStatus"}, o)
  153 + if err != nil {
  154 + o.Rollback()
  155 + log.Error("变更更主管理员失败:user_company_id=%d,err:%s", newAdminData.Id, err)
  156 + return errors.New("变更主管理员失败")
  157 + }
  158 + companyData.AdminId = newAdminData.UserId
  159 + }
  160 + companyData.Logo = newCompanyData.Logo
  161 + companyData.Name = newCompanyData.Name
  162 + companyData.Remark = newCompanyData.Remarks
  163 + err = models.UpdateCompanyById(companyData, []string{"Logo", "Name", "AdminId"}, o)
  164 + if err != nil {
  165 + o.Rollback()
130 log.Error("更新公司数据失败:%s", err) 166 log.Error("更新公司数据失败:%s", err)
131 return errors.New("更新公司数据失败") 167 return errors.New("更新公司数据失败")
132 } 168 }
  169 + o.Commit()
133 return nil 170 return nil
134 } 171 }
135 172