ScrmDataCubeJoinRestCloudServiceTest.java 21.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
package cn.com.glsx.entmicro.service;

import ch.ethz.ssh2.Connection;
import cn.com.glsx.entmicro.listener.MagicCheckCustomerDataListener;
import cn.com.glsx.entmicro.listener.SelfResearchRestCloudListener;
import cn.com.glsx.entmicro.model.MagicCubeCustomerCheckModel;
import cn.com.glsx.entmicro.model.ScrmMagicCubeCustomerModel;
import cn.com.glsx.entmicro.model.request.DataCubeBaseRpcRequest;
import cn.com.glsx.entmicro.model.request.DataCubeFileUploadRpcRequest;
import cn.com.glsx.entmicro.model.request.MagicCubeImportApiRequest;
import cn.com.glsx.entmicro.model.request.RestCloudContentRpcRequest;
import cn.com.glsx.entmicro.model.request.restcloud.DataCubeMerchantRequest;
import cn.com.glsx.entmicro.model.response.DataCubeMerchantConfigResponse;
import cn.com.glsx.entmicro.model.response.DataCubeUploadLogResponse;
import cn.com.glsx.entmicro.model.response.DataCubeUploadResult;
import cn.com.glsx.entmicro.remote.ScrmDataCubeJoinRestCloudRemoteImpl;
import cn.com.glsx.entmicro.util.DateUtil;
import cn.com.glsx.entmicro.util.FileUploadServerData;
import cn.com.glsx.entmicro.util.FileUploadUtil;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.Resource;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description
 * @author chizw
 * @create 2022/4/25 14:07
 * @version ver.1.0.0
 */
@EnableAsync
public class ScrmDataCubeJoinRestCloudServiceTest extends BaseServiceTest {

    @Autowired
    private ScrmDataCubeJoinRestCloudService scrmDataCubeJoinRestCloudService;

    @Resource
    private ScrmDataCubeJoinRestCloudRemoteImpl scrmDataCubeJoinRestCloudRemoteImpl;

    @Resource
    private Producer producer;

    @Resource
    private Environment env;

//    @Value("${scrm.datacube.restcloud.ip}")
    private String IP;

//    @Value("${scrm.datacube.restcloud.port}")
    private int PORT;

    @Value("${scrm.datacube.restcloud.ip}")
    private static String REST_CLOUD_IP;

    @Value("${scrm.datacube.restcloud.port}")
    private static int REST_CLOUD_PORT;

    @Value("${scrm.datacube.restcloud.user}")
    private String USER;

    @Value("${scrm.datacube.restcloud.password}")
    private String PASSWORD;

    @Value("${scrm.datacube.restcloud.default.path}")
    private String REST_CLOUD_DEFAULT_PATH;

    private final Connection connection = new Connection(IP, PORT);

    @Value("${scrm.datacube.restcloud.ip}")
    public void setRestCloudIp(String ip) {
        REST_CLOUD_IP = ip;
    }

    @Value("${scrm.datacube.restcloud.port}")
    public void setRestCloudPort(Integer port) {
        REST_CLOUD_PORT = port;
    }

    public static class singleton{

        private static String REST_CLOUD_IP;

        private static int REST_CLOUD_PORT;

        @Value("${scrm.datacube.restcloud.user}")
        private String USER;

        @Value("${scrm.datacube.restcloud.password}")
        private String PASSWORD;

        @Value("${scrm.datacube.restcloud.default.path}")
        private String REST_CLOUD_DEFAULT_PATH;

        @Value("${scrm.datacube.restcloud.exception.path}")
        private String EXCEPTION_DATA_FILE_PATH;

//    private final Connection connection = getConnection();

        @Value("${scrm.datacube.restcloud.ip}")
        public void setRestCloudIp(String ip) {
            REST_CLOUD_IP = ip;
        }

        @Value("${scrm.datacube.restcloud.port}")
        public void setRestCloudPort(Integer port) {
            REST_CLOUD_PORT = port;
        }

