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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.xrtygis.commons.CalendarHelper;
import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.entity.Dmaregion;
import com.tofly.xrtygis.entity.IndicatorCorrection;
import com.tofly.xrtygis.service.DmaregionService;
import com.tofly.xrtygis.service.IndicatorCorrectionService;
import com.tofly.xrtygis.service.NoiseJobService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
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 java.time.LocalDate;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 指标修正记录表
 *
 * @author admin
 * @date Thu Apr 28 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/indicatorcorrection")
@Api(tags="分区计量--指标修正记录表接口")
public class IndicatorCorrectionController {

    private final IndicatorCorrectionService indicatorCorrectionService;
    private final DmaregionService dmaregionService;
    private final NoiseJobService noiseJobService;
    /**
    * 分页查询
    * @param page 分页对象
    * @param indicatorCorrection 指标修正记录表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getIndicatorCorrectionPage(Page page, @ModelAttribute IndicatorCorrection indicatorCorrection) {
      return ResultRespone.success(indicatorCorrectionService.page(page, new QueryWrapper<IndicatorCorrection>().lambda()
              .ge(null!=indicatorCorrection.getStartTime(),IndicatorCorrection::getReviseDate,indicatorCorrection.getStartTime())
              .le(null!=indicatorCorrection.getEndTime(),IndicatorCorrection::getReviseDate,indicatorCorrection.getEndTime())
             .eq(null!=indicatorCorrection.getDmaId() && indicatorCorrection.getDmaId()!=0,IndicatorCorrection::getDmaId,indicatorCorrection.getDmaId())
              .like(StringUtils.isNotBlank(indicatorCorrection.getDmaName()),IndicatorCorrection::getDmaName,indicatorCorrection.getDmaName())));
    }


    /**
     * 通过id查询指标修正记录表
     * @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(indicatorCorrectionService.getById(id));
    }

    /**
     * 新增指标修正记录表
     * @param indicatorCorrection 指标修正记录表
     * @return ResultRespone
     */
    @ApiOperation(value = "新增指标修正记录表")
    @PostMapping
    public ResultRespone save(@RequestBody IndicatorCorrection indicatorCorrection) {
        // 判断所修正月份是否是当前月份 若为当前月份则不允许修改
        Date reviseDate = indicatorCorrection.getReviseDate();
        LocalDate firstDayOfCurrentSpecifiedMonth = DateTimeUtil.getFirstDayOfCurrentSpecifiedMonth(DateTimeUtil.fromLong2LocalDateTime(reviseDate.getTime()));
        LocalDate firstDayOfCurrentMonth = DateTimeUtil.getFirstDayOfCurrentMonth();
        if(0 == firstDayOfCurrentMonth.compareTo(firstDayOfCurrentSpecifiedMonth)){
            return ResultRespone.success("当前月份不允许修改");
        }
      return ResultRespone.success(indicatorCorrectionService.saveInfo(indicatorCorrection,""));
    }

    /**
     * 修改指标修正记录表
     * @param indicatorCorrection 指标修正记录表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改指标修正记录表")
    @PutMapping
    public ResultRespone updateById(@RequestBody IndicatorCorrection indicatorCorrection) {
      return ResultRespone.success(indicatorCorrectionService.updateById(indicatorCorrection));
    }



    /**
     * 通过id批量删除指标修正记录表
     * @param ids id
     * @return ResultRespone
     */
    @ApiOperation(value = "通过ID批量删除指标修正记录表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(indicatorCorrectionService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 统计值存储
     * @param reviseDate reviseDate
     * @return ResultRespone
     */
    @GetMapping("/leakageRateCalculate" )
    @ApiOperation(value = "统计值存储")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviseDate", value = "统计日期yyyy-MM-dd", required = true, dataType = "String")
    })
    public ResultRespone leakageRateCalculate(String  reviseDate) {
        try {
            int type=0;
            LocalDate localDate1 = DateTimeUtil.fromString2LocalDate(reviseDate, DateTimeUtil.DAY_FORMAT);
            Date localDate = DateTimeUtil.localDateToDate(localDate1);
            if(type==1){
                return ResultRespone.success(indicatorCorrectionService.leakageRateCalculate(localDate));
            }else{
                return ResultRespone.success(indicatorCorrectionService.leakageRateCalculateDay(localDate));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
     return ResultRespone.failed();
    }

    /**
     * 统计值存储
     * @param startDate startDate
     * @return ResultRespone
     */
    @GetMapping("/leakageRateCalculateNew" )
    @ApiOperation(value = "统计值存储New")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "统计开始日期yyyy-MM-dd", required = true, dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "统计结束日期yyyy-MM-dd", required = true, dataType = "String")
    })
    public ResultRespone leakageRateCalculateNew(String  startDate,String endDate) {
        try {
           List<String> dates=CalendarHelper.getDayBetweenDate(startDate,endDate,"yyyy-MM-dd");
            dates.forEach(date->{
                indicatorCorrectionService.leakageRateCalculateDay(CalendarHelper.ConvertStrToDate(date,"yyyy-MM-dd") );

            });

                return ResultRespone.success(true);

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultRespone.failed();
    }





    @GetMapping("/test" )
    @ApiOperation(value = "测试111")

    public ResultRespone noiseJobServices(String  reviseDate) {
        try {
            LocalDate localDate1 = DateTimeUtil.fromString2LocalDate(reviseDate, DateTimeUtil.DAY_FORMAT);
            Date localDate = DateTimeUtil.localDateToDate(localDate1);
            indicatorCorrectionService.totalCalculate(localDate);
            return ResultRespone.success();
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }

    }

}
