package com.ozs.web.controller.statisticalAnalysis;

import com.ozs.common.annotation.Log;
import com.ozs.common.constant.ModularConstans;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.StringUtils;
import com.ozs.pm.doman.vo.requestVo.PmDemandReqVo;
import com.ozs.pm.doman.vo.responseVo.StatisticalChartsResVo;
import com.ozs.pm.service.IPmDemandService;
import com.ozs.system.domain.SysFileInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 统计分析页面控制层
 *
 * @author buzhanyi
 */
@RestController
@RequestMapping("/statistical")
public class StatisticalAnalysisController extends BaseController {

    @Resource
    private IPmDemandService pmDemandService;


    @ApiOperation(value = "项目属性情况统计")
    @PostMapping("/countByProjectAttr")
    //@PreAuthorize("@ss.hasPermi('statistical:countByProjectAttr')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countByProjectAttr(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //按照项目属性统计所有的项目
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        List<StatisticalChartsResVo> resVos = pmDemandService.countByProjectAttr(pmDemandReqVo);
        return AjaxResult.success(resVos);
    }

    @ApiOperation(value = "项目类型情况统计")
    @PostMapping("/countByProjectType")
    //@PreAuthorize("@ss.hasPermi('statistical:countByProjectType')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countByProjectType(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //按照项目属性统计所有的项目
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        List<StatisticalChartsResVo> resVos = pmDemandService.countByProjectType(pmDemandReqVo);
        return AjaxResult.success(resVos);
    }

    @ApiOperation(value = "采购方式分布")
    @PostMapping("/countByPurchaseMode")
    //@PreAuthorize("@ss.hasPermi('statistical:countByPurchaseMode')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countByPurchaseMode(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //按照采购方式统计所有的项目
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        List<StatisticalChartsResVo> resVos = pmDemandService.countByPurchaseMode(pmDemandReqVo);
        return AjaxResult.success(resVos);
    }

    @ApiOperation(value = "需采转化情况统计")
    @PostMapping("/countByPurchaseChange")
    //@PreAuthorize("@ss.hasPermi('statistical:countByPurchaseChange')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countByPurchaseChange(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //按照采购方式统计所有的项目
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        List<StatisticalChartsResVo> resVos = pmDemandService.countByPurchaseChange(pmDemandReqVo);
        return AjaxResult.success(resVos);
    }

    @ApiOperation(value = "项目金额分布")
    @PostMapping("/countByEvaluation")
    //@PreAuthorize("@ss.hasPermi('statistical:countByEvaluation')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countByEvaluation(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //按照概算金额统计所有的项目
        //概算金额包括：100万以下采购任务、100至500万元采购任务、500至1000万元采购任务、1000至5000万元采购任务、5000至1亿元采购任务、1亿元及以上采购任务
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        List<StatisticalChartsResVo> resVos = pmDemandService.countByEvaluation(pmDemandReqVo);
        return AjaxResult.success(resVos);
    }

    @ApiOperation(value = "执行滞后分析")
    @PostMapping("/countProjectExceed")
    //@PreAuthorize("@ss.hasPermi('statistical:countProjectExceed')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countProjectExceed(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //执行滞后采购项目：本年度所有的逾期项目数
        //预算金额：本年度所有逾期项目累加的预算金额
        //滞后项目数量占比：指本年度逾期项目数量/本年度所有项目数量
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        return pmDemandService.countProjectExceed(pmDemandReqVo);
    }

    @ApiOperation(value = "滞后项目数量分析")
    @PostMapping("/exceedAnalysis")
    //@PreAuthorize("@ss.hasPermi('statistical:exceedAnalysis')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult exceedAnalysis(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //统计不同时间维度的逾期项目
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        if (StringUtils.isNull(pmDemandReqVo.getTimeType())) {
            return error("统计时间类型不能为空， 1：年度，2：季度，3：月份！");
        }
        return pmDemandService.exceedAnalysis(pmDemandReqVo);
    }

    @ApiOperation(value = "滞后项目时长分析")
    @PostMapping("/exceedMarketAnalysis")
    //@PreAuthorize("@ss.hasPermi('statistical:exceedMarketAnalysis')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult exceedMarketAnalysis(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //按照滞后时长统计项目数量
        //滞后时长包括：滞后1个月以内的采购任务、滞后1至3个月采购任务、滞后3至6个月采购任务、滞后6个月至1年采购任务、滞后1年以上采购任务
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        return pmDemandService.exceedMarketAnalysis(pmDemandReqVo);
    }

    @ApiOperation(value = "采购项目信息统计")
    @PostMapping("/purchaseProjectStatistical")
    //@PreAuthorize("@ss.hasPermi('statistical:purchaseProjectStatistical')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult purchaseProjectStatistical(@RequestBody PmDemandReqVo pmDemandReqVo) {
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        Map<String, Integer> map = pmDemandService.purchaseProjectStatistical(pmDemandReqVo);
        return success(map);
    }

    @ApiOperation(value = "重大规划采购任务专项计划管理情况")
    @PostMapping("/countMajorProject")
    //@PreAuthorize("@ss.hasPermi('statistical:countMajorProject')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countMajorProject(@RequestBody PmDemandReqVo pmDemandReqVo) {
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        return pmDemandService.countMajorProject(pmDemandReqVo);
    }


    @ApiOperation(value = "任务数量趋势分析")
    @PostMapping("/taskQuantityAnalysis")
    //@PreAuthorize("@ss.hasPermi('statistical:taskQuantityAnalysis')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult taskQuantityAnalysis(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //任务数量趋势分析：按照月、季度、年统计“项目属性”字段中“重大规划”属性的项目
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        if (StringUtils.isNull(pmDemandReqVo.getTimeType())) {
            return error("统计时间类型不能为空， 1：年度，2：季度，3：月份！");
        }
        return pmDemandService.taskQuantityAnalysis(pmDemandReqVo);
    }

    @ApiOperation(value = "各阶段采购任务数量分布")
    @PostMapping("/countEveryStatusNum")
    //@PreAuthorize("@ss.hasPermi('statistical:countEveryStatusNum')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult countEveryStatusNum(@RequestBody PmDemandReqVo pmDemandReqVo) {
        //指项目进行到各个阶段的数量
        //阶段包括：待选取代理、待上传招标文件、待发布公告、待开标、待发布中标公告、中标公式中
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        return pmDemandService.countEveryStatusNum(pmDemandReqVo);
    }

    @ApiOperation(value = "项目执行进度统计")
    @PostMapping("/purchaseProjectExecute")
    //@PreAuthorize("@ss.hasPermi('statistical:purchaseProjectExecute')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult purchaseProjectExecute(@RequestBody PmDemandReqVo pmDemandReqVo) {
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        List<StatisticalChartsResVo> resVos = pmDemandService.purchaseProjectExecute(pmDemandReqVo);
        return success(resVos);
    }

    @ApiOperation(value = "已完成项目数量统计")
    @PostMapping("/purchaseProjectCompleteNumber")
    //@PreAuthorize("@ss.hasPermi('statistical:purchaseProjectCompleteNumber')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult purchaseProjectCompleteNumber(@RequestBody PmDemandReqVo pmDemandReqVo) {
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        if (StringUtils.isNull(pmDemandReqVo.getTimeType())) {
            return error("统计时间类型不能为空， 1：年度，2：季度，3：月份！");
        }
        List<Map<String, Integer>> map = pmDemandService.purchaseProjectCompleteNumber(pmDemandReqVo);
        return success(map);
    }

    @ApiOperation(value = "已完成采购任务数量")
    @PostMapping("/purchaseTaskFinish")
    //@PreAuthorize("@ss.hasPermi('statistical:purchaseTaskFinish')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult purchaseTaskFinish(@RequestBody PmDemandReqVo pmDemandReqVo) {
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        if (StringUtils.isNull(pmDemandReqVo.getTimeType())) {
            return error("统计时间类型不能为空， 1：年度，2：季度，3：月份！");
        }
        List<Map<String, Integer>> map = pmDemandService.purchaseTaskFinish(pmDemandReqVo);
        return success(map);
    }

    @ApiOperation(value = "各阶段项目数量分布")
    @PostMapping("/purchaseProjectDistribution")
    //@PreAuthorize("@ss.hasPermi('statistical:purchaseProjectDistribution')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult purchaseProjectDistribution(@RequestBody PmDemandReqVo pmDemandReqVo) {
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        List<Map<String, String>> map = pmDemandService.purchaseProjectDistribution(pmDemandReqVo);
        return success(map);
    }

    @ApiOperation(value = "采购项目--项目数量分析/预算金额（万元）")
    @PostMapping("/purchaseProjectNumberAnalysis")
    //@PreAuthorize("@ss.hasPermi('statistical:purchaseProjectNumberAnalysis')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult purchaseProjectNumberAnalysis(@RequestBody PmDemandReqVo pmDemandReqVo) {
        // 需求单位可查看本单位及其下属单位的统计数据
        if (StringUtils.isNull(pmDemandReqVo.getPurchaseDeptId())) {
            return error("登录账号的单位 不能为空！");
        }
        if (StringUtils.isNull(pmDemandReqVo.getTimeType())) {
            return error("统计时间类型不能为空， 1：年度，2：季度，3：月份！");
        }
        List<Map<String, String>> map = pmDemandService.purchaseProjectNumberAnalysis(pmDemandReqVo);
        return success(map);
    }

    @ApiOperation(value = "生成分析报告")
    @PostMapping("/generateAnalysisReport")
    //@PreAuthorize("@ss.hasPermi('statistical:generateAnalysisReport')")
    @Log(title = ModularConstans.statisticalAnalysis, businessType = BusinessType.QUERY)
    public AjaxResult generateAnalysisReport(@RequestBody SysFileInfo file) {
        //  创建一个document对象，相当于新建一个word文档（后缀名为.docx）。
        XWPFDocument document = new XWPFDocument();
        //        创建一个段落对象。
        XWPFParagraph paragraph = document.createParagraph();
        //        创建一个run。run具体是什么，我也不知道。但是run是这里面的最小单元了。
        XWPFRun run = paragraph.createRun();
        //        插入图片
        //        创建一个输出流 即是该文档的保存位置
        try {
            //文件路径位置和文件名称
            run.addPicture(new FileInputStream(file.getFileUrl()),
                    XWPFDocument.PICTURE_TYPE_PNG,
                    file.getFileName(),
                    Units.toEMU(400),
                    Units.toEMU(200));
            //文件保存地址
            OutputStream outputStream = new FileOutputStream(file.getFileMappingPath());
            document.write(outputStream);
            outputStream.close();
            return AjaxResult.success("分析文档已生成");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        return AjaxResult.success("分析文档已生成");
    }


}