        public enum singletonConnection {
            connectionFactory;
            private Connection connection;
            private singletonConnection() {
                try {
                    System.out.println("3REST_CLOUD_IP===========" + REST_CLOUD_IP);
                    System.out.println("3REST_CLOUD_PORT===========" + REST_CLOUD_PORT);
                    connection = new Connection(REST_CLOUD_IP, REST_CLOUD_PORT);
                    System.out.println("connection1===========" + connection);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            public Connection getConn() {
                return connection;
            }
        }

        /**
         * 获取Connection链接
         * @return
         */
        public static Connection getSingletonConnection() {
            Connection conn = singletonConnection.connectionFactory.getConn();
            System.out.println("conn===============" + conn);
            return conn;
        }

        public Connection getConnection1() {
            System.out.println("1REST_CLOUD_IP===========" + REST_CLOUD_IP);
            System.out.println("1REST_CLOUD_PORT===========" + REST_CLOUD_PORT);
            return getSingletonConnection();
        }
    }

    @Test
    public void testTest(){
//        Connection connection = ScrmDataCubeJoinRestCloudService.Singleton4Holder.getSingletonConnection();
//        Connection connection = ScrmDataCubeJoinRestCloudService.getConn();
//        Connection connection1 = ScrmDataCubeJoinRestCloudService.getSingletonConnection();
//        System.out.println("connection1=================" + connection1);
//        Connection connection2 = scrmDataCubeJoinRestCloudService.getConnection1();
//        System.out.println("connection2=================" + connection2);
//        System.out.println("connection1.getPort()=================" + connection1.getPort());
//        System.out.println("connection2.getPort()=================" + connection2.getPort());
    }

    @Test
    public void uploadDataCubeFilesTest(){
        DataCubeFileUploadRpcRequest request = new DataCubeFileUploadRpcRequest();
        request.setStaffIdList(Arrays.asList(1,2,3));
        request.setRemarks("11111");
        request.setMerchantId(44210956);
        ArrayList<MagicCubeImportApiRequest> fileList = new ArrayList<>();
        MagicCubeImportApiRequest file = new MagicCubeImportApiRequest();
        file.setFileUrl("https://wx.glsx.com.cn/upload/biz/e2e6db01-04e4-4432-8398-eb2ea04cf9f5.xlsx");
        file.setFileName("10086.xlsx");
        file.setBusinessTypeId(4);
        file.setBusinessTypeName("续保");
        fileList.add(file);
        request.setFileList(fileList);
        scrmDataCubeJoinRestCloudService.uploadDataCubeFiles(fileList,10086,"20211217");
    }

    @Test
    public void queryDateCubeTemplateTest(){
        Integer merchantId = 44210956;
        List<DataCubeMerchantConfigResponse> templateList = scrmDataCubeJoinRestCloudService.queryDateCubeTemplate(merchantId);
        System.out.println(templateList);
    }

    @Test
    public void uploadDataCubeFilesImplTest(){
        DataCubeFileUploadRpcRequest request = new DataCubeFileUploadRpcRequest();
        request.setMerchantId(10001002);
        ArrayList<MagicCubeImportApiRequest> fileList = new ArrayList<>();
        MagicCubeImportApiRequest file = new MagicCubeImportApiRequest();
        file.setFileUrl("https://wx.glsx.com.cn/upload/biz/a41e7c9a-2ba2-4f1f-8ee9-efcc6c42bc07.xlsx");
        file.setFileName("10086.xlsx");
        file.setBusinessTypeId(1);
        file.setBusinessTypeName("续保");
        fileList.add(file);
        request.setFileList(fileList);
        RpcResponse<DataCubeUploadResult> response = scrmDataCubeJoinRestCloudRemoteImpl.uploadDataCubeFiles(request);
        System.out.println("response============" + response);
    }

    @Test
    public void queryDataCubeUploadLogByPageTest(){
        DataCubeBaseRpcRequest request = new DataCubeBaseRpcRequest();
        request.setPageNum(1);
        request.setPageSize(10);
        request.setMerchantId(44210956);
        RpcPagination<DataCubeUploadLogResponse> logByPage = scrmDataCubeJoinRestCloudService.queryDataCubeUploadLogByPage(request);
        System.out.println("logByPage=============" + logByPage);
    }

