package cn.newfeifan.mall.module.trade.controller.admin.jpushmessagelog;

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.trade.controller.admin.jpushmessagelog.vo.*;
import cn.newfeifan.mall.module.trade.dal.dataobject.jpushmessagelog.JPushMessageLogDO;
import cn.newfeifan.mall.module.trade.service.jpushmessagelog.JPushMessageLogService;

@Tag(name = "管理后台 - 极光推送记录")
@RestController
@RequestMapping("/trade/J-push-message-log")
@Validated
public class JPushMessageLogController {

    @Resource
    private JPushMessageLogService jPushMessageLogService;

    @PostMapping("/create")
    @Operation(summary = "创建极光推送记录")
    @PreAuthorize("@ss.hasPermission('trade:J-push-message-log:create')")
    public CommonResult<Long> createJPushMessageLog(@Valid @RequestBody JPushMessageLogSaveReqVO createReqVO) {
        return success(jPushMessageLogService.createJPushMessageLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新极光推送记录")
    @PreAuthorize("@ss.hasPermission('trade:J-push-message-log:update')")
    public CommonResult<Boolean> updateJPushMessageLog(@Valid @RequestBody JPushMessageLogSaveReqVO updateReqVO) {
        jPushMessageLogService.updateJPushMessageLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除极光推送记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:J-push-message-log:delete')")
    public CommonResult<Boolean> deleteJPushMessageLog(@RequestParam("id") Long id) {
        jPushMessageLogService.deleteJPushMessageLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得极光推送记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:J-push-message-log:query')")
    public CommonResult<JPushMessageLogRespVO> getJPushMessageLog(@RequestParam("id") Long id) {
        JPushMessageLogDO jPushMessageLog = jPushMessageLogService.getJPushMessageLog(id);
        return success(BeanUtils.toBean(jPushMessageLog, JPushMessageLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得极光推送记录分页")
    @PreAuthorize("@ss.hasPermission('trade:J-push-message-log:query')")
    public CommonResult<PageResult<JPushMessageLogRespVO>> getJPushMessageLogPage(@Valid JPushMessageLogPageReqVO pageReqVO) {
        PageResult<JPushMessageLogDO> pageResult = jPushMessageLogService.getJPushMessageLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JPushMessageLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出极光推送记录 Excel")
    @PreAuthorize("@ss.hasPermission('trade:J-push-message-log:export')")
    @OperateLog(type = EXPORT)
    public void exportJPushMessageLogExcel(@Valid JPushMessageLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JPushMessageLogDO> list = jPushMessageLogService.getJPushMessageLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "极光推送记录.xls", "数据", JPushMessageLogRespVO.class,
                        BeanUtils.toBean(list, JPushMessageLogRespVO.class));
    }

}