emp.js 5.16 KB
/*
 * @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;