Explorar el Código

同步oa数据

Zimo hace 2 horas
padre
commit
4699603d51

+ 13 - 2
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/job/OaPeopleJob.java

@@ -1,10 +1,13 @@
 package cn.iocoder.yudao.module.system.job;
 
+import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
 import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
 import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
 import cn.iocoder.yudao.module.system.oa.OaFlow;
+import com.google.common.collect.ImmutableMap;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
 @Component
@@ -16,9 +19,17 @@ public class OaPeopleJob implements JobHandler {
     @Override
     @TenantIgnore
     public String execute(String param) throws Exception {
-        oaFlow.getCompany();
-        oaFlow.getDepart();
+//        oaFlow.getCompany();
+//        oaFlow.getDepart();
         oaFlow.getPerson();
         return "";
     }
+
+    @Scheduled(cron = "0 0 4 * * ?")
+    @TenantIgnore
+    @DataPermission(enable = false)
+    public String executeOaPerson() throws Exception {
+        oaFlow.getOaPerson();
+        return "";
+    }
 }

+ 86 - 5
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/oa/OaFlow.java

@@ -1,27 +1,37 @@
 package cn.iocoder.yudao.module.system.oa;
 
 import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
+import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
+import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
+import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO;
 import cn.iocoder.yudao.module.system.dal.dataobject.oa.IotOaCompanyDO;
 import cn.iocoder.yudao.module.system.dal.dataobject.oa.IotOaDepartDO;
 import cn.iocoder.yudao.module.system.dal.dataobject.oa.IotOaPersonDO;
+import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
 import cn.iocoder.yudao.module.system.dal.mysql.oa.IotOaCompanyMapper;
 import cn.iocoder.yudao.module.system.dal.mysql.oa.IotOaDepartMapper;
 import cn.iocoder.yudao.module.system.dal.mysql.oa.IotOaPersonMapper;
+import cn.iocoder.yudao.module.system.service.user.AdminUserServiceImpl;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import lombok.extern.java.Log;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
+import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
+import software.amazon.awssdk.core.traits.ListTrait;
 
 import java.util.HashMap;
 import java.util.List;
