审查视图

pkg/application/department/sync_data_service.go 7.6 KB
tangxvhui authored
1 2
package department
tangxvhui authored
3
import (
tangxvhui authored
4
	"encoding/json"
tangxvhui authored
5 6 7 8 9 10 11 12
	"time"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/department/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
tangxvhui authored
13 14
type SyncDataDepartmentService struct{}
tangxvhui authored
15 16 17 18 19 20
// type BusinessAdminCommand struct {
// 	// department:部门
// 	Module string `json:"module"`
// 	// add:添加,edit:编辑,batchDelete:批量删除,import:导入部门
// 	Action string `json:"action"`
// 	// 具体的对象JSON数据
庄敏学 authored
21
// 	Data json.RawMessage `json:"data"`
tangxvhui authored
22
// }
tangxvhui authored
23
tangxvhui authored
24
func (srv SyncDataDepartmentService) FromBusinessAdmin(param *domain.MessageBody) error {
tangxvhui authored
25 26 27 28 29
	action := param.Module + "/" + param.Action
	var err error
	switch action {
	case "department/add":
		var param1 command.AddDepartmentCommand
庄敏学 authored
30
		err = json.Unmarshal(param.Data, &param1)
tangxvhui authored
31 32 33 34 35 36
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.addDepartment(&param1)
	case "department/edit":
		var param1 command.EditDepartmentCommand
庄敏学 authored
37
		err = json.Unmarshal(param.Data, &param1)
tangxvhui authored
38 39 40 41 42 43
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.editDepartment(&param1)
	case "department/batchDelete":
		var param1 command.BatchDeleteCommand
庄敏学 authored
44
		err = json.Unmarshal(param.Data, &param1)
tangxvhui authored
45 46 47 48 49 50
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.batchDeleteDepartment(&param1)
	case "department/import":
		var param1 []command.ImportDepartmentCommand
庄敏学 authored
51
		err = json.Unmarshal(param.Data, &param1)
tangxvhui authored
52 53 54 55 56 57 58 59
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.importDepartment(param1)
	}
	return err
}
庄敏学 authored
60 61 62
// AddDepartment
// 从BusinessAdmins 接收消息 添加部门
// module="department" action="add"
tangxvhui authored
63
func (srv SyncDataDepartmentService) addDepartment(param *command.AddDepartmentCommand) error {
tangxvhui authored
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	nowTime := time.Now()
	newDepartment := domain.Department{
		Id:            param.Id,
		CompanyId:     param.CompanyId,
		Level:         param.Level,
		Name:          param.Name,
		ParentId:      param.ParentId,
		ChargeUserIds: param.ChargeUserIds,
		Path:          param.Path,
庄敏学 authored
84 85 86
		CreatedAt:     nowTime,
		UpdatedAt:     nowTime,
		DeletedAt:     nil,
tangxvhui authored
87 88 89 90 91 92 93 94 95 96 97 98
	}

	departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, err = departmentRepo.Insert(&newDepartment)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
tangxvhui authored
99 100 101
	return nil
}
庄敏学 authored
102 103 104
// EditDepartment
// 从BusinessAdmins 接收消息 编辑部门
// module="department" action="edit"
tangxvhui authored
105
func (srv SyncDataDepartmentService) editDepartment(param *command.EditDepartmentCommand) error {
tangxvhui authored
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	departmentIds := []int64{param.Id}
	for _, v := range param.ChangeDepartmentLists {
		departmentIds = append(departmentIds, v.Id)
	}
	departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, departmentList, err := departmentRepo.Find(map[string]interface{}{
		"ids": departmentIds,
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	for i := range departmentList {
		if departmentList[i].Id == param.Id {
			departmentList[i].CompanyId = param.CompanyId
			departmentList[i].ParentId = param.ParentId
庄敏学 authored
134 135 136 137 138 139 140 141 142
			if param.Name != "" {
				departmentList[i].Name = param.Name
			}
			if param.Path != "" {
				departmentList[i].Path = param.Path
			}
			if param.Level > 0 {
				departmentList[i].Level = param.Level
			}
庄敏学 authored
143 144
			if len(param.ChargeUserIds) > 0 {
				departmentList[i].ChargeUserIds = param.ChargeUserIds
庄敏学 authored
145 146
			} else {
				departmentList[i].ChargeUserIds = make([]int64, 0)
庄敏学 authored
147
			}
tangxvhui authored
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
			continue
		}

		for _, vv := range param.ChangeDepartmentLists {
			if vv.Id == departmentList[i].Id {
				departmentList[i].Path = vv.Path
				departmentList[i].Level = vv.Level
				break
			}
		}
	}
	for i := range departmentList {
		_, err := departmentRepo.Update(departmentList[i])
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
tangxvhui authored
168 169 170
	return nil
}
庄敏学 authored
171 172 173
// batchDelete
// 从BusinessAdmins 接收消息 删除部门
// module="department" action="batchDelete"
tangxvhui authored
174
func (srv SyncDataDepartmentService) batchDeleteDepartment(param *command.BatchDeleteCommand) error {
tangxvhui authored
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	if len(param.Ids) == 0 {
		return nil
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	err = departmentRepo.Remove(param.Ids)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
tangxvhui authored
198 199 200
	return nil
}
庄敏学 authored
201 202 203
// importDepartment
// 从BusinessAdmins 接收消息 导入部门数据
// module="department" action="import"
tangxvhui authored
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
func (srv SyncDataDepartmentService) importDepartment(param []command.ImportDepartmentCommand) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	nowTime := time.Now()
	for i := range param {
		newDepartment := domain.Department{
			Id:            param[i].Id,
			CompanyId:     param[i].CompanyId,
			Level:         param[i].Level,
			Name:          param[i].Name,
			ParentId:      param[i].ParentId,
			ChargeUserIds: []int64{},
			Path:          param[i].Path,
庄敏学 authored
228 229 230
			CreatedAt:     nowTime,
			UpdatedAt:     nowTime,
			DeletedAt:     nil,
tangxvhui authored
231 232 233 234 235 236 237 238 239
		}
		_, err = departmentRepo.Insert(&newDepartment)
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
tangxvhui authored
240 241
	return nil
}