/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.commons.Constant;
import com.tofly.xrtygis.em.ExcelExportFileTypeEnum;
import com.tofly.xrtygis.entity.DmaRemoteMeter;
import com.tofly.xrtygis.entity.DmaUnfilledWater;
import com.tofly.xrtygis.entity.Dmaregion;
import com.tofly.xrtygis.service.DmaRemoteMeterService;
import com.tofly.xrtygis.service.DmaregionService;
import com.tofly.xrtygis.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DMA分区数据填报远传记录表
 *
 * @author admin
 * @date Tue May 17 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dmaremotemeter")
@Api(tags="DMA分区数据填报远传记录表接口")
public class DmaRemoteMeterController {

    private final DmaRemoteMeterService dmaRemoteMeterService;
    private final DmaregionService dmaregionService;
    /**
    * 分页查询
    * @param page 分页对象
    * @param dmaRemoteMeter DMA分区数据填报远传记录表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "export", value = "是否导出 1导出", required = false, dataType = "String")
    })
    public ResultRespone getDmaRemoteMeterPage(HttpServletResponse response, String export, Page page, @ModelAttribute DmaRemoteMeter dmaRemoteMeter) {
        if(!"1".equals(export)){
            return ResultRespone.success(dmaRemoteMeterService.page(page, new QueryWrapper<DmaRemoteMeter>().lambda()
                    .eq(null!=dmaRemoteMeter.getDmaId(),DmaRemoteMeter::getDmaId,dmaRemoteMeter.getDmaId())
                    .eq(null!=dmaRemoteMeter.getNowVal(),DmaRemoteMeter::getNowVal,dmaRemoteMeter.getNowVal())
                    .ge(null!=dmaRemoteMeter.getStartTime(),DmaRemoteMeter::getCreateTime,dmaRemoteMeter.getStartTime())
                    .le(null!=dmaRemoteMeter.getEndTime(),DmaRemoteMeter::getCreateTime,dmaRemoteMeter.getEndTime())));
        }else {
            List<DmaRemoteMeter> list = dmaRemoteMeterService.list(new QueryWrapper<DmaRemoteMeter>().lambda()
                    .eq(null != dmaRemoteMeter.getDmaId(), DmaRemoteMeter::getDmaId, dmaRemoteMeter.getDmaId())
                    .eq(null != dmaRemoteMeter.getNowVal(), DmaRemoteMeter::getNowVal, dmaRemoteMeter.getNowVal())
                    .ge(null != dmaRemoteMeter.getStartTime(), DmaRemoteMeter::getCreateTime, dmaRemoteMeter.getStartTime())
                    .le(null != dmaRemoteMeter.getEndTime(), DmaRemoteMeter::getCreateTime, dmaRemoteMeter.getEndTime()));

            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.EXPORT_DMA_REMOTE_METER;
            //将查询的结果转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;
        }

    }


    /**
     * 通过id查询DMA分区数据填报远传记录表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(dmaRemoteMeterService.getById(id));
    }

    /**
     * 获取历史数据中的填报读数
     * @param tableId tableId
     * @return ResultRespone
     */
    @GetMapping("getHisTableReading" )
    @ApiOperation(value = "获取历史数据中的填报读数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", value = "区域表id", required = true, dataType = "Long")
    })
    public ResultRespone getHisTableReading(Long tableId) {
        return ResultRespone.success(dmaRemoteMeterService.getHisTableReading(tableId));
    }

    /**
     * 新增DMA分区数据填报远传记录表
     * @param dmaRemoteMeter DMA分区数据填报远传记录表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增DMA分区数据填报远传记录表")
    @PostMapping
    public ResultRespone save(@RequestBody DmaRemoteMeter dmaRemoteMeter) {
        Dmaregion dmaregion = dmaregionService.getById(dmaRemoteMeter.getDmaId());
        dmaRemoteMeter.setDmaCode(dmaregion.getDmaCode());
        dmaRemoteMeter.setDmaName(dmaregion.getDmaName());
        dmaRemoteMeter.setDmaLevel(dmaregion.getPartitionLevel());
      return ResultRespone.success(dmaRemoteMeterService.save(dmaRemoteMeter));
    }

    /**
     * 修改DMA分区数据填报远传记录表
     * @param dmaRemoteMeter DMA分区数据填报远传记录表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改DMA分区数据填报远传记录表")
    @PutMapping
    public ResultRespone updateById(@RequestBody DmaRemoteMeter dmaRemoteMeter) {
      return ResultRespone.success(dmaRemoteMeterService.updateById(dmaRemoteMeter));
    }


    /**
     * 通过id批量删除DMA分区数据填报远传记录表
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID批量删除DMA分区数据填报远传记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(dmaRemoteMeterService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
