|
|
@@ -1,13 +1,30 @@
|
|
|
package com.usoftchina.smartschool.device.client.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.usoftchina.smartschool.device.base.Result;
|
|
|
import com.usoftchina.smartschool.device.client.jdbc.DynamicDataSourceContextHolder;
|
|
|
import com.usoftchina.smartschool.device.client.jdbc.DynamicDataSourceRegister;
|
|
|
-import com.usoftchina.smartschool.device.client.po.IcCard;
|
|
|
+import com.usoftchina.smartschool.device.client.po.*;
|
|
|
import com.usoftchina.smartschool.device.client.repository.IcCardRepository;
|
|
|
+import com.usoftchina.smartschool.device.client.utils.HmacUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.util.DefaultUriBuilderFactory;
|
|
|
+
|
|
|
+import java.net.URI;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author yingp
|
|
|
@@ -19,9 +36,32 @@ public class IcCardService {
|
|
|
@Autowired
|
|
|
private IcCardRepository icCardRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SchoolService schoolService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private DynamicDataSourceRegister dynamicDataSourceRegister;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Value("${device.icCard.icCardUrl}")
|
|
|
+ private String targetURL;
|
|
|
+
|
|
|
+ //SQL语句
|
|
|
+ private static final String insertSql = "INSERT INTO XF_AccTransDetail_push(GUID,AccNo,AccTransDay,AccTransType,IMoneyValue,OMoneyValue,CardUseNum,CardMoneyValue,DevID,DevOwner,OprtNo,Remark,XFDataSysID,XFPosMoneyByCredit,SendStatus,CreateTime) "
|
|
|
+ + " SELECT SOURCE.GUID,SOURCE.AccNo,SOURCE.AccTransDay,SOURCE.AccTransType,SOURCE.IMoneyValue,SOURCE.OMoneyValue,SOURCE.CardUseNum,SOURCE.CardMoneyValue,SOURCE.DevID,SOURCE.DevOwner,SOURCE.OprtNo,SOURCE.Remark,SOURCE.XFDataSysID,SOURCE.XFPosMoneyByCredit,'待上传',GETDATE() "
|
|
|
+ + " FROM XF_AccTransDetail SOURCE WHERE NOT EXISTS (SELECT 1 FROM XF_AccTransDetail_push TARGET WHERE SOURCE.GUID = TARGET.GUID)";
|
|
|
+
|
|
|
+ private static final String getDataSql = "select top 100 XF_AccTransDetail_push.GUID,RS_EMP.EmpSysID,RS_EMP.EmpNo,RS_EMP.EmpName,XF_AccHead.AccNo, XF_AccTransDetail_push.AccTransType,XF_AccTransDetail_push.AccTransDay,XF_AccTransDetail_push.IMoneyValue,XF_AccTransDetail_push.OMoneyValue,XF_AccTransDetail_push.CardMoneyValue "
|
|
|
+ + "from XF_AccTransDetail_push left join XF_AccHead on XF_AccTransDetail_push.AccNo=XF_AccHead.AccNo left join Tx_EmpCard on Tx_EmpCard.CardID=XF_AccHead.CardID "
|
|
|
+ + "left join RS_EMP ON RS_EMP.EmpSysID=XF_AccHead.EmpSysID where XF_AccTransDetail_push.SendStatus=? order by AccTransDay desc";
|
|
|
+
|
|
|
+ private static final String updateSql = "update XF_AccTransDetail_push set SendStatus = ? where GUID in (?)";
|
|
|
+
|
|
|
public IcCard find() {
|
|
|
return icCardRepository.find();
|
|
|
}
|
|
|
@@ -38,16 +78,90 @@ public class IcCardService {
|
|
|
dynamicDataSourceRegister.createDataSource(card);
|
|
|
}
|
|
|
|
|
|
- @Scheduled(cron = "", initialDelay = 5000)
|
|
|
+ @Scheduled(fixedRate = 1000 * 60, initialDelay = 5000)
|
|
|
public void dataTask() {
|
|
|
IcCard card = find();
|
|
|
- if (null != card && dynamicDataSourceRegister.contains(card)) {
|
|
|
+ School school = schoolService.find();
|
|
|
+ if (null != card && dynamicDataSourceRegister.contains(card) && null != school) {
|
|
|
DynamicDataSourceContextHolder.set(card);
|
|
|
try {
|
|
|
- // TODO
|
|
|
+ doTask();
|
|
|
} finally {
|
|
|
DynamicDataSourceContextHolder.clear();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private void doTask(){
|
|
|
+ //1.准备本次需要传输的数据->转移至中间表
|
|
|
+ jdbcTemplate.execute(insertSql);
|
|
|
+ //2.获取本次传输的数据
|
|
|
+ List<AccTransDetail> resultList = jdbcTemplate.query(getDataSql, new BeanPropertyRowMapper<>(AccTransDetail.class), "待上传");
|
|
|
+ List<MessageInfo> messageInfoList = new ArrayList<MessageInfo>();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ resultList.forEach(accTransDetail -> {
|
|
|
+ sb.append(accTransDetail.getGuid() + ",");
|
|
|
+ String oldType = accTransDetail.getAccTransType();
|
|
|
+ accTransDetail.setAccTransType(Transaction.getName(accTransDetail.getAccTransType()));
|
|
|
+ //构造messageInfo对象
|
|
|
+ MessageInfo messageInfo = new MessageInfo();
|
|
|
+ messageInfo.setMsgId(accTransDetail.getGuid());
|
|
|
+ messageInfo.setTouser(accTransDetail.getEmpNo());
|
|
|
+ //messageInfo.setTouser("o8lZ9uNXXY7VSfNM6be-7VZNkcOw");
|
|
|
+ messageInfo.setTouser(accTransDetail.getEmpName());
|
|
|
+ messageInfo.setUserType(2);
|
|
|
+ messageInfo.setTemplateId("FhtdzLdpzLLp4eJGtgvH4SUfIpSIF0kWwIpsWsSBp6c");
|
|
|
+ String accNo = accTransDetail.getAccNo();
|
|
|
+ String cardNo = StringUtils.isEmpty(accNo) ? null : accNo.substring(accTransDetail.getAccNo().length() - 4);
|
|
|
+ String header = "您好,您的小孩" + accTransDetail.getEmpName() + "在校的校园卡(卡号:*** " + cardNo + ")发生如下交易";
|
|
|
+ messageInfo.setTitle(header);
|
|
|
+ messageInfo.setKeyword1(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(accTransDetail.getAccTransDay()));
|
|
|
+ if (Integer.parseInt(oldType) > 15) {
|
|
|
+ messageInfo.setKeyword2(String.format("%.2f", accTransDetail.getoMoneyValue()));
|
|
|
+ }else {
|
|
|
+ messageInfo.setKeyword2(String.format("%.2f", accTransDetail.getiMoneyValue()));
|
|
|
+ }
|
|
|
+ messageInfo.setKeyword3(accTransDetail.getAccTransType());
|
|
|
+ messageInfo.setKeyword4(String.format("%.2f", accTransDetail.getCardMoneyValue()));
|
|
|
+ messageInfo.setRemark("感谢您使用");
|
|
|
+ messageInfoList.add(messageInfo);
|
|
|
+ });
|
|
|
+ //3.传输
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
|
|
|
+ requestEntity.add("data", JSON.toJSONString(messageInfoList));
|
|
|
+ HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(requestEntity, headers);
|
|
|
+ //禁止对该url进行encode操作
|
|
|
+ DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory();
|
|
|
+ defaultUriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
|
|
|
+ restTemplate.setUriTemplateHandler(defaultUriBuilderFactory);
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(getURI(targetURL, "/send"), httpEntity, String.class);
|
|
|
+ //4.完成后,将传输的成功的数据状态更新为"已上传"
|
|
|
+ if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
|
|
|
+ Result result = JSON.parseObject(responseEntity.getBody(), Result.class);
|
|
|
+ if (result.isSuccess()) {
|
|
|
+ String ids = "'" + sb.substring(0, sb.length() - 1).replaceAll(",", "','") + "'";
|
|
|
+ jdbcTemplate.update(updateSql, "已上传", ids);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getURI(String url, String subIndexOf, Object... vars){
|
|
|
+ StringBuilder accessUrl = new StringBuilder(url);
|
|
|
+ accessUrl.append(url.contains("?") ? "&" : "?");
|
|
|
+ // 身份ID
|
|
|
+ accessUrl.append("school=").append(schoolService.find().getName());
|
|
|
+ //时间戳
|
|
|
+ accessUrl.append("&_timestamp=").append(System.currentTimeMillis());
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ URI uri = restTemplate.getUriTemplateHandler().expand(accessUrl.toString(), vars);
|
|
|
+ url = uri.toString();
|
|
|
+ /*if (StringUtils.isEmpty(accessSecretKey)) {
|
|
|
+ return url + "&_signature=" + HmacUtils.encode(url.substring(url.indexOf(subIndexOf)));
|
|
|
+ }else {
|
|
|
+ return url + "&_signature=" + HmacUtils.encode(url.substring(url.indexOf(subIndexOf)), accessSecretKey);
|
|
|
+ }*/
|
|
|
+ return url + "&_signature=" + HmacUtils.encode(url.substring(url.indexOf(subIndexOf)));
|
|
|
+ }
|
|
|
}
|