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

import cn.com.victorysoft.business.indicator.constant.Constants;
import cn.com.victorysoft.business.indicator.dto.MeasureCompareDto;
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.indicator.service.WellSectionMonthService;
import cn.com.victorysoft.business.sys.entity.DateRange;
import cn.com.victorysoft.business.sys.entity.ExportResult;
import cn.com.victorysoft.business.sys.param.UnitBlockMonthWellExportParam;
import cn.com.victorysoft.business.sys.param.UnitBlockMonthWellPageParam;
import cn.com.victorysoft.business.util.BeanUtils;
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.SneakyThrows;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "注水井层段月数据")
@RestController
@RequestMapping("indicator/wellSection/month")
public class WellSectionMonthController {
    @Resource
    private WellSectionMonthService wellSectionMonthService;
    @Resource
    private SectionRateSummaryService sectionRateSummaryService;
    @Resource
    private LessSummaryService lessSummaryService;
    @Resource
    private MoreSummaryService moreSummaryService;

    @GetMapping("/page")
    @ApiOperation("查询分页数据")
    public PageInfo<WellSectionMonth> page(WellSectionMonthParam param){
        return wellSectionMonthService.selectPage(param);
    }

    @PostMapping("/export")
    @ApiOperation("导出数据")
    public ExportResult export(@RequestBody SectionRateWellExportParam param){
        List<WellSectionMonth> list = wellSectionMonthService.selectList(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @PutMapping("/save")
    @ApiOperation("维护")
    public int save(@RequestBody WellSectionMonth record){
        return wellSectionMonthService.updateEdit(record);
    }

    @PutMapping("/approve")
    @ApiOperation("审核")
    public String approve(@RequestBody WellSectionMonth record){
        return wellSectionMonthService.updateApprove(record);
    }

    @SneakyThrows
    @GetMapping("/moreCompare")
    @ApiOperation("超注治理效果分析")
    public PageInfo<MeasureCompareDto> moreCompare(UnitBlockMonthWellPageParam param){
        WellSectionMonthParam compareParam = new WellSectionMonthParam();
        BeanUtils.copyProperties(compareParam, param);
        compareParam.setEvaluate(Constants.MORE);
        return wellSectionMonthService.pageMeasureCompare(compareParam);
    }

    @SneakyThrows
    @GetMapping("/lessCompare")
    @ApiOperation("欠注治理效果分析")
    public PageInfo<MeasureCompareDto> lessCompare(UnitBlockMonthWellPageParam param){
        WellSectionMonthParam compareParam = new WellSectionMonthParam();
        BeanUtils.copyProperties(compareParam, param);
        compareParam.setEvaluate(Constants.LESS);
        return wellSectionMonthService.pageMeasureCompare(compareParam);
    }

    @SneakyThrows
    @PostMapping("/moreCompareExport")
    @ApiOperation("超注治理效果分析导出")
    public ExportResult moreCompareExport(@RequestBody UnitBlockMonthWellExportParam param){
        WellSectionMonthParam compareParam = new WellSectionMonthParam();
        BeanUtils.copyProperties(compareParam, param);
        compareParam.setEvaluate(Constants.MORE);
        List<MeasureCompareDto> list = wellSectionMonthService.selectMeasureCompare(compareParam);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @SneakyThrows
    @PostMapping("/lessCompareExport")
    @ApiOperation("欠注治理效果分析导出")
    public ExportResult lessCompareExport(@RequestBody UnitBlockMonthWellExportParam param){
        WellSectionMonthParam compareParam = new WellSectionMonthParam();
        BeanUtils.copyProperties(compareParam, param);
        compareParam.setEvaluate(Constants.LESS);
        List<MeasureCompareDto> list = wellSectionMonthService.selectMeasureCompare(compareParam);
        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);
        lessSummaryService.summary(month);
        lessSummaryService.summaryMeasure(month);
        moreSummaryService.summary(month);
        moreSummaryService.summaryMeasure(month);
    }
}
