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

import cn.com.victorysoft.business.indicator.entity.SectionRateSummary;
import cn.com.victorysoft.business.indicator.param.SectionRateWellExportParam;
import cn.com.victorysoft.business.indicator.param.WellSectionMonthParam;
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.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;
import java.util.Map;

@Api(tags = "层段合格率统计")
@RestController
@RequestMapping("indicator/sectionRate/summary")
public class SectionRateSummaryController {
    @Resource
    private SectionRateSummaryService sectionRateSummaryService;

    @GetMapping("/unitRange")
    @ApiOperation("查询单位范围数据")
    public List<SectionRateSummary> unitRange(WellSectionMonthParam param){
        // 查询12个月的
        return sectionRateSummaryService.selectRange(param, 11);
    }

    @GetMapping("/unitChildren")
    @ApiOperation("查询本单位及子单位数据")
    public List<SectionRateSummary> unitChildren(WellSectionMonthParam param){
        List<SectionRateSummary> result = sectionRateSummaryService.selectUnitChildren(param);
        if (result.size() == 0) {
            result.add(0, null);
        }
        return result;
    }

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

    @PostMapping("/zsjExport")
    @ApiOperation("导出注水井分注层段合格率月报")
    public ExportResult zsjExport(@RequestBody SectionRateWellExportParam param){
        List<Map<String, Object>> list = sectionRateSummaryService.zsjExport(this.unitChildren(param));
        return ExcelUtils.exportMap(param.getColumnConfig(), list, param.getFileName());
    }


    @GetMapping("/injectType")
    @ApiOperation("分层统计")
    public List<SectionRateSummary> injectType(WellSectionMonthParam param){
        return sectionRateSummaryService.summaryByInjectType(param);
    }

    @PostMapping("/injectTypeExport")
    @ApiOperation("分层统计导出")
    public ExportResult injectTypeExport(@RequestBody SectionRateWellExportParam param){
        List<SectionRateSummary> list = sectionRateSummaryService.summaryByInjectType(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @GetMapping("/single")
    @ApiOperation("单注统计")
    public List<SectionRateSummary> single(WellSectionMonthParam param){
        return sectionRateSummaryService.summarySingle(param);
    }

    @PostMapping("/singleExport")
    @ApiOperation("单注统计导出")
    public ExportResult singleExport(@RequestBody SectionRateWellExportParam param){
        List<SectionRateSummary> list = sectionRateSummaryService.summarySingle(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @GetMapping("/summaryBlock")
    @ApiOperation("层段合格率统计(单元)")
    public List<SectionRateSummary> summaryBlock(WellSectionMonthParam param){
        return sectionRateSummaryService.summaryBlock(param);
    }

    @PostMapping("/summaryBlockExport")
    @ApiOperation("层段合格率统计(单元)导出")
    public ExportResult summaryBlockExport(@RequestBody SectionRateWellExportParam param){
        List<SectionRateSummary> list = sectionRateSummaryService.summaryBlock(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @GetMapping("/split")
    @ApiOperation("分注统计")
    public List<SectionRateSummary> split(WellSectionMonthParam param){
        return sectionRateSummaryService.summarySplit(param);
    }

    @PostMapping("/splitExport")
    @ApiOperation("分注统计导出")
    public ExportResult splitExport(@RequestBody SectionRateWellExportParam param){
        List<SectionRateSummary> list = sectionRateSummaryService.summarySplit(param);
        return ExcelUtils.export(param.getColumnConfig(), list, 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();
        }
        sectionRateSummaryService.summary(month);
    }
}
