|  |  | 
 |  |  | package com.doumee.core.erp; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSONArray; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.alibaba.fastjson.TypeReference; | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.erp.model.openapi.request.erp.ApproveAddRequest; | 
 |  |  | import com.doumee.core.erp.model.openapi.request.erp.OrgListRequest; | 
 |  |  | import com.doumee.core.erp.model.openapi.request.erp.UserFailRequest; | 
 |  |  | 
 |  |  | import com.doumee.core.erp.model.openapi.response.erp.ERPApiResponse; | 
 |  |  | import com.doumee.core.erp.model.openapi.response.erp.ErpOrgListResponse; | 
 |  |  | import com.doumee.core.erp.model.openapi.response.erp.ErpUserListResponse; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.model.PageData; | 
 |  |  | import com.doumee.core.model.PageWrap; | 
 |  |  | import com.doumee.core.utils.Constants; | 
 |  |  | import com.doumee.core.utils.HttpsUtil; | 
 |  |  | import com.doumee.dao.business.InterfaceLogMapper; | 
 |  |  | import com.doumee.dao.business.model.InterfaceLog; | 
 |  |  | import com.doumee.dao.business.model.Member; | 
 |  |  | import com.doumee.service.business.InterfaceLogService; | 
 |  |  | import com.github.pagehelper.PageInfo; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.apache.ibatis.session.SqlSession; | 
 |  |  | import org.apache.ibatis.session.SqlSessionFactory; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  |  | 
 |  |  | @Service | 
 |  |  | public class ErpTool { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private InterfaceLogService interfaceLogService; | 
 |  |  |     @Autowired | 
 |  |  |     private SqlSessionFactory sqlSessionFactory; | 
 |  |  |     public void saveInterfaceLog(Object param, String path,String result,Integer type) { | 
 |  |  |         SqlSession sqlSession2 = sqlSessionFactory.openSession(true); | 
 |  |  |         InterfaceLogMapper mapper2 = sqlSession2.getMapper(InterfaceLogMapper.class); | 
 |  |  |  | 
 |  |  |         InterfaceLog interfaceLog=new InterfaceLog(); | 
 |  |  |         interfaceLog.setType(type); | 
 |  |  |         interfaceLog.setCreateDate(new Date()); | 
 |  |  |         interfaceLog.setIsdeleted(Constants.ZERO); | 
 |  |  |         if(param!=null){ | 
 |  |  |             interfaceLog.setRequest(JSONObject.toJSONString(param)); | 
 |  |  |         } | 
 |  |  |         interfaceLog.setPlat(Constants.ONE); | 
 |  |  |         interfaceLog.setRepose(result); | 
 |  |  |         interfaceLog.setName(path); | 
 |  |  |         interfaceLog.setUrl(path); | 
 |  |  |         mapper2.insert(interfaceLog); | 
 |  |  |         sqlSession2.commit(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 同步组织 | 
 |  |  |      * @param url | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static List<ErpOrgListResponse> getErpOrgList(String url, OrgListRequest param){ | 
 |  |  |  | 
 |  |  |     public   List<ErpOrgListResponse> getErpOrgList(String url, OrgListRequest param){ | 
 |  |  |         List<ErpOrgListResponse> list = new ArrayList<>(); | 
 |  |  |         int page =1; | 
 |  |  |         int pageSize  = 100; | 
 |  |  |         boolean hasLast = true; | 
 |  |  |         while (hasLast){ | 
 |  |  |             hasLast = false; | 
 |  |  |             PageWrap<OrgListRequest> pageWrap  = new PageWrap<>(); | 
 |  |  |             pageWrap.setModel(param); | 
 |  |  |             pageWrap.setPage(page); | 
 |  |  |             pageWrap.setCapacity(pageSize); | 
 |  |  |             String result = HttpsUtil.postJson(url, JSONObject.toJSONString(pageWrap)); | 
 |  |  |             if(StringUtils.isNotBlank(result)){ | 
 |  |  |                 TypeReference typeReference = | 
 |  |  |                         new TypeReference<ERPApiResponse<PageData<ErpOrgListResponse>>>(){}; | 
 |  |  |                 ERPApiResponse<PageData<ErpOrgListResponse>> response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |                 if(response!=null && response.getData()!=null&& response.getData().getRecords()!=null){ | 
 |  |  |                     list.addAll(response.getData().getRecords()); | 
 |  |  |                         if(100*pageSize < response.getData().getTotal() ){ | 
 |  |  |                             hasLast =true;//还有下一页 | 
 |  |  |         try { | 
 |  |  |             int page = 1; | 
 |  |  |             int pageSize = 100; | 
 |  |  |             boolean hasLast = true; | 
 |  |  |             while (hasLast) { | 
 |  |  |                 hasLast = false; | 
 |  |  |                 PageWrap<OrgListRequest> pageWrap = new PageWrap<>(); | 
 |  |  |                 pageWrap.setModel(param); | 
 |  |  |                 pageWrap.setPage(page); | 
 |  |  |                 pageWrap.setCapacity(pageSize); | 
 |  |  |                 String result = HttpsUtil.postJson(url, JSONObject.toJSONString(pageWrap)); | 
 |  |  |                 if (StringUtils.isNotBlank(result)) { | 
 |  |  |                     TypeReference typeReference = | 
 |  |  |                             new TypeReference<ERPApiResponse<PageData<ErpOrgListResponse>>>() { | 
 |  |  |                             }; | 
 |  |  |                     ERPApiResponse<PageData<ErpOrgListResponse>> response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |                     if (response != null && response.getData() != null && response.getData().getRecords() != null) { | 
 |  |  |                         list.addAll(response.getData().getRecords()); | 
 |  |  |                         if (page * pageSize < response.getData().getTotal()) { | 
 |  |  |                             hasLast = true;//还有下一页 | 
 |  |  |                         } | 
 |  |  |                         page++; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             throw e; | 
 |  |  |         }finally { | 
 |  |  |             saveInterfaceLog(param,url,JSONArray.toJSONString(list),Constants.ZERO); | 
 |  |  |         } | 
 |  |  |         return  list; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static List<ErpUserListResponse> getErpUserList(String url, UserListRequest param){ | 
 |  |  |     public List<ErpUserListResponse> getErpUserList(String url, UserListRequest param){ | 
 |  |  |  | 
 |  |  |         List<ErpUserListResponse> list = new ArrayList<>(); | 
 |  |  |         int page =1; | 
 |  |  |         int pageSize  = 100; | 
 |  |  |         boolean hasLast = true; | 
 |  |  |         while (hasLast){ | 
 |  |  |             hasLast = false; | 
 |  |  |             PageWrap<UserListRequest> pageWrap  = new PageWrap<>(); | 
 |  |  |             pageWrap.setModel(param); | 
 |  |  |             pageWrap.setPage(page); | 
 |  |  |             pageWrap.setCapacity(pageSize); | 
 |  |  |             String result = HttpsUtil.postJson(url, JSONObject.toJSONString(pageWrap)); | 
 |  |  |             if(StringUtils.isNotBlank(result)){ | 
 |  |  |                 TypeReference typeReference = | 
 |  |  |                         new TypeReference<ERPApiResponse<PageData<ErpUserListResponse>>>(){}; | 
 |  |  |                 ERPApiResponse<PageData <ErpUserListResponse>> response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |                 if(response!=null && response.getData()!=null && response.getData().getRecords()!=null){ | 
 |  |  |                     list.addAll(response.getData().getRecords()); | 
 |  |  |                     if(100*pageSize < response.getData().getTotal() ){ | 
 |  |  |                         hasLast =true;//还有下一页 | 
 |  |  |         try{ | 
 |  |  |             int page =1; | 
 |  |  |             int pageSize  = 100; | 
 |  |  |             boolean hasLast = true; | 
 |  |  |             while (hasLast){ | 
 |  |  |                 hasLast = false; | 
 |  |  |                 PageWrap<UserListRequest> pageWrap  = new PageWrap<>(); | 
 |  |  |                 pageWrap.setModel(param); | 
 |  |  |                 pageWrap.setPage(page); | 
 |  |  |                 pageWrap.setCapacity(pageSize); | 
 |  |  |                 String result = HttpsUtil.postJson(url, JSONObject.toJSONString(pageWrap)); | 
 |  |  |                 if(StringUtils.isNotBlank(result)){ | 
 |  |  |                     TypeReference typeReference = | 
 |  |  |                             new TypeReference<ERPApiResponse<PageData<ErpUserListResponse>>>(){}; | 
 |  |  |                    ERPApiResponse<PageData <ErpUserListResponse>> response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |                     if(response!=null && response.getData()!=null && response.getData().getRecords()!=null){ | 
 |  |  |                         list.addAll(response.getData().getRecords()); | 
 |  |  |                         if(pageSize*page < response.getData().getTotal() ){ | 
 |  |  |                             hasLast =true;//还有下一页 | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                     page++; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             throw e; | 
 |  |  |         }finally { | 
 |  |  |             saveInterfaceLog(param,url, JSONArray.toJSONString(list),Constants.ZERO); | 
 |  |  |         } | 
 |  |  |         return  list; | 
 |  |  |  | 
 |  |  | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static ApproveInfoResponse submitApprove(String url, ApproveAddRequest param){ | 
 |  |  |     public  ApproveInfoResponse submitApprove(String url, ApproveAddRequest param){ | 
 |  |  |         System.out.println(JSONObject.toJSONString(param)); | 
 |  |  |         String result = HttpsUtil.postJson(url, JSONObject.toJSONString(param)); | 
 |  |  |         if(StringUtils.isNotBlank(result)){ | 
 |  |  | //            TypeReference typeReference = | 
 |  |  | //                    new TypeReference<ERPApiResponse<ApproveInfoResponse>>(){}; | 
 |  |  | //            ERPApiResponse<ApproveInfoResponse> response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  | //            if(response!=null && response.isSuccess()){ | 
 |  |  | //                return response.getData(); | 
 |  |  | //            } | 
 |  |  |             TypeReference typeReference = | 
 |  |  |                     new TypeReference<ERPApiResponse<String>>(){}; | 
 |  |  |             ERPApiResponse<String> response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |             if(response!=null && response.isSuccess()){ | 
 |  |  |                 ApproveInfoResponse approveInfoResponse = new ApproveInfoResponse(); | 
 |  |  |                 approveInfoResponse.setId(response.getData()); | 
 |  |  |                 return approveInfoResponse; | 
 |  |  |         try{ | 
 |  |  |             if(StringUtils.isNotBlank(result)){ | 
 |  |  |                 TypeReference typeReference = | 
 |  |  |                         new TypeReference<ERPApiResponse<String>>(){}; | 
 |  |  |                 ERPApiResponse<String> response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |                 if(response!=null && response.isSuccess()){ | 
 |  |  |                     ApproveInfoResponse approveInfoResponse = new ApproveInfoResponse(); | 
 |  |  |                     approveInfoResponse.setId(response.getData()); | 
 |  |  |                     return approveInfoResponse; | 
 |  |  |                 }else if(response!=null && !response.isSuccess()){ | 
 |  |  |                     throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),response.getMessage()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             throw e; | 
 |  |  |         }finally { | 
 |  |  |            saveInterfaceLog(param,url,JSONObject.toJSONString(result),Constants.ZERO); | 
 |  |  |         } | 
 |  |  |         return  null; | 
 |  |  |     } | 
 |  |  | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static ERPApiResponse noticeUserStatus(String url, UserFailRequest param){ | 
 |  |  |     public   ERPApiResponse noticeUserStatus(String url, UserFailRequest param){ | 
 |  |  |         String result = HttpsUtil.postJson(url, JSONObject.toJSONString(param)); | 
 |  |  |         if(StringUtils.isNotBlank(result)){ | 
 |  |  |             TypeReference typeReference = | 
 |  |  |                     new TypeReference<ERPApiResponse>(){}; | 
 |  |  |             ERPApiResponse response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |             if(response!=null){ | 
 |  |  |                 return response; | 
 |  |  |         try{ | 
 |  |  |             if(StringUtils.isNotBlank(result)){ | 
 |  |  |                 TypeReference typeReference = | 
 |  |  |                         new TypeReference<ERPApiResponse>(){}; | 
 |  |  |                 ERPApiResponse response = JSONObject.parseObject(result, typeReference.getType()); | 
 |  |  |                 if(response!=null){ | 
 |  |  |                     return response; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             throw e; | 
 |  |  |         }finally { | 
 |  |  |             saveInterfaceLog(param,url,JSONObject.toJSONString(result),Constants.ZERO); | 
 |  |  |         } | 
 |  |  |         return  null; | 
 |  |  |     } |