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

import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;
import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import cn.newfeifan.mall.framework.security.core.annotations.PreAuthenticated;
import cn.newfeifan.mall.module.distri.controller.app.ptprofitlog.vo.PtProfitLogPageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.ptprofitlog.vo.PtProfitLogRespVO;
import cn.newfeifan.mall.module.distri.controller.app.ptprofitlog.vo.PtProfitLogSaveReqVO;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitlog.PtProfitLogDO;
import cn.newfeifan.mall.module.distri.service.ptprofitlog.PtProfitLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "用户 APP - 平台利润记录")
@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 = "获得平台利润记录分页")
    @PreAuthenticated
    public CommonResult<PageResult<PtProfitLogRespVO>> getPtProfitLogPage(@Valid PtProfitLogPageReqVO pageReqVO) {
        PageResult<PtProfitLogRespVO> pageResult = ptProfitLogService.getPtProfitLogPage(pageReqVO);
        return success(pageResult);
    }
    @GetMapping("/getMaxAmountPage")
    @Operation(summary = "获得峰值记录分页")
    @PreAuthenticated
    public CommonResult<PageResult<PtProfitLogRespVO>> getMaxAmountPage(@Valid PtProfitLogPageReqVO pageReqVO) {
        PageResult<PtProfitLogRespVO> pageResult = ptProfitLogService.getMaxAmountPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台利润记录 Excel")
    @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);
    }

}