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

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.entity.dma.ConstomCondition;
import com.tofly.xrtygis.custom.service.CustomAreaService;
import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.entity.Dmaandobser;
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 java.math.BigDecimal;
import java.time.LocalDateTime;
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;

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

    @GetMapping("getYearPressureVal")
    @ApiOperation(value = "获取当前设备的年平均压力")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceIds", value = "设备id列表,多个以,分隔", required = true, dataType = "String")
    })
    public ResultRespone getYearPressureVal(String deviceIds) {
        BigDecimal yearPressure = BigDecimal.ZERO;
        if(StringUtils.isNotBlank(deviceIds)){
            List<Long> deviceIdList = Arrays.stream(deviceIds.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
            String year = DateTimeUtil.getLocalDateTime(LocalDateTime.now(), DateTimeUtil.YEAR);
            yearPressure =  areaService.getYearPressure(year, deviceIdList);
        }

        return ResultRespone.success(yearPressure==null? BigDecimal.ZERO:yearPressure);
    }

    @PostMapping("/tableCoord")
    @ApiOperation(value = "区域表坐标查看")
    public ResultRespone tableCoord() {
        return ResultRespone.success(areaService.tableCoord());
    }
    @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());
    }

    @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));
    }
}
