审查视图

pkg/port/beego/controllers/user_controller.go 4.5 KB
庄敏学 authored
1 2 3
package controllers

import (
郑周 authored
4
	"github.com/linmadan/egglib-go/core/application"
庄敏学 authored
5
	"github.com/linmadan/egglib-go/web/beego"
郑周 authored
6
	"github.com/xuri/excelize/v2"
庄敏学 authored
7
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user"
郑周 authored
8
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/adapter"
郑周 authored
9
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
庄敏学 authored
10 11
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
郑周 authored
12
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
郑周 authored
13
	"strings"
庄敏学 authored
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
)

type UserController struct {
	beego.BaseController
}

// ListUsers 搜索用户
func (controller *UserController) ListUsers() {
	listUserQuery := &query.ListUserQuery{}
	_ = controller.Unmarshal(listUserQuery)
	userAuth := controller.Ctx.Input.GetData(domain.UserAuth{}).(*domain.UserAuth)
	listUserQuery.CompanyId = userAuth.CompanyId
	resp, err := (&user.UserService{}).ListUsers(listUserQuery)
	controller.Response(resp, err)
}
郑周 authored
29 30 31 32 33 34 35 36 37 38 39

func (controller *UserController) ListByDepartment() {
	in := &query.ListByDepartmentQuery{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = ua.CompanyId
		controller.Response((&user.UserService{}).ListByDepartment(in))
	}
}
郑周 authored
40 41 42 43 44 45 46 47 48 49 50 51

func (controller *UserController) EditParentUser() {
	in := &command.EditParentCommand{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = int(ua.CompanyId)
		in.OperatorId = int(ua.UserId)
		controller.Response(nil, (&user.UserService{}).EditParentUser(in))
	}
}
郑周 authored
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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

// ImportParentUser 导入用户上级
func (controller *UserController) ImportParentUser() {
	in := &command.ImportParentUserCommand{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		if itcArray, err := controller.readExcelFormUserParent(); err != nil {
			controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
		} else {
			ua := middlewares.GetUser(controller.Ctx)
			in.CompanyId = int(ua.CompanyId)
			in.OperatorId = int(ua.UserId)
			in.Data = itcArray
			if data, err := (&user.UserService{}).ImportParentUser(in); nil != err {
				controller.Response(nil, err)
			} else {
				controller.Response(data, nil)
			}
		}
	}
}

func (controller *UserController) readExcelFormUserParent() ([]adapter.ImportParentUser, error) {
	// 读取文件
	_, header, err := controller.GetFile("file")
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
		return nil, err
	}
	file, err := header.Open()
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
		return nil, err
	}
	defer func() {
		if err := file.Close(); err != nil {
			return
		}
	}()

	reader, err := excelize.OpenReader(file)
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
		return nil, err
	}

	index := reader.GetActiveSheetIndex()
	rows, err := reader.GetRows(reader.GetSheetName(index))
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "读取excel错误:"+err.Error()))
		return nil, err
	}
	if len(rows) <= 0 {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "文件内数据不能为空:"+err.Error())
	}

	adapters := make([]adapter.ImportParentUser, 0)
	for rowIndex, row := range rows {
		if rowIndex < 2 { // 头2行不读取
			continue
		}
		ipu := adapter.ImportParentUser{}
		for colIndex, colCell := range row {
			switch colIndex {
			case 0:
				ipu.Name = strings.TrimSpace(colCell) // 员工名称
			case 1:
				ipu.Phone = strings.TrimSpace(colCell) // 员工手机号码
			case 2:
				ipu.ParentName = strings.TrimSpace(colCell) // 直接上级名称
			case 3:
				ipu.ParentPhone = strings.TrimSpace(colCell) // 直接上级手机号码
			}
		}
		adapters = append(adapters, ipu)
	}
	if len(adapters) <= 0 {
		return adapters, application.ThrowError(application.INTERNAL_SERVER_ERROR, "文件内数据不能为空:"+err.Error())
	}
	return adapters, err
}