package org.dromara.biz.controller;

import java.util.List;

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.biz.domain.vo.StatDeptVo;
import org.dromara.biz.domain.bo.StatDeptBo;
import org.dromara.biz.service.IStatDeptService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 单位每日数据统计
 *
 * @author YIN
 * @date 2025-05-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/statDept")
public class StatDeptController extends BaseController {

    private final IStatDeptService statDeptService;

    /**
     * 查询单位每日数据统计列表
     */
    @SaCheckPermission("biz:statDept:list")
    @GetMapping("/list")
    public TableDataInfo<StatDeptVo> list(StatDeptBo bo, PageQuery pageQuery) {
        return statDeptService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/liststat")
    public TableDataInfo<StatDeptVo> liststat(StatDeptBo bo, PageQuery pageQuery) {
        return statDeptService.queryPageStat(bo, pageQuery);
    }

    /**
     * 导出单位每日数据统计列表
     */
    @SaCheckPermission("biz:statDept:export")
    @Log(title = "单位每日数据统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StatDeptBo bo, HttpServletResponse response) {
        List<StatDeptVo> list = statDeptService.queryList(bo);
        ExcelUtil.exportExcel(list, "单位每日数据统计", StatDeptVo.class, response);
    }

    /**
     * 获取单位每日数据统计详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("biz:statDept:query")
    @GetMapping("/{id}")
    public R<StatDeptVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(statDeptService.queryById(id));
    }

    /**
     * 新增单位每日数据统计
     */
    @SaCheckPermission("biz:statDept:add")
    @Log(title = "单位每日数据统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StatDeptBo bo) {
        return toAjax(statDeptService.insertByBo(bo));
    }

    /**
     * 修改单位每日数据统计
     */
    @SaCheckPermission("biz:statDept:edit")
    @Log(title = "单位每日数据统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StatDeptBo bo) {
        return toAjax(statDeptService.updateByBo(bo));
    }

    /**
     * 删除单位每日数据统计
     *
     * @param ids 主键串
     */
    @SaCheckPermission("biz:statDept:remove")
    @Log(title = "单位每日数据统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(statDeptService.deleteWithValidByIds(List.of(ids), true));
    }
}
