WebPiccPushController.java 9.3 KB
package com.lhcredit.project.webbusiness.controller;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lhcredit.common.utils.FileSysUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.mail.SendMailUtil;
import com.lhcredit.project.business.bCreditReport.domain.BCreditReport;
import com.lhcredit.project.business.bCreditReport.mapper.BCreditReportMapper;
import com.lhcredit.project.business.bCreditReport.service.IBCreditReportService;
import com.lhcredit.project.business.reportMake.ReportOverseasMakeService;
import com.lhcredit.project.business.worldboxlog.domain.Worldboxlog;
import com.lhcredit.project.worldbox.EasyAPI;
import com.lhcredit.project.worldbox.WorldBoxAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Controller
@RequestMapping("/web/piccPush")
public class WebPiccPushController {

    @Autowired
    private IBCreditReportService bCreditReportService;

    @Autowired
    BCreditReportMapper bCreditReportMapper;

    @Autowired
    ReportOverseasMakeService reportOverseasMakeService;

    @Autowired
    WorldBoxAPI WorldBoxAPI;

    @Autowired
    EasyAPI easyAPI;

    @RequestMapping("/piccGetPush")
    public void piccGetPush(){
        try {
            System.out.println("===========执行获取海外报告任务!");
            //查询制作中的海外订单
            BCreditReport bCreditReport=new BCreditReport();
            bCreditReport.setReportType("-10");
            bCreditReport.setReportProgress("3");
            List<BCreditReport> listReport=bCreditReportService.selectBCreditReportListXml(bCreditReport);
            listReport.parallelStream().forEach(report->{
                System.out.println("===========订单编号!"+report.getReferenceNo());
                //记录日志
                Worldboxlog worldboxlog1 = new Worldboxlog();
                worldboxlog1.setName(report.getEnterpriseName());
                worldboxlog1.setCreateBy("获取订单数据");
                worldboxlog1.setCreateTime(new Date());
                //获取订单
                String str= WorldBoxAPI.getOrder(report.getReferenceNo(),worldboxlog1);
                JSONObject json=JSONObject.parseObject(str);
//                System.out.println("===========返回数据!"+str);
                if(StringUtils.isNotNull(json)) {
                    JSONArray array = json.getJSONArray("orderitems");
                    if (array.size() > 0) {
                        JSONObject obj = array.getJSONObject(0);
                        if(obj.getString("statuscode").equals("6")) {
                            String base64Str = obj.getString("binary");
                            byte[] decodedBytes = Base64.decode(base64Str);
                            InputStream inputStream = new ByteArrayInputStream(decodedBytes);
                            //上传文件
                            String path = FileSysUtils.upLoadFile(inputStream, report.getEnterpriseName() + ".xml", "3");
                            JSONObject json1 = JSONObject.parseObject(path);
                            String url = FileSysUtils.host + "/" + json1.get("data");
                            System.out.println("===========xmlUrl返回数据!"+url);
                            //读取数据
                            String xmlStr = null;
                            try {
                                xmlStr = new String(decodedBytes, "utf-8");
                            } catch (UnsupportedEncodingException e) {
                                throw new RuntimeException(e);
                            }
                            Map<String, Object> xmlMap = bCreditReportService.readXml(xmlStr);
                            List lt1=new ArrayList();
                            Map mp1=new HashMap();
                            mp1.put("fileName",report.getEnterpriseName());
                            mp1.put("fileExtension","xml");
                            mp1.put("fileAttachments",url);

                            lt1.add(mp1);

                            //发送请求
                            //发送数据
//                            long timespan = System.currentTimeMillis()/1000;
//                            String result = HttpRequest.post("https://qyld.piccltd.com.cn/apiserviceTest/PICCDataInfo/haiWaiCreditReportInfo").header("token", PiccMd5Token.string2MD5(Key + timespan + SecretKey).toUpperCase()).header("timespan",timespan+"").header("Key",Key).contentType("application/json; utf-8").form(json.toJSONString(pushMap)).execute().body();
//                            System.err.println(result);
                            //修改订单状态
                            bCreditReportMapper.updateStatusXmlUrl(report.getId(),"3","获取订单交付",new Date(), json.toJSONString(lt1),xmlMap.get("lrd").toString());
                            System.out.println("===========修改数据!"+report.getId()+"  "+json.toJSONString(lt1));

                        }else if(obj.getString("statuscode").equals("9")){//取消订单
                            //修改订单状态
                            bCreditReportMapper.updateStatusXmlUrl(report.getId(),"7","取消制作",new Date(), "","");

                        }
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("===========获取海外报告任务结束!");
    }


    @RequestMapping("/piccGetDoc")
    public void piccGetDoc(HttpServletResponse response){
            System.out.println("===========执行获取海外报告任务!");
            //查询制作中的海外订单
            BCreditReport bCreditReport=new BCreditReport();
            bCreditReport.setReportType("-10");
            bCreditReport.setReportProgress("3");
            List<BCreditReport> listReport=bCreditReportService.selectBCreditReportList(bCreditReport);
            listReport.parallelStream().forEach(report->{
                System.out.println("===========订单编号!"+report.getReferenceNo());
                String xmlUrl=report.getUrl();
                if(StringUtils.isNotEmpty(xmlUrl)) {
                    JSONArray array =JSONArray.parseArray(xmlUrl);
                    JSONObject json=JSONObject.parseObject(array.get(0).toString());
                    String url =json.getString("fileAttachments");

                    System.out.println("===========生成doc");
                    String docxUrl ="";
                    try {
                        byte[] bytes=FileSysUtils.downloadFile(url,response);
                        Map<String, Object> xmlMap = bCreditReportService.readXml(new String(bytes, "utf-8"));
                        xmlMap.put("companyName",report.getEnterpriseName());
                         docxUrl =reportOverseasMakeService.markOverseasReportPath(xmlMap,report.getEnterpriseName());
                        List lt2=new ArrayList();
                        Map mp2=new HashMap();
                        mp2.put("fileName",report.getEnterpriseName());
                        mp2.put("fileExtension","docx");
                        mp2.put("fileAttachments",docxUrl);
                        lt2.add(mp2);
                        //修改订单状态
                        bCreditReportMapper.updateStatusDocUrl(report.getId(),"4","订单待交付",new Date(),json.toJSONString(lt2));
                    }catch (Exception e){
                        bCreditReportMapper.updateStatusDocUrl(report.getId(),"8","生成doc异常:"+e.getMessage(),new Date(), "");
                        e.printStackTrace();
                    }
                }
            });
        System.out.println("===========生成海外报告结束!");
    }

    @GetMapping("/easyOrder")
    public void easyOrder(String entName,String countryIso){
        //查询所有
        String queryStr=easyAPI.query(entName,countryIso,"test");
        if(StringUtils.isNotEmpty(queryStr)){
            JSONObject obj=JSONObject.parseObject(queryStr);
            if(obj.getInteger("code")==200){
                //
                JSONArray array=obj.getJSONArray("data");
                for(int i=0;i<array.size();i++){
                    JSONObject obj1=array.getJSONObject(i);
                    String eid=obj1.getString("eid");
                    String nameEnglish=obj1.getString("nameEnglish");
                    String status=obj1.getString("status");
                    //entName.trim().equals(nameEnglish.trim())&&
                    if(status.equals("Active")){
                        //下单
                        String order=easyAPI.addOrder(eid,entName,"test");
                        System.out.println(order);
                    }
                }
            }
        }
    }

    @GetMapping("/getOrder")
    public void getOrder(String reportId,String entName){
        String queryStr=easyAPI.getOrder(reportId,entName,"test");
        System.out.println(queryStr);
    }



}