BusinessContractReceiveServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package com.chinaitop.depot.business.service.impl;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import javax.annotation.Resource;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import com.alibaba.fastjson.JSON;
  15. import com.alibaba.fastjson.JSONArray;
  16. import com.alibaba.fastjson.JSONObject;
  17. import com.chinaitop.depot.business.controller.BusinessContractController;
  18. import com.chinaitop.depot.business.mapper.BusinessContractReceiveMapper;
  19. import com.chinaitop.depot.business.model.BusinessContractReceive;
  20. import com.chinaitop.depot.business.model.BusinessContractReceiveExample;
  21. import com.chinaitop.depot.business.model.BusinessContractReceiveExample.Criteria;
  22. import com.chinaitop.depot.business.service.BusinessContractReceiveService;
  23. import com.chinaitop.depot.business.service.FeignBasicService;
  24. import com.chinaitop.depot.feignService.DepotKeeperAccountFeignService;
  25. import com.chinaitop.depot.utils.ParameterUtil;
  26. @Service
  27. @SuppressWarnings("all")
  28. public class BusinessContractReceiveServiceImpl implements BusinessContractReceiveService {
  29. @Resource
  30. private BusinessContractReceiveMapper businessContractReceiveMapper;
  31. @Resource
  32. private FeignBasicService feignBasicService;
  33. @Resource
  34. private DepotKeeperAccountFeignService feigenKeeperAccountService;
  35. public static final Logger logger = LoggerFactory.getLogger(BusinessContractReceiveServiceImpl.class);
  36. @Override
  37. @Transactional(rollbackFor = Exception.class)
  38. public Map<String, String> receiveContractData(String data) throws Exception {
  39. Map<String, String> resultMap = new HashMap<>();
  40. if (StringUtils.isBlank(data)) {
  41. resultMap.put("status", "500");
  42. resultMap.put("msg", "下发数据不能为空");
  43. return resultMap;
  44. }
  45. JSONObject obj = JSONObject.parseObject(data);
  46. Map<String, Object> check_map = checkDatas(obj);
  47. String msg = check_map.get("msg")+"";
  48. if (msg.length() > 0) {
  49. resultMap.put("status", "500");
  50. resultMap.put("msg", msg);
  51. return resultMap;
  52. } else {
  53. BusinessContractReceive obj_map = (BusinessContractReceive) check_map.get("data");
  54. //先处理客户信息数据
  55. Integer customerId = editCustomer(obj_map.getOrgId(), obj_map.getKhmc());
  56. //把新增或查到的客户ID给当前合同的客户ID赋值
  57. obj_map.setKhid(customerId);
  58. obj_map.setDataType("0");
  59. //保存合同数据
  60. updateOrSave(obj_map);
  61. resultMap.put("status", "200");
  62. resultMap.put("msg", "推送成功");
  63. }
  64. return resultMap;
  65. }
  66. private Map<String, Object> checkDatas(JSONObject obj) {
  67. BusinessContractReceive dataobj = new BusinessContractReceive();
  68. List<BusinessContractReceive> list = new ArrayList<>();
  69. StringBuffer sbf = new StringBuffer(100);
  70. String htbh = obj.getString("htbh");
  71. if (StringUtils.isNotBlank(htbh)) {
  72. dataobj.setHtbh(htbh.trim());
  73. } else {
  74. sbf.append("合同编号不能为空,");
  75. }
  76. String htlx = obj.getString("htlx");
  77. if (StringUtils.isNotBlank(htlx)) {
  78. dataobj.setHtlx(htlx.trim());
  79. } else {
  80. sbf.append("合同类型不能为空,");
  81. }
  82. String htsl = obj.getString("htsl");
  83. if (StringUtils.isNotBlank(htsl)) {
  84. dataobj.setHtzsl(new BigDecimal(htsl));
  85. } else {
  86. sbf.append("合同数量不能为空,");
  87. }
  88. String htdj = obj.getString("dj");
  89. if (StringUtils.isNotBlank(htdj)) {
  90. dataobj.setHtdj(new BigDecimal(htdj));
  91. } else {
  92. sbf.append("合同单价不能为空,");
  93. }
  94. String htzj = obj.getString("zj");
  95. if (StringUtils.isNotBlank(htzj)) {
  96. dataobj.setHtzj(new BigDecimal(htzj));
  97. } else {
  98. sbf.append("合同总价不能为空,");
  99. }
  100. String pz = obj.getString("pz");
  101. if (StringUtils.isNotBlank(pz)) {
  102. String lypz = "";
  103. if (pz.equals("2130000") || pz.equals("1410000")) {
  104. lypz = "7144";
  105. logger.info("根据"+pz+"获取到的品种数据如下:");
  106. logger.info("原油对应的字典ID是:7144");
  107. } else {
  108. Map<String, Object> basicMap = feignBasicService.getIdByNameAndParentId(1061, null, pz);
  109. lypz = basicMap.get("enumId")+"";
  110. logger.info("根据"+pz+"获取到的品种数据如下:");
  111. logger.info(basicMap.toString());
  112. }
  113. if (StringUtils.isNotBlank(lypz)) {
  114. dataobj.setLypz(Integer.parseInt(lypz));
  115. } else {
  116. sbf.append("粮食品种的值"+pz+"有误或不合理,");
  117. }
  118. } else {
  119. sbf.append("粮食品种不能为空,");
  120. }
  121. String mxpz = obj.getString("mxpz");
  122. if (StringUtils.isNotBlank(mxpz)) {
  123. if (null != dataobj.getLypz()) {
  124. Integer enumid = null;
  125. if (mxpz.equals("2131000")) {
  126. enumid = 7145;
  127. } else {
  128. enumid = getEnumId(dataobj.getLypz(), mxpz);
  129. }
  130. logger.info("根据"+mxpz+"获取到的明细品种数据是:"+enumid);
  131. if (null != enumid) {
  132. dataobj.setMxpz(enumid);
  133. } else {
  134. sbf.append("粮食明细品种的值"+mxpz+"有误或不合理,");
  135. }
  136. }
  137. } else {
  138. sbf.append("粮食明细品种不能为空,");
  139. }
  140. String lydj = obj.getString("lsdj");
  141. if (StringUtils.isNotBlank(lydj)) {
  142. lydj = lydj.replace(" ", "");
  143. Map<String, Object> basicMap = feignBasicService.getIdByNameAndParentId(1002, null, lydj);
  144. logger.info("获取到的粮食等级数据如下:");
  145. logger.info(basicMap.toString());
  146. String dj = basicMap.get("enumId")+"";
  147. if (StringUtils.isNotBlank(dj)) {
  148. dataobj.setLydj(Integer.parseInt(dj));
  149. } else {
  150. sbf.append("粮食等级的值"+lydj+"有误或不合理,");
  151. }
  152. } else {
  153. if ("1410000".equals(pz)) {
  154. dataobj.setLydj(7166);
  155. }
  156. }
  157. String lyxz = obj.getString("lsxz");
  158. if (StringUtils.isNotBlank(lyxz)) {
  159. Integer enumid = getEnumId(1032, lyxz);
  160. logger.info("根据"+lyxz+"获取到的粮食性质数据如下:"+enumid);
  161. if (null != enumid) {
  162. dataobj.setLyxz(enumid);
  163. } else {
  164. sbf.append("粮食性质的值"+lyxz+"有误,");
  165. }
  166. } else {
  167. sbf.append("粮食等级不能为空,");
  168. }
  169. String scnf = obj.getString("scnf");
  170. if (StringUtils.isNotBlank(scnf)) {
  171. Map<String, Object> basicMap = feignBasicService.getIdByNameAndParentId(1004, scnf, null);
  172. logger.info("获取到的生产年份数据如下:");
  173. logger.info(basicMap.toString());
  174. String scnd = basicMap.get("enumId")+"";
  175. if (StringUtils.isNotBlank(scnd)) {
  176. dataobj.setScnf(Integer.parseInt(scnd));
  177. dataobj.setShnd(dataobj.getScnf());
  178. } else {
  179. sbf.append("生产年份的值"+scnf+"有误,");
  180. }
  181. } else {
  182. sbf.append("生产年份不能为空,");
  183. }
  184. String khmc = obj.getString("khmc");
  185. if (StringUtils.isNotBlank(khmc)) {
  186. dataobj.setKhmc(khmc.replace(" ", "").trim());
  187. } else {
  188. sbf.append("客户名称不能为空,");
  189. }
  190. String hw_sptid = obj.getString("hwbm");//市平台货位数据主键ID
  191. if (StringUtils.isNotBlank(hw_sptid)) {
  192. Map<String, Object> basicMap = feignBasicService.getWareOrTankObj(hw_sptid);
  193. logger.info("根据值"+hw_sptid+"获取到的货位数据如下:");
  194. logger.info(basicMap+"");
  195. if (!"0".equals(basicMap.get("houseId").toString()) && !"0".equals(basicMap.get("warehouseId").toString()) && !"0".equals(basicMap.get("orgId").toString())) {
  196. dataobj.setHouseId(Integer.parseInt(basicMap.get("houseId").toString()));
  197. if (hw_sptid.length() > 4) {//说明是粮食仓房的。如果是小于等于4,那就是油罐的数据,货位的值为空
  198. dataobj.setWarehouseId(Integer.parseInt(basicMap.get("warehouseId").toString()));
  199. }
  200. dataobj.setOrgId(Integer.parseInt(basicMap.get("orgId").toString()));
  201. dataobj.setInputTime(new Date());
  202. dataobj.setUpdateTime(dataobj.getInputTime());
  203. } else {
  204. sbf.append("未查询到hwbm的值对应的货位数据,");
  205. }
  206. } else {
  207. sbf.append("hwbm的值不能为空,");
  208. }
  209. String id = obj.getString("htbm");//市平台合同主键ID
  210. if (StringUtils.isNotBlank(hw_sptid)) {
  211. dataobj.setSptHtid(id);
  212. }
  213. Map<String, Object> resultMap = new HashMap<>();
  214. resultMap.put("msg", sbf.toString());
  215. resultMap.put("data", dataobj);
  216. return resultMap;
  217. }
  218. /**
  219. * 根据字典父级ID和国标编码获取下面的一个字典ID
  220. * @param parentId
  221. * @param gbCode
  222. * @return
  223. */
  224. private Integer getEnumId(int parentId, String gbCode) {
  225. Integer enumId = null;
  226. Map<String, Object> map = feignBasicService.findConditionEnum(parentId, null, null);
  227. if (null != map) {
  228. String json_str = JSON.toJSONString(map);
  229. JSONObject json_obj = JSONObject.parseObject(json_str);
  230. JSONArray array = JSONArray.parseArray(json_obj.getString("enumList"));
  231. for (int i = 0; i < array.size(); i++) {
  232. JSONObject object = JSONObject.parseObject(array.get(i).toString());
  233. String code = object.getString("gbcode");
  234. if (code.equals(gbCode)) {
  235. enumId = Integer.parseInt(object.getString("id"));
  236. break;
  237. }
  238. }
  239. }
  240. return enumId;
  241. }
  242. @Override
  243. public List<BusinessContractReceive> queryByExample(String htbh, Integer orgId, Integer houseId, Integer warehouseId, String htlx)
  244. throws Exception {
  245. List<BusinessContractReceive> list = null;
  246. BusinessContractReceiveExample example = new BusinessContractReceiveExample();
  247. Criteria criteria = example.createCriteria();
  248. if (StringUtils.isNotBlank(htbh)) {
  249. criteria.andHtbhLike("%"+htbh+"%");
  250. }
  251. if (null != orgId) {
  252. criteria.andOrgIdEqualTo(orgId);
  253. }
  254. if (null != houseId) {
  255. criteria.andHouseIdEqualTo(houseId);
  256. }
  257. if (null != warehouseId) {
  258. criteria.andWarehouseIdEqualTo(warehouseId);
  259. }
  260. if (StringUtils.isNotBlank(htlx)) {
  261. criteria.andHtlxEqualTo(htlx);
  262. }
  263. example.setOrderByClause("input_time desc");
  264. list = businessContractReceiveMapper.selectByExample(example);
  265. return list;
  266. }
  267. @Override
  268. public BusinessContractReceive findbyParmaryKey(Integer id) throws Exception {
  269. BusinessContractReceive businessContractReceive = businessContractReceiveMapper.selectByPrimaryKey(id);
  270. return businessContractReceive;
  271. }
  272. private Integer editCustomer(Integer orgId, String customerName) throws Exception {
  273. //查询这个客户在这个库是否存在
  274. Integer customerId = feigenKeeperAccountService.queryCustomerObj(orgId, 3153, customerName);
  275. //如果不存在就新增一条不完整的客户
  276. if (null == customerId) {//说明这个客户没再库级维护
  277. //形成客户编码
  278. StringBuffer sbf = new StringBuffer();
  279. //把单位ID格式化成3位,不足三位的补0
  280. String sxm = String.format("%03d", orgId);
  281. sbf.append("KH-").append(sxm).append("-").append(ParameterUtil.getDateYMDHMS(new Date()));
  282. JSONObject json = new JSONObject();
  283. json.put("name", customerName);
  284. json.put("classify", 3153);
  285. json.put("sfwz", "1");//客户数据是否完整(0:完整,1:不完整)
  286. json.put("customerNumber", sbf.toString());
  287. Map<String, Object> modelMap = feigenKeeperAccountService.save(json.toString(), orgId);
  288. if ("error".equals(modelMap.get("status"))) {
  289. throw new Exception("不完整客户保存异常");
  290. } else {
  291. customerId = Integer.parseInt(modelMap.get("id").toString());
  292. }
  293. }
  294. return customerId;
  295. }
  296. @Override
  297. public void updateOrSave(BusinessContractReceive businessContractReceive) throws Exception {
  298. //构建条件
  299. BusinessContractReceiveExample example = new BusinessContractReceiveExample();
  300. BusinessContractReceiveExample.Criteria criteria = example.createCriteria();
  301. criteria.andSptHtidEqualTo(businessContractReceive.getSptHtid());
  302. //查询符合条件的数据
  303. List<BusinessContractReceive> list = businessContractReceiveMapper.selectByExample(example);
  304. //没有符合的就新增,有的话就修改
  305. if (null == list || list.size() == 0) {
  306. businessContractReceive.setYjssl(new BigDecimal(0));
  307. businessContractReceiveMapper.insert(businessContractReceive);
  308. } else {
  309. businessContractReceive.setId(list.get(0).getId());
  310. businessContractReceive.setUpdateTime(new Date());
  311. businessContractReceiveMapper.updateByExample(businessContractReceive, example);
  312. }
  313. }
  314. @Override
  315. public Map<String, String> updateYjssl(Integer htid, String bcjssl) throws Exception {
  316. Map<String, String> map = new HashMap<String, String>();
  317. BusinessContractReceive businessContractReceive = businessContractReceiveMapper.selectByPrimaryKey(htid);
  318. if (null == businessContractReceive) {
  319. map.put("status", "500");
  320. map.put("msg", "未检测到原合同信息,请联系管理员");
  321. return map;
  322. } else {
  323. BigDecimal new_bcjssl = new BigDecimal(bcjssl);//本次结算数量
  324. BigDecimal yjssl = businessContractReceive.getYjssl();
  325. yjssl = yjssl.add(new_bcjssl);
  326. businessContractReceive.setYjssl(yjssl);
  327. businessContractReceiveMapper.updateByPrimaryKey(businessContractReceive);
  328. map.put("status", "200");
  329. map.put("msg", "操作成功");
  330. }
  331. return map;
  332. }
  333. }