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

import cn.com.victorysoft.business.indicator.dto.LessSummaryIndex;
import cn.com.victorysoft.business.indicator.entity.LessSummary;
import cn.com.victorysoft.business.indicator.service.LessSummaryService;
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/less/summary")
public class LessSummaryController {
    @Resource
    private LessSummaryService lessSummaryService;

    @GetMapping("/index")
    @ApiOperation("查询首页数据")
    public LessSummaryIndex index(UnitMonthParam param){
        LessSummaryIndex index = new LessSummaryIndex();
        index.setMonth2(this.childrenMonth2(param));
        index.setUnitReason(this.unitReason(param));
        index.setUnitChildren(this.unitChildren(param));
        return index;
    }

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

//    @GetMapping("/childrenMonth2")
    @ApiOperation("查询子单位本月和上月数据")
    public List<List<LessSummary>> childrenMonth2(UnitMonthParam param){
        return lessSummaryService.selectChildrenMonth2(param);
    }

//    @GetMapping("/unitReason")
    @ApiOperation("查询本单位原因数据")
    public List<LessSummary> unitReason(UnitMonthParam param){
        return lessSummaryService.selectUnitReason(param);
    }

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

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

    @GetMapping("/measure")
    @ApiOperation("欠注治理对策分析")
    public DynamicColumnData measure(UnitMonthParam param){
        return lessSummaryService.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());
    }

    @GetMapping("/reason")
    @ApiOperation("欠注原因分析")
    public DynamicColumnData reason(UnitMonthParam param){
        return lessSummaryService.selectReason(param);
    }

    @PostMapping("/reasonExport")
    @ApiOperation("欠注原因分析导出")
    public ExportResult reasonExport(@RequestBody UnitMonthExportParam param){
        DynamicColumnData data = this.reason(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();
        }
        lessSummaryService.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();
        }
        lessSummaryService.summaryMeasure(month);
    }

}
