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

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.excel.core.util.ExcelUtils;
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.excel.DailyExpensesExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.DishesOrderStatisticsExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.restaurant.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.DishesOrderReportTimeslotDO;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.DishesOrderStatisticsDO;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.EmployeeDishesOrderReportDO;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.EmployeeDishesOrderReportVO;
import cn.iocoder.yudao.module.system.service.restaurant.DishesOrderStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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


@Tag(name = "管理后台 - 长者点餐统计")
@RestController
@RequestMapping("/restaurant/dishesOrderStatistics")
@Validated
public class DishesOrderStatisticsController {

    @Resource
    private DishesOrderStatisticsService dishesOrderStatisticsService;


    @PostMapping("/createDishesOrderStatistics")
    @Operation(summary = "创建长者点餐统计")
    public CommonResult<Boolean> createDishesOrderStatistics(@Valid @RequestBody DishesOrderStatisticsSaveReqVO createReqVO) {
        dishesOrderStatisticsService.createDishesOrderStatistics(createReqVO);
        return success(true);
    }


    @PutMapping("/updateDishesOrderStatistics")
    @Operation(summary = "更新长者点餐统计")
    public CommonResult<Boolean> updateDishesOrderStatistics(@Valid @RequestBody DishesOrderStatisticsSaveReqVO updateReqVO) {
        dishesOrderStatisticsService.updateDishesOrderStatistics(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除长者点餐统计")
    public CommonResult<Boolean> deleteDishesOrderStatistics(@RequestBody Long[] ids) {
        dishesOrderStatisticsService.deleteDishesOrderStatistics(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者点餐统计详情")
    @Parameter(name = "id", description = "点餐ID", required = true, example = "1024")
    public CommonResult<DishesOrderStatisticsRespVO> getDishesOrderStatistics(@RequestParam("id") Long id) {
        DishesOrderStatisticsRespVO dishesOrderStatistics = dishesOrderStatisticsService.getDishesOrderStatistics(id);
        return success(dishesOrderStatistics);
    }

    @GetMapping("/getDishesOrderStatisticsPage")
    @Operation(summary = "获得长者点餐统计分页")
    @TenantIgnore
    public CommonResult<PageResult<DishesOrderStatisticsDO>> getDishesOrderStatisticsPage(@Valid DishesOrderStatisticsPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<DishesOrderStatisticsDO> pageResult = dishesOrderStatisticsService.getDishesOrderStatisticsPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出长者点餐统计 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDishesOrderStatisticsExcel(@Valid DishesOrderStatisticsPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DishesOrderExportVO> list = dishesOrderStatisticsService.exportDishesOrderStatisticsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "长者点餐统计.xls", "数据", DishesOrderExportVO.class, list);
    }

    @GetMapping("/exportDishesOrderItemExcel")
    @Operation(summary = "导出长者点餐统计详情 Excel")
    public void exportDishesOrderItemExcel(@RequestParam Long id, HttpServletResponse response) throws IOException {
        List<DishesOrderExportVO> list = dishesOrderStatisticsService.exportDishesOrderItemExcel(id);
        ExcelUtils.write(response, "长者点餐统计.xls", "数据", DishesOrderExportVO.class, list);
    }


    @GetMapping("/cancel")
    @Operation(summary = "取消点餐")
    @Parameter(name = "id", description = "点餐ID", required = true, example = "1024")
    public CommonResult<Boolean> cancel(@RequestParam("id") Long id) {
        dishesOrderStatisticsService.cancel(id);
        return success(true);
    }

    @GetMapping("/getEmployeeDishesOrderReportPage")
    @Operation(summary = "获得员工报餐统计分页")
    @TenantIgnore
    public CommonResult<PageResult<EmployeeDishesOrderReportVO>> getEmployeeDishesOrderReportPage(@Valid EmployeeDishesOrderReportPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<EmployeeDishesOrderReportVO> pageResult = dishesOrderStatisticsService.getEmployeeDishesOrderReportPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/downloadExcel")
    @Operation(summary = "下载点餐统计导入模板")
    public void downloadFile(HttpServletResponse response) throws IOException {
        String filePath = "files/点餐统计导入模板.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(filePath);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=点餐统计导入模板");
        IOUtils.copy(classPathResource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
    }


    @PostMapping("/importDishesOrderStatisticsExcel")
    @Operation(summary = "导入点餐统计Excel")
    public CommonResult<String> importDailyExpensesExcel(@RequestBody MultipartFile file) throws IOException {
        List<DishesOrderStatisticsExcelVO> list = ExcelUtils.read(file, DishesOrderStatisticsExcelVO.class);
        Map<String, Object> result = dishesOrderStatisticsService.importDailyExpensesExcel(list);
        if(result.get("errorMsg") == null){
            return success("success");
        }
        return error(-1,result.get("errorMsg").toString());
    }

    @PostMapping("/createReportTimeslot")
    @Operation(summary = "创建点餐时段记录")
    public CommonResult<Boolean> createReportTimeslot(@RequestBody List<DishesOrderReportTimeslotDO> dishesOrderReportTimeslotList) {
        dishesOrderStatisticsService.createReportTimeslot(dishesOrderReportTimeslotList);
        return success(true);
    }
    @GetMapping("/getReportTimeslot")
    @Operation(summary = "获得当前机构点餐时段记录")
    @Parameter(name = "tenantId", description = "机构id", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<List<DishesOrderReportTimeslotDO>> getReportTimeslot(@RequestParam("tenantId") Long tenantId) {
        return success(dishesOrderStatisticsService.getReportTimeslot(tenantId == null ? TenantContextHolder.getTenantId() : tenantId));
    }

    @PostMapping("/updateReportTimeslot")
    @Operation(summary = "更新点餐时段记录")
    public CommonResult<Boolean> updateReportTimeslot(@RequestBody List<DishesOrderReportTimeslotDO> dishesOrderReportTimeslotList) {
        dishesOrderStatisticsService.updateReportTimeslot(dishesOrderReportTimeslotList);
        return success(true);
    }
}