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

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.fixed.vo.AttendanceFixedPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.fixed.vo.AttendanceFixedRespVO;
import cn.iocoder.yudao.module.system.controller.admin.fixed.vo.AttendanceFixedSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.fixed.AttendanceFixedDO;
import cn.iocoder.yudao.module.system.service.attendance.fixed.AttendanceFixedService;
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/fixed")
@Validated
public class AttendanceFixedController {

    @Resource
    private AttendanceFixedService fixedService;

    @PostMapping("/batchCreateOrUpdate")
    @Operation(summary = "批量新增修改固定班制考勤设置")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:create')")
    public CommonResult<Long> batchCreateOrUpdate(@RequestParam Long attendanceGroupId,
                                                  @Valid @RequestBody List<AttendanceFixedSaveReqVO> vos) {
        Long groupId = fixedService.batchCreateOrUpdate(attendanceGroupId, vos);
        return success(groupId);
    }

    //  如果返回的有值 (判断是否有值) - 那么前端 跳出提示框 (提示是否立即更新)  下面两个按钮   立即生效 和 (推荐)次日生效
    //  调用接口 /system/attendance/group-shift/effectiveImmediately


    @GetMapping("/get")
    @Operation(summary = "获得固定班制考勤设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:query')")
    public CommonResult<AttendanceFixedRespVO> getFixed(@RequestParam("id") Long id) {
        AttendanceFixedDO fixed = fixedService.getFixed(id);
        return success(BeanUtils.toBean(fixed, AttendanceFixedRespVO.class));
    }

    @GetMapping("/getListByGroupId")
    @Operation(summary = "根据考勤组ID获得固定班制考勤设置列表")
    public CommonResult<List<AttendanceFixedRespVO>> getListByGroupId(@RequestParam Long attendanceGroupId) {
        List<AttendanceFixedRespVO> list = fixedService.getListByGroupId(attendanceGroupId);
        return success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "获得固定班制考勤设置分页")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:query')")
    public CommonResult<PageResult<AttendanceFixedRespVO>> getFixedPage(@Valid AttendanceFixedPageReqVO pageReqVO) {
        PageResult<AttendanceFixedDO> pageResult = fixedService.getFixedPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceFixedRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出固定班制考勤设置 Excel")
    @PreAuthorize("@ss.hasPermission('attendance:fixed:export')")
    @OperateLog(type = EXPORT)
    public void exportFixedExcel(@Valid AttendanceFixedPageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceFixedDO> list = fixedService.getFixedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "固定班制考勤设置.xls", "数据", AttendanceFixedRespVO.class,
                BeanUtils.toBean(list, AttendanceFixedRespVO.class));
    }

}