WebStatisticsFinancePageselectController.java 9.27 KB
package com.lhcredit.project.webbusiness.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.security.ShiroUtils;
import com.lhcredit.framework.aspectj.lang.enums.OperatorType;
import com.lhcredit.framework.web.page.TableDataInfo;
import com.lhcredit.project.business.frontDept.domain.FrontDept;
import com.lhcredit.project.business.frontDept.service.IFrontDeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lhcredit.framework.aspectj.lang.annotation.Log;
import com.lhcredit.framework.aspectj.lang.enums.BusinessType;
import com.lhcredit.project.business.statisticsFinancePageselect.domain.StatisticsFinancePageselect;
import com.lhcredit.project.business.statisticsFinancePageselect.service.IStatisticsFinancePageselectService;
import com.lhcredit.framework.web.controller.BaseController;
import com.lhcredit.framework.web.domain.AjaxResult;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


/**
 * 前台财务调用统计信息对外接口
 *
 * @author lhcredit
 * @date 2025-03-03
 */
@Slf4j
@RestController
@RequestMapping("/web/statisticsFinancePageselect")
public class WebStatisticsFinancePageselectController extends BaseController {
    @Autowired
    private IStatisticsFinancePageselectService statisticsFinancePageselectService;
    @Autowired
    private IFrontDeptService frontDeptService;

    /**
     * 查询前台财务调用统计列表接口
     */
    @ApiOperation("查询前台财务调用统计列表")
    @Log(title = "前台财务调用统计", businessType = BusinessType.LIST, operatorType = OperatorType.WEB)
    @GetMapping
    public AjaxResult list(StatisticsFinancePageselect statisticsFinancePageselect) {
        startPage();

        List<StatisticsFinancePageselect> list = statisticsFinancePageselectService.changeModel(statisticsFinancePageselectService.selectStatisticsFinancePageselectList(statisticsFinancePageselect));
        return toAjax(list);
    }

