package cn.iocoder.yudao.module.system.controller.admin.groupshift;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.groupshift.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupshift.AttendanceGroupShiftDO;
import cn.iocoder.yudao.module.system.service.attendance.groupshift.AttendanceGroupShiftService;

@Tag(name = "管理后台 - 考勤组班次")
@RestController
@RequestMapping("/system/attendance/group-shift")
@Validated
public class AttendanceGroupShiftController {

    @Resource
    private AttendanceGroupShiftService groupShiftService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤组班次")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:create')")
    public CommonResult<Long> createGroupShift(@Valid @RequestBody AttendanceGroupShiftSaveReqVO createReqVO) {
        return success(groupShiftService.createGroupShift(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤组班次")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:update')")
    public CommonResult<Boolean> updateGroupShift(@Valid @RequestBody AttendanceGroupShiftSaveReqVO updateReqVO) {
        groupShiftService.updateGroupShift(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考勤组班次")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:delete')")
    public CommonResult<Boolean> deleteGroupShift(@RequestParam("id") Long id) {
        groupShiftService.deleteGroupShift(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考勤组班次")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:query')")
    public CommonResult<AttendanceGroupShiftRespVO> getGroupShift(@RequestParam("id") Long id) {
        AttendanceGroupShiftDO groupShift = groupShiftService.getGroupShift(id);
        return success(BeanUtils.toBean(groupShift, AttendanceGroupShiftRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤组班次分页")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:query')")
    public CommonResult<PageResult<AttendanceGroupShiftRespVO>> getGroupShiftPage(@Valid AttendanceGroupShiftPageReqVO pageReqVO) {
        PageResult<AttendanceGroupShiftDO> pageResult = groupShiftService.getGroupShiftPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceGroupShiftRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤组班次 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:group-shift:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupShiftExcel(@Valid AttendanceGroupShiftPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceGroupShiftDO> list = groupShiftService.getGroupShiftPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤组班次.xls", "数据", AttendanceGroupShiftRespVO.class,
                        BeanUtils.toBean(list, AttendanceGroupShiftRespVO.class));
    }

}