package cn.iocoder.yudao.module.attendance.controller.admin.employeesetting;

import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingBatchSaveReqVO;
import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingPageReqVO;
import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingRespVO;
import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingSaveReqVO;
import cn.iocoder.yudao.module.attendance.dal.dataobject.employeesetting.AttendanceEmployeeSettingDO;
import cn.iocoder.yudao.module.attendance.service.employeesetting.AttendanceEmployeeSettingService;
import org.springframework.web.bind.annotation.*;
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 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.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 考勤员工设置")
@RestController
@RequestMapping("/personnel/attendance/employee-setting")
@Validated
public class AttendanceEmployeeSettingController {

    @Resource
    private AttendanceEmployeeSettingService employeeSettingService;

    @PostMapping("/create")
    @Operation(summary = "创建考勤员工设置")
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:create')")
    public CommonResult<Long> createEmployeeSetting(@Valid @RequestBody AttendanceEmployeeSettingSaveReqVO createReqVO) {
        return success(employeeSettingService.createEmployeeSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考勤员工设置")
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:update')")
    public CommonResult<Boolean> updateEmployeeSetting(@Valid @RequestBody AttendanceEmployeeSettingSaveReqVO updateReqVO) {
        employeeSettingService.updateEmployeeSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考勤员工设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:delete')")
    public CommonResult<Boolean> deleteEmployeeSetting(@RequestParam("id") Long id) {
        employeeSettingService.deleteEmployeeSetting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考勤员工设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:query')")
    public CommonResult<AttendanceEmployeeSettingRespVO> getEmployeeSetting(@RequestParam("id") Long id) {
        AttendanceEmployeeSettingDO employeeSetting = employeeSettingService.getEmployeeSetting(id);
        return success(BeanUtils.toBean(employeeSetting, AttendanceEmployeeSettingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考勤员工设置分页")
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:query')")
    public CommonResult<PageResult<AttendanceEmployeeSettingRespVO>> getEmployeeSettingPage(@Valid AttendanceEmployeeSettingPageReqVO pageReqVO) {
        PageResult<AttendanceEmployeeSettingDO> pageResult = employeeSettingService.getEmployeeSettingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceEmployeeSettingRespVO.class));
    }

    @GetMapping("/info-page")
    @Operation(summary = "获得考勤员工设置分页")
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:query')")
    public CommonResult<PageResult<AttendanceEmployeeSettingRespVO>> getEmployeeInfoSettingPage(@Valid AttendanceEmployeeSettingPageReqVO pageReqVO) {
        return success(employeeSettingService.getEmployeeInfoSettingPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考勤员工设置 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEmployeeSettingExcel(@Valid AttendanceEmployeeSettingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceEmployeeSettingDO> list = employeeSettingService.getEmployeeSettingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考勤员工设置.xls", "数据", AttendanceEmployeeSettingRespVO.class,
                        BeanUtils.toBean(list, AttendanceEmployeeSettingRespVO.class));
    }

    @PutMapping("/batch-update")
    @Operation(summary = "更新考勤员工设置")
    @PreAuthorize("@ss.hasPermission('attendance:employee-setting:update')")
    public CommonResult<Boolean> batchUpdateEmployeeSetting(@Valid @RequestBody AttendanceEmployeeSettingBatchSaveReqVO updateReqVO) {
        employeeSettingService.batchUpdateEmployeeSetting(updateReqVO);
        return success(true);
    }

}