Commit bb0c448b by liyanlin

netsuite任务

parent c7968b50
package com.blt.other.common.job;
import com.blt.other.module.cost.service.UserCostFinansysService;
import com.blt.other.module.netsuite.service.NetsuiteLogService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
......@@ -18,13 +19,11 @@ public class NetSuiteSyncJob extends QuartzJobBean {
private static Logger logger = LoggerFactory.getLogger(NetSuiteSyncJob.class);
@Autowired
private UserCostFinansysService userCostFinansysService;
private NetsuiteLogService netsuiteLogService;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
String result = userCostFinansysService.syncUserFinance();
if (!"新增 0 条记录,更新 0 条记录".equals(result)) {
logger.info("Job:从财务系统获取财务信息," + result);
}
netsuiteLogService.publishToNetsuite(100);
logger.info("Job:上传费用信息到Netsuite中心");
}
}
......@@ -2,6 +2,7 @@ package com.blt.other.common.job;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: li.yanlin
......@@ -9,9 +10,10 @@ import org.springframework.context.annotation.Bean;
* @Date: Created in 17:12 2021-04-24
* @Modified by:
*/
@Configuration
public class NetSuiteSyncJobConfiguration {
// 扫描主体列表时间间隔:(秒)
private static final int TIME = 86400;
private static final int TIME = 3600;
// JobDetail 定义要执行的 job
@Bean
......
......@@ -67,7 +67,7 @@ public class HttpUtil {
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static RestTemplate getHttpsRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
/*public static RestTemplate getHttpsRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
......@@ -75,7 +75,7 @@ public class HttpUtil {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
return new RestTemplate(requestFactory);
}
}*/
/**
* 发送https get请求
......@@ -83,9 +83,9 @@ public class HttpUtil {
* @return
* @throws Exception
*/
public static String httpsGet(String url) throws Exception{
/*public static String httpsGet(String url) throws Exception{
RestTemplate restTemplate = getHttpsRestTemplate();
return restTemplate.getForObject(url, String.class);
}
}*/
}
package com.blt.other.common.util;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
......@@ -81,10 +79,8 @@ public class OAuth1Util {
* @return
* @throws Exception
*/
public static HttpResponse executePost(String customURIString, Map<String, String> header, String parameter) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
URIBuilder builder = null;
public static String executePost(String customURIString, Map<String, String> header, String parameter) throws Exception {
URIBuilder builder;
builder = new URIBuilder(customURIString);
HttpPost request = new HttpPost(builder.build());
HttpEntity httpEntity = new StringEntity(parameter, "application/json", "utf-8");
......@@ -97,6 +93,29 @@ public class OAuth1Util {
authorize(request);
HttpClient httpClient = new DefaultHttpClient();
return httpClient.execute(request);
HttpResponse response = httpClient.execute(request);
return inputStreamToString(response.getEntity().getContent());
}
private static String inputStreamToString(InputStream inputStream) {
StringBuffer buffer = new StringBuffer();
InputStreamReader inputStreamReader;
try {
inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
}
......@@ -33,5 +33,6 @@ public class CostCashiercallbackDomain {
private String cashiermothercard;
private BigDecimal toRmbRate;
private Date actualTime;
private String erpAccountingType;
}
......@@ -9,4 +9,6 @@ import org.springframework.stereotype.Repository;
public interface CostCashiercallbackDao {
Integer insert(CostCashiercallbackDomain costCashiercallbackDomain);
CostCashiercallbackDomain selectByCostNo(String costNo);
}
package com.blt.other.module.cost.service.impl.cost;
import com.alibaba.fastjson.JSON;
import com.bailuntec.api.bailuntec.cw.CwApi;
import com.bailuntec.common.JsonUtilByFsJson;
import com.bailuntec.common.JsonUtilByJackson;
......@@ -25,6 +26,7 @@ import com.blt.other.module.auth.model.OaUser;
import com.blt.other.module.cost.dao.*;
import com.blt.other.module.cost.dto.request.CheckCostListReq;
import com.blt.other.module.cost.dto.request.CostQueryPageReq;
import com.blt.other.module.cost.dto.response.CostTypeResult;
import com.blt.other.module.cost.dto.response.UpdateCostResp;
import com.blt.other.module.cost.model.*;
import com.blt.other.module.cost.service.*;
......@@ -32,20 +34,24 @@ import com.blt.other.module.cost.vo.ApplyCallbackUrlDataDataVo;
import com.blt.other.module.cost.vo.ApplyCallbackUrlVo;
import com.blt.other.module.cost.vo.CashierCallbackUrlDataDataVo;
import com.blt.other.module.cost.vo.CashierCallbackUrlVo;
import com.blt.other.module.netsuite.dao.NetsuiteLogDao;
import com.blt.other.module.netsuite.dto.NetsuiCostinfoDto;
import com.blt.other.module.netsuite.dto.NetsuiteDataDto;
import com.blt.other.module.netsuite.model.NetsuiteLogDomain;
import com.blt.other.module.sys.service.UserService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import sun.util.locale.provider.DateFormatProviderImpl;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.spi.DateFormatProvider;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
......@@ -79,6 +85,8 @@ public abstract class AbstractCostService implements CostService {
CostUrlProperties costUrlProperties;
@Resource
OaDepartmentMapper oaDepartmentMapper;
@Resource
NetsuiteLogDao netsuiteLogDao;
@Override
public void setPrimaryDepartment(List<CostDomain> costDomains) {
......@@ -370,10 +378,56 @@ public abstract class AbstractCostService implements CostService {
}
//通过
else {
//netsuite保存一份,只保存香港百伦
this.saveNetsuiteJob(costDomain, costCashiercallbackDomain);
this.cashierCallbackPass(costCashiercallbackDomain);
}
}
void saveNetsuiteJob(CostDomain costDomain, CostCashiercallbackDomain costCashiercallbackDomain) {
//只保存香港百伦
if (costDomain.getCompanyValue() == 2) {
//xgbl代表香港百伦
String companyCode = "xgbl";
CostTypeResult costTypeResult = costTypeDao.queryDetail(costDomain.getTypeId());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
NetsuiteDataDto netsuiteDataDto = new NetsuiteDataDto();
netsuiteDataDto.setTranid(costDomain.getCostNo());
netsuiteDataDto.setCurrency(costCashiercallbackDomain.getCashierunitcode().toUpperCase());
netsuiteDataDto.setSubsidiary(companyCode);
netsuiteDataDto.setTrandate(sdf.format(costCashiercallbackDomain.getActualTime()));
netsuiteDataDto.setCustbody_iofeetype(costDomain.getCostForm().toString());
//费用详情
List<NetsuiCostinfoDto> netsuiCostInfoDtos = new ArrayList<>();
//费用系统信息
NetsuiCostinfoDto feeSysInfo = new NetsuiCostinfoDto(
costTypeResult.getNsAccountingSubjectId().toString(),
costDomain.getAmount().toString(),
costDomain.getCostReason()
);
netsuiCostInfoDtos.add(feeSysInfo);
//财务系统信息
NetsuiCostinfoDto cwSysInfo = new NetsuiCostinfoDto(
costCashiercallbackDomain.getErpAccountingType(),
costCashiercallbackDomain.getPayamount().toString(),
costCashiercallbackDomain.getCashierunitcode().toUpperCase()
);
netsuiCostInfoDtos.add(cwSysInfo);
netsuiteDataDto.setCostinfo(netsuiCostInfoDtos);
NetsuiteLogDomain netsuiteLogDomain = new NetsuiteLogDomain();
netsuiteLogDomain.setCostNo(costDomain.getCostNo());
netsuiteLogDomain.setPublishStatus(NetsuiteLogDomain.UNPUBLISH);
netsuiteLogDomain.setScript(NetsuiteLogDomain.SCRIPT_FEE);
netsuiteLogDomain.setJsonStr(JSON.toJSONString(netsuiteDataDto));
netsuiteLogDao.upsert(netsuiteLogDomain);
}
}
@Override
public void reject(String costNo) {
......
......@@ -133,6 +133,7 @@ public class Lend2PayCostServiceImpl extends AbstractCostService implements Cost
costDomain.setCostStatus(CostDomain.STATUS_PAYED);
costDomain.setPayUserId(costCashiercallbackDomain.getPayuserid());
costDomain.setPayTime(new Date());
costDomain.setActualTime(costCashiercallbackDomain.getActualTime());
BigDecimal toRmbRate = CurUtils.getCur(costDomain.getDic(), "CNY");
costDomain.setToRmbRate(toRmbRate);
......
package com.blt.other.module.netsuite.dao;
import com.blt.other.module.netsuite.dto.NetsuiteLogDto;
import com.blt.other.module.netsuite.model.NetsuiteLogDomain;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
@Mapper
@Repository
public interface NetsuiteLogDao {
/**
* 更新或插入
*
* @param netsuiteLogDomain
* @return
*/
Integer upsert(NetsuiteLogDomain netsuiteLogDomain);
/**
* 查询最早更新且未推送的前Top条数据
*
* @param top
* @return
*/
List<NetsuiteLogDto> selectNonPublishTop(@Param("script") Integer script, @Param("top") Integer top);
/**
* 更新推送状态
*
* @param map
* @return
*/
Integer updatePublishStatus(Map<String,Object> map);
}
package com.blt.other.module.netsuite.dto;
import com.sun.istack.NotNull;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
@Getter
@Setter
public class NetsuiCostinfoDto {
/**
* 付款银行
*/
private String account;
/**
* 金额
*/
private String amount;
/**
* 理由
*/
private String memo;
public NetsuiCostinfoDto(@NotNull String account,@NotNull String amount,String memo){
this.account = account;
this.amount = amount;
this.memo = memo;
}
}
package com.blt.other.module.netsuite.dto;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
@Data
public class NetsuiteDataDto {
/**
* 费用单号
*/
private String tranid;
private String currency;
/**
* 付款主体
*/
private String subsidiary;
/**
* 费用单关联的出纳单日期格式为YYYY/MM/DD
*/
private String trandate;
/**
* 费用类型
*/
private String custbody_iofeetype;
private List<NetsuiCostinfoDto> costinfo;
}
package com.blt.other.module.netsuite.dto;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
@Component
@ConfigurationProperties(prefix = "net-suite")
@Data
public class NetsuiteKey {
private String baseUrl;
private String consumerKey;
private String consumerSecret;
private String token;
private String tokenSecret;
private String realm;
}
package com.blt.other.module.netsuite.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
@Data
public class NetsuiteLogDto implements Serializable {
@ApiModelProperty("费用单号")
private String costNo;
@ApiModelProperty("json参数")
private String jsonStr;
}
package com.blt.other.module.netsuite.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
@ApiModel("Netsuite日志")
@TableName(value = "netsuite_log", autoResultMap = true)
@Data
public class NetsuiteLogDomain implements Serializable {
/**
* 未推送
*/
public final static Integer UNPUBLISH = 0;
/**
* 已推送
*/
public final static Integer PUBLISHED = 1;
/**
* 推送失败
*/
public final static Integer FAILURE = 99;
/**
* netsuite 传输类型;78:费用单
*/
public final static Integer SCRIPT_FEE = 78;
@TableId(type = IdType.AUTO)
private Integer id;
@ApiModelProperty("费用单号")
private String costNo;
@ApiModelProperty("json参数")
private String jsonStr;
@ApiModelProperty("脚本ID,78:费用单")
private Integer script;
@ApiModelProperty("请求异常信息")
private String errorMsg;
@ApiModelProperty("推送状态,0:未推送,1:已推送,99:推送失败")
private Integer publishStatus;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("最后更新时间")
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("最后更新时间")
private LocalDateTime lastModifyTime;
}
package com.blt.other.module.netsuite.service;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
public interface NetsuiteLogService {
void publishToNetsuite(int top);
}
package com.blt.other.module.netsuite.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bailuntec.common.ListUtil;
import com.blt.other.common.util.OAuth1Util;
import com.blt.other.module.netsuite.dao.NetsuiteLogDao;
import com.blt.other.module.netsuite.dto.NetsuiteDataDto;
import com.blt.other.module.netsuite.dto.NetsuiteKey;
import com.blt.other.module.netsuite.dto.NetsuiteLogDto;
import com.blt.other.module.netsuite.model.NetsuiteLogDomain;
import com.blt.other.module.netsuite.service.NetsuiteLogService;
import com.qiniu.util.Json;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.http.HttpResponse;
import org.jose4j.json.JsonUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Author: li.yanlin
* @Description:
* @Date: Created in 2021-04-28
* @Modified by:
*/
@Service
@Slf4j
public class NetsuiteLogServiceImpl implements NetsuiteLogService {
@Resource
private NetsuiteLogDao netsuiteLogDao;
@Resource
private NetsuiteKey netsuiteKey;
@Override
public void publishToNetsuite(int top) {
//78:费用单
Map<String,Object> resultMap = new HashMap<>();
Integer script = 78;
List<NetsuiteLogDto> list = netsuiteLogDao.selectNonPublishTop(script,top);
if(ListUtil.isEmpty(list)){
return;
}
List<NetsuiteDataDto> netsuiteDataDtoList = list.stream()
.map(x -> JSON.parseObject(x.getJsonStr(), NetsuiteDataDto.class))
.collect(Collectors.toList());
List<String> costNos = list.stream().map(x -> x.getCostNo()).collect(Collectors.toList());
resultMap.put("costNos",costNos);
OAuth1Util.setupContext(netsuiteKey.getConsumerKey(),
netsuiteKey.getConsumerSecret(),
netsuiteKey.getToken(),
netsuiteKey.getTokenSecret(),
netsuiteKey.getRealm());
try {
String response = OAuth1Util.executePost(String.format(netsuiteKey.getBaseUrl(),script),
null,
JSON.toJSONString(netsuiteDataDtoList));
JSONObject jobj = JSON.parseObject(response);
if(!BooleanUtils.toBoolean(jobj.get("status").toString())){
String errorMsg = String.format("同步到NetSuite中心异常,\nNetSuite返回内容:%s\n任务ID:78\n请求参数:%s",
response,
JSON.toJSONString(netsuiteDataDtoList));
resultMap.put("status", NetsuiteLogDomain.FAILURE);
resultMap.put("error",errorMsg);
log.info(errorMsg);
}
else{
resultMap.put("status", NetsuiteLogDomain.PUBLISHED);
}
} catch (Exception e) {
resultMap.put("status", NetsuiteLogDomain.FAILURE);
resultMap.put("error",e.getMessage());
log.info(e.toString());
}
netsuiteLogDao.updatePublishStatus(resultMap);
}
}
......@@ -95,7 +95,7 @@ server:
# NetSuite Api Base
net-suite:
base-url: https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script={}&deploy=1
base-url: https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=%s&deploy=1
consumer-key: 4e1786849ffd6cb866f2f22b43cce56793e446d56f1cf60fe45674aa02424646
consumer-secret: d66b2d4c6d6a9bf59b7923e3d32535b1e3685babf4109581884230fdb00ec330
token: 042c17360b2f3c831c9531365c8b98ef6aeeaf40ce4e7c46144ee70aad2e7667
......
......@@ -95,7 +95,7 @@ server:
# NetSuite Api Base
net-suite:
base-url: https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script={}&deploy=1
base-url: https://6916374-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=%s&deploy=1
consumer-key: 4e1786849ffd6cb866f2f22b43cce56793e446d56f1cf60fe45674aa02424646
consumer-secret: d66b2d4c6d6a9bf59b7923e3d32535b1e3685babf4109581884230fdb00ec330
token: 042c17360b2f3c831c9531365c8b98ef6aeeaf40ce4e7c46144ee70aad2e7667
......
......@@ -9,14 +9,21 @@
cost_no,message,payid,payno,payamount,payuserid,payusername,paynote,cashierbankaccountid,
cashierbankname,cashierbankcard,cashierbankcardname,cashierunitcode,cashierunitname,
cashierrate,cashierpaymoney,cashierpaymoneyrmb,cashierservicemoneyrmb,otherordercode,
cashierannex,cashierbankcardtype,cashiermothercard,to_rmb_rate,actualTime
cashierannex,cashierbankcardtype,cashiermothercard,to_rmb_rate,actualTime,erp_accounting_type
)
VALUE
(
#{costNo},#{message}, #{payid}, #{payno}, #{payamount}, #{payuserid}, #{payusername}, #{paynote}, #{cashierbankaccountid},
#{cashierbankname}, #{cashierbankcard}, #{cashierbankcardname}, #{cashierunitcode}, #{cashierunitname},
#{cashierrate}, #{cashierpaymoney}, #{cashierpaymoneyrmb}, #{cashierservicemoneyrmb}, #{otherordercode},
#{cashierannex}, #{cashierbankcardtype}, #{cashiermothercard},#{toRmbRate},#{actualTime}
#{cashierannex}, #{cashierbankcardtype}, #{cashiermothercard},#{toRmbRate},#{actualTime},#{erpAccountingType}
)
</insert>
<select id="selectByCostNo" resultType="com.blt.other.database.model.CostCashiercallbackDomain">
select id,cost_no,message,payid,payno,payamount,payuserid,payusername,paynote,cashierbankaccountid,
cashierbankname,cashierbankcard,cashierbankcardname,cashierunitcode,cashierunitname,
cashierrate,cashierpaymoney,cashierpaymoneyrmb,cashierservicemoneyrmb,otherordercode,
cashierannex,cashierbankcardtype,cashiermothercard,to_rmb_rate,actualTime
from cost_cashiercallback where cost_no = #{costNo} limit 1
</select>
</mapper>
......@@ -56,7 +56,8 @@
t1.last_update_time,
t1.create_user_id,
t1.create_user,
t1.is_manage_cost
t1.is_manage_cost,
t1.ns_accounting_subject_id
from cost_type t1
left join accounting_subject t2 on t1.accounting_subject_no = t2.subject_no
where t1.id = #{id}
......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.blt.other.module.netsuite.dao.NetsuiteLogDao">
<insert id="upsert" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="com.blt.other.module.netsuite.model.NetsuiteLogDomain">
INSERT INTO
netsuite_log(
cost_no,json_str,script
)
VALUE
(
#{costNo,jdbcType=VARCHAR},#{jsonStr,jdbcType=VARCHAR},#{script,jdbcType=INTEGER}
)
on duplicate key update
json_str = #{jsonStr,jdbcType=VARCHAR},
publish_status = #{publishStatus,jdbcType=INTEGER}
</insert>
<select id="selectNonPublishTop" resultType="com.blt.other.module.netsuite.dto.NetsuiteLogDto" parameterType="integer">
select cost_no as costNo,json_str as jsonStr from netsuite_log nl where publish_status in(0,99) and script = #{script} order by last_modify_time desc limit #{top}
</select>
<update id="updatePublishStatus" parameterType="map">
update netsuite_log set publish_status = #{status,jdbcType=INTEGER},error_msg = #{error,jdbcType=VARCHAR}
where cost_no in
<foreach collection="costNos" item="costNos" separator="," open="(" close=")">
#{costNos,jdbcType=VARCHAR}
</foreach>
</update>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment