作者 唐旭辉

日常提交保存

@@ -28,3 +28,11 @@ func CreateOrderBaseDao(options map[string]interface{}) (*dao.OrderBaseDao, erro @@ -28,3 +28,11 @@ func CreateOrderBaseDao(options map[string]interface{}) (*dao.OrderBaseDao, erro
28 } 28 }
29 return dao.NewOrderBaseDao(transactionContext) 29 return dao.NewOrderBaseDao(transactionContext)
30 } 30 }
  31 +
  32 +func CreateUsersDao(options map[string]interface{}) (*dao.UsersDao, error) {
  33 + var transactionContext *transaction.TransactionContext
  34 + if value, ok := options["transactionContext"]; ok {
  35 + transactionContext = value.(*transaction.TransactionContext)
  36 + }
  37 + return dao.NewUsersDao(transactionContext)
  38 +}
@@ -4,6 +4,12 @@ import ( @@ -4,6 +4,12 @@ import (
4 "encoding/json" 4 "encoding/json"
5 "errors" 5 "errors"
6 "fmt" 6 "fmt"
  7 + "time"
  8 +
  9 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
  10 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
  12 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
7 ) 13 )
8 14
9 // UserDepartData 用户的部门数据 15 // UserDepartData 用户的部门数据
@@ -47,12 +53,32 @@ type EmployeeData struct { @@ -47,12 +53,32 @@ type EmployeeData struct {
47 UserPositions []UserPositionData `json:"user_positions"` 53 UserPositions []UserPositionData `json:"user_positions"`
48 } 54 }
49 55
  56 +//DeleteUserData 批量删除用户
  57 +type DeleteUserData struct {
  58 + CompanyId int64 `json:"companyId"`
  59 + Ids []int64 `json:"ids"`
  60 +}
  61 +
  62 +//ForbidAllowUserData 禁用启用用户
  63 +type ForbidAllowUserData struct {
  64 + CompanyId int64 `json:"companyId"`
  65 + Ids []int64 `json:"ids"`
  66 + Status int8 `json:"status"`
  67 +}
  68 +
  69 +//ImportEmployeeData 批量导入用户
  70 +type ImportEmployeeData struct {
  71 + Add []EmployeeData `json:"add"`
  72 + Edit []EmployeeData `json:"edit"`
  73 +}
  74 +
