package com.jeesharp.business.modules.member.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.member.application.MemberPointsLogManageApplicationService;
import com.jeesharp.business.modules.member.entity.MemberPointsLog;
import com.jeesharp.business.modules.member.vo.MemberPointsLogExportVO;
import com.jeesharp.business.modules.member.vo.MemberPointsLogInfoVO;
import com.jeesharp.business.modules.member.vo.MemberPointsLogListVO;
import com.jeesharp.business.modules.member.vo.MemberPointsLogQueryVO;
import com.jeesharp.framework.office.excel.util.ExcelExportUtil;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会员积分流水管理端 REST 接口（只读：列表、详情、导出）。
 *
 * @author jeesharp
 * @since 2026-06-24
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/member/pointsLog/v1")
@Tag(name = "积分流水", description = "会员积分变动流水查询与导出")
@AllArgsConstructor
public class MemberPointsLogController extends BaseController {

    private final MemberPointsLogManageApplicationService memberPointsLogManageApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('member:pointsLog:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<MemberPointsLogListVO>> list(@RequestBody MemberPointsLogQueryVO query) {
        Page<MemberPointsLog> page = new Page<>(query);
        return AjaxResult.okData(memberPointsLogManageApplicationService.findPage(page, query));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "详情")
    @PreAuthorize("@ss.hasPermission('member:pointsLog:info')")
    @GetMapping("info")
    public AjaxResult<MemberPointsLogInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(memberPointsLogManageApplicationService.get(id));
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "导出")
    @PreAuthorize("@ss.hasPermission('member:pointsLog:export')")
    @PostMapping("export")
    public void export(@RequestBody MemberPointsLogQueryVO query, HttpServletResponse response) {
        List<MemberPointsLogExportVO> list = memberPointsLogManageApplicationService.findList(query);
        ExcelExportUtil.export(response, MemberPointsLogExportVO.class, list, "积分流水");
    }
}
