package com.olm.common; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.lowagie.text.DocumentException; import com.olm.util.Configure; import com.olm.util.FileUtilText; import com.olm.util.LasMonth; import com.rkhd.platform.sdk.http.CommonData; import com.rkhd.platform.sdk.http.CommonHttpClient; import lombok.extern.slf4j.Slf4j; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URISyntaxException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.logging.Logger; import static com.olm.util.TokenTest.getAccessTokenString; @Slf4j /** * ClassName:RequestUtils Package:PACKAGE_NAME Date:2020/7/27 Auth:penghui@olm.com.cn */ public class RequestUtilsLowagie { static Logger logger = Logger.getLogger(String.valueOf(RequestUtilsLowagie.class)); static String fistAll; static String fileoutpath; public static byte[] getRecord(CommonHttpClient rkhdHttpClient, String accessToken, String urls) { CommonData data = new CommonData(); try { data.putHeader("Authorization", accessToken); data.setCall_type("GET"); data.setCallString(urls); JSONObject response = JSONObject.parseObject(rkhdHttpClient.execute(data).getResult()); if (response.getString("data") == null) { System.out.println("getRecord数据查询错误." + urls); } return response.getBytes("data"); } catch (Exception e) { e.printStackTrace(); } return null; } public static JSONArray postRecord(CommonHttpClient rkhdHttpClient, String accessToken, String xoql) { CommonData data = new CommonData(); try { data.putHeader("Authorization", accessToken); data.setCall_type("POST"); data.setCallString("https://api.xiaoshouyi.com/rest/data/v2.0/query/xoql"); data.putFormData("xoql", xoql); data.putFormData("useSimpleCode", true); JSONObject response = JSONObject.parseObject(rkhdHttpClient.execute(data).getResult()); log.info("CRM接口返回信息:" + response.toJSONString()); JSONObject result = JSONObject.parseObject(response.getString("data")); if (result!= null && result.containsKey("records")){ return result.getJSONArray("records"); } } catch (Exception e) { e.printStackTrace(); } return null; } public static JSONObject postRecordObject(CommonHttpClient rkhdHttpClient, String accessToken, String xoql) { CommonData data = new CommonData(); try { data.putHeader("Authorization", accessToken); data.setCall_type("POST"); data.setCallString("https://api.xiaoshouyi.com/rest/data/v2.0/query/xoql"); data.putFormData("xoql", xoql); data.putFormData("useSimpleCode", true); JSONObject response = JSONObject.parseObject(rkhdHttpClient.execute(data).getResult()); JSONObject result = JSONObject.parseObject(response.getString("data")); //JSONObject records = result.getJSONArray("data"); /* if (response.getString("records") == null){ System.out.println("postRecord数据查询错误."); }*/ return result; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 非驻场 * * @param rkhdHttpClient * @param accessToken */ public static void fzc(CommonHttpClient rkhdHttpClient, String accessToken, int setps) { try { //处理服务报告单----非驻场,包含售前、售后 //获得上上个月26号到上个月25号的服务报告单 //驻场类型--非驻场 Long customItem52 = Long.parseLong("2"); //获得时间访问 SimpleDateFormat format233 = new SimpleDateFormat("yyyy-MM-dd"); Calendar d = Calendar.getInstance(); Date ld = new LasMonth.LastMonthDate().convert(d); Date lld = new LasMonth.LastLastMonthDate().convert(d); StringBuffer fzc = new StringBuffer(); fzc.append("select id,name,customItem1__c,customItem29__c,customItem52__c,D1__c,customItem98__c,customItem42__c from customEntity1__c where approvalStatus= 3"); fzc.append(" and customItem52__c = "); fzc.append(customItem52); if (fistAll.equals("true")) { fzc.append(" order by name asc"); fzc.append(" limit "); fzc.append(setps); fzc.append(","); fzc.append("3000"); } else { fzc.append(" and D1__c > "); fzc.append(lld.getTime()); fzc.append(" and D1__c < "); fzc.append(ld.getTime()); } JSONArray records = RequestUtilsLowagie.postRecord(rkhdHttpClient, accessToken, fzc.toString()); FileUtilText.writeTxtFile("非驻场-售后服务报告单-" + "总数:" + records.size()); for (int i = 0; i < records.size(); i++) { StringBuffer sb = new StringBuffer(); Object id = records.getJSONObject(i).get("id"); Object fname = records.getJSONObject(i).get("name"); Object officename = records.getJSONObject(i).getString("customItem98__c"); Object custemname = records.getJSONObject(i).getString("customItem42__c"); Object customItem29__c = records.getJSONObject(i).getString("customItem29__c"); sb.append("https://api.xiaoshouyi.com/rest/template/v2.0/customEntity1__c/"); sb.append(id.toString()); sb.append("/756457200189639"); System.out.println("文件数:" + records.size() + "-" + i); logger.info("文件数:" + records.size() + "-" + i); byte[] printdate = RequestUtilsLowagie.getRecord(rkhdHttpClient, accessToken, sb.toString()); new RequestUtilsLowagie().writePdf(printdate, fname.toString(), officename.toString(), custemname.toString(), customItem29__c.toString()); } FileUtilText.writeTxtFile("非驻场-售后服务报告单-" + "总数:" + records.size() + ",执行完成."); } catch (Exception e) { FileUtilText.writeTxtFile("非驻场-售后服务报告单-" + ",执行失败."); System.out.println("Exception:" + e); } } /** * 派工单-驻场-售后派工单 现在都是管理层创建,只有400条左右是早期工程师创建输入这个范围,一次性输出。 * * @param rkhdHttpClient * @param accessToken */ public static void zc(CommonHttpClient rkhdHttpClient, String accessToken) { try { //处理服务报告单----驻场,包含售后派工单 //获得上上个月26号到上个月25号的服务报告单 //驻场类型--驻场 //获得时间访问 SimpleDateFormat format233 = new SimpleDateFormat("yyyy-MM-dd"); Calendar d = Calendar.getInstance(); Date ld = new LasMonth.LastMonthDate().convert(d); Date lld = new LasMonth.LastLastMonthDate().convert(d); StringBuffer fzc = new StringBuffer(); fzc.append("select id,customItem31__c,caseNo,customItem91__c,fieldJobAccountId,fieldJobAccountId from fieldJob where customItem55__c ='驻场' and customItem48__c ='售后派工单'"); //现在都是管理层创建,只有400条左右输入这个范围,所以去掉过滤。 if (!fistAll.equals("true")) { fzc.append(" and appointmentEndTime > "); fzc.append(lld.getTime()); fzc.append(" and appointmentEndTime < "); fzc.append(ld.getTime()); } JSONArray records = RequestUtilsLowagie.postRecord(rkhdHttpClient, accessToken, fzc.toString()); //获得客户列表 String custemsql = "select id,accountName from account"; JSONArray custemrecords = RequestUtilsLowagie.postRecord(rkhdHttpClient, accessToken, custemsql); HashMap hashMap = new HashMap(); for (int j = 0; j < custemrecords.size(); j++) { hashMap.put(custemrecords.getJSONObject(j).get("id"), custemrecords.getJSONObject(j).get("accountName")); } FileUtilText.writeTxtFile("驻场-售后派工单-" + "总数:" + records.size()); for (int i = 0; i < records.size(); i++) { StringBuffer sb = new StringBuffer(); Object id = records.getJSONObject(i).get("id"); Object fname = records.getJSONObject(i).get("caseNo"); Object officename = records.getJSONObject(i).getString("customItem91__c"); Object customItem31__c = records.getJSONObject(i).getString("customItem31__c"); Object custemname = records.getJSONObject(i).getString("fieldJobAccountId"); //客户id sb.append("https://api.xiaoshouyi.com/rest/template/v2.0/fieldJob/"); sb.append(id.toString()); sb.append("/794158019789010"); System.out.println("文件数:" + records.size() + "-" + i); logger.info("文件数:" + records.size() + "-" + i); byte[] printdate = RequestUtilsLowagie.getRecord(rkhdHttpClient, accessToken, sb.toString()); new RequestUtilsLowagie().writePdf(printdate, fname.toString(), officename.toString(), hashMap.get(custemname.toString()).toString(), customItem31__c.toString()); } FileUtilText.writeTxtFile("驻场-售后派工单-" + "总数:" + records.size() + ",执行完成."); } catch (Exception e) { FileUtilText.writeTxtFile("驻场-售后派工单-" + ",执行失败."); System.out.println("Exception:" + e); } } /** * 派工单-驻场-售前派工单 804204168790256 805605105664259 派工单-驻场-提前执行派工单 805604259545318 805603764846892 派工单-驻场-提前执行派工单-管理层创建 派工单-驻场-售前派工单-管理层创建 派工单-驻场-售后派工单-管理层创建 805605251514666 794158019789010 * * @param rkhdHttpClient * @param accessToken */ public static void zcnew(CommonHttpClient rkhdHttpClient, String accessToken, String customItem55, String customItem48, String templatePrintId) { try { //处理服务报告单----驻场,包含售前、售后 //获得上上个月26号到上个月25号的服务报告单 //驻场类型--驻场 //获得时间访问 Calendar d = Calendar.getInstance(); Date ld = new LasMonth.LastMonthDate().convert(d); Date lld = new LasMonth.LastLastMonthDate().convert(d); StringBuffer fzc = new StringBuffer(); fzc.append("select id,caseNo,customItem91__c,customItem31__c,fieldJobAccountId,fieldJobAccountId from fieldJob where "); fzc.append("1=1"); if (customItem55 != null) { fzc.append(" and customItem55__c = "); fzc.append("'"); fzc.append(customItem55); fzc.append("'"); } fzc.append(" and customItem48__c = "); fzc.append("'"); fzc.append(customItem48); fzc.append("'"); //现在都是管理层创建,只有400条左右输入这个范围,所以去掉过滤。 if (!fistAll.equals("true")) { fzc.append(" and appointmentEndTime > "); fzc.append(lld.getTime()); fzc.append(" and appointmentEndTime < "); fzc.append(ld.getTime()); } JSONArray records = RequestUtilsLowagie.postRecord(rkhdHttpClient, accessToken, fzc.toString()); //获得客户列表 String custemsql = "select id,accountName from account"; JSONArray custemrecords = RequestUtilsLowagie.postRecord(rkhdHttpClient, accessToken, custemsql); HashMap hashMap = new HashMap(); for (int j = 0; j < custemrecords.size(); j++) { hashMap.put(custemrecords.getJSONObject(j).get("id"), custemrecords.getJSONObject(j).get("accountName")); } FileUtilText.writeTxtFile("总数:" + records.size()); for (int i = 0; i < records.size(); i++) { StringBuffer sb = new StringBuffer(); Object id = records.getJSONObject(i).get("id"); Object fname = records.getJSONObject(i).get("caseNo"); Object officename = records.getJSONObject(i).getString("customItem91__c"); Object customItem31__c = records.getJSONObject(i).getString("customItem31__c"); Object custemname = records.getJSONObject(i).getString("fieldJobAccountId"); //客户id sb.append("https://api.xiaoshouyi.com/rest/template/v2.0/fieldJob/"); sb.append(id.toString()); sb.append("/"); sb.append(templatePrintId); System.out.println("文件数:" + records.size() + "-" + i); logger.info("文件数:" + records.size() + "-" + i); byte[] printdate = RequestUtilsLowagie.getRecord(rkhdHttpClient, accessToken, sb.toString()); new RequestUtilsLowagie().writePdf(printdate, fname.toString(), officename.toString(), hashMap.get(custemname.toString()).toString(), customItem31__c.toString()); } FileUtilText.writeTxtFile("总数:" + records.size() + ",执行完成"); } catch (Exception e) { FileUtilText.writeTxtFile(",执行失败."); System.out.println("Exception:" + e); } } public static int[] getstep() { int data[] = new int[9]; data[0] = 0; data[1] = 3000; data[2] = 6000; data[3] = 9000; data[4] = 12000; data[5] = 15000; data[6] = 18000; data[7] = 21000; data[8] = 24000; return data; } public static void main(String args[]) throws URISyntaxException, IOException { fileoutpath = Configure.getValue("fileoutpath"); if (StrUtil.contains(fileoutpath, "2022")){ fileoutpath = fileoutpath.replace("2022", DateUtil.format(DateUtil.date(), "YYYY")); } fistAll = Configure.getValue("fistAll"); FileUtilText.writeTxtFile("------------------------------------- \n"); //当前时间 Calendar calendar = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(formatter.format(calendar.getTime())); logger.info("-------------------------------------"); logger.info("执行开始时间:" + formatter.format(calendar.getTime())); FileUtilText.writeTxtFile("执行开始时间:" + formatter.format(new Date())); String accessToken = getAccessTokenString(); CommonHttpClient rkhdHttpClient = CommonHttpClient.instance(); FileUtilText.writeTxtFile("非驻场业务-服务报告单:"); //执行非驻场业务.需要通过limit n,m 取值 if (fistAll.equals("true")) { int[] setps = RequestUtilsLowagie.getstep(); for (int setp : setps) { RequestUtilsLowagie.fzc(rkhdHttpClient, accessToken, setp); System.out.println("服务报告单处理数:" + setp); } } else { RequestUtilsLowagie.fzc(rkhdHttpClient, accessToken, 1); System.out.println("服务报告单处理每月数."); } //执行驻场-售后派工单。400多条。打印模板id:794158019789010,fieldJob FileUtilText.writeTxtFile("执行驻场-售后派工单:"); RequestUtilsLowagie.zc(rkhdHttpClient, accessToken); //执行驻场-售后派工单-管理层创建。500多条。打印模板id:805605251514666,fieldJob FileUtilText.writeTxtFile("售后派工单-管理层创建:"); RequestUtilsLowagie.zcnew(rkhdHttpClient, accessToken, "驻场", "售后派工单-管理层创建", "805605251514666"); //派工单-驻场-售前派工单.301条。 804204168790256 805605105664259 FileUtilText.writeTxtFile("售前派工单:"); RequestUtilsLowagie.zcnew(rkhdHttpClient, accessToken, null, "售前派工单", "804204168790256"); //派工单-售前派工单-管理层创建.146条。 805605105664259 FileUtilText.writeTxtFile("售前派工单-管理层创建:"); RequestUtilsLowagie.zcnew(rkhdHttpClient, accessToken, null, "售前派工单-管理层创建", "805605105664259"); //派工单-提前执行派工单.80条 805603764846892 FileUtilText.writeTxtFile("提前执行派工单:"); RequestUtilsLowagie.zcnew(rkhdHttpClient, accessToken, null, "提前执行派工单", "805603764846892"); //派工单-提前执行派工单.63条 805604259545318 FileUtilText.writeTxtFile("提前执行派工单-管理层创建:"); RequestUtilsLowagie.zcnew(rkhdHttpClient, accessToken, null, "提前执行派工单-管理层创建", "805604259545318"); FileUtilText.writeTxtFile("执行结束时间:" + DateUtil.now()); FileUtilText.writeTxtFile("-------------------------------------"); } public void writePdf(byte[] s, String id, String officename, String custemname, String customItem29__c) throws IOException, DocumentException { String outpath = fileoutpath; String pdffile = null; try { String oficname = outpath.concat(officename);//分公司 String custname = oficname.concat(File.separator).concat(custemname);//客户名称 if (customItem29__c != null) { pdffile = custname.concat(File.separator).concat(customItem29__c).concat("_").concat(id).concat(".pdf"); } else { pdffile = custname.concat(File.separator).concat(id).concat(".pdf"); } //判断是否存在,如果不存在则创建 this.creatDir(custname, pdffile); FileOutputStream fileOutputStream = new FileOutputStream(new File(pdffile)); if (fileOutputStream == null) { System.out.println("文件数据为空:" + id); } fileOutputStream.write(s); fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } public void creatDir(String filePath, String filename) throws IOException { File file = new File(filePath); if (!file.exists()) { //用来测试此路径名表示的文件或目录是否存在 //file.isDirectory(); //来判断这是不是一个文件夹。 file.mkdirs();//创建目录 System.out.println("文件夹不存在"); } File file2 = new File(filename); if (!file2.exists()) { file2.createNewFile();//创建文件 System.out.println("文件不存在"); } } }