    @Test
    public void executeNextBatchTest(){
        Integer merchanId = 44209572;
        scrmDataCubeJoinRestCloudService.executeNextBatch(merchanId);
    }

    @Test
    public void restCloudFileDownloadTest(){
        List<String> list = Arrays.asList(" 44209515_1_20220508263338", " 44209515_2_20220508263338", " 44209515_3_20220508263338");
        for (String name : list) {
            String s = scrmDataCubeJoinRestCloudService.restCloudFileDownload(name, String.valueOf(UUID.randomUUID()),44209515);
            System.out.println(name + "===============" + s);
        }
    }

    @Test
    public void fileUploadServerScript(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date date1 = DateUtil.addDays(new Date(),1);
        String date = format.format(date1);
        List<String> list = Arrays.asList("数据模板(客户表)","数据模板(车辆表)","数据模板(车辆进场记录)","数据模板(车辆出场记录)");
        for (String fileName : list) {
            fileName = fileName + ".xlsx";
            String filePath = "C:/Users/chizw/Desktop/模板/" + fileName;
            File file = new File(filePath);
            try (FileInputStream fis = new FileInputStream(file);
                 ByteArrayOutputStream ops = new ByteArrayOutputStream();){
                byte[] b = new byte[4096];
                int i;
                while ((i = fis.read(b)) != -1){
                    ops.write(b,0,i);
                }
                byte[] bytes = ops.toByteArray();
                fileName = fileName.substring(0,fileName.indexOf("."));
                FileUploadServerData uploadResult = FileUploadUtil.uploadToFileSystem(fileName + date, bytes, "xlsx");
                String fileUrl = uploadResult.getUrl();
                fileUrl = scrmDataCubeJoinRestCloudService.httpsConversion(fileUrl);
                System.out.println(fileName + "===============" + fileUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void restCloudUploadResultCallbackTest(){
        RestCloudContentRpcRequest content = new RestCloudContentRpcRequest();
        content.setResoult(1);
        content.setFileExist1(1);
        content.setFileExist2(1);
        content.setFileExist3(1);
        content.setFileExist4(1);
        content.setInPutFile1(" 44210895_1_20220509299244");
        content.setInPutFile2(" 44210895_2_20220509299244");
        content.setInPutFile3(" 44210895_3_20220509299244");
        content.setInPutFile4(" 44210895_4_20220509299244");
        scrmDataCubeJoinRestCloudService.restCloudUploadResultCallback(content);
    }

    /**
     * 设置上传路径
     *
     * @param ftp
     */
    public void changeUploadPath(FTPClient ftp) {
        try {
            String basePath = "/home/ftpuser/image/";
            Date currentDate = new Date();
            String dateStr = new SimpleDateFormat("yyyy/MM/dd").format(currentDate);
            for(String pathStr:dateStr.split("/")){
                basePath += pathStr + "/";
                boolean flag = ftp.changeWorkingDirectory(basePath);
                if(!flag){
                    //创建上传的路径  该方法只能创建一级目录,在这里如果/home/ftpuser存在则可创建image
                    ftp.makeDirectory(basePath);
                }
            }
            //指定上传路径
            ftp.changeWorkingDirectory(basePath);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void excelDecode(byte[] fileByte){
        try (
                InputStream inputStream = new ByteArrayInputStream(fileByte);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ){
            MagicCheckCustomerDataListener magicCheckCustomerDataListener = new MagicCheckCustomerDataListener();
            EasyExcel.read(inputStream, ScrmMagicCubeCustomerModel.class, magicCheckCustomerDataListener).sheet().doRead();
            List<MagicCubeCustomerCheckModel> list = magicCheckCustomerDataListener.getList();
            EasyExcel.write(outputStream, MagicCubeCustomerCheckModel.class).sheet("模板").doWrite(list);
            FileUploadServerData fileUploadServerData = FileUploadUtil.uploadToFileSystem("数据魔方导入数据校验结果"+System.currentTimeMillis(),outputStream.toByteArray(), "xlsx");
            String url = fileUploadServerData.getUrl();
//            return RpcResponse.success(url);
        }catch (Exception e){
//            log.error("魔方导入数据校验失败!err_msg:{}", e.getMessage());
//            return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage());
//            throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, e.getMessage());
        }
    }

    @Async("taskExecutor")
    public void asyn(){
        for (int i = 0; i < 100; i++) {
            System.out.println("执行成功");
        }
//        Example example = new Example(DataCubeTemplateConfigDO.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("merchantId",merchantId);
//        criteria.andEqualTo("delFlag",0);
//        List<DataCubeTemplateConfigDO> templateConfigDOList = dataCubeTemplateConfigMapper.selectByExample(example);
    }

    public static void getSecondTest() throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = format.parse("2022-08-16 00:00:00");
        Calendar curDate = Calendar.getInstance();
        curDate.setTime(parse);
        Calendar tommorowDate = new GregorianCalendar(
                curDate.get(Calendar.YEAR),
                curDate.get(Calendar.MONTH),
                curDate.get(Calendar.DATE) + 1,
                3, 0, 0);
        //返回 明天凌晨0点 和 今天当前时间 的差值(秒数)
        try {
            Thread.sleep(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long timeInMillis = tommorowDate.getTimeInMillis();
        long timeInMillis1 = curDate.getTimeInMillis();
        int i = (int) (timeInMillis - timeInMillis1) / 100;
        System.out.println("timeInMillis=====================" + timeInMillis);
        System.out.println("timeInMillis1=====================" + timeInMillis1);
        System.out.println("i=====================" + i);
    }

    @Test
    public void selfResearchRestCloudTest(){
//        List<String> list = Arrays.asList("客户列表1652099548693");
        List<String> list = Arrays.asList("fe593f56-4517-4dcd-816b-1309a450df2f");
        for (String fileName : list) {
            fileName = fileName + ".xlsx";
            String filePath = "C:/Users/chizw/Desktop/" + fileName;
            File file = new File(filePath);
            try (FileInputStream fis = new FileInputStream(file);
                 ByteArrayOutputStream ops = new ByteArrayOutputStream();){
                byte[] b = new byte[4096];
                int i;
                while ((i = fis.read(b)) != -1){
                    ops.write(b,0,i);
                }
                byte[] bytes = ops.toByteArray();
                scrmDataCubeJoinRestCloudService.selfResearchRestCloud(bytes,44210909,1,"20221010603608");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void selfResearchRestCloudTest2(){
//        List<String> list = Arrays.asList("客户列表1652099548693");
        int businessType = 4;
        List<String> list = Arrays.asList("海马刀数据");
        for (String fileName : list) {
            fileName = fileName + ".xlsx";
            String filePath = "C:/Users/chizw/Desktop/" + fileName;
            File file = new File(filePath);
            try (FileInputStream fis = new FileInputStream(file);
                 ByteArrayOutputStream ops = new ByteArrayOutputStream();){
                byte[] b = new byte[4096];
                int i;
                while ((i = fis.read(b)) != -1){
                    ops.write(b,0,i);
                }
                byte[] bytes = ops.toByteArray();
                scrmDataCubeJoinRestCloudService.selfResearchRestCloud(bytes,44211227, 1,"20221010603608");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void sendTest(){
        String json = "{\"car_vin\":\"\",\"user_id\":\"\",\"car_number\":\"粤A124576\",\"customer_phone\":\"123456789033\",\"merchant_id\":44209846}";
        String magicTopic = "wxwork.notify.msg";
        KeyedMessage<byte[], byte[]> message = new KeyedMessage<>(magicTopic, "1".getBytes(), json.getBytes());
        producer.send(message);
    }
    @Test
    public void startProcessTest() throws Exception {
        Boolean aBoolean = scrmDataCubeJoinRestCloudService.startProcess(null);
    }

    @Test
    public void invokeTest() throws Exception {
        String json = "{\"0\":\"524778088278659072\",\"1\":\"魔方加班4\",\"2\":\"null\",\"3\":\"15874478547\",\"4\":\"男\",\"5\":\"null\",\"6\":\"否\",\"7\":\"阿斯顿・马丁\",\"8\":\"阿斯顿・马丁DB11\",\"9\":\"2022款 4.0T V8 Volante\",\"10\":\"粤A12345\",\"11\":\"WKLKLJKNMSADASDBA\",\"12\":\"null\",\"13\":\"498000\",\"14\":\"null\",\"15\":\"null\",\"16\":\"null\",\"17\":\"null\",\"18\":\"null\",\"19\":\"正常\",\"20\":\"null\",\"21\":\"企微魔方\",\"22\":\"2022-05-08\"}";
        Map<Integer,Object> map = JSONObject.parseObject(json, HashMap.class);
        System.out.println("map================" + map);
        SelfResearchRestCloudListener selfResearchRestCloudListener = new SelfResearchRestCloudListener();
        selfResearchRestCloudListener.invoke(map,null);
    }

    private static void addrScript(String str){
        String mapStr = "{\"克孜勒苏柯尔克孜自治州\":\"阿图什市\",\"阿坝藏族羌族自治州\":\"马尔康市\",\"恩施土家族苗族自治州\":\"恩施市\",\"楚雄彝族自治州\":\"楚雄市\",\"海南藏族自治州\":\"共和县\",\"德宏傣族景颇族自治州\":\"芒市\",\"甘孜藏族自治州\":\"康定市\",\"玉树藏族自治州\":\"玉树市\",\"巴音郭楞蒙古自治州\":\"库尔勒市\",\"黔南布依族苗族自治州\":\"都匀市\",\"文山壮族苗族自治州\":\"文山市\",\"昌吉回族自治州\":\"昌吉市\",\"西双版纳傣族自治州\":\"景洪市\",\"凉山彝族自治州\":\"西昌市\",\"黔西南布依族苗族自治州\":\"兴义市\",\"大理白族自治州\":\"大理市\",\"海西蒙古族藏族自治州\":\"德令哈市\",\"甘南藏族自治州\":\"合作市\",\"海北藏族自治州\":\"海晏县西海镇\",\"怒江傈僳族自治州\":\"泸水市\",\"迪庆藏族自治州\":\"香格里拉市\",\"果洛藏族自治州\":\"玛沁县\",\"博尔塔拉蒙古自治州\":\"博乐市\",\"湘西土家族苗族自治州\":\"吉首市\",\"红河哈尼族彝族自治州\":\"蒙自市\",\"临夏回族自治州\":\"临夏市\",\"黄南藏族自治州\":\"同仁市\",\"黔东南苗族侗族自治州\":\"凯里市\",\"伊犁哈萨克自治州\":\"伊宁市\",\"延边朝鲜族自治州\":\"延吉市\"}";
        Map<String,String> map = JSON.parseObject(mapStr, HashMap.class);
        String[] split = str.split(",");
        List<JSONObject> list = new ArrayList<>();
        for (String s : split) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("regex","^(?=(.|\\n)*(" + s + "))(.|\\n)*$");
            String s1 = map.get(s);
            if (s1 != null){
                jsonObject.put("convert",s1);
            } else {
                jsonObject.put("convert",s.replaceAll("市",""));
            }
            list.add(jsonObject);
        }
        System.out.println(JSON.toJSONString(list));
    }

    @Test
    public void reportDataProcessTest() {
        String json = "{\n" +
                "        \"customer_phone\": \"13603070903\",\n" +
                "        \"template_id\": 49,\n" +
                "        \"merchant_id\": 44211227,\n" +
                "        \"community\": \"小区2\",\n" +
                "        \"add_way\": 108,\n" +
                "        \"building\": \"B栋\",\n" +
                "        \"room\": \"102\",\n" +
                "        \"customer_qw_nickname\": \"HX\",\n" +
                "        \"child_sex\": \"男\",\n" +
                "        \"child_age\": 18,\n" +
                "        \"child_count\": 5,\n" +
                "        \"homeowner_type\": \"业主\",\n" +
                "        \"car_series\": \"日系车\",\n" +
                "        \"car_brand\": \"日产\",\n" +
                "        \"car_vin\": \"12334556\",\n" +
                "        \"car_number\": \"12334556\"\n" +
                "}";
        scrmDataCubeJoinRestCloudService.reportDataProcess(json);
    }
}