package com.tofly.spire.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.scada.common.BaseController;
import com.tofly.spire.entity.SpireBigGrid;
import com.tofly.spire.entity.dto.SpireBigGridDto;
import com.tofly.spire.service.SpireBigGridService;
import com.tofly.spire.service.SpireBigGridXxlJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 大表（管网）(SpireBigGrid)表控制层
 *
 * @author HaiQiu
 * @since 2022-11-11 09:16:06
 */
@Api(tags = "大表（管网）")
@RestController
@RequestMapping("spireBigGrid")
public class SpireBigGridController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SpireBigGridService spireBigGridService;

    @Resource
    private SpireBigGridXxlJobService spireBigGridXxlJobService;

    /**
     * 分页查询数据
     *
     * @param page         分页对象
     * @param spireBigGrid 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询数据")
    @GetMapping("/page")
    public ResultRespone pageList(Page page, SpireBigGrid spireBigGrid) {
        return decide(spireBigGridService.pageList(page, spireBigGrid));
    }

    /**
     * 查询所有数据
     *
     * @param spireBigGrid 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "查询所有数据")
    @GetMapping("/list")
    public ResultRespone allList(SpireBigGrid spireBigGrid) {
        return decide(spireBigGridService.allList(spireBigGrid));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据")
    @ApiImplicitParam(name = "id", value = "数据ID")
    @GetMapping("{id}")
    public ResultRespone get(@PathVariable Serializable id) {
        return decide(spireBigGridService.get(id));
    }

    /**
     * 新增数据
     *
     * @param spireBigGridDto 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据")
    @PostMapping
    public ResultRespone insert(@Validated @RequestBody SpireBigGridDto spireBigGridDto) {
        spireBigGridService.insert(spireBigGridDto);
        return decide("新增成功");
    }

    /**
     * 修改数据
     *
     * @param spireBigGridDto 实体
     * @return 修改结果
     */
    @ApiOperation(value = "修改数据")
    @PutMapping
    public ResultRespone edit(@Validated @RequestBody SpireBigGridDto spireBigGridDto) {
        spireBigGridService.edit(spireBigGridDto);
        return decide("修改成功");
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除数据")
    @ApiImplicitParam(name = "idList", value = "数据ID数组集合")
    @DeleteMapping
    public ResultRespone delete(@RequestParam("idList") List<Long> idList) {
        spireBigGridService.delete(idList);
        return decide("删除成功");
    }

    /**
     * 数据导入Excel模板下载
     */
    @ApiOperation(value = "模版下载")
    @GetMapping("/template")
    public void template() {
        spireBigGridService.template();
    }

    /**
     * Excel数据导入
     *
     * @param file 导入的Excel数据文件
     * @return 导入结果
     */
    @ApiOperation(value = "数据导入")
    @ApiImplicitParam(name = "file", value = "Excel文件", required = true)
    @PostMapping("/import")
    public ResultRespone importData(@RequestParam(name = "file") MultipartFile file) {
        spireBigGridService.importData(file);
        return decide("导入成功");
    }


    /**
     * 数据导出
     *
     * @param page         分页
     * @param spireBigGrid 查询模型
     * @param isAll        是否导出全部数据，true是，false否
     */
    @ApiOperation(value = "数据导出")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "isAll", value = "是否导出全部数据，true是，false不是"),
    })
    @GetMapping("/export")
    public void export(Page page, SpireBigGrid spireBigGrid, Boolean isAll) {
        spireBigGridService.export(page, spireBigGrid, isAll);
    }



    @ApiOperation(value = "同步管网数据,多线程")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "date", value = "日期 yyyy-mm-dd"),
           @ApiImplicitParam(name = "sn", value = "设备号,多个逗号"),
    })
    @GetMapping("/asyncBulkMeterData")
    public ResultRespone getBulkMeterDataNew(@RequestParam(required = false) String date,
                                       @RequestParam(required = false) String sn) {
        spireBigGridXxlJobService.synBulkMeterThread(date, sn);
        return ResultRespone.success(true);
    }
    /**
     * 手动分发管网数据到历史表，指标表等
     *
     */
    @ApiOperation(value = "手动分发管网数据到历史表，指标表等")
   @GetMapping("/asyncData")
    public ResultRespone synData() {
        spireBigGridXxlJobService.synData();
        return ResultRespone.success(true);
    }

    @ApiOperation(value = "同步管网实时数据")
    @GetMapping("/synBulkMeterMonitorDataThread")
    public ResultRespone synBulkMeterMonitorDataThread() {
        spireBigGridXxlJobService.synBulkMeterMonitorDataThread();
        return ResultRespone.success(true);
    }
    /**
     * 手动同步管网大表基础数据，并且同步地图坐标到设备表
     *
     */
    @ApiOperation(value = "手动同步管网大表基础数据，并且同步地图坐标到设备表")
    @GetMapping("/synBulkMeterBigBaseData")
    public ResultRespone synBulkMeterBigBaseData() {
        spireBigGridXxlJobService.getBulkMeterBigBaseData();
        spireBigGridXxlJobService.getMapGdDataNew();
        spireBigGridXxlJobService.synDeviceArchiveManage();
        return ResultRespone.success(true);
    }


}

