package com.gxwebsoft.glt.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.glt.service.GltUserTicketLogService;
import com.gxwebsoft.glt.entity.GltUserTicketLog;
import com.gxwebsoft.glt.param.GltUserTicketLogParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消费日志控制器
 *
 * @author 科技小王子
 * @since 2026-02-03 18:55:55
 */
@Tag(name = "消费日志管理")
@RestController
@RequestMapping("/api/glt/glt-user-ticket-log")
public class GltUserTicketLogController extends BaseController {
    @Resource
    private GltUserTicketLogService gltUserTicketLogService;

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:list')")
    @Operation(summary = "分页查询消费日志")
    @GetMapping("/page")
    public ApiResult<PageResult<GltUserTicketLog>> page(GltUserTicketLogParam param) {
        // 使用关联查询
        return success(gltUserTicketLogService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:list')")
    @Operation(summary = "查询全部消费日志")
    @GetMapping()
    public ApiResult<List<GltUserTicketLog>> list(GltUserTicketLogParam param) {
        // 使用关联查询
        return success(gltUserTicketLogService.listRel(param));
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:list')")
    @Operation(summary = "根据id查询消费日志")
    @GetMapping("/{id}")
    public ApiResult<GltUserTicketLog> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(gltUserTicketLogService.getByIdRel(id));
    }

    @Operation(summary = "添加消费日志")
    @PostMapping()
    public ApiResult<?> save(@RequestBody GltUserTicketLog gltUserTicketLog) {
        // 记录当前登录用户id
         User loginUser = getLoginUser();
         if (loginUser != null) {
         gltUserTicketLog.setUserId(loginUser.getUserId());
         }
        if (gltUserTicketLogService.save(gltUserTicketLog)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:update')")
    @OperationLog
    @Operation(summary = "修改消费日志")
    @PutMapping()
    public ApiResult<?> update(@RequestBody GltUserTicketLog gltUserTicketLog) {
        if (gltUserTicketLogService.updateById(gltUserTicketLog)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:remove')")
    @OperationLog
    @Operation(summary = "删除消费日志")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (gltUserTicketLogService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:save')")
    @OperationLog
    @Operation(summary = "批量添加消费日志")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<GltUserTicketLog> list) {
        if (gltUserTicketLogService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:update')")
    @OperationLog
    @Operation(summary = "批量修改消费日志")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<GltUserTicketLog> batchParam) {
        if (batchParam.update(gltUserTicketLogService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('glt:gltUserTicketLog:remove')")
    @OperationLog
    @Operation(summary = "批量删除消费日志")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (gltUserTicketLogService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
