RosteringController.java 3.2 KB
package com.pipihelper.project.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.pipihelper.project.core.Result;
import com.pipihelper.project.core.ResultGenerator;
import com.pipihelper.project.rostering.model.DateRuleModel;
import com.pipihelper.project.rostering.model.RosteringExportModel;
import com.pipihelper.project.rostering.model.RosteringModel;
import com.pipihelper.project.rostering.model.ShiftRuleModel;
import com.pipihelper.project.rostering.model.StaffRuleModel;
import com.pipihelper.project.rostering.service.RosteringService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: zsw
 * @create: 2022-10-15 20:01
 **/
@RestController
@RequestMapping("/api/v1/rostering")
public class RosteringController {

    @Autowired
    private RosteringService rosteringService;

    @PostMapping(value = "list-staff")
    public Result listStaff() {
        return ResultGenerator.genSuccessResult(rosteringService.findAllStaff());
    }

    @RequestMapping(value = "export")
    public void listStaff(String datas, String date, HttpServletResponse response) {
        List<RosteringExportModel> rosteringModels = JSONObject.parseArray(datas, RosteringExportModel.class);
        Workbook workbook = rosteringService.export(rosteringModels);
        try {
            String fileName = DateUtil.format(DateUtil.parse(date), "yyyy-MM") + "rostering.xls";
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }




    @PostMapping(value = "gen")
    public Result genRostering(@RequestParam("month") int month,
                               @RequestParam("shiftRuleModel") String shiftRuleModelStr,
                               @RequestParam("staffRuleModel") String staffRuleModelStr,
                               @RequestParam("dateRuleModel") String dateRuleModelStr) {

        List<ShiftRuleModel> shiftRuleModels = JSONObject.parseArray(shiftRuleModelStr, ShiftRuleModel.class);
        List<StaffRuleModel> staffRuleModels = JSONObject.parseArray(staffRuleModelStr, StaffRuleModel.class);
        List<DateRuleModel> dateRuleModels = JSONObject.parseArray(dateRuleModelStr, DateRuleModel.class);
        return ResultGenerator.genSuccessResult(rosteringService.gen(month, shiftRuleModels, staffRuleModels, dateRuleModels));
    }



}