sunyize 2 years ago
parent
commit
9ab6c008b6

+ 220 - 47
tnc-sms/src/main/java/org/jeecg/modules/smscheck/controller/SmsCheckTaskController.java

@@ -43,6 +43,7 @@ import org.springframework.web.servlet.ModelAndView;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.*;
+import java.util.concurrent.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -75,6 +76,7 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
     private DataYearMonthImpl dataYearMonth;
     @Autowired
     private ISmsCheckCustomerInfoErrorService smsCheckCustomerInfoErrorService;
+    private static int i=0;
     /**
      * 分页列表查询
      *
@@ -220,7 +222,7 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
      * @return 导入结果
      */
     @PostMapping(value = "/importCustomerExcel")
-    public Result<?> importCustomerExcel(@RequestPart("file") MultipartFile file) {
+    public Result<?> importCustomerExcel(@RequestPart("file") MultipartFile file)  throws Exception{
         String filename = file.getOriginalFilename();
         if (!filename.endsWith(".xlsx") && !filename.endsWith(".xls")) {
             return Result.error("文件类型不正确!请导入文件类型->.xlsx或.xls");
@@ -244,9 +246,22 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
             });
 
             Tuple dictTuple = new Tuple(dictItemMap);
+            //普通写法
+            long start = System.currentTimeMillis();
 
+            //用户状态
+            List<SysUser> userList = sysUserService.list(new LambdaQueryWrapper<SysUser>()
+                    .eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0));
+
+
+            Map<String, SysUser> userMap
+                    = userList.stream().collect(Collectors.toMap(SysUser::getWorkNo, Function.identity(), (o, n) -> o));
+
+            List<SysDepartModel> sysDepartModel = customerInfoService.getSysDepartModel();
+            Map<String, SysDepartModel> sysDepartModelMap
+                    = sysDepartModel.stream().collect(Collectors.toMap(SysDepartModel::getOrgCode,Function.identity(), (o, n) -> o));
             for (SmsCheckCustomerInfoVo vo : list) {
-                SmsCheckCustomerInfoVo infoVo = checkCustomerInfo(vo, dictTuple);
+                SmsCheckCustomerInfoVo infoVo = checkCustomerInfoAdd(vo, dictTuple,userMap,sysDepartModel,sysDepartModelMap);
                 if (ObjectUtil.isNotNull(infoVo)) {
                     if (StrUtil.isNotBlank(infoVo.getErrorMsg())) {
                         errorCount++;
@@ -256,6 +271,14 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
             }
             dict.put("errorCount", errorCount);
             dict.put("data", voList);
+            System.err.println("执行任务消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");
+
+
+            //多线程写法
+//            List<SmsCheckCustomerInfoVo> smsCheckCustomerInfoVos = callSmsCheckCustomerInfoVo(list, dictTuple);
+//            dict.put("errorCount", i);
+//             dict.put("data", smsCheckCustomerInfoVos);
+//            System.err.println("fei执行任务消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");
         } catch (Exception e) {
             log.error("客户信息导入失败", e);
         }
@@ -263,6 +286,88 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
         return Result.ok(dict);
     }
 
