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

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.module.trade.controller.admin.messagelog.vo.MessageLogPageReqVO;
import cn.newfeifan.mall.module.trade.controller.admin.messagelog.vo.MessageLogRespVO;
import cn.newfeifan.mall.module.trade.controller.admin.messagelog.vo.MessageLogSaveReqVO;
import cn.newfeifan.mall.module.trade.dal.dataobject.messagelog.MessageLogDO;
import cn.newfeifan.mall.module.trade.service.messagelog.MessageLogService;
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 = "管理后台 - 微信消息记录")
@RestController
@RequestMapping("/wechat/message-log")
@Validated
public class MessageLogController {

    @Resource
    private MessageLogService messageLogService;

    @PostMapping("/create")
    @Operation(summary = "创建微信消息记录")
    @PreAuthorize("@ss.hasPermission('wechat:message-log:create')")
    public CommonResult<Long> createMessageLog(@Valid @RequestBody MessageLogSaveReqVO createReqVO) {
        return success(messageLogService.createMessageLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信消息记录")
    @PreAuthorize("@ss.hasPermission('wechat:message-log:update')")
    public CommonResult<Boolean> updateMessageLog(@Valid @RequestBody MessageLogSaveReqVO updateReqVO) {
        messageLogService.updateMessageLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信消息记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wechat:message-log:delete')")
    public CommonResult<Boolean> deleteMessageLog(@RequestParam("id") Long id) {
        messageLogService.deleteMessageLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信消息记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wechat:message-log:query')")
    public CommonResult<MessageLogRespVO> getMessageLog(@RequestParam("id") Long id) {
        MessageLogDO messageLog = messageLogService.getMessageLog(id);
        return success(BeanUtils.toBean(messageLog, MessageLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信消息记录分页")
    @PreAuthorize("@ss.hasPermission('wechat:message-log:query')")
    public CommonResult<PageResult<MessageLogRespVO>> getMessageLogPage(@Valid MessageLogPageReqVO pageReqVO) {
        PageResult<MessageLogDO> pageResult = messageLogService.getMessageLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MessageLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信消息记录 Excel")
    @PreAuthorize("@ss.hasPermission('wechat:message-log:export')")
    @OperateLog(type = EXPORT)
    public void exportMessageLogExcel(@Valid MessageLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MessageLogDO> list = messageLogService.getMessageLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信消息记录.xls", "数据", MessageLogRespVO.class,
                        BeanUtils.toBean(list, MessageLogRespVO.class));
    }

}
