unified_user_center.go 5.0 KB
package service

import (
	"encoding/json"
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/unifiedUserCenter/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
)

// 统一用户中心适配服务
type UnifiedUserCenterService struct {
}

// 同步企业员工回调
func (unifiedUserCenterService *UnifiedUserCenterService) SyncEmployeeCallback(syncEmployeeCallbackCommand *command.SyncEmployeeCallbackCommand) (interface{}, error) {
	if err := syncEmployeeCallbackCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var employeeRepository domain.EmployeeRepository
	if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		employeeRepository = value
	}
	var employeeDao *dao.EmployeeDao
	if value, err := factory.CreateEmployeeDao(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	} else {
		employeeDao = value
	}
	var companyId int64
	var uid int64
	var employeeName string
	var employeeAccount string
	var employeeAvatarUrl string
	var isPrincipal bool
	var status int
	var uids []int64
	var data map[string]interface{}
	if err := json.Unmarshal([]byte(syncEmployeeCallbackCommand.Data), &data); err != nil {
		return false, err
	}
	if value, ok := data["company_id"]; ok {
		companyId = int64(value.(float64))
	}
	if value, ok := data["id"]; ok {
		uid = int64(value.(float64))
	}
	if value, ok := data["name"]; ok {
		employeeName = value.(string)
	}
	if value, ok := data["phone"]; ok {
		employeeAccount = value.(string)
	}
	if value, ok := data["avatar"]; ok {
		employeeAvatarUrl = value.(string)
	}
	if value, ok := data["admin_type"]; ok {
		if int(value.(float64)) == 2 {
			isPrincipal = true
		} else {
			isPrincipal = false
		}
	}
	if value, ok := data["status"]; ok {
		status = int(value.(float64))
	}
	if value, ok := data["ids"]; ok {
		for _, uid := range value.([]interface{}) {
			uids = append(uids, int64(uid.(float64)))
		}
	}
	if syncEmployeeCallbackCommand.Module == "employee" {
		switch syncEmployeeCallbackCommand.Action {
		case "add":
			employee := &domain.Employee{
				CompanyId: companyId,
				EmployeeInfo: &domain.EmployeeInfo{
					Uid:               uid,
					EmployeeName:      employeeName,
					EmployeeAccount:   employeeAccount,
					EmployeeAvatarUrl: employeeAvatarUrl,
					IsPrincipal:       isPrincipal,
				},
				Status:  status,
				SuMoney: 0,
			}
			if _, err := employeeRepository.Save(employee); err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
			break
		case "edit":
			employee, err := employeeRepository.FindOne(map[string]interface{}{"uid": uid})
			if err != nil {
				return false, nil
			}
			if employee == nil {
				return false, nil
			}
			updateData := make(map[string]interface{})
			updateData["employeeName"] = employeeName
			updateData["employeeAccount"] = employeeAccount
			updateData["employeeAvatarUrl"] = employeeAvatarUrl
			updateData["isPrincipal"] = isPrincipal
			updateData["status"] = status
			if err := employee.Update(updateData); err != nil {
				return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
			}
			if _, err := employeeRepository.Save(employee); err != nil {
				return false, nil
			}
			break
		case "batchDelete":
			err := employeeDao.BatchRemove(uids)
			if err != nil {
				return false, nil
			}
			break
		case "batchForbid":
			err := employeeDao.BatchSetStatus(uids, status)
			if err != nil {
				return false, nil
			}
			break
		default:
			return false, nil
		}
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return true, nil
	} else if syncEmployeeCallbackCommand.Module == "company" {
		switch syncEmployeeCallbackCommand.Action {
		case "changeAdmin":
			err := employeeDao.ChangePrincipal(companyId, employeeAccount)
			if err != nil {
				return false, nil
			}
			break
		default:
			return false, nil
		}
		return true, nil
	} else {
		return false, nil
	}
}

func NewUnifiedUserCenterService(options map[string]interface{}) *UnifiedUserCenterService {
	newUnifiedUserCenterService := &UnifiedUserCenterService{}
	return newUnifiedUserCenterService
}