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

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.ptprofitlogdetails.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitlogdetails.PtProfitLogDetailsDO;
import cn.newfeifan.mall.module.distri.service.ptprofitlogdetails.PtProfitLogDetailsService;

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

    @Resource
    private PtProfitLogDetailsService ptProfitLogDetailsService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得平台利润记录详情分页")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit-log-details:query')")
    public CommonResult<PageResult<PtProfitLogDetailsRespVO>> getPtProfitLogDetailsPage(@Valid PtProfitLogDetailsPageReqVO pageReqVO) {
        PageResult<PtProfitLogDetailsDO> pageResult = ptProfitLogDetailsService.getPtProfitLogDetailsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PtProfitLogDetailsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台利润记录详情 Excel")
    @PreAuthorize("@ss.hasPermission('distri:pt-profit-log-details:export')")
    @OperateLog(type = EXPORT)
    public void exportPtProfitLogDetailsExcel(@Valid PtProfitLogDetailsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PtProfitLogDetailsDO> list = ptProfitLogDetailsService.getPtProfitLogDetailsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台利润记录详情.xls", "数据", PtProfitLogDetailsRespVO.class,
                        BeanUtils.toBean(list, PtProfitLogDetailsRespVO.class));
    }

}