package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.GuardLog;
import com.ruoyi.cai.service.GuardLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 守护赠送流水
 *
 * @author 77
 * @date 2023-12-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/guardLog")
public class GuardLogController extends BaseController {

    private final GuardLogService guardLogService;

    /**
     * 查询守护赠送流水列表
     */
    @SaCheckPermission("cai:guardLog:list")
    @GetMapping("/list")
    public TableDataInfo<GuardLog> list(GuardLog bo, PageQuery pageQuery) {
        Page<GuardLog> page = guardLogService.page(pageQuery.build(),
                Wrappers.lambdaQuery(bo).orderByDesc(GuardLog::getCreateTime));
        return TableDataInfo.build(page);
    }


    /**
     * 获取守护赠送流水详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:guardLog:query")
    @GetMapping("/{id}")
    public R<GuardLog> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(guardLogService.getById(id));
    }

    @GetMapping("/trace")
    public R<GuardLog> getInfoTraceId(String traceId) {
        GuardLog res = guardLogService.getOne(Wrappers.lambdaQuery(GuardLog.class).eq(GuardLog::getTraceId,traceId).last("limit 1"));
        return R.ok(res);
    }

}
