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

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.consumptiontransferlog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.consumptiontransferlog.ConsumptionTransferLogDO;
import cn.newfeifan.mall.module.distri.service.consumptiontransferlog.ConsumptionTransferLogService;

@Tag(name = "管理后台 - 消费分转让记录")
@RestController
@RequestMapping("/distri/consumption-transfer-log")
@Validated
public class ConsumptionTransferLogController {

    @Resource
    private ConsumptionTransferLogService consumptionTransferLogService;

    @PostMapping("/create")
    @Operation(summary = "创建消费分转让记录")
    @PreAuthorize("@ss.hasPermission('distri:consumption-transfer-log:create')")
    public CommonResult<Long> createConsumptionTransferLog(@Valid @RequestBody ConsumptionTransferLogSaveReqVO createReqVO) {
        return success(consumptionTransferLogService.createConsumptionTransferLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新消费分转让记录")
    @PreAuthorize("@ss.hasPermission('distri:consumption-transfer-log:update')")
    public CommonResult<Boolean> updateConsumptionTransferLog(@Valid @RequestBody ConsumptionTransferLogSaveReqVO updateReqVO) {
        consumptionTransferLogService.updateConsumptionTransferLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除消费分转让记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:consumption-transfer-log:delete')")
    public CommonResult<Boolean> deleteConsumptionTransferLog(@RequestParam("id") Long id) {
        consumptionTransferLogService.deleteConsumptionTransferLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得消费分转让记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:consumption-transfer-log:query')")
    public CommonResult<ConsumptionTransferLogRespVO> getConsumptionTransferLog(@RequestParam("id") Long id) {
        ConsumptionTransferLogDO consumptionTransferLog = consumptionTransferLogService.getConsumptionTransferLog(id);
        return success(BeanUtils.toBean(consumptionTransferLog, ConsumptionTransferLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得消费分转让记录分页")
    @PreAuthorize("@ss.hasPermission('distri:consumption-transfer-log:query')")
    public CommonResult<PageResult<ConsumptionTransferLogRespVO>> getConsumptionTransferLogPage(@Valid ConsumptionTransferLogPageReqVO pageReqVO) {
        PageResult<ConsumptionTransferLogDO> pageResult = consumptionTransferLogService.getConsumptionTransferLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConsumptionTransferLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出消费分转让记录 Excel")
    @PreAuthorize("@ss.hasPermission('distri:consumption-transfer-log:export')")
    @OperateLog(type = EXPORT)
    public void exportConsumptionTransferLogExcel(@Valid ConsumptionTransferLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConsumptionTransferLogDO> list = consumptionTransferLogService.getConsumptionTransferLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "消费分转让记录.xls", "数据", ConsumptionTransferLogRespVO.class,
                        BeanUtils.toBean(list, ConsumptionTransferLogRespVO.class));
    }

}