package com.tofly.xrtygis.custom.controller.dma;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.xrtygis.commons.Constant;

import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.em.ExcelExportFileTypeEnum;
import com.tofly.xrtygis.entity.dto.AnalyzeDto;
import com.tofly.xrtygis.entity.dto.DmaTableDto;
import com.tofly.xrtygis.custom.service.dma.CustomDmaTableService;
import com.tofly.xrtygis.entity.Dmatable;
import com.tofly.xrtygis.entity.Dmatablechange;
import com.tofly.xrtygis.entity.vo.UserWaterMeterAnalyzeVo;
import com.tofly.xrtygis.entity.vo.UserWaterMeterInfoVo;
import com.tofly.xrtygis.service.DmatableService;
import com.tofly.xrtygis.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author 20126
 */
@RestController
@AllArgsConstructor
@Api(tags = "分区计量--区域表管理")
@RequestMapping("/dmaTable")
public class CustomDmatableController {

    private final CustomDmaTableService customDmaTableService;
    private final DmatableService dmatableService;

    @PostMapping("/insertTableInfo")
    @ApiOperation(value = "添加表信息")
    public ResultRespone insertTableInfo(@RequestBody Dmatable dmatable) {
        return ResultRespone.success(customDmaTableService.insertTableInfo(dmatable));
    }


    @DeleteMapping("/deleteTableInfo")
    @ApiOperation(value = "删除表信息")
    public ResultRespone deleteTableInfo(String ids) {
        return ResultRespone.success(customDmaTableService.deleteTableInfo(ids));
    }

    @PutMapping("/updateTableInfo")
    @ApiOperation(value = "修改表信息")
    public ResultRespone updateTableInfo(@RequestBody Dmatable dmatable) {
        return ResultRespone.success(customDmaTableService.updateTableInfo(dmatable));
    }

    @GetMapping("/selectTableInfo")
    @ApiOperation(value = "查看表信息")
    public ResultRespone selectTableInfo(Page page, Long id, String dto) {
        return ResultRespone.success(customDmaTableService.selectTableInfo(page, id, dto));
    }

