package cn.iocoder.yudao.module.workReport.controller.admin;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.workReport.entity.WorkReport;
import cn.iocoder.yudao.module.workReport.service.WorkReportService;
import cn.iocoder.yudao.module.workReport.vo.WorkReportPageReqVO;
import cn.iocoder.yudao.module.workReport.vo.WorkReportRespVO;
import cn.iocoder.yudao.module.workReport.vo.WorkReportSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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 static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 工作汇报")
@RestController
@RequestMapping("/work/report")
@Validated
public class WorkReportController {

    @Resource
    private WorkReportService reportService;

    @PostMapping("/create")
    @Operation(summary = "创建工作汇报")
//    @PreAuthorize("@ss.hasPermission('work:report:create')")
    public CommonResult<Integer> createReport(@Valid @RequestBody WorkReportSaveReqVO createReqVO) {
        createReqVO.setUserId(Objects.requireNonNull(SecurityFrameworkUtils.getLoginUser()).getId());
        return success(reportService.createReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工作汇报")
//    @PreAuthorize("@ss.hasPermission('work:report:update')")
    public CommonResult<Boolean> updateReport(@Valid @RequestBody WorkReportSaveReqVO updateReqVO) {
        reportService.updateReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作汇报")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('work:report:delete')")
    public CommonResult<Boolean> deleteReport(@RequestParam("id") Integer id) {
        reportService.deleteReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工作汇报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('work:report:query')")
    public CommonResult<WorkReportRespVO> getReport(@RequestParam("id") Integer id) {
        WorkReport report = reportService.getReport(id);
        WorkReportRespVO bean = BeanUtils.toBean(report, WorkReportRespVO.class);
        //将汇报对象转为数组
        bean.setReportTos(Arrays.stream(report.getReportTo().split(",")).map(Long::parseLong)  // 转换为 Long 类型
                .collect(Collectors.toList()));
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得工作汇报分页")
//    @PreAuthorize("@ss.hasPermission('work:report:query')")
    public CommonResult<IPage<WorkReportRespVO>> getReportPage(@Valid WorkReportPageReqVO pageReqVO) {
        Page<WorkReport> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        pageReqVO.setUserId(Objects.requireNonNull(SecurityFrameworkUtils.getLoginUser()).getId());
        IPage<WorkReportRespVO> pageResult = reportService.getReportPage(page, pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工作汇报 Excel")
    @PreAuthorize("@ss.hasPermission('work:report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportExcel(@Valid WorkReportPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        Page<WorkReport> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<WorkReportRespVO> pageResult = reportService.getReportPage(page, pageReqVO);
        List<WorkReportRespVO> list = pageResult.getRecords();
        // 导出 Excel
        ExcelUtils.write(response, "工作汇报.xls", "数据", WorkReportRespVO.class, list);
    }

    /**
     * 查询汇报对象
     *
     * @param dictType 字典类型
     * @return
     */
    @GetMapping("/queryReportTo")
    public CommonResult<?> queryReportTo(String dictType) {
        return success(reportService.queryReportTo(dictType));
    }

    /**
     * 工作汇报预览
     *
     * @param id
     * @return
     */
    @GetMapping("/workReportView")
    public CommonResult<?> workReportView(@RequestParam("id") Integer id) {
        return success(reportService.workReportView(id));
    }

}