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

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.group.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.group.AttendanceGroupDO;
import cn.iocoder.yudao.module.system.service.attendance.group.AttendanceGroupService;

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

    @Resource
    private AttendanceGroupService groupService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤组")
    @PreAuthorize("@ss.hasPermission('attendance:group:create')")
    public CommonResult<Long> createGroup(@Valid @RequestBody AttendanceGroupSaveReqVO createReqVO) {
        return success(groupService.createGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤组")
    @PreAuthorize("@ss.hasPermission('attendance:group:update')")
    public CommonResult<Boolean> updateGroup(@Valid @RequestBody AttendanceGroupSaveReqVO updateReqVO) {
        groupService.updateGroup(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得考勤组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:group:query')")
    public CommonResult<AttendanceGroupRespVO> getGroup(@RequestParam("id") Long id) {
        AttendanceGroupDO group = groupService.getGroup(id);
        return success(BeanUtils.toBean(group, AttendanceGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤组分页")
    @PreAuthorize("@ss.hasPermission('attendance:group:query')")
    public CommonResult<PageResult<AttendanceGroupRespVO>> getGroupPage(@Valid AttendanceGroupPageReqVO pageReqVO) {
        PageResult<AttendanceGroupDO> pageResult = groupService.getGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤组 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:group:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupExcel(@Valid AttendanceGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceGroupDO> list = groupService.getGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤组.xls", "数据", AttendanceGroupRespVO.class,
                        BeanUtils.toBean(list, AttendanceGroupRespVO.class));
    }

}