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

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.groupuser.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupuser.AttendanceGroupUserDO;
import cn.iocoder.yudao.module.system.service.attendance.groupuser.AttendanceGroupUserService;

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

    @Resource
    private AttendanceGroupUserService groupUserService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤组人员")
    @PreAuthorize("@ss.hasPermission('attendance:group-user:create')")
    public CommonResult<Long> createGroupUser(@Valid @RequestBody AttendanceGroupUserSaveReqVO createReqVO) {
        return success(groupUserService.createGroupUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤组人员")
    @PreAuthorize("@ss.hasPermission('attendance:group-user:update')")
    public CommonResult<Boolean> updateGroupUser(@Valid @RequestBody AttendanceGroupUserSaveReqVO updateReqVO) {
        groupUserService.updateGroupUser(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得考勤组人员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:group-user:query')")
    public CommonResult<AttendanceGroupUserRespVO> getGroupUser(@RequestParam("id") Long id) {
        AttendanceGroupUserDO groupUser = groupUserService.getGroupUser(id);
        return success(BeanUtils.toBean(groupUser, AttendanceGroupUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤组人员分页")
    @PreAuthorize("@ss.hasPermission('attendance:group-user:query')")
    public CommonResult<PageResult<AttendanceGroupUserRespVO>> getGroupUserPage(@Valid AttendanceGroupUserPageReqVO pageReqVO) {
        PageResult<AttendanceGroupUserDO> pageResult = groupUserService.getGroupUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceGroupUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤组人员 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:group-user:export')")
    @OperateLog(type = EXPORT)
    public void exportGroupUserExcel(@Valid AttendanceGroupUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceGroupUserDO> list = groupUserService.getGroupUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤组人员.xls", "数据", AttendanceGroupUserRespVO.class,
                        BeanUtils.toBean(list, AttendanceGroupUserRespVO.class));
    }

}