package com.gyt.shr.controller.electronicProof;


import com.gyt.shr.entity.DetailedCertificationBill;
import com.gyt.shr.service.EleProofService;
import com.gyt.shr.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import static com.gyt.shr.constant.GlobalConstant.NORMAL_USER_LOGIN_STATUS;



/**
 * 电子证明列表控制器
 * @author coyle
 * 202501
 */

@Controller
@CrossOrigin
public class EleProofController {

    @Autowired
    private EleProofService eleProofService ;



    /**
     *  电子证明列表页面
     * @param
     * @param request
     * @return
     */
    @GetMapping("/normalUser/toDtList")
    public String getDtList(HttpServletRequest request, @RequestParam String pid, Model model ) {
        if (request.getSession().getAttribute(NORMAL_USER_LOGIN_STATUS)!= null  ){
            model.addAttribute("pid",pid);
            return "normalUser/detailedList";
        }else{
            return "redirect:/";
        }

    }

    /**
     * @param mapData
     * @param request
     * @return
     */
    @PostMapping("/proof/listContent")
    @ResponseBody
    public Result<List<DetailedCertificationBill>> getListContent(HttpServletRequest request, @RequestParam Map<String, Object> mapData ) {
        String pid = (String) mapData.get("pid");
        Result<List<DetailedCertificationBill>> certificatByPerson = eleProofService.getCertificatByPerson(pid);
        return certificatByPerson;
    }



    /**
     *  跳转到电子证明编辑页面
     * @param
     * @param request
     * @return
     */
    @GetMapping("/proof/pdfView")
    public String toPdfView(HttpServletRequest request, @RequestParam String urlDir, @RequestParam String fileName, RedirectAttributes redirectAttributes) {
        if (request.getSession().getAttribute(NORMAL_USER_LOGIN_STATUS) != null) {
            redirectAttributes.addAttribute("sole", urlDir);
            redirectAttributes.addAttribute("fname", fileName);
            return "redirect:/normalUser/vpdfpage";
        } else {
            return "redirect:/";
        }
    }



        /**
         *  电子证明列表页面
         * @return
         */
    @GetMapping("/proof/getPdfFile")
    public ResponseEntity<InputStreamResource> getPdf(@RequestParam String sole, @RequestParam String fname) {
        String fileURL = "https://shr.gooeto.com:8043/exeTools/downElecPdf.jsp?sole=" + sole.trim() + "&fname=" + fname.trim();
//        String fileURL = "https://shr.gooeto.com:8043/exeTools/vElecFile.jsp?sole=e2a62bff32&fname=33c10a840b.pdf";
        try {
            // 创建URL对象
            URL url = new URL(fileURL);
            // 打开连接
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            int responseCode = httpConn.getResponseCode();
            // 总是检查HTTP响应代码
            if (responseCode == HttpURLConnection.HTTP_OK) { // 成功
                InputStream inputStream = httpConn.getInputStream();
                // 设置响应头
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=file.pdf");
                // 返回响应实体
                return ResponseEntity
                        .ok()
                        .headers(headers)
                        .contentType(MediaType.APPLICATION_PDF)
                        .body(new InputStreamResource(inputStream));
            } else {
                System.out.println("无法下载文件。HTTP响应代码: " + responseCode);
                return ResponseEntity.status(responseCode).body(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpURLConnection.HTTP_INTERNAL_ERROR).body(null);
        }
    }




    @GetMapping("/proof/downPdfFile")
    public ResponseEntity<InputStreamResource> downPdfFile(@RequestParam String sole, @RequestParam String fname) {
        String fileURL = "https://shr.gooeto.com:8043/exeTools/downElecPdf.jsp?sole=" + sole.trim() + "&fname=" + fname.trim();
        try {
            // 创建URL对象
            URL url = new URL(fileURL);
            // 打开连接
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            int responseCode = httpConn.getResponseCode();
            // 总是检查HTTP响应代码
            if (responseCode == HttpURLConnection.HTTP_OK) { // 成功
                InputStream inputStream = httpConn.getInputStream();
                // 设置响应头
                HttpHeaders headers = new HttpHeaders();
                headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fname);
                // 返回响应实体
                return ResponseEntity
                        .ok()
                        .headers(headers)
                        .contentType(MediaType.APPLICATION_PDF)
                        .body(new InputStreamResource(inputStream));
            } else {
                System.out.println("无法下载文件。HTTP响应代码: " + responseCode);
                return ResponseEntity.status(responseCode).body(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpURLConnection.HTTP_INTERNAL_ERROR).body(null);
        }
    }

}



