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

import cn.com.victorysoft.business.indicator.dto.MoreSummaryDto;
import cn.com.victorysoft.business.indicator.service.MoreSummaryService;
import cn.com.victorysoft.business.sys.dto.DynamicColumnData;
import cn.com.victorysoft.business.sys.entity.DateRange;
import cn.com.victorysoft.business.sys.entity.ExportResult;
import cn.com.victorysoft.business.sys.param.UnitMonthExportParam;
import cn.com.victorysoft.business.sys.param.UnitMonthParam;
import cn.com.victorysoft.business.util.DateUtils;
import cn.com.victorysoft.business.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "超注井统计")
@RestController
@RequestMapping("indicator/more/summary")
public class MoreSummaryController {
    @Resource
    private MoreSummaryService moreSummaryService;

    @GetMapping("/month2")
    @ApiOperation("查询本月和上月数据")
    public List<MoreSummaryDto> month2(UnitMonthParam param){
        return moreSummaryService.selectMonth2(param);
    }

    @GetMapping("/unitChildren")
    @ApiOperation("查询本单位及子单位数据")
    public List<MoreSummaryDto> unitChildren(UnitMonthParam param){
        List<MoreSummaryDto> result = moreSummaryService.selectChildren(param);
        MoreSummaryDto unitData = moreSummaryService.selectUnit(param);
        if (unitData != null) {
            result.add(0, unitData);
        }
        return result;
    }

    @PostMapping("/unitChildrenExport")
    @ApiOperation("导出本单位及字段数据")
    public ExportResult unitChildrenExport(@RequestBody UnitMonthExportParam param){
        List<MoreSummaryDto> list = this.unitChildren(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @GetMapping("/measure")
    @ApiOperation("超注治理对策分析")
    public DynamicColumnData measure(UnitMonthParam param){
        return moreSummaryService.selectMeasure(param);
    }

    @PostMapping("/measureExport")
    @ApiOperation("超注治理对策分析导出")
    public ExportResult measureExport(@RequestBody UnitMonthExportParam param){
        DynamicColumnData data = this.measure(param);
        return ExcelUtils.exportMap(param.getColumnConfig(), data.getDataList(), param.getFileName());
    }

    @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();
        }
        moreSummaryService.summary(month);
    }

    @PostMapping("/summaryMeasure")
    @ApiOperation("汇总措施数据")
    public void summaryMeasure(@RequestParam(required = false) String month) {
        // 默认上月
        if (month == null || "".equals(month)) {
            DateRange dateRange = DateUtils.getLastMonth(new Date());
            month = dateRange.getName();
        }
        moreSummaryService.summaryMeasure(month);
    }

}
