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

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.ptprofit.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofit.PtProfitDO;
import cn.newfeifan.mall.module.distri.service.ptprofit.PtProfitService;

@Tag(name = "管理后台 - 平台利润")
@RestController
@RequestMapping("/distri/pt-profit")
@Validated
public class PtProfitController {

    @Resource
    private PtProfitService ptProfitService;

    @PostMapping("/create")
    @Operation(summary = "创建平台利润")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit:create')")
    public CommonResult<Long> createPtProfit(@Valid @RequestBody PtProfitSaveReqVO createReqVO) {
        return success(ptProfitService.createPtProfit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台利润")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit:update')")
    public CommonResult<Boolean> updatePtProfit(@Valid @RequestBody PtProfitSaveReqVO updateReqVO) {
        ptProfitService.updatePtProfit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台利润")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:pt-profit:delete')")
    public CommonResult<Boolean> deletePtProfit(@RequestParam("id") Long id) {
        ptProfitService.deletePtProfit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台利润")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit:query')")
    public CommonResult<PtProfitRespVO> getPtProfit(@RequestParam("id") Long id) {
        PtProfitDO ptProfit = ptProfitService.getPtProfit(id);
        return success(BeanUtils.toBean(ptProfit, PtProfitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台利润分页")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit:query')")
    public CommonResult<PageResult<PtProfitRespVO>> getPtProfitPage(@Valid PtProfitPageReqVO pageReqVO) {
        PageResult<PtProfitDO> pageResult = ptProfitService.getPtProfitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PtProfitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台利润 Excel")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit:export')")
    @OperateLog(type = EXPORT)
    public void exportPtProfitExcel(@Valid PtProfitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PtProfitDO> list = ptProfitService.getPtProfitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台利润.xls", "数据", PtProfitRespVO.class,
                        BeanUtils.toBean(list, PtProfitRespVO.class));
    }

}