package org.dromara.elevator.controller;

import java.util.ArrayList;
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.common.core.constant.Constants;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.elevator.domain.vo.*;
import org.dromara.elevator.listener.ElevatorInfoImportListener;
import org.springframework.http.MediaType;
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.bo.EElevatorInfoBo;
import org.dromara.elevator.service.IEElevatorInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 电梯基础信息;
 *
 * @author zy
 * @date 2024-07-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/elevator/info")
public class EElevatorInfoController extends BaseController {

    private final IEElevatorInfoService eElevatorInfoService;

    /**
     * 查询电梯基础信息;列表
     */
    @SaCheckPermission("elevator:info:list")
    @GetMapping("/list")
    public TableDataInfo<EElevatorInfoVo> list(EElevatorInfoBo bo, PageQuery pageQuery) {
        return eElevatorInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯基础信息;列表
     */
    @SaCheckPermission("elevator:info:export")
    @Log(title = "电梯基础信息;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EElevatorInfoBo bo, HttpServletResponse response) {
        List<EElevatorInfoVo> list = eElevatorInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯基础信息;", EElevatorInfoVo.class, response);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "电梯基础数据", EElevatorInfoImportVo.class, response);
    }

    /**
     * 导入电梯基础数据
     *
     * @param file          电梯基础数据
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "电梯基础信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("elevator:info:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<EElevatorInfoImportVo> result = ExcelUtil.importExcel(file.getInputStream(), EElevatorInfoImportVo.class, new ElevatorInfoImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取电梯基础信息;详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:info:query")
    @GetMapping("/{id}")
    public R<EElevatorInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eElevatorInfoService.queryById(id));
    }

    /**
     * 根据电梯编号获取电梯信息3333
     *
     * @param elevatorNum 电梯编号
     */
    @SaCheckPermission("elevator:info:query")
    @GetMapping("/detail/{elevatorNum}")
    public R<EElevatorInfoVo> getElevatorInfo(@PathVariable String elevatorNum) {
        return R.ok(eElevatorInfoService.selectByElevatorNum(elevatorNum));
    }

    /**
     * 新增电梯基础信息;
     */
    @SaCheckPermission("elevator:info:add")
    @Log(title = "电梯基础信息;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EElevatorInfoBo bo) {
        return toAjax(eElevatorInfoService.insertByBo(bo));
    }

    /**
     * 修改电梯基础信息;
     */
    @SaCheckPermission("elevator:info:edit")
    @Log(title = "电梯基础信息;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EElevatorInfoBo bo) {
        return toAjax(eElevatorInfoService.updateByBo(bo));
    }

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

    /**
     * 查询电梯品牌统计
     */
    @SaCheckPermission("elevator:info:list")
    @GetMapping("/brand/statistics/chart/list")
    public R<List<EElevatorBrandStatisticsVo>> brandStatisticsChartList(EElevatorInfoBo bo) {
        return R.ok(eElevatorInfoService.brandStatisticsChartList(bo));
    }

    /**
     * 查询电梯使用场景统计
     */
    @SaCheckPermission("elevator:info:list")
    @GetMapping("/scenario/statistics/chart/list")
    public R<List<EElevatorUseScenarioStatisticsVo>> scenarioStatisticsChartList(EElevatorInfoBo bo) {
        return R.ok(eElevatorInfoService.scenarioStatisticsChartList(bo));
    }

    /**
     * 查询电梯梯龄统计
     */
    @SaCheckPermission("elevator:info:list")
    @GetMapping("/scenario/statistics/list")
    public R<List<EElevatorAgeStatisticsVo>> eleAgeStatisticsChartList(EElevatorInfoBo bo) {
        return R.ok(eElevatorInfoService.eleAgeStatisticsChartList(bo));
    }

    /**
     * 查询电梯状态统计;列表
     */
    @SaCheckPermission("elevator:info:list")
    @GetMapping("/state/statistics/list")
    public TableDataInfo<EElevatorStateStatisticsVo> stateStatisticsList(EElevatorInfoBo bo, PageQuery pageQuery) {
        return eElevatorInfoService.queryPageStateStatisticsList(bo, pageQuery);
    }

    /**
     * 电梯综合统计查询
     */
    @SaCheckPermission("elevator:info:list")
    @GetMapping("/comprehensive/statistics/list")
    public R<EElevatorComprehensiveStatisticsVo> comprehensiveStatisticsList(EElevatorInfoBo bo) {
        return R.ok(eElevatorInfoService.comprehensiveStatisticsList(bo));
    }

    /**
     * 获取电梯实时状态信息
     *
     * @param elevatorNum 电梯编号
     */
    @SaCheckPermission("elevator:info:query")
    @GetMapping("/real/{elevatorNum}")
    public R<ElevatorRealInfoVo> getInfo(@NotBlank(message = "电梯编号不能为空")
                                         @PathVariable String elevatorNum) {
        return R.ok(eElevatorInfoService.queryElevatorRealInfo(elevatorNum));
    }

    /**
     * 获取地图区域信息
     *
     * @param code 区域编号
     */
    @GetMapping("/area/{code}")
    public R<String> getAreaInfo(@PathVariable String code) {
        return R.ok(Constants.SUCCESS, eElevatorInfoService.getAreaInfo(code));
    }

    /**
     * 获取视频监控信息
     *
     * @param elevatorNum 电梯编号
     */
    @GetMapping("/video/monitor/{elevatorNum}")
    public R<String> getVideoMonitorInfo(@PathVariable String elevatorNum) {
        return R.ok(Constants.SUCCESS, eElevatorInfoService.getVideoMonitorInfo(elevatorNum));
    }


}
