package com.chinaweal.aiccs.crgs.transfor.controller;


import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.crgs.transfor.service.OtNpermitDownloadLogService;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 行政许可信息下载清单记录 前端控制器
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-11-05
 */
@RestController
@RequestMapping("/npermit/download")
public class OtNpermitDownloadLogController extends BaseController {

    @Autowired
    private OtNpermitDownloadLogService otNpermitDownloadLogService;

    /**
     * 行政许可信息下载
     *
     * @param request 请求
     * @param ids     数据id
     * @author lroyia
     * @since 2021年11月3日 14:59:24
     */
    @ApiOperation("行政许可信息下载")
    @PostMapping("")
    public void download(HttpServletRequest request, HttpServletResponse response, @ApiParam("下载数据的id，多个用,获取") @RequestParam String ids) {
        HSSFWorkbook workbook = otNpermitDownloadLogService.download(ids);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=download.xls");
        OutputStream os;
        try {
            os = response.getOutputStream();
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询行政许可信息下载记录分页
     *
     * @param request        请求
     * @param pageRequestDto 分页参数
     * @return 查询结果
     * @author lroyia
     * @since 2021年11月8日 14:20:40
     */
    @ApiOperation("查询下载记录分页")
    @PostMapping("page")
    public RestResult<?> downloadPage(HttpServletRequest request, @ApiParam("分页参数") @RequestBody PageRequestDto pageRequestDto) {
        String orderField = pageRequestDto.getOrderField();
        Pattern PATTERN = Pattern.compile("select|insert|delete|drop|stop|start|update|union|--|;|true|trancate|char|substr|ascii|declare|exec|count|master|into|execute|or");
        Matcher matcher = PATTERN.matcher(orderField);
        if (matcher.find()) {
            return RestResult.error(ResultCode.PARAM_IS_INVALID,"参数有误");
        }
        return RestResult.ok(otNpermitDownloadLogService.defaultPaging(pageRequestDto, null, null));
    }
}

