package cn.iocoder.yudao.module.member.controller.admin.orderdetaillog;

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.orderdetaillog.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.orderdetaillog.OrderDetailLogDO;
import cn.iocoder.yudao.module.member.service.orderdetaillog.OrderDetailLogService;

@Tag(name = "管理后台 - 打菜日志")
@RestController
@RequestMapping("/member/order-detail-log")
@Validated
public class OrderDetailLogController {

    @Resource
    private OrderDetailLogService orderDetailLogService;

    @PostMapping("/create")
    @Operation(summary = "创建打菜日志")
    @PreAuthorize("@ss.hasPermission('member:order-detail-log:create')")
    public CommonResult<Long> createOrderDetailLog(@Valid @RequestBody OrderDetailLogSaveReqVO createReqVO) {
        return success(orderDetailLogService.createOrderDetailLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打菜日志")
    @PreAuthorize("@ss.hasPermission('member:order-detail-log:update')")
    public CommonResult<Boolean> updateOrderDetailLog(@Valid @RequestBody OrderDetailLogSaveReqVO updateReqVO) {
        orderDetailLogService.updateOrderDetailLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除打菜日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:order-detail-log:delete')")
    public CommonResult<Boolean> deleteOrderDetailLog(@RequestParam("id") Long id) {
        orderDetailLogService.deleteOrderDetailLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得打菜日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:order-detail-log:query')")
    public CommonResult<OrderDetailLogRespVO> getOrderDetailLog(@RequestParam("id") Long id) {
        OrderDetailLogDO orderDetailLog = orderDetailLogService.getOrderDetailLog(id);
        return success(BeanUtils.toBean(orderDetailLog, OrderDetailLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打菜日志分页")
    @PreAuthorize("@ss.hasPermission('member:order-detail-log:query')")
    public CommonResult<PageResult<OrderDetailLogRespVO>> getOrderDetailLogPage(@Valid OrderDetailLogPageReqVO pageReqVO) {
        PageResult<OrderDetailLogDO> pageResult = orderDetailLogService.getOrderDetailLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderDetailLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出打菜日志 Excel")
    @PreAuthorize("@ss.hasPermission('member:order-detail-log:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderDetailLogExcel(@Valid OrderDetailLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderDetailLogDO> list = orderDetailLogService.getOrderDetailLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "打菜日志.xls", "数据", OrderDetailLogRespVO.class,
                        BeanUtils.toBean(list, OrderDetailLogRespVO.class));
    }

}