package cn.newfeifan.mall.module.distri.controller.admin.ordercalc;

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.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.ordercalc.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ordercalc.OrderCalcDO;
import cn.newfeifan.mall.module.distri.service.ordercalc.OrderCalcService;

@Tag(name = "管理后台 - 订单产生数据计算")
@RestController
@RequestMapping("/distri/order-calc")
@Validated
public class OrderCalcController {

    @Resource
    private OrderCalcService orderCalcService;

    @PostMapping("/create")
    @Operation(summary = "创建订单产生数据计算")
    @PreAuthorize("@ss.hasPermission('distri:order-calc:create')")
    public CommonResult<Long> createOrderCalc(@Valid @RequestBody OrderCalcSaveReqVO createReqVO) {
        return success(orderCalcService.createOrderCalc(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单产生数据计算")
    @PreAuthorize("@ss.hasPermission('distri:order-calc:update')")
    public CommonResult<Boolean> updateOrderCalc(@Valid @RequestBody OrderCalcSaveReqVO updateReqVO) {
        orderCalcService.updateOrderCalc(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单产生数据计算")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:order-calc:delete')")
    public CommonResult<Boolean> deleteOrderCalc(@RequestParam("id") Long id) {
        orderCalcService.deleteOrderCalc(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单产生数据计算")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:order-calc:query')")
    public CommonResult<OrderCalcRespVO> getOrderCalc(@RequestParam("id") Long id) {
        OrderCalcDO orderCalc = orderCalcService.getOrderCalc(id);
        return success(BeanUtils.toBean(orderCalc, OrderCalcRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单产生数据计算分页")
    @PreAuthorize("@ss.hasPermission('distri:order-calc:query')")
    public CommonResult<PageResult<OrderCalcRespVO>> getOrderCalcPage(@Valid OrderCalcPageReqVO pageReqVO) {
        PageResult<OrderCalcDO> pageResult = orderCalcService.getOrderCalcPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderCalcRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单产生数据计算 Excel")
    @PreAuthorize("@ss.hasPermission('distri:order-calc:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderCalcExcel(@Valid OrderCalcPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderCalcDO> list = orderCalcService.getOrderCalcPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单产生数据计算.xls", "数据", OrderCalcRespVO.class,
                        BeanUtils.toBean(list, OrderCalcRespVO.class));
    }

}