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

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.ptdailybill.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptdailybill.PtDailyBillDO;
import cn.newfeifan.mall.module.distri.service.ptdailybill.PtDailyBillService;

@Tag(name = "管理后台 - 平台每日账单统计")
@RestController
@RequestMapping("/distri/pt-daily-bill")
@Validated
public class PtDailyBillController {

    @Resource
    private PtDailyBillService ptDailyBillService;

    @PostMapping("/create")
    @Operation(summary = "创建平台每日账单统计")
    @PreAuthorize("@ss.hasPermission('distri:pt-daily-bill:create')")
    public CommonResult<Long> createPtDailyBill(@Valid @RequestBody PtDailyBillSaveReqVO createReqVO) {
        return success(ptDailyBillService.createPtDailyBill(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台每日账单统计")
    @PreAuthorize("@ss.hasPermission('distri:pt-daily-bill:update')")
    public CommonResult<Boolean> updatePtDailyBill(@Valid @RequestBody PtDailyBillSaveReqVO updateReqVO) {
        ptDailyBillService.updatePtDailyBill(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台每日账单统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:pt-daily-bill:delete')")
    public CommonResult<Boolean> deletePtDailyBill(@RequestParam("id") Long id) {
        ptDailyBillService.deletePtDailyBill(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台每日账单统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:pt-daily-bill:query')")
    public CommonResult<PtDailyBillRespVO> getPtDailyBill(@RequestParam("id") Long id) {
        PtDailyBillDO ptDailyBill = ptDailyBillService.getPtDailyBill(id);
        return success(BeanUtils.toBean(ptDailyBill, PtDailyBillRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台每日账单统计分页")
    @PreAuthorize("@ss.hasPermission('distri:pt-daily-bill:query')")
    public CommonResult<PageResult<PtDailyBillRespVO>> getPtDailyBillPage(@Valid PtDailyBillPageReqVO pageReqVO) {
        PageResult<PtDailyBillDO> pageResult = ptDailyBillService.getPtDailyBillPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PtDailyBillRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台每日账单统计 Excel")
    @PreAuthorize("@ss.hasPermission('distri:pt-daily-bill:export')")
    @OperateLog(type = EXPORT)
    public void exportPtDailyBillExcel(@Valid PtDailyBillPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PtDailyBillDO> list = ptDailyBillService.getPtDailyBillPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台每日账单统计.xls", "数据", PtDailyBillRespVO.class,
                        BeanUtils.toBean(list, PtDailyBillRespVO.class));
    }

}