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

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.*;
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.ptprofitlog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitlog.PtProfitLogDO;
import cn.newfeifan.mall.module.distri.service.ptprofitlog.PtProfitLogService;

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

    @Resource
    private PtProfitLogService ptProfitLogService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得平台利润记录分页")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit-log:query')")
    public CommonResult<PageResult<PtProfitLogRespVO>> getPtProfitLogPage(@Valid PtProfitLogPageReqVO pageReqVO) {
        PageResult<PtProfitLogRespVO> pageResult = ptProfitLogService.getPtProfitLogPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台利润记录 Excel")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit-log:export')")
    @OperateLog(type = EXPORT)
    public void exportPtProfitLogExcel(@Valid PtProfitLogPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PtProfitLogRespVO> list = ptProfitLogService.getPtProfitLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台利润记录.xls", "数据", PtProfitLogRespVO.class,list);
    }

}