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

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.scheduling.vo.AttendanceSchedulingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.scheduling.vo.AttendanceSchedulingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.scheduling.vo.AttendanceSchedulingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.scheduling.AttendanceSchedulingDO;
import cn.iocoder.yudao.module.system.service.attendance.scheduling.AttendanceSchedulingService;
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/scheduling")
@Validated
public class AttendanceSchedulingController {

    @Resource
    private AttendanceSchedulingService schedulingService;

    @PostMapping("/batchCreateOrUpdate")
    @Operation(summary = "批量新增修改排班制考勤设置")
    public CommonResult<Long> batchCreateOrUpdate(@RequestParam Long attendanceGroupId,
                                            @Valid @RequestBody List<AttendanceSchedulingSaveReqVO> createReqVO) {
        Long groupId = schedulingService.batchCreateOrUpdate(attendanceGroupId, createReqVO);
        return success(groupId);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得排班制考勤设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AttendanceSchedulingRespVO> getScheduling(@RequestParam("id") Long id) {
        AttendanceSchedulingDO scheduling = schedulingService.getScheduling(id);
        return success(BeanUtils.toBean(scheduling, AttendanceSchedulingRespVO.class));
    }

    @GetMapping("/getListByGroupId")
    @Operation(summary = "根据考勤id获取排版制设置列表")
    public CommonResult<List<AttendanceSchedulingRespVO>> getListByGroupId(@RequestParam("id") Long attendanceGroupId) {
        List<AttendanceSchedulingRespVO> vos = schedulingService.getListByGroupId(attendanceGroupId);
        return success(vos);
    }

    @GetMapping("/page")
    @Operation(summary = "获得排班制考勤设置分页")
    public CommonResult<PageResult<AttendanceSchedulingRespVO>> getSchedulingPage(@Valid AttendanceSchedulingPageReqVO pageReqVO) {
        PageResult<AttendanceSchedulingDO> pageResult = schedulingService.getSchedulingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttendanceSchedulingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排班制考勤设置 Excel")
    @OperateLog(type = EXPORT)
    public void exportSchedulingExcel(@Valid AttendanceSchedulingPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttendanceSchedulingDO> list = schedulingService.getSchedulingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排班制考勤设置.xls", "数据", AttendanceSchedulingRespVO.class,
                BeanUtils.toBean(list, AttendanceSchedulingRespVO.class));
    }

}