+    public static synchronized void test(){
+        i++;
+    }
+
+    private List<SmsCheckCustomerInfoVo> callSmsCheckCustomerInfoVo( List<SmsCheckCustomerInfoVo> list, Tuple dictTuple ) throws  Exception {
+
+        int errorCount =0;
+
+        List<SmsCheckCustomerInfoVo> voLists = new ArrayList<>();
+        // 开始时间
+        long start = System.currentTimeMillis();
+
+        Map<String,List<SmsCheckCustomerInfoVo>> map = new HashMap<>();
+
+        // 每50条数据开启一条线程
+        int threadSize = 50;
+        // 总数据条数
+        int dataSize = list.size();
+        // 线程数
+        int threadNum = dataSize / threadSize + 1;
+        // 定义标记,过滤threadNum为整数
+        boolean special = dataSize % threadSize == 0;
+        // 创建一个线程池
+        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadNum,
+                threadNum,
+                2L,
+                TimeUnit.SECONDS,
+                new ArrayBlockingQueue<>(3),
+                Executors.defaultThreadFactory(),
+                new ThreadPoolExecutor.AbortPolicy());
+
+        // 定义一个任务集合
+        List<Callable<List<SmsCheckCustomerInfoVo>>> tasks = new ArrayList<Callable<List<SmsCheckCustomerInfoVo>>>();
+        Callable<List<SmsCheckCustomerInfoVo>> task = null;
+        List<SmsCheckCustomerInfoVo> cutList = null;
+        // 确定每条线程的数据
+        for (int i = 0; i < threadNum; i++) {
+            //最后一次
+            if (i == threadNum - 1) {
+                if (special) {
+                    break;
+                }
+                cutList = list.subList(threadSize * i, dataSize);
+            } else {
+                cutList = list.subList(threadSize * i, threadSize * (i + 1));
+            }
+            final List<SmsCheckCustomerInfoVo> listStr = cutList;
+            task = new Callable<List<SmsCheckCustomerInfoVo>>() {
+                @Override
+                public List<SmsCheckCustomerInfoVo> call() throws Exception {
+                    System.out.println("线程" + Thread.currentThread().getName() + "正在执行");
+                    List<SmsCheckCustomerInfoVo> voList = new ArrayList<>();
+                    for (SmsCheckCustomerInfoVo vo : listStr) {
+
+                        SmsCheckCustomerInfoVo infoVo = checkCustomerInfo(vo, dictTuple);
+                        if (ObjectUtil.isNotNull(infoVo)) {
+                            if (StrUtil.isNotBlank(infoVo.getErrorMsg())) {
+                                test();
+                            }
+                            voList.add(infoVo);
+                        }
+                    }
+                    System.err.println("return voList; :" +  System.currentTimeMillis()  + "毫秒");
+                    return voList;
+                }
+            };
+//            Future<List<SmsCheckCustomerInfoVo>> submit = threadPoolExecutor.submit(task);
+//            voLists.addAll(submit.get());
+            tasks.add(task);
+        }
+
+        List<Future<List<SmsCheckCustomerInfoVo>>> results = threadPoolExecutor.invokeAll(tasks);
+        for (Future<List<SmsCheckCustomerInfoVo>> future : results) {
+            List<SmsCheckCustomerInfoVo> smsCheckCustomerInfoVo = future.get();
+            voLists.addAll(smsCheckCustomerInfoVo);
+        }
+        // 关闭线程池
+        threadPoolExecutor.shutdown();
+        System.out.println("线程任务执行结束");
+        System.err.println("执行任务消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");
+        return voLists;
+    }
     /**
      * 通过excel导入数据
      *
@@ -280,17 +385,20 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
         //导入失败数据
         List<SmsCheckCustomerInfoVo> listErro = new ArrayList<>();
         int customerCount = 0;
+
+        List<SysUser> userList = sysUserService.list(new LambdaQueryWrapper<SysUser>()
+                .eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0));
+
+        Map<String, SysUser> userMap
+                = userList.stream().collect(Collectors.toMap(SysUser::getWorkNo, Function.identity(), (o, n) -> o));
+
         for (int i = 0; i < list.size(); i++) {
             SmsCheckCustomerInfoVo infoVo = list.get(i);
             infoVo.setBatchNo(uuid.toString());
             infoVo.setTaskId(taskId);
             SmsCheckCustomerInfo info = new SmsCheckCustomerInfo();
             BeanUtil.copyProperties(infoVo, info, true);
-            List<SysUser> userList = sysUserService.list(new LambdaQueryWrapper<SysUser>()
-                    .eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0));
 
-            Map<String, SysUser> userMap
-                    = userList.stream().collect(Collectors.toMap(SysUser::getWorkNo, Function.identity(), (o, n) -> o));
 //            SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
 //                    .eq(SysUser::getWorkNo, infoVo.getStaffNo()));
             //放入用户信息
@@ -303,6 +411,7 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
             }
             info.setStaffUsername(user.getUsername());
             if (info.getSysOrgCode()!=null){
+                // 转派 可以根据页面传 orgCOde进行 判断    撤回不可以 所以在这让撤回只走 原逻辑    去掉 if (info.getSysOrgCode()!=null){
                 if (info.getSysOrgCode()==null){
                     infoVo.setResult("0");
                     infoVo.setErrorMsg("机构未查到");
@@ -542,53 +651,117 @@ public class SmsCheckTaskController extends JeecgController<SmsCheckTask, ISmsCh
                 vo.setStaffDeptLevel3(stringsThree.toString()+"("+stringsThreeOrg.toString()+")");
                 vo.setSysOrgCode(orgCode1);
             }
+        }
+        return vo;
+    }
+
 
+
+    /**
+     * 校验客户信息
+     *
+     * @param vo        客户信息
+     * @param dictTuple 字典转换
+     * @return 客户信息
+     */
+    private SmsCheckCustomerInfoVo checkCustomerInfoAdd(SmsCheckCustomerInfoVo vo, Tuple dictTuple
+            ,  Map<String, SysUser> userMap,List<SysDepartModel> sysDepartModel , Map<String, SysDepartModel> sysDepartModelMap) {
+        // TODO 客户重复性校验、数据合法性校验等
+        // 过滤客户编号和员工编号为空的信息
+        if (StrUtil.isBlank(vo.getCustomerNo()) && StrUtil.isBlank(vo.getUserNo())) {
+            return null;
         }
-        //员工三级部门
 
-//        if (!deptNameByUserIds.isEmpty()){
-//            vo.setStaffDeptLevel2No(sysDepartModelTow.getOrgCode());
-//            vo.setStaffDeptLevel3Name(deptNameByUserIds.toString());
-//            vo.setStaffDeptLevel2(sysDepartModelTow.getDepartName()+"("+sysDepartModelTow.getOrgCode()+")");
-//        }
-//        if (StringUtils.isNotBlank(orgCode) &&orgCode.length()>9){
-//            String towString = orgCode.substring(0, 9);
-//            SysDepartModel sysDepartModelTow= sysDepartModelMap.get(towString);
-//            if (sysDepartModelTow!=null){
-//                vo.setStaffDeptLevel2No(sysDepartModelTow.getOrgCode());
-//                vo.setStaffDeptLevel2Name(sysDepartModelTow.getDepartName());
-//                vo.setStaffDeptLevel2(sysDepartModelTow.getDepartName()+"("+sysDepartModelTow.getOrgCode()+")");
-//            }
-//            //员工三级部门
-//            String threeString = orgCode.substring(0, 13);
-//            SysDepartModel sysDepartModelThree = sysDepartModelMap.get(threeString);
-//            if (sysDepartModelThree!=null){
-//                vo.setStaffDeptLevel3No(sysDepartModelThree.getOrgCode());
-//                vo.setStaffDeptLevel3Name(sysDepartModelThree.getDepartName());
-//                vo.setStaffDeptLevel3(sysDepartModelThree.getDepartName()+"("+sysDepartModelThree.getOrgCode()+")");
-//            }
-      //  }
-
-
-//        // 拆分员工二级部门
-//        String staffDeptLevel2 = vo.getStaffDeptLevel2();
-//        List<String> staffDeptLevel2s = removeBrackets(staffDeptLevel2);
-//        if (staffDeptLevel2s.size() == 2) {
-//            vo.setStaffDeptLevel2Name(staffDeptLevel2s.get(0));
-//            vo.setStaffDeptLevel2No(staffDeptLevel2s.get(1));
-//        }
-//
-//        // 拆分员工三级部门
-//        String staffDeptLevel3 = vo.getStaffDeptLevel3();
-//        List<String> staffDeptLevel3s = removeBrackets(staffDeptLevel3);
-//        if (staffDeptLevel3s.size() == 2) {
-//            vo.setStaffDeptLevel3Name(staffDeptLevel3s.get(0));
-//            vo.setStaffDeptLevel3No(staffDeptLevel3s.get(1));
-//        }
+        // 格式化入网时间
+        String accessTime = vo.getNetworkAccessTime();
+        DateTime accessTimeDate = DateUtil.parse(accessTime, "yyyyMMdd");
+        vo.setNetworkAccessTime(accessTimeDate.toDateStr());
+
+        // 首次激活时间(固网起租时间)
+        String firstActivation = vo.getFirstActivationDate();
+        if (firstActivation!=null){
+            DateTime firstActivationDate = DateUtil.parse(firstActivation, "yyyyMMdd");
+            vo.setFirstActivationDate(firstActivationDate.toDateStr());
+        }
+        //  员工工号
+        String staffInfo = vo.getStaffInfo();
+        vo.setStaffNo(staffInfo);
+        // }
+
+        SysUser sysUser = userMap.get(vo.getStaffNo());
+        //  状态(1:正常  2:冻结 )
+        String orgCode="";
+        //员工姓名
+        if (sysUser!=null){
+            vo.setStaffName(sysUser.getRealname());
+            vo.setStaffUsername( sysUser.getUsername());
+            Integer status = sysUser.getStatus();
+            if (status!=1){
+                vo.setUserState("冻结");
+            }else {
+                vo.setUserState("正常");
+            }
+            orgCode = sysUser.getOrgCode();
+        }
+        //员工二级部门
+        if (sysUser!=null && vo.getSysOrgName()==null){
+            List<String> deptNameByUserId = taskService.getDeptNameByUserId( sysUser.getWorkNo());
+            if (!deptNameByUserId.isEmpty()){
+                Set<String>strings=new HashSet<>();
+                Set<String>stringsOrg=new HashSet<>();
+                Set<String>stringsThree=new HashSet<>();
+                Set<String>stringsThreeOrg=new HashSet<>();
+                for (String s : deptNameByUserId) {
+                    if (s.length()>=3){
+                        String substring = s.substring(0, 3);
+                        strings.add(sysDepartModelMap.get(substring).getDepartName());
+                        stringsOrg.add(substring);
+                    }
+                    if (s.length()>=6){
+                        String substring = s.substring(0, 6);
+                        stringsThree.add(sysDepartModelMap.get(substring).getDepartName());
+                        stringsThreeOrg.add(substring);
+                    }
+                }
+                vo.setStaffDeptLevel2No(stringsOrg.toString());
+                vo.setStaffDeptLevel2Name(strings.toString());
+                vo.setStaffDeptLevel2(strings.toString()+"("+stringsOrg.toString()+")");
+                vo.setStaffDeptLevel3No(stringsThreeOrg.toString());
+                vo.setStaffDeptLevel3Name(stringsThree.toString());
+                vo.setStaffDeptLevel3(stringsThree.toString()+"("+stringsThreeOrg.toString()+")");
+            }
+        }
+        if (vo.getSysOrgName()!=null){
+            Map<String, SysDepartModel> sysDepartModelMapCode
+                    = sysDepartModel.stream().collect(Collectors.toMap(SysDepartModel::getDepartName,Function.identity(), (o, n) -> o));
+            Set<String>strings=new HashSet<>();
+            Set<String>stringsOrg=new HashSet<>();
+            Set<String>stringsThree=new HashSet<>();
+            Set<String>stringsThreeOrg=new HashSet<>();
 
+            if (sysDepartModelMapCode.get(vo.getSysOrgName().trim())!=null){
+                String orgCode1 = sysDepartModelMapCode.get(vo.getSysOrgName().trim()).getOrgCode();
+                if (orgCode1.length()>=3){
+                    String substring = orgCode1.substring(0, 3);
+                    strings.add(sysDepartModelMap.get(substring).getDepartName());
+                    stringsOrg.add(substring);
+                }
+                if (orgCode1.length()>=6){
+                    String substring = orgCode1.substring(0, 6);
+                    stringsThree.add(sysDepartModelMap.get(substring).getDepartName());
+                    stringsThreeOrg.add(substring);
+                }
+                vo.setStaffDeptLevel2No(stringsOrg.toString());
+                vo.setStaffDeptLevel2Name(strings.toString());
+                vo.setStaffDeptLevel2(strings.toString()+"("+stringsOrg.toString()+")");
+                vo.setStaffDeptLevel3No(stringsThreeOrg.toString());
+                vo.setStaffDeptLevel3Name(stringsThree.toString());
+                vo.setStaffDeptLevel3(stringsThree.toString()+"("+stringsThreeOrg.toString()+")");
+                vo.setSysOrgCode(orgCode1);
+            }
+        }
         return vo;
     }
-
     /**
      * 移除括号并获取分段数据
      *