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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.groupshiftitem.vo.AttendanceGroupShiftItemPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.groupshiftitem.vo.AttendanceGroupShiftItemRespVO;
import cn.iocoder.yudao.module.system.controller.admin.groupshiftitem.vo.AttendanceGroupShiftItemSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupshiftitem.AttendanceGroupShiftItemDO;
import cn.iocoder.yudao.module.system.service.attendance.groupshiftitem.AttendanceGroupShiftItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

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

    @Resource
    private AttendanceGroupShiftItemService attendanceGroupShiftItemService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得考勤组班次子表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-group-shift-item:query')")
    public CommonResult<AttendanceGroupShiftItemRespVO> getAttendanceGroupShiftItem(@RequestParam("id") Long id) {
        AttendanceGroupShiftItemDO attendanceGroupShiftItem = attendanceGroupShiftItemService.getAttendanceGroupShiftItem(id);
        return success(BeanUtils.toBean(attendanceGroupShiftItem, AttendanceGroupShiftItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤组班次子表分页")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-group-shift-item:query')")
    public CommonResult<PageResult<AttendanceGroupShiftItemRespVO>> getAttendanceGroupShiftItemPage(@Valid AttendanceGroupShiftItemPageReqVO pageReqVO) {
        PageResult<AttendanceGroupShiftItemDO> pageResult = attendanceGroupShiftItemService.getAttendanceGroupShiftItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceGroupShiftItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤组班次子表 Excel")
    @PreAuthorize("@ss.hasPermission('system.attendance:attendance-group-shift-item:export')")
    @OperateLog(type = EXPORT)
    public void exportAttendanceGroupShiftItemExcel(@Valid AttendanceGroupShiftItemPageReqVO pageReqVO,
                                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceGroupShiftItemDO> list = attendanceGroupShiftItemService.getAttendanceGroupShiftItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤组班次子表.xls", "数据", AttendanceGroupShiftItemRespVO.class,
                BeanUtils.toBean(list, AttendanceGroupShiftItemRespVO.class));
    }

}