    /**
     * 查询前台财务调用统计详情接口
     */
    @ApiOperation("查询前台财务调用统计详情")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "int", paramType = "path")
    @Log(title = "前台财务调用统计", businessType = BusinessType.DETAIL, operatorType = OperatorType.WEB)
    @GetMapping("/{id}")
    public AjaxResult detail(@PathVariable Integer id) {
        StatisticsFinancePageselect statisticsFinancePageselect =statisticsFinancePageselectService.changeModel(statisticsFinancePageselectService.selectStatisticsFinancePageselectById(id));
        if (StringUtils.isNull(statisticsFinancePageselect)) {
            return AjaxResult.error("该信息不存在");
        }
        return toAjax(statisticsFinancePageselect);
    }

    /**
     * 新增保存前台财务调用统计接口
     */
    @ApiOperation("新增前台财务调用统计")
    @ApiImplicitParam(name = "statisticsFinancePageselect", value = "前台财务调用统计", dataType = "StatisticsFinancePageselect")
    @Log(title = "前台财务调用统计", businessType = BusinessType.INSERT, operatorType = OperatorType.WEB)
    @PostMapping
    public AjaxResult addSave(StatisticsFinancePageselect statisticsFinancePageselect) {
        statisticsFinancePageselect.setCreateBy(ShiroUtils.getLoginName());
        statisticsFinancePageselect.setCreateTime(new Date());
        statisticsFinancePageselect.setUpdateBy(ShiroUtils.getLoginName());
        statisticsFinancePageselect.setUpdateTime(new Date());
        return toAjax(statisticsFinancePageselectService.insertStatisticsFinancePageselect(statisticsFinancePageselect));
    }

    /**
     * 修改保存前台财务调用统计接口
     */
    @ApiOperation("修改前台财务调用统计")
    @ApiImplicitParam(name = "statisticsFinancePageselect", value = "前台财务调用统计", dataType = "StatisticsFinancePageselect")
    @Log(title = "前台财务调用统计", businessType = BusinessType.UPDATE, operatorType = OperatorType.WEB)
    @PutMapping
    public AjaxResult update(StatisticsFinancePageselect statisticsFinancePageselect) {
        if (StringUtils.isNull(statisticsFinancePageselect) || StringUtils.isNull(statisticsFinancePageselect.getId())){
            return AjaxResult.error("主键id不能为空");
        }
        statisticsFinancePageselect.setUpdateBy(ShiroUtils.getLoginName());
        statisticsFinancePageselect.setUpdateTime(new Date());
        return toAjax(statisticsFinancePageselectService.updateStatisticsFinancePageselect(statisticsFinancePageselect));
    }

    /**
     * 删除前台财务调用统计接口
     */
    @ApiOperation("删除前台财务调用统计")
    @ApiImplicitParam(name = "ids", value = "主键id,多条以英文逗号分隔", required = true, dataType = "String", paramType = "path")
    @Log(title = "前台财务调用统计", businessType = BusinessType.DELETE, operatorType = OperatorType.WEB)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable String ids) {
        return toAjax(statisticsFinancePageselectService.deleteStatisticsFinancePageselectByIds(ids));
    }

    /**
     * 财务数据订单
     */
    @PostMapping("/getOrder")
    @ResponseBody
    public TableDataInfo getOrder(@RequestBody StatisticsFinancePageselect statisticsFinancePageselect) {
        startPage(statisticsFinancePageselect.getPageNum(),statisticsFinancePageselect.getPageSize());
        return getDataTable(statisticsFinancePageselectService.getOrder(statisticsFinancePageselect));
    }

    /**
     * 编辑对账状态
     */
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(@RequestBody String requestJson) {
        return AjaxResult.success(statisticsFinancePageselectService.updateStatus(requestJson));
    }

    /**
     * 财务数据订单导出
     */
    @PostMapping("/orderExport")
    @ResponseBody
    public AjaxResult orderExport(@RequestBody StatisticsFinancePageselect statisticsFinancePageselect, HttpServletResponse response) throws IOException {
        List<StatisticsFinancePageselect> list = statisticsFinancePageselectService.getOrder(statisticsFinancePageselect);

        List<Long> collect = list.stream().map(item -> item.getOrgId()).distinct().collect(Collectors.toList());
        Map<Long, String> frontDeptMap = new HashMap<>();
        collect.forEach(item -> {
            FrontDept frontDept = frontDeptService.selectFrontDeptById(item);
            frontDeptMap.put(item, frontDept.getDeptName());
        });
        list.forEach(item -> {
            item.setDeptName(frontDeptMap.get(item.getOrgId()));
        });
       /* List<JSONObject> jsonObjects = list.stream().map(item -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", item.getId());
            jsonObject.put("企业名称", item.getEname());
            jsonObject.put("用户所属公司", frontDeptMap.get(item.getOrgId()));
            jsonObject.put("用户名称", item.getUserName());
            jsonObject.put("数据类型", item.getDataType());
            jsonObject.put("调取时间", item.getCreateTime());
            jsonObject.put("查询结果", item.getResultRemark());
            jsonObject.put("是否对账", "Y".equals(item.getReconciliation()) ? "是" : "否");
            return jsonObject; // ⚠️ 注意:必须返回构建好的 jsonObject
        }).collect(Collectors.toList());

        // 设置响应头
        configureResponseHeader(response, "财务数据");

        // 导出数据到Excel
        exportToExcel(jsonObjects, response.getOutputStream());*/
        return AjaxResult.success(list);
    }
    private void configureResponseHeader(HttpServletResponse response, String fileName) {
        try {
            if (StrUtil.isBlank(fileName)) {
                fileName = "企业数据导出";
            }
            // 确保文件名不包含非法字符
            fileName = fileName.replaceAll("[\\\\/:*?\"<>|]", "_");
            // 进行URL编码,防止中文乱码
            String encodedFileName = URLEncoder.encode(fileName + ".xlsx", String.valueOf(StandardCharsets.UTF_8));

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
        } catch (Exception e) {
            log.error("设置响应头失败", e);
        }
    }

    private void exportToExcel(List<JSONObject> list, ServletOutputStream outputStream) {
        // 创建ExcelWriter
        try (ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter()) {
            // 写入数据到Sheet
            writer.write(list, true);

            // 输出到流并关闭writer
            writer.flush(outputStream, true);
        }
    }

}