package org.dromara.elevator.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.dromara.elevator.domain.bo.BehaviorMessageStatisticsBo;
import org.dromara.elevator.domain.vo.ElevatorBehaviorStatisticsVo;
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.elevator.domain.vo.EBehaviorStatisticsVo;
import org.dromara.elevator.domain.bo.EBehaviorStatisticsBo;
import org.dromara.elevator.service.IEBehaviorStatisticsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 电梯运行统计
 *
 * @author ym
 * @date 2024-09-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/elevator/behaviorStatistics")
public class EBehaviorStatisticsController extends BaseController {

    private final IEBehaviorStatisticsService eBehaviorStatisticsService;

    /**
     * 查询电梯运行统计列表
     */
    @SaCheckPermission("elevator:behaviorStatistics:list")
    @GetMapping("/list")
    public TableDataInfo<EBehaviorStatisticsVo> list(EBehaviorStatisticsBo bo, PageQuery pageQuery) {
        return eBehaviorStatisticsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯运行统计列表
     */
    @SaCheckPermission("elevator:behaviorStatistics:export")
    @Log(title = "电梯运行统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EBehaviorStatisticsBo bo, HttpServletResponse response) {
        List<EBehaviorStatisticsVo> list = eBehaviorStatisticsService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯运行统计", EBehaviorStatisticsVo.class, response);
    }

    /**
     * 获取电梯运行统计详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:behaviorStatistics:query")
    @GetMapping("/{id}")
    public R<EBehaviorStatisticsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eBehaviorStatisticsService.queryById(id));
    }

    /**
     * 新增电梯运行统计
     */
    @SaCheckPermission("elevator:behaviorStatistics:add")
    @Log(title = "电梯运行统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EBehaviorStatisticsBo bo) {
        return toAjax(eBehaviorStatisticsService.insertByBo(bo));
    }

    /**
     * 修改电梯运行统计
     */
    @SaCheckPermission("elevator:behaviorStatistics:edit")
    @Log(title = "电梯运行统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EBehaviorStatisticsBo bo) {
        return toAjax(eBehaviorStatisticsService.updateByBo(bo));
    }

    /**
     * 删除电梯运行统计
     *
     * @param ids 主键串
     */
    @SaCheckPermission("elevator:behaviorStatistics:remove")
    @Log(title = "电梯运行统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eBehaviorStatisticsService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 电梯运行历史统计数据补充处理
     */
    @SaCheckPermission("elevator:behaviorStatistics:edit")
    @RepeatSubmit()
    @PostMapping("/history")
    public R<Void> historyStatistics(@Validated() @RequestBody BehaviorMessageStatisticsBo bo) {
        return toAjax(eBehaviorStatisticsService.historyStatistics(bo));
    }

    /**
     * 获取电梯日周月详情运行统计
     */
    @SaCheckPermission("elevator:behaviorStatistics:query")
    @GetMapping("/elevator/detail/statistics")
    public R<ElevatorBehaviorStatisticsVo> getElevatorStatisticsInfo(EBehaviorStatisticsBo bo) {
        return R.ok(eBehaviorStatisticsService.queryElevatorStatisticsInfo(bo));
    }

}
