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.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.custom.entity.dma.ConstomCondition;
import com.tofly.xrtygis.custom.service.CustomAreaService;
import com.tofly.xrtygis.em.Constant;
import com.tofly.xrtygis.entity.DmaStatistical;
import com.tofly.xrtygis.entity.Dmaandobser;
import com.tofly.xrtygis.entity.vo.DmaLeakageVo;
import com.tofly.xrtygis.service.DmaandobserService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FlashWJL
 * @date 2021/3/29 15:13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/area")
@Api(tags = "分区计量--DMA分区")
public class CustomAreaController {

    private final CustomAreaService areaService;
    private final DmaandobserService dmaandobserService;

    //(title = "增加分区/传了ID就是修改")
    @PostMapping("/partition")
    @ApiOperation(value = "增加分区/传了ID就是修改")
    public ResultRespone addPartition(String info) {
        return ResultRespone.success(areaService.addPartition(info));
    }

    //(title = "分区中新增管线管材")
    @PostMapping("/addPipe")
    @ApiOperation(value = "分区中新增管线管材")
    public ResultRespone addPipe(String info) {
        return ResultRespone.success(areaService.addPipe(info));
    }
    //(title = "删除分区中管线管材")
    @DeleteMapping("/delPipe")
    @ApiOperation(value = "删除分区中管线管材")
    public ResultRespone delPipe(Long parId , Long pipeId) {
        return ResultRespone.success(areaService.delPipe(parId , pipeId));
    }


    //(title = "分区与区域表绑定")
    @PostMapping("/bingParAndObser")
    @ApiOperation(value = "分区与区域表绑定")
    public ResultRespone bingParAndArea(String info ) {
        return ResultRespone.success(areaService.bingParAndArea(info));
    }
    //(title = "删除分区与区域表绑定")
    @PostMapping("/delBing")
    @ApiOperation(value = "删除分区与区域表绑定")
    public ResultRespone delBing(String ids ) {
        return ResultRespone.success(areaService.delBing(ids));
    }
    //(title = "查看分区与区域表绑定")
    @PostMapping("/getBing")
    @ApiOperation(value = "查看分区与区域表绑定")
    public ResultRespone getBing(Page page , Dmaandobser dmaandobser) {
        return ResultRespone.success(areaService.getBing(page , dmaandobser));
    }


    @PostMapping("/tableCoord")
    @ApiOperation(value = "区域表坐标查看")
    public ResultRespone tableCoord(Page page) {
        return ResultRespone.success(areaService.tableCoord(page ));
    }
    @GetMapping("/partition")
    @ApiOperation(value = "分区列表，树状结构")
    public ResultRespone getPartition(String partitionName, Long partitionId, Page page) {
        return ResultRespone.success(areaService.getPartition(partitionName,partitionId,page));
    }
    @GetMapping("/partitionNum")
    @ApiOperation(value = "获取不同等级分区个数")
    public ResultRespone partitionNum() {
        return ResultRespone.success(areaService.partitionNum());
    }
    @GetMapping("/partitionNumSynthe")
    @ApiOperation(value = "获取不同等级分区个数(综合展示的)")
    public ResultRespone partitionNumSynthe() {
        return ResultRespone.success(areaService.partitionNumSynthe());
    }


    //(title = "根据ID删除分区(可多个ID)")
    @DeleteMapping("/partition")
    @ApiOperation(value = "根据ID删除分区(可多个ID)")
    public ResultRespone delPartition(String ids) {
        return ResultRespone.success(areaService.delPartition(ids));
    }



    @GetMapping("/partitionById")
    @ApiOperation(value = "根据ID 、输入框模糊查询 查看分区所有子级列表（有分区列表导出功能）")
    public ResultRespone getPartitionById(HttpServletResponse response , Page page , ConstomCondition condition) {
        return ResultRespone.success(areaService.getPartitionById(response , page, condition));
    }
    @GetMapping("/getDetailById")
    @ApiOperation(value = "根据ID查看分区详情")
    public ResultRespone getDetailById( Long id ) {
        return ResultRespone.success(areaService.getDetailById(id));
    }


    @GetMapping("/getBingCbb")
    @ApiOperation(value = "获取抄表本(未绑定、已绑定)")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "dmaId", value = "DMA分区ID", required = true, dataType = "long")
    )
    public ResultRespone getBingCbb(Long dmaId) {
        return ResultRespone.success(areaService.getBingCbb(dmaId));
    }
    @PostMapping("/bingCbb")
    @ApiOperation(value = "绑定抄表本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dmaId", value = "DMA分区ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "cbbList", value = "抄表本编码，多个逗号隔开", required = true, dataType = "string")
    })
    public ResultRespone bingCbb(Long dmaId  , String cbbList) {
        return ResultRespone.success(areaService.bingCbb(dmaId , cbbList));
    }

    @GetMapping("/areaLevelInfo")
    @ApiOperation("获取同级片区信息")
    public ResultRespone areaLevelInfo(String level){
        return ResultRespone.success(areaService.areaLevelInfo(level));
    }

    @GetMapping("/getRegin")
    @ApiOperation("根据分区等级条件获取分区ID、名称、geometry")
    public ResultRespone getReginPage(Page page , Integer level){
        return ResultRespone.success(areaService.getReginPage(page , level));
    }

    @GetMapping("/leakageRateAnalyse")
    @ApiOperation("漏损率分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dmaIds", value = "分区ID（多个用,分隔）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "开始时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束时间 yyyy-MM-dd HH:mm:ss", required = true, dataType = "String"),
            @ApiImplicitParam(name = "isExport", value = "是否导出（0否;1是）", required = true, dataType = "String")
    })
    public ResultRespone<List<DmaLeakageVo>> leakageRateAnalyse(String dmaIds, String startDate, String endDate){
        List<Long> dmaIdList = null;
        if (StringUtils.isNotBlank(dmaIds)) {
            dmaIdList = Arrays.stream(dmaIds.split(Constant.CSV)).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        }
        return ResultRespone.success(dmaandobserService.leakageRateAnalyse(dmaIdList, startDate, endDate));
    }

    @GetMapping("/getDmaStatistical")
    @ApiOperation(value = "获取分区每日统计数据")
    @ToFlyAppLog(title = "获取分区每日统计数据")
    public ResultRespone<List<DmaStatistical>> getDmaStatistical(@ApiParam("开始时间（yyyy-MM-dd HH:mm:ss）") @RequestParam(value = "startDate") String startDate,
                                                                 @ApiParam("结束时间（yyyy-MM-dd HH:mm:ss）") @RequestParam(value = "endDate") String endDate,
                                                                 @ApiParam("分区ID（多个用,分隔）") @RequestParam(value = "dmaIds", required = false) String dmaIds){
        try {
            return ResultRespone.success(dmaandobserService.getDmaStatistical(startDate, endDate, dmaIds));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }
    }

//    //(title = "修改分区")
//    @PutMapping("/partition")
//    @ApiOperation(value = "修改分区")
//    public ResultRespone updatePartition(@RequestBody Dmaregion region) {
//        return ResultRespone.success(areaService.updatePartition(region));
//    }

}
