package com.ruoyi.gysl.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.gysl.entity.BasicInformation;
import com.ruoyi.gysl.entity.request.ZwIdPageReq;
import com.ruoyi.gysl.entity.stats.AllProjectResponse;
import com.ruoyi.gysl.entity.stats.RibbonResponse;
import com.ruoyi.gysl.service.BasicInformationService;
import com.ruoyi.gysl.service.ZwStatsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 政务统计控制层
 *
 * @author makejava
 * @since 2025-02-24 09:25:51
 */
@Api(tags = "政务统计")
@RestController
@RequestMapping("/gysl/zwStats")
@PreAuthorize("@ss.hasAnyRoles('admin,common,gov')")
public class ZwStatsController extends BaseController {

    @Resource
    private ZwStatsService zwStatsService;


    @Resource
    private BasicInformationService basicInformationService;

    /**
     * 项目情况
     */
    @ApiOperation("整体项目情况")
    @GetMapping("/allProject")
    public AjaxResult allProject(String years) {
        return success(zwStatsService.allProject(years));
    }

    /**
     * 功能区
     */
    @ApiOperation("功能区")
    @GetMapping("/ribbon")
    public AjaxResult ribbon(String years) {
        return success(zwStatsService.ribbon(years));
    }

    /**
     * 投资主体
     */
    @ApiOperation("投资主体")
    @GetMapping("/investors")
    public AjaxResult investors(String years) {
        return success(zwStatsService.investors(years));
    }

    /**
     * 有经纬度的项目列表
     */
    @ApiOperation("有经纬度的项目列表")
    @GetMapping("/projectList")
    public AjaxResult projectList(Page<BasicInformation> page) {
        return success(basicInformationService.projectList(page));
    }

    /**
     * 消息通知
     */
    @ApiOperation("消息通知")
    @GetMapping("/zwNotice")
    public AjaxResult zwNotice() {
        return success(zwStatsService.zwNotice(SecurityUtils.getUserId()));
    }


    /**
     * 政务消息通知数量
     */
    @ApiOperation("政务消息通知数量")
    @GetMapping("/zwNoticeCount")
    public AjaxResult zwNoticeCount() {
        return success(zwStatsService.zwNoticeCount(SecurityUtils.getUserId()));
    }

    /**
     * 产业导向细分产业分析
     */
    @ApiOperation("产业导向细分产业分析")
    @GetMapping("/xfcyfx")
    public AjaxResult xfcyfx() {
        return success(zwStatsService.xfcyfx());
    }

    /**
     * 产业导向目录分析
     */
    @ApiOperation("产业导向目录分析")
    @GetMapping("/mlfx")
    public AjaxResult mlfx() {
        return success(zwStatsService.mlfx());
    }


    /**
     * 储备项目统计分析
     */
    @ApiOperation("储备项目统计分析")
    @GetMapping("/cbxmtj")
    public AjaxResult cbxmtj() {
        return success(zwStatsService.cbxmtj());
    }

    /**
     * 产业数据分析
     */
    @ApiOperation("产业数据分析")
    @GetMapping("/slfx")
    public AjaxResult slfx() {
        return success(zwStatsService.slfx());
    }




    /**
     * 导出项目总体报告
     */
    @ApiOperation(value = "导出项目总体报告")
    @PostMapping(value = "/exportBg")
    public void exportBg(HttpServletResponse response, @Valid @NotBlank String years)
            throws Exception {

        // 设置响应内容类型和头部
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment; filename=\"example.docx\"");
        //获取总体项目情况
        AllProjectResponse allProjectResponse = zwStatsService.allProject(years);
        //获取功能区
        List<RibbonResponse> ribbon = zwStatsService.ribbon(years);
        //获取投资主体
        List<RibbonResponse> investors = zwStatsService.investors(years);
        Map<String, Object> dt = new HashMap<>();
        dt.put("{{years}}", years);
        dt.put("{{allProject}}", allProjectResponse.getAllProject());
        dt.put("{{allGrossArea}}", allProjectResponse.getAllGrossArea());
        dt.put("{{allBuilding1}}", allProjectResponse.getAllBuilding1());
        dt.put("{{allBuilding2}}", allProjectResponse.getAllBuilding2());
        dt.put("{{allBuilding3}}", allProjectResponse.getAllBuilding3());
        dt.put("{{currentYearProject}}", allProjectResponse.getCurrentYearProject());
        dt.put("{{currentYearGrossArea}}", allProjectResponse.getCurrentYearGrossArea());
        dt.put("{{currentBuilding1}}", allProjectResponse.getCurrentBuilding1());
        dt.put("{{currentBuilding2}}", allProjectResponse.getCurrentBuilding2());
        dt.put("{{currentBuilding3}}", allProjectResponse.getCurrentBuilding3());
        ribbon.forEach(x -> {
            switch (x.getSsgnq()) {
                case "高端制造与国际贸易区":
                    dt.put("{{gn1}}", x.getCount());
                    break;
                case "阳澄湖半岛旅游度假区":
                    dt.put("{{gn2}}", x.getCount());
                    break;
                case "金鸡湖商务区":
                    dt.put("{{gn3}}", x.getCount());
                    break;
                case "苏相合作区":
                    dt.put("{{gn4}}", x.getCount());
                    break;
                case "独墅湖科教创新区":
                    dt.put("{{gn5}}", x.getCount());
                    break;
                default:
                    break;
            }
        });
        investors.forEach(x -> {
            switch (x.getSsgnq()) {
                case "国企":
                    dt.put("{{tz1}}", x.getCount());
                    break;
                case "民营企业":
                    dt.put("{{tz2}}", x.getCount());
                    break;
                case "外资企业":
                    dt.put("{{tz3}}", x.getCount());
                    break;
                default:
                    break;
            }
        });
        // 4. 生成并写入文档
        try (OutputStream out = response.getOutputStream()) {
            basicInformationService.generateDocument(dt, "ztbgmb.docx", out);
        }
    }
}

