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

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.orderpercentage.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.orderpercentage.OrderPercentageDO;
import cn.newfeifan.mall.module.distri.service.orderpercentage.OrderPercentageService;

@Tag(name = "管理后台 - 积分相关计算浮动百分比设置")
@RestController
@RequestMapping("/distri/order-percentage")
@Validated
public class OrderPercentageController {

    @Resource
    private OrderPercentageService orderPercentageService;

    @PostMapping("/create")
    @Operation(summary = "创建积分相关计算浮动百分比设置")
    @PreAuthorize("@ss.hasPermission('distri:order-percentage:create')")
    public CommonResult<Long> createOrderPercentage(@Valid @RequestBody OrderPercentageSaveReqVO createReqVO) {
        return success(orderPercentageService.createOrderPercentage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分相关计算浮动百分比设置")
    @PreAuthorize("@ss.hasPermission('distri:order-percentage:update')")
    public CommonResult<Boolean> updateOrderPercentage(@Valid @RequestBody OrderPercentageSaveReqVO updateReqVO) {
        orderPercentageService.updateOrderPercentage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分相关计算浮动百分比设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:order-percentage:delete')")
    public CommonResult<Boolean> deleteOrderPercentage(@RequestParam("id") Long id) {
        orderPercentageService.deleteOrderPercentage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分相关计算浮动百分比设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:order-percentage:query')")
    public CommonResult<OrderPercentageRespVO> getOrderPercentage(@RequestParam("id") Long id) {
        OrderPercentageDO orderPercentage = orderPercentageService.getOrderPercentage(id);
        return success(BeanUtils.toBean(orderPercentage, OrderPercentageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分相关计算浮动百分比设置分页")
    @PreAuthorize("@ss.hasPermission('distri:order-percentage:query')")
    public CommonResult<PageResult<OrderPercentageRespVO>> getOrderPercentagePage(@Valid OrderPercentagePageReqVO pageReqVO) {
        PageResult<OrderPercentageDO> pageResult = orderPercentageService.getOrderPercentagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderPercentageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分相关计算浮动百分比设置 Excel")
    @PreAuthorize("@ss.hasPermission('distri:order-percentage:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderPercentageExcel(@Valid OrderPercentagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderPercentageDO> list = orderPercentageService.getOrderPercentagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "积分相关计算浮动百分比设置.xls", "数据", OrderPercentageRespVO.class,
                        BeanUtils.toBean(list, OrderPercentageRespVO.class));
    }

}