50 //SyncEmployeeService 同步用户数据 75 //SyncEmployeeService 同步用户数据
51 type SyncEmployeeService struct{} 76 type SyncEmployeeService struct{}
52 77
53 func (service SyncEmployeeService) DoAction(action string, byteData []byte) error { 78 func (service SyncEmployeeService) DoAction(action string, byteData []byte) error {
54 switch action { 79 switch action {
55 case "add": 80 case "add":
  81 + //添加
56 var ( 82 var (
57 data EmployeeData 83 data EmployeeData
58 err error 84 err error
@@ -61,9 +87,10 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro @@ -61,9 +87,10 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
61 if err != nil { 87 if err != nil {
62 return fmt.Errorf("数据解析失败:%s", err) 88 return fmt.Errorf("数据解析失败:%s", err)
63 } 89 }
64 - err = service.AddEmployeeData(data) 90 + err = service.addEmployeeData(data)
65 return err 91 return err
66 case "edit": 92 case "edit":
  93 + //编辑更新
67 var ( 94 var (
68 data EmployeeData 95 data EmployeeData
69 err error 96 err error
@@ -72,23 +99,195 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro @@ -72,23 +99,195 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
72 if err != nil { 99 if err != nil {
73 return fmt.Errorf("数据解析失败:%s", err) 100 return fmt.Errorf("数据解析失败:%s", err)
74 } 101 }
75 - err = service.UpdateEmployeeData(data) 102 + err = service.updateEmployeeData(data)
76 return err 103 return err
77 case "batchDelete": 104 case "batchDelete":
  105 + //批量删除
  106 + var (
  107 + err error
  108 + data DeleteUserData
  109 + )
  110 + err = json.Unmarshal(byteData, &data)
  111 + if err != nil {
  112 + return fmt.Errorf("数据解析失败:%s", err)
  113 + }
  114 + return service.deleteEmployeeData(data)
78 case "batchForbid": 115 case "batchForbid":
  116 + //启用禁用
  117 + var (
  118 + err error
  119 + data ForbidAllowUserData
  120 + )
  121 + err = json.Unmarshal(byteData, &data)
  122 + if err != nil {
  123 + return fmt.Errorf("数据解析失败:%s", err)
  124 + }
  125 + return service.updateUsersStatus(data)
  126 +
79 case "batchRemove": 127 case "batchRemove":
  128 + //移动通讯录用户部门 暂时不需要
  129 + return nil
80 case "import": 130 case "import":
  131 + //批量导入
  132 + var (
  133 + data ImportEmployeeData
  134 + err error
  135 + )
  136 + err = json.Unmarshal(byteData, &data)
  137 + if err != nil {
  138 + return fmt.Errorf("数据解析失败:%s", err)
  139 + }
  140 + // service.addEmployeeData(data.Add)
  141 + // for i := range data.Edit {
  142 + // UpdateEmployeeData(data.Edit[i])
  143 + // }
  144 + return nil
  145 +
81 default: 146 default:
82 - return errors.New("action not found") 147 + return errors.New("nothing todo")
83 } 148 }
84 return nil 149 return nil
85 } 150 }
86 151
87 -func (service SyncEmployeeService) AddEmployeeData(data EmployeeData) error { 152 +func (service SyncEmployeeService) addEmployeeData(data EmployeeData) error {
  153 + var (
  154 + transactionContext, _ = factory.CreateTransactionContext(nil)
  155 + err error
  156 + )
  157 + if err = transactionContext.StartTransaction(); err != nil {
  158 + return err
  159 + }
  160 + defer func() {
  161 + transactionContext.RollbackTransaction()
  162 + }()
  163 + var usersRepository domain.UsersRepository
  164 + if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
  165 + "transactionContext": transactionContext,
  166 + }); err != nil {
  167 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  168 + }
  169 + newUser := domain.Users{
  170 + Id: data.Id,
  171 + CompanyId: data.CompanyId,
  172 + OpenId: data.OpenId,
  173 + Name: data.Name,
  174 + Sex: data.Sex,
  175 + JobNum: data.JobNum,
  176 + Phone: data.Phone,
  177 + PrivatePhone: data.PrivatePhone,
  178 + Email: data.Email,
  179 + ExtensionNum: data.ExtensionNum,
  180 + Workspace: data.WorkSpace,
  181 + Status: data.Status,
  182 + Avatar: data.Avatar,
  183 + Remarks: data.Remarks,
  184 + ChargeStatus: data.ChargeStatus,
  185 + Permission: []domain.AdminPermissionBase{}, //权限
  186 + AccessPartners: []domain.Partner{},
  187 + }
  188 + newUser.EntryTime, _ = time.Parse("2006-01-02", data.EntryTime)
  189 + if err = usersRepository.Add(&newUser); err != nil {
  190 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  191 + }
  192 + err = transactionContext.CommitTransaction()
  193 + return err
  194 +}
88 195
89 - return nil 196 +func (service SyncEmployeeService) updateEmployeeData(data EmployeeData) error {
  197 + var (
  198 + transactionContext, _ = factory.CreateTransactionContext(nil)
  199 + err error
  200 + )
  201 + if err = transactionContext.StartTransaction(); err != nil {
  202 + return err
  203 + }
  204 + defer func() {
  205 + transactionContext.RollbackTransaction()
  206 + }()
  207 + var usersRepository domain.UsersRepository
  208 + if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
  209 + "transactionContext": transactionContext,
  210 + }); err != nil {
  211 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  212 + }
  213 + var oldUser domain.Users
  214 + oldUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{
  215 + Id: data.Id,
  216 + })
  217 + if err != nil {
  218 + return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
  219 + }
  220 + entryTime, _ := time.Parse("2006-01-02", data.EntryTime)
  221 + _ = oldUser.Update(map[string]interface{}{
  222 + "Id": data.Id,
  223 + "CompanyId": data.CompanyId,
  224 + "OpenId": data.OpenId,
  225 + "Name": data.Name,
  226 + "Sex": data.Sex,
  227 + "JobNum": data.JobNum,
  228 + "Phone": data.Phone,
  229 + "PrivatePhone": data.PrivatePhone,
  230 + "Email": data.Email,
  231 + "ExtensionNum": data.ExtensionNum,
  232 + "Workspace": data.WorkSpace,
  233 + "Status": data.Status,
  234 + "Avatar": data.Avatar,
  235 + "Remarks": data.Remarks,
  236 + "ChargeStatus": data.ChargeStatus,
  237 + "EntryTime": entryTime,
  238 + })
  239 + if err = usersRepository.Edit(&oldUser); err != nil {
  240 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  241 + }
  242 + err = transactionContext.CommitTransaction()
  243 + return err
90 } 244 }
91 245
92 -func (service SyncEmployeeService) UpdateEmployeeData(data EmployeeData) error {  
93 - return nil 246 +func (service SyncEmployeeService) deleteEmployeeData(data DeleteUserData) error {
  247 + var (
  248 + transactionContext, _ = factory.CreateTransactionContext(nil)
  249 + err error
  250 + )
  251 + if err = transactionContext.StartTransaction(); err != nil {
  252 + return err
  253 + }
  254 + defer func() {
  255 + transactionContext.RollbackTransaction()
  256 + }()
  257 + var usersRepository domain.UsersRepository
  258 + if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
  259 + "transactionContext": transactionContext,
  260 + }); err != nil {
  261 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  262 + }
  263 + if err = usersRepository.Remove(data.Ids); err != nil {
  264 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  265 + }
  266 + err = transactionContext.CommitTransaction()
  267 + return err
  268 +}
  269 +
  270 +//UpdateUsersStatus 批量更新用户的状态
  271 +func (service SyncEmployeeService) updateUsersStatus(data ForbidAllowUserData) error {
  272 + var (
  273 + transactionContext, _ = factory.CreateTransactionContext(nil)
  274 + err error
  275 + )
  276 + if err = transactionContext.StartTransaction(); err != nil {
  277 + return err
  278 + }
  279 + defer func() {
  280 + transactionContext.RollbackTransaction()
  281 + }()
  282 + var uDao *dao.UsersDao
  283 + if uDao, err = factory.CreateUsersDao(map[string]interface{}{
  284 + "transactionContext": transactionContext,
  285 + }); err != nil {
  286 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  287 + }
  288 + if err = uDao.UpdateUserStatus(data.Ids, data.Status); err != nil {
  289 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  290 + }
  291 + err = transactionContext.CommitTransaction()
  292 + return err
94 } 293 }
@@ -52,6 +52,61 @@ func (u Users) InCompany(companyid int64) bool { @@ -52,6 +52,61 @@ func (u Users) InCompany(companyid int64) bool {
52 return u.CompanyId == companyid 52 return u.CompanyId == companyid
53 } 53 }
54 54
  55 +func (u *Users) Update(m map[string]interface{}) error {
  56 + if v, ok := m["CompanyId"]; ok {
  57 + u.CompanyId = v.(int64)
  58 + }
  59 + if v, ok := m["OpenId"]; ok {
  60 + u.OpenId = v.(int64)
  61 + }
  62 + if v, ok := m["Name"]; ok {
  63 + u.Name = v.(string)
  64 + }
  65 + if v, ok := m["Sex"]; ok {
  66 + u.Sex = v.(int8)
  67 + }
  68 + if v, ok := m["JobNum"]; ok {
  69 + u.JobNum = v.(string)
  70 + }
  71 + if v, ok := m["Phone"]; ok {
  72 + u.Phone = v.(string)
  73 + }
  74 + if v, ok := m["PrivatePhone"]; ok {
  75 + u.PrivatePhone = v.(string)
  76 + }
  77 + if v, ok := m["Email"]; ok {
  78 + u.Email = v.(string)
  79 + }
  80 + if v, ok := m["ExtensionNum"]; ok {
  81 + u.ExtensionNum = v.(string)
  82 + }
  83 + if v, ok := m["EntryTime"]; ok {
  84 + u.EntryTime = v.(time.Time)
  85 + }
  86 + if v, ok := m["Workspace"]; ok {
  87 + u.Workspace = v.(string)
  88 + }
  89 + if v, ok := m["Status"]; ok {
  90 + u.Status = v.(int8)
  91 + }
  92 + if v, ok := m["Avatar"]; ok {
  93 + u.Avatar = v.(string)
  94 + }
  95 + if v, ok := m["Remarks"]; ok {
  96 + u.Remarks = v.(string)
  97 + }
  98 + if v, ok := m["ChargeStatus"]; ok {
  99 + u.ChargeStatus = v.(int8)
  100 + }
  101 + if v, ok := m["Permission"]; ok {
  102 + u.Permission = v.([]AdminPermissionBase)
  103 + }
  104 + if v, ok := m["AccessPartners"]; ok {
  105 + u.AccessPartners = v.([]Partner)
  106 + }
  107 + return nil
  108 +}
  109 +
