|
|
@@ -6,7 +6,8 @@ import com.usoftchina.smartschool.device.client.jdbc.DynamicDataSourceContextHol
|
|
|
import com.usoftchina.smartschool.device.client.jdbc.DynamicDataSourceRegister;
|
|
|
import com.usoftchina.smartschool.device.client.po.*;
|
|
|
import com.usoftchina.smartschool.device.client.repository.IcCardRepository;
|
|
|
-import com.usoftchina.smartschool.device.client.utils.HmacUtils;
|
|
|
+import com.usoftchina.smartschool.device.dto.AccTransDetail;
|
|
|
+import com.usoftchina.smartschool.device.exception.ExceptionCode;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.*;
|
|
|
@@ -19,11 +20,7 @@ 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;
|
|
|
|
|
|
/**
|
|
|
@@ -97,71 +94,27 @@ public class IcCardService {
|
|
|
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));
|
|
|
+ requestEntity.add("data", JSON.toJSONString(resultList));
|
|
|
+ requestEntity.add("school", schoolService.find().getName());
|
|
|
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);
|
|
|
+ ResponseEntity<Result> response = restTemplate.postForEntity(targetURL, resultList, Result.class);
|
|
|
+ if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
+ Result<String> result = response.getBody();
|
|
|
+ if (!result.isSuccess()) {
|
|
|
+ ExceptionCode.ERROR_UNKNOWN.occur(result.getMessage());
|
|
|
+ }else {
|
|
|
+ //更新发送的数据
|
|
|
+ String ids = result.getData();
|
|
|
+ if (!StringUtils.isEmpty(ids)){
|
|
|
+ jdbcTemplate.update(updateSql, "已上传", ids);
|
|
|
+ }
|
|
|
}
|
|
|
+ } else {
|
|
|
+ ExceptionCode.ERROR_UNKNOWN.occur("IC卡消费记录发送失败");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- 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)));
|
|
|
- }
|
|
|
}
|