package org.dromara.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.domain.vo.SenFlowDetailVo;
import org.dromara.domain.bo.SenFlowDetailBo;
import org.dromara.service.ISenFlowDetailService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 流量明细
 *
 * @author luogang
 * @date 2024-09-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sense/flowDetail")
public class SenFlowDetailController extends BaseController {

    private final ISenFlowDetailService senFlowDetailService;

    /**
     * 查询流量明细列表
     */
    @SaCheckPermission("sense:flowDetail:list")
    @GetMapping("/list")
    public TableDataInfo<SenFlowDetailVo> list(SenFlowDetailBo bo, PageQuery pageQuery) {
        return senFlowDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出流量明细列表
     */
    @SaCheckPermission("sense:flowDetail:export")
    @Log(title = "流量明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SenFlowDetailBo bo, HttpServletResponse response) {
        List<SenFlowDetailVo> list = senFlowDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "流量明细", SenFlowDetailVo.class, response);
    }

    /**
     * 获取流量明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sense:flowDetail:query")
    @GetMapping("/{id}")
    public R<SenFlowDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(senFlowDetailService.queryById(id));
    }

    /**
     * 新增流量明细
     */
    @SaCheckPermission("sense:flowDetail:add")
    @Log(title = "流量明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SenFlowDetailBo bo) {
        return toAjax(senFlowDetailService.insertByBo(bo));
    }

    /**
     * 修改流量明细
     */
    @SaCheckPermission("sense:flowDetail:edit")
    @Log(title = "流量明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SenFlowDetailBo bo) {
        return toAjax(senFlowDetailService.updateByBo(bo));
    }

    /**
     * 删除流量明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("sense:flowDetail:remove")
    @Log(title = "流量明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(senFlowDetailService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取流量前3的路段流量折线图数据
     */
    @SaCheckPermission("sense:flowDetail:list")
    @GetMapping("/lineCharts")
    public R<List<Map<String,Object>>> lineCharts() {
        return R.ok(senFlowDetailService.lineCharts());
    }
}