55 type UsersFindOneQuery struct { 110 type UsersFindOneQuery struct {
56 Id int64 111 Id int64
57 Phone string 112 Phone string
@@ -3,6 +3,7 @@ package dao @@ -3,6 +3,7 @@ package dao
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 5
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
6 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" 7 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
7 ) 8 )
8 9
@@ -10,7 +11,7 @@ type UsersDao struct { @@ -10,7 +11,7 @@ type UsersDao struct {
10 transactionContext *transaction.TransactionContext 11 transactionContext *transaction.TransactionContext
11 } 12 }
12 13
13 -func NewUserDao(transactionContext *transaction.TransactionContext) (*UsersDao, error) { 14 +func NewUsersDao(transactionContext *transaction.TransactionContext) (*UsersDao, error) {
14 if transactionContext == nil { 15 if transactionContext == nil {
15 return nil, fmt.Errorf("transactionContext参数不能为nil") 16 return nil, fmt.Errorf("transactionContext参数不能为nil")
16 } else { 17 } else {
@@ -19,3 +20,13 @@ func NewUserDao(transactionContext *transaction.TransactionContext) (*UsersDao, @@ -19,3 +20,13 @@ func NewUserDao(transactionContext *transaction.TransactionContext) (*UsersDao,
19 }, nil 20 }, nil
20 } 21 }
21 } 22 }
  23 +
  24 +func (dao UsersDao) UpdateUserStatus(ids []int64, ststus int8) error {
  25 + tx := dao.transactionContext.PgTx
  26 + m := &models.Users{}
  27 + _, err := tx.Model(m).
  28 + Set("status=?", ststus).
  29 + Where("id in(?)", ids).
  30 + Update()
  31 + return err
  32 +}