package cn.com.victorysoft.business.indicator.controller;
import cn.com.victorysoft.business.indicator.dto.UnitMonthDto;
import cn.com.victorysoft.business.indicator.entity.UnitMonth;
import cn.com.victorysoft.business.indicator.service.UnitMonthService;
import cn.com.victorysoft.business.sys.dto.Result;
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.sys.param.UnitMonthRangeExportParam;
import cn.com.victorysoft.business.sys.param.UnitMonthRangeParam;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Api(tags = "指标管理")
@RestController
@RequestMapping("indicator/summary")
public class UnitMonthController {

    @Resource
    private UnitMonthService unitMonthService;

    @GetMapping("/technology")
    @ApiOperation("技术指标-柱状图")
    public List<UnitMonth> technology(UnitMonthParam param){
        return unitMonthService.selectTechnologyBar(param);
    }

    @PostMapping("/technologyExport")
    @ApiOperation("技术指标导出")
    public ExportResult technologyExport(@RequestBody UnitMonthExportParam param){
        List<UnitMonthDto> list = this.technologyTable(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @GetMapping("/technologyTable")
    @ApiOperation("技术指标-表格")
    public List<UnitMonthDto> technologyTable(UnitMonthParam param){
        return unitMonthService.selectTechnologyTable(param);
    }

    @GetMapping("/rangeList")
    @ApiOperation("管理指标")
    public List<UnitMonth> rangeList(UnitMonthParam param){
        return unitMonthService.selectListRange(param);
    }

    @GetMapping("/monthList")
    @ApiOperation("月度指标")
    public List<UnitMonth> monthList(UnitMonthRangeParam param){
        return unitMonthService.selectMonthRange(param);
    }

    @PostMapping("/monthExport")
    @ApiOperation("导出管理指标/月度指标")
    public ExportResult monthExport(@RequestBody UnitMonthExportParam param){
        List<UnitMonth> list = unitMonthService.selectListMonth(param);
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @PostMapping("/monthRangeExport")
    @ApiOperation("导出范围月度指标")
    public ExportResult monthRangeExport(@RequestBody UnitMonthRangeExportParam param){
        List<UnitMonth> list = unitMonthService.selectMonthRange(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();
        }
        unitMonthService.syncUnitMonth(month);
    }

    @GetMapping("/template")
    @ApiOperation("模板下载")
    public void template(HttpServletResponse response) throws IOException {
        unitMonthService.downloadTemplate("单位月度指标.xlsx", response);
    }

    @PostMapping("/upload")
    @ApiOperation("导入")
    public Result upload(MultipartFile file) {
        return unitMonthService.upload(file);
    }

}
