package cn.com.victorysoft.business.calc.controller;

import cn.com.victorysoft.business.calc.service.*;
import cn.com.victorysoft.business.calc.task.DataSyncTask;
import cn.com.victorysoft.business.indicator.entity.WellSectionMonth;
import cn.com.victorysoft.business.indicator.param.SectionRateWellExportParam;
import cn.com.victorysoft.business.indicator.param.WellSectionMonthParam;
import cn.com.victorysoft.business.indicator.service.LessSummaryService;
import cn.com.victorysoft.business.indicator.service.MoreSummaryService;
import cn.com.victorysoft.business.indicator.service.SectionRateSummaryService;
import cn.com.victorysoft.business.sys.entity.DateRange;
import cn.com.victorysoft.business.sys.entity.ExportResult;
import cn.com.victorysoft.business.sys.param.UnitDateRangeParam;
import cn.com.victorysoft.business.util.DateUtils;
import cn.com.victorysoft.business.util.ExcelUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(tags = "计算服务")
@Slf4j
@RestController
@RequestMapping("calc")
public class DataCalcController {
    @Resource
    private DataSyncService dataSyncService;
    @Resource
    private QualityDataSyncService qualityDataSyncService;
    @Resource
    private FlushDataSyncService flushDataSyncService;
    @Resource
    private AdjustDataSyncService adjustDataSyncService;
    @Resource
    private AbnormalDataSyncService abnormalDataSyncService;
    @Resource
    private ConditionDataSyncService conditionDataSyncService;
    @Resource
    private RealtimeService realtimeService;
    @Resource
    private SectionRateSummaryService sectionRateSummaryService;
    @Resource
    private LessSummaryService lessSummaryService;
    @Resource
    private MoreSummaryService moreSummaryService;
    @Resource
    private WellToolDataSyncService wellToolDataSyncService;
    @Resource
    private WellTubeDataSyncService wellTubeDataSyncService;

    @PostMapping("/static")
    @ApiOperation("静态数据")
    public void syncStaticData(){
        dataSyncService.syncStaticData();
    }

    @PostMapping("/daily")
    @ApiOperation("日度数据")
    public void daily(Date date){
        dataSyncService.syncWellSectionDaily(date, date);
    }

    @PostMapping("/dailyRange")
    @ApiOperation("日度阶段数据")
    public void dailyRange(Date startDate, Date endDate) {
        dataSyncService.syncWellSectionDaily(startDate, endDate);
    }

    @GetMapping("/wellSectionDaily")
    @ApiOperation("计算日度时间段的层段数据")
    public List<WellSectionMonth> wellSectionDaily(WellSectionMonthParam param){
        return dataSyncService.calcWellSectionDaily(param);
    }

    @PostMapping("/wellSectionDaily/export")
    @ApiOperation("导出日度时间段的层段数据")
    public ExportResult wellSectionDailyExport(@RequestBody SectionRateWellExportParam param){
        List<WellSectionMonth> list = dataSyncService.calcWellSectionDaily(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @PostMapping("/dailyRangeInjectType")
    @ApiOperation("更新阶段注水方式数据")
    public void dailyRangeInjectType(Date startDate, Date endDate) {
        dataSyncService.updateInjectType(startDate, endDate);
    }

    @PostMapping("/summary")
    @ApiOperation("汇总层段合格率")
    public void summary(@RequestParam(required = false) String month) {
        // 默认上月
        if (month == null || "".equals(month)) {
            DateRange dateRange = DateUtils.getLastMonth(new Date());
            month = dateRange.getName();
        }
        // 汇总月度
        dataSyncService.summaryWellSectionMonth(month);
        // 计算层段合格率
        sectionRateSummaryService.summary(month);
        // 汇总超欠注 暂停，没有原因和措施，不能汇总
//        lessSummaryService.summary(month);
//        lessSummaryService.summaryMeasure(month);
//        moreSummaryService.summary(month);
//        moreSummaryService.summaryMeasure(month);
    }

    @PostMapping("/quality")
    @ApiOperation("水质检测数据")
    public void quality(Date startDate, Date endDate){
        qualityDataSyncService.syncQualityData(startDate, endDate);
    }

    @PostMapping("/flush")
    @ApiOperation("洗井数据")
    public void flush(Date startDate, Date endDate){
        flushDataSyncService.syncFlushRecord(startDate, endDate);
    }

    @PostMapping("/adjust")
    @ApiOperation("测调数据")
    public void adjust(Date date){
        adjustDataSyncService.generateAdjustPlan(date);
    }

    @PostMapping("/finishAdjust")
    @ApiOperation("完成测调数据")
    public void finishAdjust(){
        adjustDataSyncService.finishAdjustPlan();
    }

    @PostMapping("/abnormal")
    @ApiOperation("异常井数据")
    public void abnormal(Date date){
        abnormalDataSyncService.syncAbnormalWell(date);
        abnormalDataSyncService.summaryAbnormalUnit(date);
    }

    @PostMapping("/condition")
    @ApiOperation("工况数据")
    public void condition(Date date){
        conditionDataSyncService.summary(date);
    }

    @PostMapping("/pump")
    @ApiOperation("注水泵数据")
    public void pump(){
        realtimeService.syncPumpDeviceList();
    }

    @PostMapping("/wellTool")
    @ApiOperation("井下工具数据")
    public void wellTool(Date startDate, Date endDate){
        wellToolDataSyncService.sync(startDate, endDate);
    }

    @PostMapping("/wellTube")
    @ApiOperation("在井管柱数据")
    public void wellTube(Date startDate, Date endDate){
        wellTubeDataSyncService.sync(startDate, endDate);
    }

    @PostMapping("/summaryMonth")
    @ApiOperation("汇总月度数据")
    public void summaryMonth(String month){
        dataSyncService.summaryMonth(month);
    }

    @PostMapping("/daily-all")
    @ApiOperation("水质、洗井、异常井、单位工况")
    public void dailyAll(Date date){
        log.info("水质监测数据同步任务开始");
        // 30天前
//            qualityDataSyncService.syncQualityData(DateUtils.addDays(date, -30), date);
        qualityDataSyncService.xchyfxSzjcsj(DateUtils.addDays(date, -30), date);
        log.info("水质监测同步任务结束");

        log.info("洗井数据同步任务开始");
        // 2天前
        flushDataSyncService.syncFlushRecord(DateUtils.addDays(date, -30), date);
        log.info("洗井数据同步任务结束");

        log.info("异常井数据同步任务开始");
        abnormalDataSyncService.syncAbnormalWell(date);
        abnormalDataSyncService.summaryAbnormalUnit(date);
        log.info("异常井数据同步任务结束");

        log.info("单位工况数据汇总任务开始");
        conditionDataSyncService.summary(date);
        log.info("单位工况数据汇总任务结束");
    }
}
