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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.excel.core.util.ExportData;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.ScheduleShiftExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NurseScheduleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ScheduleShiftDO;
import cn.iocoder.yudao.module.system.service.biz.ScheduleShiftService;
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 java.util.Objects;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @Author: jinyd
 * @CreateTime: 2024/9/14  9:28
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 用户班次排班")
@RestController
@RequestMapping("/nurse/scheduleShift")
@Validated
public class ScheduleShiftController {

    @Resource
    private ScheduleShiftService scheduleShiftService;

    @PostMapping("/create")
    @Operation(summary = "创建用户班次排班")
//    @PreAuthorize("@ss.hasPermission('nurse:schedule-shift:create')")
    @TenantIgnore
    public CommonResult<Long> createScheduleShift(@Valid @RequestBody ScheduleShiftSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(scheduleShiftService.createScheduleShift(createReqVO));
    }

//    @PutMapping("/update")
//    @Operation(summary = "更新用户班次排班")
//    @PreAuthorize("@ss.hasPermission('nurse:schedule-shift:update')")
//    public CommonResult<Boolean> updateScheduleShift(@Valid @RequestBody ScheduleShiftSaveDetailReqVO updateReqVO) {
//        scheduleShiftService.updateScheduleShift(updateReqVO);
//        return success(true);
//    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户班次排班")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('nurse:schedule-shift:delete')")
    public CommonResult<Boolean> deleteScheduleShift(@RequestParam("id") Long id) {
        scheduleShiftService.deleteScheduleShift(id);
        return success(true);
    }

    @GetMapping("/getNurseScheduleList")
    @Operation(summary = "根据部门和日期获取排班表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('nurse:schedule-shift:query')")
    public CommonResult<List<NurseScheduleDO>> getNurseScheduleList(@Valid ScheduleShiftPageReqVO pageReqVO) {
        List<NurseScheduleDO> list = scheduleShiftService.getNurseScheduleList(pageReqVO);
        return success(list);
    }

    @PostMapping("/syncNurseSchedule")
    @Operation(summary = "同步排班")
//    @PreAuthorize("@ss.hasPermission('nurse:schedule-shift:create')")
    public CommonResult<Boolean> syncNurseSchedule(@Valid @RequestBody ScheduleShiftSaveReqVO createReqVO) {
        return success(scheduleShiftService.syncNurseSchedule(createReqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户班次排班分页")
//    @PreAuthorize("@ss.hasPermission('nurse:schedule-shift:query')")
    @TenantIgnore
    public CommonResult<PageResult<ScheduleShiftPageListRespVO>> getScheduleShiftPage(@Valid ScheduleShiftPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<ScheduleShiftPageListRespVO> pageResult = scheduleShiftService.getScheduleShiftPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户班次排班 Excel")
    public void exportScheduleShiftExcel(@Valid ScheduleShiftPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
//        List<ScheduleShiftExcelVO> list = scheduleShiftService.getScheduleShiftExcel(pageReqVO);
//        List<ScheduleShiftPageRespVO> list = scheduleShiftService.getScheduleShiftPage(pageReqVO).getList();
        List<ExportData> dataList = scheduleShiftService.getExportData(pageReqVO);
        // 导出 Excel
//        ExcelUtils.write(response, "用户班次排班.xls", "数据", ScheduleShiftRespVO.class,
//                BeanUtils.toBean(list, ScheduleShiftRespVO.class));


        // 导出 Excel
//        ExcelUtils.otherWrite(response, "用户班次排班.xls", "数据", ExportData.class, dataList);
        ExcelUtils.otherWrite(response, "用户班次排班.xls", "排班数据", ExportData.class, dataList);



    }


}