    @GetMapping("/viewIndex")
    @ApiOperation(value = "查询符合条件指标编号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "站点id", readOnly = true, dataType = "long"),
    })
    public ResultRespone viewIndex(Long stationId) {
        return ResultRespone.success(customDmaTableService.viewIndex(stationId));
    }

    @PostMapping("/bindIndex")
    @ApiOperation("区域表绑定指标")
    public ResultRespone bindIndex(Long tableId, Long itid, String itype) {
        return ResultRespone.success(customDmaTableService.bindIndex(tableId, itid, itype));
    }

    @DeleteMapping("/untieIndex")
    @ApiOperation("区域表解绑指标")
    public ResultRespone untieIndex(String id) {
        return ResultRespone.success(customDmaTableService.untieIndex(id));
    }


    @GetMapping("/bindTableInfo")
    @ApiOperation("查看绑定表信息")
    @ApiImplicitParam(name = "id", value = "配置表id", required = true, dataType = "long")
    public ResultRespone getBindTableInfo(Page page, Long id) {
        return ResultRespone.success(customDmaTableService.getBindTableInfo(page, id));
    }

    @ApiOperation(value = "区域表导出")
    @GetMapping("/exportTableInfo")
    public void exportTableInfo(HttpServletResponse response, Page page, DmaTableDto dmaTableDto) {
        customDmaTableService.exportTableInfo(response, page, dmaTableDto);
    }

    @GetMapping("/oldTableData")
    @ApiOperation("换表-旧表数据")
    @ApiImplicitParam(name = "id", value = "配置表id", required = true, dataType = "long")
    public ResultRespone oldTableData(Long id) {
        return ResultRespone.success(customDmaTableService.oldTableData(id));
    }

    @GetMapping("/historicalTraffic")
    @ApiOperation("换表-历史流量")
    @ApiImplicitParam(name = "id", value = "配置表id", required = true, dataType = "long")
    public ResultRespone historicalTraffic(Long id, String start, String end) {
        return ResultRespone.success(customDmaTableService.historicalTraffic(id, start, end));
    }

    @GetMapping("/avgFlow")
    @ApiOperation("换表-平均流量")
    @ApiImplicitParam(name = "id", value = "配置表id", required = true, dataType = "long")
    public ResultRespone avgFlow(Long id, String start, String end) {
        return ResultRespone.success(customDmaTableService.avgFlow(id, start, end));
    }

    @PostMapping("/changeTable")
    @ApiOperation("换表")
    public ResultRespone changeTable(@RequestBody Dmatablechange dmatablechange) {
        return ResultRespone.success(customDmaTableService.changeTable(dmatablechange));
    }

    @GetMapping("/viewFlowMeter")
    @ApiOperation(value = "查看流量计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "export", value = "是否导出 1导出", required = false, dataType = "String")
    })
    public ResultRespone viewFlowMeter(HttpServletResponse response, String export,Page page, DmaTableDto dmaTableDto) {
        if(!"1".equals(export)){
            return ResultRespone.success(customDmaTableService.viewFlowMeter(page, dmaTableDto));
        }else {
            List<Dmatable> dmatableList = customDmaTableService.getDmatableList(dmaTableDto);
            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.EXPORT_VIEW_FLOW_METER;
            //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
            // 从模型类型枚举根据templateServiceType获取策略对应模板数据
            Map<String, Object> dataMap = new HashMap<>(12);
            dataMap.put("list", dmatableList);
            ExcelUtil.export(dataMap, excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
            return null;
        }

    }

    @GetMapping("/updateFlowMeter")
    @ApiOperation(value = "修改流量计")
    public ResultRespone updateFlowMeter(@Valid Dmatable dmaTable) {
        return ResultRespone.success(dmatableService.updateById(dmaTable));
    }

    @GetMapping("/viewFlowIndexById/{id}")
    @ApiOperation(value = "通过区域表id查询流量指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone viewFlowIndexById(@PathVariable("id") Long id) {
        return ResultRespone.success(customDmaTableService.viewFlowIndexById(id));
    }


    @GetMapping("/getUserWaterMeterInfo")
    @ApiOperation(value = "分页获取用户水表信息台账信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "export", value = "是否导出 1导出", required = false, dataType = "String")
    })
    public ResultRespone getUserWaterMeterInfo(HttpServletResponse response, String export,Page page,String likeStr) {
        if(!"1".equals(export)){
            return ResultRespone.success(customDmaTableService.getUserWaterMeterInfo(page, likeStr));
        }else {
            List<UserWaterMeterInfoVo> list = customDmaTableService.getUserWaterMeterInfo(likeStr);
            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.EXPORT_USER_WATER_METER_INFO;
            //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
            // 从模型类型枚举根据templateServiceType获取策略对应模板数据
            Map<String, Object> dataMap = new HashMap<>(12);
            dataMap.put("list", list);
            ExcelUtil.export(dataMap, excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
            return null;
        }

    }
    @PostMapping("/getUserWaterMeterAnalyze")
    @ApiOperation(value = "获取用户水表用水量分析")
    public ResultRespone getUserWaterMeterAnalyze(@RequestBody AnalyzeDto dto) {
        Date startTime = dto.getStartTime();
        Date endTime = dto.getEndTime();
        String userCode = dto.getUserCode();
        String waterMeterCode = dto.getWaterMeterCode();
        List<UserWaterMeterAnalyzeVo> userWaterMeterAnalyze = customDmaTableService.getUserWaterMeterAnalyze(userCode, waterMeterCode, startTime, endTime);
        if(null!=userWaterMeterAnalyze &&userWaterMeterAnalyze.size()>0){
            return ResultRespone.success(userWaterMeterAnalyze);
        }
        return ResultRespone.success(new ArrayList<>());
    }

}
