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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.worklog.dto.statistics.LogStatisticsDetailsListDTO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.dto.statistics.MyManageModelDTO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.dto.statistics.NeedWriteHistoryDTO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsDetailsListVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsDetailsVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsModelVO;
import cn.iocoder.yudao.module.system.service.worklog.LogStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
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/worklog/statistics")
@Validated
public class LogStatisticsController {

    @Resource
    private LogStatisticsService logStatisticsService;

    @GetMapping("/myManageModel")
    @Operation(summary = "获取我管理的模版")
    public CommonResult<List<LogStatisticsModelVO>> myManageModel() {
        return success(logStatisticsService.myManageModel());
    }

    @GetMapping("/getMyManageModelStatistics")
    @Operation(summary = "获取我管理的模版的统计")
    @PermitAll
    public CommonResult<List<LogStatisticsModelVO>> getMyManageModelStatistics(@ModelAttribute MyManageModelDTO dto) {
        return success(logStatisticsService.getMyManageModelStatistics(dto));
    }

    @GetMapping("/getStatistics")
    @Operation(summary = "获取日志统计")
    public CommonResult<LogStatisticsDetailsListVO> getStatistics(@ModelAttribute LogStatisticsDetailsListDTO dto) {
        return success(logStatisticsService.getStatistics(dto));
    }

    @GetMapping("/getNeedWrite")
    @Operation(summary = "获取需要填写的")
    public CommonResult<List<LogStatisticsModelVO>> getNeedWrite() {
        List<LogStatisticsModelVO> list = logStatisticsService.getNeedWrite();
        return success(list);
    }

    @GetMapping("/getNeedWriteHistory")
    @Operation(summary = "获取需要填写的历史记录")
    public CommonResult<List<LogStatisticsModelVO>> getNeedWriteHistory(@ModelAttribute NeedWriteHistoryDTO dto) {
        List<LogStatisticsModelVO> list = logStatisticsService.getNeedWriteHistory(dto);
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出日志规则 Excel")
    @OperateLog(type = EXPORT)
    public void exportLogRuleExcel(@ModelAttribute LogStatisticsDetailsListDTO dto,
                                   HttpServletResponse response) throws IOException {
        List<LogStatisticsDetailsVO> list = logStatisticsService.exportLogRuleExcel(dto);
        // 导出 Excel
        ExcelUtils.write(response, "日志规则.xls", "数据", LogStatisticsExcelVO.class,
                BeanUtils.toBean(list, LogStatisticsExcelVO.class));
    }
}
