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.ConsumeLog;
import com.ruoyi.cai.service.ConsumeLogService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
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 com.ruoyi.common.enums.BusinessType;
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;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/consumeLog")
public class ConsumeLogController extends BaseController {

    private final ConsumeLogService consumeLogService;

    @SaCheckPermission("cai:consumeLog:list")
    @GetMapping("/list")
    public TableDataInfo<ConsumeLog> list(ConsumeLog bo, PageQuery pageQuery) {
        Page<ConsumeLog> page = consumeLogService.page(pageQuery.build(),
                Wrappers.lambdaQuery(bo).orderByDesc(ConsumeLog::getCreateTime));
        return TableDataInfo.build(page);
    }

    @SaCheckPermission("cai:consumeLog:query")
    @GetMapping("/{id}")
    public R<ConsumeLog> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(consumeLogService.getById(id));
    }

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

    @SaCheckPermission("cai:consumeLog:edit")
    @Log(title = "计算分销", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/consumer")
    public R<Void> consumer(Long id) {
        consumeLogService.dealFenxiao(id,false);
        return R.ok();
    }

}
