/*
 * 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.DmaUnfilledWater;
import com.tofly.xrtygis.entity.Dmaregion;
import com.tofly.xrtygis.entity.vo.ResultUserWaterMeterInfoVo;
import com.tofly.xrtygis.service.DmaUnfilledWaterService;
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.text.ParseException;
import java.text.SimpleDateFormat;
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("/dmaunfilledwater")
@Api(tags="DMA分区未上盘水量数据填报记录表接口")
public class DmaUnfilledWaterController {

    private final DmaUnfilledWaterService dmaUnfilledWaterService;
    private final DmaregionService dmaregionService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param dmaUnfilledWater DMA分区未上盘水量数据填报记录表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "export", value = "是否导出 1导出", required = false, dataType = "String")
    })
    public ResultRespone getDmaUnfilledWaterPage(HttpServletResponse response, String export, Page page, @ModelAttribute DmaUnfilledWater dmaUnfilledWater) {
        if(!"1".equals(export)){
            Page<DmaUnfilledWater> dmaUnfilledWaterPage = dmaUnfilledWaterService.page(page, new QueryWrapper<DmaUnfilledWater>().lambda()
                    .eq(null!=dmaUnfilledWater.getDmaId(),DmaUnfilledWater::getDmaId,dmaUnfilledWater.getDmaId())
                    .ge(null!=dmaUnfilledWater.getStartTime(),DmaUnfilledWater::getWriteDate,dmaUnfilledWater.getStartTime())
                    .le(null!=dmaUnfilledWater.getEndTime(),DmaUnfilledWater::getWriteDate,dmaUnfilledWater.getEndTime()));
            for(DmaUnfilledWater one : dmaUnfilledWaterPage.getRecords()){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                one.setWriteDateDis(sdf.format(one.getWriteDate()));
            }
            return ResultRespone.success(dmaUnfilledWaterPage);
        }else {
            List<DmaUnfilledWater> dmaUnfilledWaterList =  dmaUnfilledWaterService.list(new QueryWrapper<DmaUnfilledWater>().lambda()
                    .eq(null!=dmaUnfilledWater.getDmaId(),DmaUnfilledWater::getDmaId,dmaUnfilledWater.getDmaId())
                    .ge(null!=dmaUnfilledWater.getStartTime(),DmaUnfilledWater::getWriteDate,dmaUnfilledWater.getStartTime())
                    .le(null!=dmaUnfilledWater.getEndTime(),DmaUnfilledWater::getWriteDate,dmaUnfilledWater.getEndTime()));
            for(DmaUnfilledWater one : dmaUnfilledWaterList){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                one.setWriteDateDis(sdf.format(one.getWriteDate()));
            }
            ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.EXPORT_STATS_DMA_FILLED_WATER;
            //将查询的结果转json字符串的格式，因为导出的时候只能传一个字符串
            // 从模型类型枚举根据templateServiceType获取策略对应模板数据
            Map<String, Object> dataMap = new HashMap<>(12);
            dataMap.put("list", dmaUnfilledWaterList);
            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(dmaUnfilledWaterService.getById(id));
    }

    /**
     * 新增DMA分区未上盘水量数据填报记录表
     * @param dmaUnfilledWater DMA分区未上盘水量数据填报记录表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增DMA分区未上盘水量数据填报记录表")
    @PostMapping
    public ResultRespone save(@RequestBody DmaUnfilledWater dmaUnfilledWater) {
        Dmaregion dmaregion = dmaregionService.getById(dmaUnfilledWater.getDmaId());
        dmaUnfilledWater.setDmaCode(dmaregion.getDmaCode());
        dmaUnfilledWater.setDmaName(dmaregion.getDmaName());
        dmaUnfilledWater.setDmaLevel(dmaregion.getPartitionLevel());
        return ResultRespone.success(dmaUnfilledWaterService.save(dmaUnfilledWater));
    }

    /**
     * 修改DMA分区未上盘水量数据填报记录表
     * @param dmaUnfilledWater DMA分区未上盘水量数据填报记录表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改DMA分区未上盘水量数据填报记录表")
    @PutMapping
    public ResultRespone updateById(@RequestBody DmaUnfilledWater dmaUnfilledWater) {
      return ResultRespone.success(dmaUnfilledWaterService.updateById(dmaUnfilledWater));
    }


    /**
     * 通过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(dmaUnfilledWaterService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
