emp.js
5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-02-17 15:16:49
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-06-09 11:14:13
* @FilePath: \achievements-system\app\controller\emp.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
"use strict";
const Controller = require("egg").Controller;
const StatusCode = require("../utils/status-code");
class empController extends Controller {
// 查询员工列表
async index() {
const { ctx } = this;
try {
const res = await ctx.service.empService.getList(ctx.query);
ctx.body = StatusCode.SUCCESS_200("查询成功!", res);
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 导出员工列表
async downExcel() {
const { ctx } = this;
const params = ctx.query;
//两层数组,一层是行,一层是列,允许多行
let headers = [
[
{ t: "排名", f: "ranking", totalRow: true },
{ t: "员工姓名", f: "name", totalRow: true },
{ t: "部门", f: "department", totalRow: true },
{ t: "职位", f: "position", totalRow: true },
{ t: "分数", f: "score", totalRow: true },
{ t: "性别", f: "sex", totalRow: true },
],
];
// 注:f的字段名需要跟数据库的字段一致
// const data = await ctx.model.Emp.findAll({
// where: {},
// raw: true,
// });
let data = await ctx.service.empService.getList(ctx.query);
let dataList = data.rows;
console.log(dataList);
dataList = dataList.sort((a, b) => b.score - a.score);
dataList.forEach((i, k) => {
i.ranking = k + 1;
i.sex = i.sex == 0 ? "男" : i.sex == 1 ? "女" : "";
});
let res = await ctx.service.exportExcel.excelCommon(
headers,
dataList,
"员工信息"
);
// ctx.body = ctx.helper.resSuc(res); // 包一层有问题
ctx.body = res;
}
async downExcelDetail() {
const { ctx } = this;
//两层数组,一层是行,一层是列,允许多行
let headers = [
[
{ t: "指标", f: "module", totalRow: true },
{ t: "指标说明", f: "explain", totalRow: true, w: 60 },
{ t: "完成情况说明", f: "circumstances", totalRow: true, w: 25 },
{ t: "考核权重", f: "weight", totalRow: true, w: 25 },
{ t: "考核得分", f: "grade", totalRow: true, w: 25 },
],
];
// 注:f的字段名需要跟数据库的字段一致
// const data = await ctx.model.Emp.findAll({
// where: {},
// raw: true,
// });
let data = await ctx.service.empService.getList(ctx.query);
console.log(data.rows, "------------------");
let dataList = data.rows;
let res = await ctx.service.exportExcel.excelDetail(
headers,
dataList,
"员工信息"
);
ctx.body = res;
}
// 创建员工
async create() {
const { ctx } = this;
try {
let data = ctx.request.body;
let res = await ctx.model.Emp.create(data);
if (!res) return (ctx.body = StatusCode.ERROR_400("添加失败!"));
ctx.body = StatusCode.SUCCESS_200("添加成功!");
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 编辑员工
async update() {
const { ctx } = this;
const params = ctx.request.body;
// 定义编辑员工接口的请求参数规则
const ider = await ctx.model.Emp.findOne({ where: { id: params.id } });
if (!ider) {
ctx.body = StatusCode.ERROR_400("不存在该记录!");
return false;
}
try {
let res = await ctx.model.Emp.update(params, {
where: { id: params.id },
});
if (!res) return (ctx.body = StatusCode.ERROR_400("更新失败!"));
ctx.body = StatusCode.SUCCESS_200("更新成功!");
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 员工详情
async detail() {
const { ctx } = this;
try {
let { id } = ctx.request.body;
let res = await ctx.model.Emp.findOne({ where: { id } });
if (!res) return (ctx.body = StatusCode.ERROR_400("不存在该记录!"));
ctx.body = StatusCode.SUCCESS_200("操作成功!", res);
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 删除员工
async delete() {
const { ctx } = this;
try {
let { id } = ctx.request.body;
let res = await ctx.model.Emp.destroy({ where: { id: id } });
if (!res) return (ctx.body = StatusCode.ERROR_400("删除失败!"));
ctx.body = StatusCode.SUCCESS_200("删除成功!");
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
// 删除批量删除
async deleteList() {
const { ctx } = this;
try {
let delRow = ctx.request.body;
let res = await ctx.model.Emp.destroy({
where: { id: { [Op.in]: delRow } },
});
if (!res) return (ctx.body = StatusCode.ERROR_400("删除失败!"));
ctx.body = StatusCode.SUCCESS_200("删除成功!");
} catch (e) {
ctx.body = StatusCode.ERROR_400(e.toString());
}
}
}
module.exports = empController;