@@ -31,6 +41,8 @@ import java.util.stream.Collectors;
 
 @Component
 public class OaFlow {
+    @Autowired
+    private RestTemplate restTemplate;
     private final IotOaCompanyMapper iotOaCompanyMapper;
     private final IotOaDepartMapper iotOaDepartMapper;
     private final IotOaPersonMapper iotOaPersonMapper;
@@ -61,6 +73,12 @@ public class OaFlow {
 
     private static String spk = "";
     private static String secret = "";
+    @Autowired
+    private PasswordEncoder passwordEncoder;
+    @Autowired
+    private AdminUserApi adminUserApi;
+    @Autowired
+    private AdminUserServiceImpl adminUserService;
 
     public OaFlow(IotOaCompanyMapper iotOaCompanyMapper, IotOaDepartMapper iotOaDepartMapper, IotOaPersonMapper iotOaPersonMapper) {
         this.iotOaCompanyMapper = iotOaCompanyMapper;
@@ -195,8 +213,8 @@ public class OaFlow {
     public void getPerson() throws Exception {
         String token = getToken();
         HttpHeaders headersOut = new HttpHeaders();
-        headersOut.add("token", token);
-        headersOut.add("appid", appid);
+//        headersOut.add("token", token);
+//        headersOut.add("appid", appid);
         // headersOut.add("userid", userid);
 
         Map<String, Object> params = new HashMap<>();
@@ -206,7 +224,7 @@ public class OaFlow {
         params.put("params", map);
         // 3. 组合请求头和请求体
         HttpEntity<Map<String, Object>> requestEntityOut = new HttpEntity<>(params, headersOut);
-        RestTemplate restTemplate = SslSkippingRestTemplate.createRestTemplate();
+//        RestTemplate restTemplate = SslSkippingRestTemplate.createRestTemplate();
         String company = restTemplate.postForObject(userUrl, requestEntityOut, String.class);
         JSONObject companyInfo = JSON.parseObject(company);
         //请求成功
@@ -232,8 +250,71 @@ public class OaFlow {
         }
     }
 
-    @Autowired
-    private RestTemplate restTemplate;
+    @Transactional(rollbackFor = Exception.class)
+    public void getOaPerson() throws Exception {
+        Map<String, Object> params = new HashMap<>();
+        // 3. 组合请求头和请求体
+        HttpEntity<Map<String, Object>> requestEntityOut = new HttpEntity<>(params);
+//        RestTemplate restTemplate = SslSkippingRestTemplate.createRestTemplate();
+        String company = restTemplate.postForObject(userUrl, requestEntityOut, String.class);
+        JSONObject companyInfo = JSON.parseObject(company);
+        //请求成功
+        if (Objects.nonNull(companyInfo) && "true".equals(String.valueOf(companyInfo.get("success")))) {
+            if (Objects.nonNull(companyInfo.get("data"))) {
+                List<OaPersonVO> data = JSON.parseArray(companyInfo.get("data").toString(), OaPersonVO.class);
+                List<IotOaPersonDO> iotOaPersonDOS = iotOaPersonMapper.selectList();
+                List<String> workcodes = iotOaPersonDOS.stream().map(IotOaPersonDO::getWorkcode).collect(Collectors.toList());
+                TenantUtils.execute(1L, () -> {
+                    data.forEach(item -> {
+                        //更新
+                        if (Objects.nonNull(item.getLoginid())&&StringUtils.isNotBlank(item.getLoginid())) {
+                            if (workcodes.contains(item.getLoginid())) {
+                                iotOaPersonDOS.stream().filter(f -> Objects.nonNull(f.getWorkcode())&&f.getWorkcode().equals(item.getLoginid())).findAny().ifPresent(workcode -> {
+                                    workcode.setDepartmentid(String.valueOf(item.getDepartmentid()));
+                                    workcode.setStatus(String.valueOf(item.getStatus()));
+                                    iotOaPersonMapper.updateById(workcode);
+                                });
+                            } else {//新增
+                                IotOaPersonDO iotOaPersonDO = new IotOaPersonDO();
+                                iotOaPersonDO.setOaId(String.valueOf(item.getId()));
+                                iotOaPersonDO.setDeleted(false);
+                                iotOaPersonDO.setWorkcode(item.getLoginid());
+                                iotOaPersonDO.setMobile(String.valueOf(item.getMobile()));
+                                iotOaPersonDO.setLastname(String.valueOf(item.getLastname()));
+                                iotOaPersonDO.setStatus(String.valueOf(item.getStatus()));
+                                iotOaPersonMapper.insert(iotOaPersonDO);
+                                AdminUserRespDTO userByUsername = adminUserApi.getUserByUsername(item.getLoginid());
+                                if (Objects.isNull(userByUsername)) {
+                                    AdminUserDO adminUserDO = new AdminUserDO();
+                                    adminUserDO.setNickname(item.getLastname());
+                                    adminUserDO.setDeleted(false);
+                                    adminUserDO.setUsername(item.getLoginid());
+                                    if (StringUtils.isNotBlank(item.getMobile())&&item.getMobile().length()<12)
+                                    adminUserDO.setMobile(item.getMobile());
+                                    adminUserDO.setPassword(passwordEncoder.encode("rqny2026*"));
+                                    UserSaveReqVO userSaveReqVO = new UserSaveReqVO();
+                                    BeanUtils.copyProperties(adminUserDO, userSaveReqVO);
+                                    adminUserService.createUser(userSaveReqVO);
+                                }
+
+                            }
+                        }
+                    });
+                });
+            }
+        }
+    }
+
+    @Data
+    public static class OaPersonVO{
+        private String loginid;
+        private String departmentid;
+        private String mobile;
+        private String id;
+        private String subcompanyid1;
+        private String lastname;
+        private String status;
+    }
 
     /* public void createOutRepairFlow(IotMaintainDO iotMaintainDO) throws Exception {
         String token = getToken();

+ 6 - 0
yudao-server/src/main/java/cn/iocoder/yudao/server/controller/admin/TodoController.java

@@ -184,6 +184,12 @@ public class TodoController {
         return CommonResult.success(srmNotice);
     }
 
+    @GetMapping("/oa/person/init")
+    @PermitAll
+    public void oainit(String workcode) throws Exception {
+        oaFlow.getOaPerson();
+    }
+
     @GetMapping("/ehr/notice")
     @PermitAll
     public CommonResult<List> ehrNotice(String workcode) throws Exception {