UserServiceSMOImpl.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. package com.java110.user.smo.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.JSONPath;
  5. import com.java110.common.constant.StateConstant;
  6. import com.java110.common.log.LoggerEngine;
  7. import com.java110.common.util.Assert;
  8. import com.java110.common.util.ProtocolUtil;
  9. import com.java110.core.base.smo.BaseServiceSMO;
  10. import com.java110.entity.order.BusiOrder;
  11. import com.java110.entity.user.BoCust;
  12. import com.java110.entity.user.BoCustAttr;
  13. import com.java110.entity.user.Cust;
  14. import com.java110.entity.user.CustAttr;
  15. import com.java110.feign.base.IPrimaryKeyService;
  16. import com.java110.user.dao.IUserServiceDao;
  17. import com.java110.user.smo.IUserServiceSMO;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.ObjectUtils;
  23. import java.util.*;
  24. /**
  25. * 用户服务信息管理业务信息实现
  26. * Created by wuxw on 2017/4/5.
  27. */
  28. @Service("userServiceSMOImpl")
  29. @Transactional
  30. public class UserServiceSMOImpl extends BaseServiceSMO implements IUserServiceSMO {
  31. @Autowired
  32. IPrimaryKeyService iPrimaryKeyService;
  33. @Autowired
  34. IUserServiceDao iUserServiceDao;
  35. //新增用户
  36. private final static String USER_ACTION_ADD = "ADD";
  37. //新增用户
  38. private final static String USER_ACTION_KIP = "KIP";
  39. //新增用户
  40. private final static String USER_ACTION_DEL = "DEL";
  41. /**
  42. * 保存用户信息
  43. *
  44. * @param userInfoJson 入参为用户信息json传
  45. * @return
  46. */
  47. public String saveUser(String userInfoJson) throws Exception{
  48. JSONObject reqUserJSON = null;
  49. try {
  50. reqUserJSON = this.simpleValidateJSON(userInfoJson);
  51. //boCust增加Action (动作)
  52. if (reqUserJSON.containsKey("boCust")) {
  53. JSONObject boCust = reqUserJSON.getJSONObject("boCust");
  54. boCust.put("state", USER_ACTION_ADD);
  55. }
  56. //boCustAttr增加Action(动作)
  57. if (reqUserJSON.containsKey("boCustAttr")) {
  58. JSONArray boCustAttrs = reqUserJSON.getJSONArray("boCustAttr");
  59. for (int attrIndex = 0; attrIndex < boCustAttrs.size(); attrIndex++) {
  60. JSONObject boCustAttr = boCustAttrs.getJSONObject(attrIndex);
  61. boCustAttr.put("state", USER_ACTION_ADD);
  62. }
  63. }
  64. } catch (RuntimeException e) {
  65. //返回异常信息
  66. return e.getMessage();
  67. }
  68. return soUserService(reqUserJSON);
  69. }
  70. /**
  71. * 所有服务处理类
  72. * {
  73. *
  74. * 'boCust':[{}],
  75. * 'boCustAttr':[{}]
  76. * }
  77. *
  78. * 返回报文:
  79. *
  80. * {'RESULT_CODE':'0000','RESULT_MSG':'成功','RESULT_INFO':{'custId':'7000123,718881991'}}
  81. * @param userInfoJson
  82. * @return
  83. */
  84. public String soUserService(JSONObject userInfoJson) throws Exception{
  85. LoggerEngine.debug("用户服务操作客户入参:" + userInfoJson);
  86. JSONObject paramJson = new JSONObject();
  87. JSONObject resultInfo = null;
  88. //存放生成的custId 主键为 custId-1 71000010100
  89. Map custIdKey = new HashMap();
  90. if (userInfoJson == null){
  91. throw new IllegalArgumentException("soUserService 入参 为空"+userInfoJson);
  92. }
  93. // 客户信息处理 处理boCust节点
  94. doProcessBoCust(userInfoJson, paramJson, custIdKey, resultInfo);
  95. //客户属性信息处理 处理boCustAttr节点
  96. doProcessBoCustAttr(userInfoJson, paramJson, custIdKey, resultInfo);
  97. return ProtocolUtil.createResultMsg(ProtocolUtil.RETURN_MSG_SUCCESS,"成功",resultInfo);
  98. }
  99. /**
  100. *
  101. * 请求报文为:
  102. *
  103. * {
  104. "data": [
  105. {
  106. "actionTypeCd": "C1",
  107. "boCust": [{},{}],
  108. "boCustAttr": [{ }, {}]
  109. },
  110. {
  111. "actionTypeCd": "C1",
  112. "boCust": [{},{}],
  113. "boCustAttr": [{ }, {}]
  114. }
  115. ]
  116. }
  117. 返回报文 :
  118. { 'RESULT_CODE': '0000', 'RESULT_MSG': '成功', 'RESULT_INFO': {'cust':[{'oldCustId':'-1','custId':'12345678'},{'oldCustId':'-2','custId':'12345678'}]} }
  119. * @param userInfoJson
  120. * @return
  121. * @throws Exception
  122. */
  123. @Override
  124. public String soUserServiceForOrderService(JSONObject userInfoJson) throws Exception {
  125. Assert.isNotNull(userInfoJson,"data","请求报文缺少 data 节点,请检查");
  126. JSONArray custInfos = userInfoJson.getJSONArray("data");
  127. Assert.isNull(custInfos,"请求报文中data节点,没有子节点,data子节点应该为JSONArray,custInfos="+custInfos);
  128. JSONObject custInfoJ = new JSONObject();
  129. JSONArray resultCustIdArray = new JSONArray();
  130. for(int custInfoIndex = 0 ;custInfoIndex < custInfos.size();custInfoIndex ++){
  131. JSONObject custInfoJson = custInfos.getJSONObject(custInfoIndex);
  132. String soUserServiceResult = this.soUserService(custInfoJson);
  133. JSONObject resultInfo = new JSONObject();
  134. if(!ProtocolUtil.validateReturnJson(soUserServiceResult,resultInfo)){
  135. throw new RuntimeException("客户信息受理失败,原因为:"+resultInfo.getString(ProtocolUtil.RESULT_MSG));
  136. }
  137. if(resultInfo.getJSONObject(ProtocolUtil.RESULT_INFO) != null
  138. && resultInfo.getJSONObject(ProtocolUtil.RESULT_INFO).containsKey("custId")) {
  139. String custIds = custInfoJ.getString("custId");
  140. // custIds += "," + resultInfo.getJSONObject(ProtocolUtil.RESULT_INFO).getString("custId");
  141. // custIds = custIds.startsWith(",") && custIds.length()>1 ? custIds.substring(1,custIds.length()):custIds;
  142. //custInfoJ.put("custId", custIds);
  143. JSONArray boCusts = custInfoJson.getJSONArray("boCust");
  144. Object custIdObj = JSONPath.eval(custInfoJson,"$.boCust[custId < '0'][0].custId");
  145. if(StringUtils.isNotBlank(custIds) && !ObjectUtils.isEmpty(custIdObj)) {
  146. String[] allNewCustIds = custIds.split(",");
  147. JSONObject newCustIdJson = null;
  148. for (String custId : allNewCustIds) {
  149. newCustIdJson = new JSONObject();
  150. newCustIdJson.put("oldCustId",custIdObj);
  151. newCustIdJson.put("custId",custId);
  152. resultCustIdArray.add(newCustIdJson);
  153. }
  154. }
  155. }
  156. }
  157. custInfoJ.put("cust",resultCustIdArray);
  158. return ProtocolUtil.createResultMsg(ProtocolUtil.RETURN_MSG_SUCCESS,"成功",custInfoJ);
  159. }
  160. /**
  161. * {
  162. * boCust:[{},{}]
  163. * }
  164. * 客户信心处理
  165. *
  166. *
  167. * @param boCusts
  168. * @return 成功 会带上处理客户的客户ID
  169. * {'RESULT_CODE':'0000','RESULT_MSG':'成功','RESULT_INFO':{'custId':'7000123,718881991'}}
  170. * @throws Exception
  171. */
  172. public String soBoCust(String boCusts) throws Exception{
  173. return soBoCust(boCusts,null);
  174. }
  175. /**
  176. * 将生成的custId 封装在map中返回
  177. * ...
  178. * custIdKey.put("custId-1","710020404040");
  179. *
  180. * ...
  181. *
  182. * key 为 custId 加原前的值
  183. *
  184. * custIdKey 如果为空不做处理
  185. * @param boCusts 客户信息
  186. * @param custIdKey custIdKeymap
  187. * @return
  188. * @throws Exception
  189. */
  190. public String soBoCust(String boCusts,Map custIdKey) throws Exception{
  191. // 将 jsonArray 转为list<BoCust> 对象
  192. JSONObject jsonObject = JSONObject.parseObject(boCusts);
  193. JSONObject resultInfo = new JSONObject();
  194. String custIds = "";
  195. List<BoCust> boCustList = JSONObject.parseArray(jsonObject.getJSONArray("boCust").toJSONString(), BoCust.class);
  196. Collections.sort(boCustList);
  197. //保存数据
  198. for(BoCust boCust : boCustList){
  199. // for(int boCustIndex = 0 ; boCustIndex < boCustList.size();boCustIndex++){
  200. // BoCust boCust = boCustList.get(boCustIndex);
  201. String custId = boCust.getBoId();
  202. //如果客户ID小于0 ,则自己生成客户ID,这个只有在有 主键生成服务时使用,否则为了防止出错,需要前段调用时需要生成custId
  203. if(StringUtils.isBlank(custId) || custId.startsWith("-") ){
  204. /*JSONObject data = new JSONObject();
  205. data.put("type","CUST_ID");
  206. //要求接口返回 {"RESULT_CODE":"0000","RESULT_INFO":{"user_id":"7020170411000041"},"RESULT_MSG":"成功"}
  207. String custIdJSONStr = iPrimaryKeyService.queryPrimaryKey(data.toJSONString());
  208. JSONObject custIdJSONTmp = JSONObject.parseObject(custIdJSONStr);
  209. if(custIdJSONTmp.containsKey("RESULT_CODE")
  210. && ProtocolUtil.RETURN_MSG_SUCCESS.equals(custIdJSONTmp.getString("RESULT_CODE"))
  211. && custIdJSONTmp.containsKey("RESULT_INFO")){
  212. //从接口生成custId
  213. custId = NumberUtils.toInt(custIdJSONTmp.getJSONObject("RESULT_INFO").getString("CUST_ID"),-1);
  214. }*/
  215. custId = this.queryPrimaryKey(iPrimaryKeyService,"CUST_ID");
  216. //将 新生成的custId保存至 map中 custId-1 custId-2 主键方式存入
  217. if(custIdKey != null){
  218. custIdKey.put("custId"+boCust.getCustId(),custId);
  219. }
  220. }
  221. boCust.setCustId(custId);
  222. //保存数据至 bo_cust 表中
  223. int saveBoCustFlag = iUserServiceDao.saveDataToBoCust(boCust);
  224. if(saveBoCustFlag < 1){ // 如果没有保存成功,抛出异常,有事物 回退事物
  225. throw new RuntimeException("保存过程[bo_cust]数据失败,印象记录数为"+saveBoCustFlag+",boCust : "+boCust);
  226. }
  227. //建档 处理 实例数据
  228. int saveCustFlag = 0;
  229. if("ADD".equals(boCust.getState())){
  230. /* List<BoCust> boCustsTmp = boCustList;
  231. //在增加之间首先要判断是否有相应删的动作
  232. // for(BoCust boCustTmp : boCustsTmp){
  233. for(int boCustTmpIndex = boCustIndex+1;boCustTmpIndex < boCustsTmp.size();boCustTmpIndex++){
  234. BoCust boCustTmp = boCustsTmp.get(boCustTmpIndex);
  235. if(StringUtils.isNotBlank(boCust.getCustId())
  236. && boCust.getCustId().equals(boCustTmp.getCustId())
  237. &&"DEL".equals(boCustTmp.getState())){
  238. //先调用删除客户信息
  239. saveCustFlag = iUserServiceDao.deleteDataToCust(boCust.convert());
  240. if(saveCustFlag < 1){
  241. throw new RuntimeException("删除实例[cust]数据失败,影响记录数为"+saveCustFlag+", cust : "+boCust.convert());
  242. }
  243. //则把已经删除过的从list中删除,以防重复删除
  244. boCustList.remove(boCustTmp);
  245. }
  246. }*/
  247. saveCustFlag = iUserServiceDao.saveDataToCust(boCust.convert());
  248. }else if("DEL".equals(boCust.getState())){
  249. saveCustFlag = iUserServiceDao.deleteDataToCust(boCust.convert());
  250. }else if("KIP".equals(boCust.getState())){
  251. //按理这里到不了,KIP表示实例数据不变,所以这里默认写成1 认为是成功
  252. saveCustFlag = 1;
  253. }else{
  254. //这里单独抛出异常,不走下面统一异常抛出,是为了说明更具体点
  255. throw new RuntimeException("入参错误boCust 的 state 目前只支持 [ADD,DEL,KIP] , boCust : " +boCust);
  256. }
  257. if(saveCustFlag < 1){
  258. throw new RuntimeException("保存实例[cust]数据失败,影响记录数为"+saveCustFlag+", cust : "+boCust.convert());
  259. }
  260. custIds +=","+custId;
  261. }
  262. //去除第一个逗号
  263. if (custIds.length()>0){
  264. custIds = custIds.substring(1);
  265. }
  266. resultInfo.put("custId",custIds);
  267. return ProtocolUtil.createResultMsg(ProtocolUtil.RETURN_MSG_SUCCESS,"成功",resultInfo);
  268. }
  269. /**
  270. * 注意在调用这个接口时,相应的客户信息必须存在
  271. *
  272. *
  273. * 客户信息属性处理
  274. * 协议:
  275. *{
  276. * boCustAttr:[{},{}]
  277. * }
  278. * @param boCustAttrs
  279. * @return
  280. * @throws Exception
  281. */
  282. @Override
  283. public String soBoCustAttr(String boCustAttrs) throws Exception {
  284. //这里可以加入基本客户信息是否存在的校验,暂时没有必要实现
  285. // 将 jsonArray 转为list<BoCust> 对象
  286. JSONObject jsonObject = JSONObject.parseObject(boCustAttrs);
  287. List<BoCustAttr> boCustAttrList = JSONObject.parseArray(jsonObject.getJSONArray("boCustAttr").toJSONString(), BoCustAttr.class);
  288. //先拍个序 先处理DEL 再处理ADD
  289. Collections.sort(boCustAttrList);
  290. //保存数据
  291. for(BoCustAttr boCustAttr : boCustAttrList) {
  292. //保存数据至 bo_cust_attr 表中
  293. int saveBoCustAttrFlag = iUserServiceDao.saveDataToBoCustAttr(boCustAttr);
  294. if(saveBoCustAttrFlag < 1){ // 如果没有保存成功,抛出异常,有事物 回退事物
  295. throw new RuntimeException("保存过程[bo_cust_attr]数据失败,印象记录数为"+saveBoCustAttrFlag+",boCustAttr : "+boCustAttr);
  296. }
  297. //建档 处理 实例数据
  298. int saveCustAttrFlag = 0;
  299. if("ADD".equals(boCustAttr.getState())){
  300. saveCustAttrFlag = iUserServiceDao.saveDataToCustAttr(boCustAttr.convert());
  301. }else if("DEL".equals(boCustAttr.getState())){
  302. saveCustAttrFlag = iUserServiceDao.deleteDataToCustAttr(boCustAttr.convert());
  303. }else if("KIP".equals(boCustAttr.getState())){
  304. //按理这里到不了,KIP表示实例数据不变,所以这里默认写成1 认为是成功
  305. saveCustAttrFlag = 1;
  306. }else{
  307. //这里单独抛出异常,不走下面统一异常抛出,是为了说明更具体点
  308. throw new RuntimeException("入参错误boCustAttr 的 state 目前只支持 [ADD,DEL,KIP] , boCust : " +boCustAttr);
  309. }
  310. if(saveCustAttrFlag < 1){
  311. throw new RuntimeException("保存实例[cust_attr]数据失败,影响记录数为"+saveCustAttrFlag+", cust : "+boCustAttr.convert());
  312. }
  313. }
  314. return ProtocolUtil.createResultMsg(ProtocolUtil.RETURN_MSG_SUCCESS,"成功",null);
  315. }
  316. /**
  317. * 作废客户信息
  318. * [{},{},{}]
  319. *
  320. * @param datas
  321. * @return
  322. * @throws Exception
  323. */
  324. @Override
  325. public String soDeleteCustInfo(JSONArray datas) throws Exception {
  326. Assert.isNull(datas,"传入的data节点下没有任何内容");
  327. for(int boIdIndex = 0 ; boIdIndex < datas.size(); boIdIndex++){
  328. JSONObject data = datas.getJSONObject(boIdIndex);
  329. Assert.isNotNull(data,"boId","当前节点中没有包含boId节点格式错误"+data);
  330. // 复原Cust
  331. doDeleteBoCust(data);
  332. // 复原CustAttr
  333. doDeleteBoCustAttr(data);
  334. }
  335. return null;
  336. }
  337. /**
  338. * 查询客户信息
  339. * 包括 基本信息cust 和 属性信息 custAttr
  340. * @param cust
  341. * @return
  342. * @throws Exception
  343. */
  344. public String queryCust(Cust cust) throws Exception{
  345. LoggerEngine.debug("客户信息查询入参:" + cust);
  346. if(cust == null || StringUtils.isBlank(cust.getCustId()) ){
  347. throw new IllegalArgumentException("客户信息查询入参为空,custId 为空 "+cust);
  348. }
  349. Cust newCust = iUserServiceDao.queryDataToCust(cust);
  350. if(newCust == null){
  351. return ProtocolUtil.createResultMsg(ProtocolUtil.RETURN_MSG_ERROR,"未找到用户信息",null);
  352. }
  353. return ProtocolUtil.createResultMsg(ProtocolUtil.RETURN_MSG_SUCCESS,"成功",JSONObject.parseObject(JSONObject.toJSONString(newCust)));
  354. }
  355. /**
  356. * 根据olID查询客户信息
  357. * @param busiOrderStr
  358. * @return
  359. * @throws Exception
  360. */
  361. public String queryCustInfoByOlId(String busiOrderStr) throws Exception{
  362. return doQueryCustInfoByOlId(busiOrderStr,false);
  363. }
  364. /**
  365. * 根据olID查询客户信息 需要作废的发起的报文
  366. * @param busiOrderStr
  367. * @return
  368. * @throws Exception
  369. */
  370. public String queryNeedDeleteCustInfoByOlId(String busiOrderStr) throws Exception{
  371. return doQueryCustInfoByOlId(busiOrderStr,true);
  372. }
  373. /**
  374. * 查询客户订单信息
  375. * @param busiOrderStr 订单项信息
  376. * @param isNeedDelete 是否是撤单报文 true 查询撤单报文 false
  377. * @return
  378. * @throws Exception
  379. */
  380. private String doQueryCustInfoByOlId(String busiOrderStr,Boolean isNeedDelete) throws Exception{
  381. BusiOrder busiOrder = JSONObject.parseObject(busiOrderStr, BusiOrder.class);
  382. if(busiOrder == null || "".equals(busiOrder.getOlId())){
  383. throw new IllegalArgumentException("客户信息查询入参为空,olId 为空 "+busiOrderStr);
  384. }
  385. Cust cust = new Cust();
  386. cust.setVersionId(busiOrder.getOlId());
  387. //根据版本ID查询实例数据
  388. Cust newCust = iUserServiceDao.queryDataToCust(cust);
  389. JSONObject returnJson = JSONObject.parseObject("{'data':{}}");
  390. if(newCust == null){
  391. return returnJson.toJSONString();
  392. }
  393. BoCust boCust = new BoCust();
  394. boCust.setBoId(busiOrder.getBoId());
  395. boCust.setCustId(newCust.getCustId());
  396. boCust.setVersionId(busiOrder.getOlId());
  397. List<BoCust> boCusts = iUserServiceDao.queryBoCust(boCust);
  398. //一般情况下没有这种情况存在,除非 人工 改了数据,或没按流程完成数据处理
  399. if(boCusts == null || boCusts.size() == 0){
  400. return returnJson.toJSONString();
  401. }
  402. JSONArray boCustArray = new JSONArray();
  403. //单纯的删除 和单纯 增加
  404. for(int boCustIndex = 0 ; boCustIndex < boCusts.size();boCustIndex++) {
  405. BoCust newBoCust = boCusts.get(boCustIndex);
  406. if(isNeedDelete) {
  407. if (StateConstant.STATE_DEL.equals(newBoCust.getState())) {
  408. newBoCust.setBoId("");
  409. newBoCust.setState(StateConstant.STATE_ADD);
  410. } else if (StateConstant.STATE_ADD.equals(newBoCust.getState())) {
  411. newBoCust.setState(StateConstant.STATE_DEL);
  412. } else {
  413. newBoCust.setState(StateConstant.STATE_KIP);
  414. }
  415. }
  416. boCustArray.add(newBoCust);
  417. }
  418. returnJson.getJSONObject("data").put("boCust",JSONObject.toJSONString(boCustArray));
  419. //属性处理
  420. CustAttr oldCustAttr = new CustAttr();
  421. oldCustAttr.setCustId(newCust.getCustId());
  422. oldCustAttr.setVersionId(busiOrder.getOlId());
  423. List<CustAttr> custAttrs = iUserServiceDao.queryDataToCustAttr(oldCustAttr);
  424. if(custAttrs == null || custAttrs.size() == 0){
  425. return returnJson.toJSONString();
  426. }
  427. /**
  428. * 查询客户查询的过程数据
  429. */
  430. BoCustAttr boCustAttr = new BoCustAttr();
  431. boCustAttr.setCustId(newCust.getCustId());
  432. boCustAttr.setVersionId(busiOrder.getOlId());
  433. List<BoCustAttr> boCustAttrs = iUserServiceDao.queryBoCustAttr(boCustAttr);
  434. //一般情况下没有这种情况存在,除非 人工 改了数据,或没按流程完成数据处理
  435. if(boCustAttrs == null || boCustAttrs.size() == 0){
  436. return returnJson.toJSONString();
  437. }
  438. JSONArray boCustAttrArray = new JSONArray();
  439. //单纯的删除 和单纯 增加
  440. for(BoCustAttr newBoCustAttr : boCustAttrs) {
  441. if(isNeedDelete) {
  442. if (StateConstant.STATE_DEL.equals(newBoCustAttr.getState())) {
  443. newBoCustAttr.setBoId("");
  444. newBoCustAttr.setState(StateConstant.STATE_ADD);
  445. } else if (StateConstant.STATE_ADD.equals(newBoCustAttr.getState())) {
  446. newBoCustAttr.setState(StateConstant.STATE_DEL);
  447. } else {
  448. newBoCustAttr.setState(StateConstant.STATE_KIP);
  449. }
  450. }
  451. boCustAttrArray.add(newBoCustAttr);
  452. }
  453. returnJson.getJSONObject("data").put("boCustAttr",JSONObject.toJSONString(boCustAttrArray));
  454. return returnJson.toJSONString();
  455. }
  456. /**
  457. * 处理boCust 节点
  458. * @throws Exception
  459. */
  460. public void doProcessBoCust(JSONObject userInfoJson,JSONObject paramJson,Map custIdKey, JSONObject resultInfo) throws Exception{
  461. if(userInfoJson.containsKey("boCust")){
  462. JSONArray boCusts = userInfoJson.getJSONArray("boCust");
  463. JSONObject boCustObj = new JSONObject();
  464. boCustObj.put("boCust",boCusts);
  465. String returnSaveBoCust = this.soBoCust(boCustObj.toJSONString(),custIdKey);
  466. if(!ProtocolUtil.validateReturnJson(returnSaveBoCust,paramJson)){
  467. throw new RuntimeException("保存 bo_cust 失败:"+boCustObj+(paramJson != null
  468. && paramJson.containsKey("RESULT_MSG")?paramJson.getString("RESULT_MSG"):"未知异常"));
  469. }
  470. resultInfo = paramJson.getJSONObject("RESULT_INFO");
  471. }
  472. }
  473. /**
  474. * 处理boCustAttr 节点
  475. * @param userInfoJson
  476. * @param paramJson
  477. * @param custIdKey
  478. * @param resultInfo
  479. */
  480. public void doProcessBoCustAttr(JSONObject userInfoJson,JSONObject paramJson,Map custIdKey, JSONObject resultInfo) throws Exception{
  481. if(userInfoJson.containsKey("boCustAttr")){
  482. JSONArray boCustAttrs = userInfoJson.getJSONArray("boCustAttr");
  483. //首先对custId 进行处理
  484. if(custIdKey != null && custIdKey.size() > 0 ){
  485. for(int boCustAttrIndex = 0 ; boCustAttrIndex < boCustAttrs.size();boCustAttrIndex++){
  486. JSONObject boCustAttr = boCustAttrs.getJSONObject(boCustAttrIndex);
  487. boCustAttr.put("custId",custIdKey.get("custId"+boCustAttr.getString("custId")));
  488. }
  489. }
  490. JSONObject boCustAttrObj = new JSONObject();
  491. boCustAttrObj.put("boCustAttr",boCustAttrs);
  492. String returnSaveBoCustAttr = soBoCustAttr(boCustAttrObj.toJSONString());
  493. if(!ProtocolUtil.validateReturnJson(returnSaveBoCustAttr,paramJson)){
  494. throw new RuntimeException("保存 bo_cust 失败:"+boCustAttrObj+(paramJson != null
  495. && paramJson.containsKey("RESULT_MSG")?paramJson.getString("RESULT_MSG"):"未知异常"));
  496. }
  497. }
  498. }
  499. /**
  500. * 作废 boCust 信息
  501. * @param data
  502. * @throws Exception
  503. */
  504. public void doDeleteBoCust(JSONObject data) throws Exception{
  505. Cust deleteCust = null;
  506. //根据boId 查询bo_cust 表,是否有数据,没数据直接返回
  507. BoCust boCust = new BoCust();
  508. boCust.setBoId(data.getString("boId"));
  509. List<BoCust> boCusts = iUserServiceDao.queryBoCust(boCust);
  510. //Assert.isOne(boCusts,"在表bo_cust中未找到boId 为["+data.getString("boId")+"]的数据 或有多条数据,请检查");
  511. if(boCusts == null || boCusts.size() < 1){
  512. LoggerEngine.error("当前没有查到数为 "+data+"请检查数据");
  513. return;
  514. }
  515. //在过程表中补一条作废的数据,然后根据boId的动作对实例数据进行处理
  516. boCust.setCustId(boCusts.get(0).getCustId());
  517. boCust.setBoId("");
  518. //查询出所有custId 一样的数据
  519. List<BoCust> boCustAll = iUserServiceDao.queryBoCust(boCust);
  520. Assert.isNull(boCustAll,"当前没有查到custId 为 "+boCusts.get(0).getCustId()+"请检查数据");
  521. boCust = boCusts.get(0);
  522. BoCust newBoCust = new BoCust();
  523. newBoCust.setBoId(data.getString("newBoId"));
  524. newBoCust.setCustId(boCust.getCustId());
  525. newBoCust.setState("DEL");
  526. int saveBoCustFlag = iUserServiceDao.saveDataToBoCust(newBoCust);
  527. if(saveBoCustFlag < 1){
  528. throw new RuntimeException("向bo_cust表中保存数据失败,boCust="+JSONObject.toJSONString(newBoCust));
  529. }
  530. //首先删除实例数据
  531. deleteCust = new Cust();
  532. deleteCust.setCustId(boCust.getCustId());
  533. if(iUserServiceDao.deleteDataToCust(deleteCust) < 1){
  534. throw new RuntimeException("删除cust实例数据失败"+JSONObject.toJSONString(deleteCust));
  535. }
  536. //如果有多条数据,则恢复 前一条数据信息,这边存在bug 如果上一条的数据没有分装以前数据的情况下会有问题,
  537. // 所以我们的原则是再更新或删除数据时一定要在过程表中保存完整是实例数据信息
  538. if(boCustAll.size() > 1){
  539. Cust oldCust = boCustAll.get(1).convert();
  540. if(iUserServiceDao.saveDataToCust(oldCust)<1 ){
  541. throw new RuntimeException("cust 表恢复老数据信息失败,cust 为:"+JSONObject.toJSONString(oldCust));
  542. }
  543. }
  544. }
  545. /**
  546. * 删除 bo_cust_attr
  547. * @param data
  548. * @throws Exception
  549. */
  550. public void doDeleteBoCustAttr(JSONObject data) throws Exception{
  551. BoCustAttr boCustAttrTmp = new BoCustAttr();
  552. boCustAttrTmp.setBoId(data.getString("boId"));
  553. List<BoCustAttr> boCustAttrs = iUserServiceDao.queryBoCustAttr(boCustAttrTmp);
  554. if(boCustAttrs == null || boCustAttrs.size() < 1){
  555. LoggerEngine.error("当前没有查到数为 "+data+"请检查数据");
  556. return;
  557. }
  558. boCustAttrTmp.setBoId("");
  559. boCustAttrTmp.setCustId(boCustAttrs.get(0).getCustId());
  560. List<BoCustAttr> boCustAttrsTmps = iUserServiceDao.queryBoCustAttr(boCustAttrTmp);
  561. Assert.isNull(boCustAttrsTmps,"当前没有查到custId 为 "+boCustAttrs.get(0).getCustId()+"请检查数据");
  562. //获取上一次所有的属性
  563. List<BoCustAttr> preBoCustAttrTmps = getPreBoCustAttrs(boCustAttrsTmps);
  564. //保存过程表
  565. for(BoCustAttr boCustAttr : boCustAttrs){
  566. boCustAttr.setBoId("newBoId");
  567. boCustAttr.setState("DEL");
  568. if(iUserServiceDao.saveDataToBoCustAttr(boCustAttr) < 1){
  569. throw new RuntimeException("保存数据失败,保存数据为boCustAttr = "+ JSONObject.toJSONString(boCustAttr));
  570. }
  571. }
  572. //删除实例数据 这里思路是,删除实例数据中数据,将上一次ADD数据重新写一遍
  573. CustAttr custAttrTmp = new CustAttr();
  574. custAttrTmp.setCustId(boCustAttrs.get(0).getCustId());
  575. if(iUserServiceDao.deleteDataToCustAttr(custAttrTmp) < 1){
  576. throw new RuntimeException("删除CustAttr 实例数据失败,数据为:"+JSONObject.toJSONString(custAttrTmp));
  577. }
  578. for(BoCustAttr boCustAttr : preBoCustAttrTmps){
  579. if("ADD".equals(boCustAttr.getState())){
  580. if(iUserServiceDao.deleteDataToCustAttr(boCustAttr.convert()) < 1){
  581. throw new RuntimeException("复原原始数据失败,数据为:" + JSONObject.toJSONString(boCustAttr));
  582. }
  583. }
  584. }
  585. }
  586. /**
  587. * 获取上上一次的操作
  588. * @param boCustAttrs
  589. * @return
  590. */
  591. private List<BoCustAttr> getPreBoCustAttrs(List<BoCustAttr> boCustAttrs){
  592. String firstBoId = boCustAttrs.get(0).getBoId();
  593. String preBoId = "";
  594. List<BoCustAttr> preBoCustAttrs = new ArrayList<BoCustAttr>();
  595. for(BoCustAttr boCustAttr : boCustAttrs){
  596. if(!firstBoId.equals(boCustAttr.getBoId())){
  597. if(!preBoId.equals(boCustAttr.getBoId()) && !"".equals(preBoId)){
  598. break;
  599. }
  600. preBoId = boCustAttr.getBoId();
  601. preBoCustAttrs.add(boCustAttr);
  602. }
  603. }
  604. return preBoCustAttrs;
  605. }
  606. public IPrimaryKeyService getiPrimaryKeyService() {
  607. return iPrimaryKeyService;
  608. }
  609. public void setiPrimaryKeyService(IPrimaryKeyService iPrimaryKeyService) {
  610. this.iPrimaryKeyService = iPrimaryKeyService;
  611. }
  612. public IUserServiceDao getiUserServiceDao() {
  613. return iUserServiceDao;
  614. }
  615. public void setiUserServiceDao(IUserServiceDao iUserServiceDao) {
  616. this.iUserServiceDao = iUserServiceDao;
  617. }
  618. }