MaterialExtendServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. package com.jsh.erp.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.jsh.erp.constants.BusinessConstants;
  8. import com.jsh.erp.datasource.dto.MaterialDto;
  9. import com.jsh.erp.datasource.entities.*;
  10. import com.jsh.erp.datasource.mappers.MaterialCurrentStockMapper;
  11. import com.jsh.erp.datasource.mappers.MaterialExtendMapper;
  12. import com.jsh.erp.datasource.mappers.MaterialExtendMapperEx;
  13. import com.jsh.erp.datasource.vo.MaterialExtendVo4List;
  14. import com.jsh.erp.exception.JshException;
  15. import com.jsh.erp.service.*;
  16. import com.jsh.erp.utils.DateUtils;
  17. import com.jsh.erp.utils.RandomHelper;
  18. import com.jsh.erp.utils.StringUtil;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import org.springframework.web.context.request.RequestContextHolder;
  24. import org.springframework.web.context.request.ServletRequestAttributes;
  25. import javax.annotation.Resource;
  26. import javax.servlet.http.HttpServletRequest;
  27. import java.math.BigDecimal;
  28. import java.util.ArrayList;
  29. import java.util.Date;
  30. import java.util.List;
  31. @Service
  32. public class MaterialExtendServiceImpl extends ServiceImpl<MaterialExtendMapper, MaterialExtend> implements MaterialExtendService {
  33. private Logger logger = LoggerFactory.getLogger(MaterialExtendServiceImpl.class);
  34. @Resource
  35. private MaterialExtendMapper materialExtendMapper;
  36. @Resource
  37. private MaterialExtendMapperEx materialExtendMapperEx;
  38. @Resource
  39. private MaterialCurrentStockMapper materialCurrentStockMapper;
  40. @Resource
  41. private UserService userService;
  42. @Resource
  43. private RedisService redisService;
  44. @Resource
  45. private MaterialService materialService;
  46. @Resource
  47. private DepotItemService depotItemService;
  48. @Resource
  49. private InventoryLogService inventoryLogService;
  50. @Resource
  51. private SyncTescoSystemService syncTescoSystemService;
  52. @Override
  53. public MaterialExtend getMaterialExtend(long id)throws Exception {
  54. MaterialExtend result=null;
  55. try{
  56. result=materialExtendMapper.selectByPrimaryKey(id);
  57. }catch(Exception e){
  58. JshException.readFail(logger, e);
  59. }
  60. return result;
  61. }
  62. /**
  63. * 根据产品id查询产品价格拓展信息
  64. * @param materialId 产品id
  65. */
  66. @Override
  67. public List<MaterialExtendVo4List> getDetailList(Long materialId) {
  68. List<MaterialExtendVo4List> list=null;
  69. try{
  70. list = materialExtendMapperEx.getDetailList(materialId);
  71. }catch(Exception e){
  72. JshException.readFail(logger, e);
  73. }
  74. return list;
  75. }
  76. @Override
  77. public List<MaterialExtend> getListByMIds(List<Long> idList) {
  78. List<MaterialExtend> meList = null;
  79. try{
  80. Long [] idArray= StringUtil.listToLongArray(idList);
  81. if(idArray!=null && idArray.length>0) {
  82. meList = materialExtendMapperEx.getListByMId(idArray);
  83. }
  84. }catch(Exception e){
  85. JshException.readFail(logger, e);
  86. }
  87. return meList;
  88. }
  89. // /**
  90. // * 保存产品拓展记录
  91. // * @param obj
  92. // * @param sortList
  93. // * @param materialId
  94. // * @param type
  95. // * @return
  96. // * @throws Exception
  97. // */
  98. // @Override
  99. // @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  100. // public String saveDetails(JSONObject obj, String sortList, Long materialId, String type) throws Exception {
  101. // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  102. // //获取商品条码集合
  103. // JSONArray meArr = obj.getJSONArray("meList");
  104. // //添加商品拓展集合
  105. // JSONArray insertedJson = new JSONArray();
  106. // //修改商品拓展集合
  107. // JSONArray updatedJson = new JSONArray();
  108. // //移除商品集合
  109. // JSONArray deletedJson = obj.getJSONArray("meDeleteIdList");
  110. // JSONArray sortJson = JSONArray.parseArray(sortList);
  111. // //添加/修改行数据处理
  112. // if (null != meArr) {
  113. // if("insert".equals(type)){
  114. // for (int i = 0; i < meArr.size(); i++) {
  115. // JSONObject tempJson = meArr.getJSONObject(i);
  116. // insertedJson.add(tempJson);
  117. // }
  118. // } else if("update".equals(type)){
  119. // for (int i = 0; i < meArr.size(); i++) {
  120. // JSONObject tempJson = meArr.getJSONObject(i);
  121. // String tempId = tempJson.getString("id");
  122. // if(tempId.length()>19){
  123. // //id长度大于19,属于新增数据
  124. // insertedJson.add(tempJson);
  125. // } else {
  126. // updatedJson.add(tempJson);
  127. // }
  128. // }
  129. // }
  130. // }
  131. // //删除行数据处理
  132. // if (null != deletedJson && deletedJson.size()>0) {
  133. // StringBuffer bf=new StringBuffer();
  134. // for (int i = 0; i < deletedJson.size(); i++) {
  135. // bf.append(deletedJson.getString(i));
  136. // if(i<(deletedJson.size()-1)){
  137. // bf.append(",");
  138. // }
  139. // }
  140. // //删除拓展行
  141. // this.batchDeleteMaterialExtendByIds(bf.toString(), request);
  142. // }
  143. //
  144. // //添加产品拓展行
  145. // if (null != insertedJson) {
  146. // for (int i = 0; i < insertedJson.size(); i++) {
  147. // //商品拓展
  148. // MaterialExtend materialExtend = new MaterialExtend();
  149. // JSONObject tempInsertedJson = JSONObject.parseObject(insertedJson.getString(i));
  150. // //设置商品id
  151. // materialExtend.setMaterialId(materialId);
  152. // //设置商品单位
  153. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("commodityUnit"))) {
  154. // materialExtend.setCommodityUnit(tempInsertedJson.getString("commodityUnit"));
  155. // }
  156. // //设置商品属性
  157. // if (tempInsertedJson.get("sku")!=null) {
  158. // materialExtend.setSku(tempInsertedJson.getString("sku"));
  159. // }
  160. // //设置采购价格
  161. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("purchaseDecimal"))) {
  162. // materialExtend.setPurchaseDecimal(tempInsertedJson.getBigDecimal("purchaseDecimal"));
  163. // }
  164. // //设置零售价格
  165. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("commodityDecimal"))) {
  166. // materialExtend.setCommodityDecimal(tempInsertedJson.getBigDecimal("commodityDecimal"));
  167. // }
  168. // //设置销售价格
  169. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("wholesaleDecimal"))) {
  170. // materialExtend.setWholesaleDecimal(tempInsertedJson.getBigDecimal("wholesaleDecimal"));
  171. // }
  172. // //设置最低售价
  173. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("lowDecimal"))) {
  174. // materialExtend.setLowDecimal(tempInsertedJson.getBigDecimal("lowDecimal"));
  175. // }
  176. // //设置生产日期
  177. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("productionDate"))) {
  178. // materialExtend.setProductionDate(tempInsertedJson.getDate("productionDate"));
  179. // }
  180. // //设置保质期天数
  181. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("expiryNum"))) {
  182. // materialExtend.setExpiryNum(tempInsertedJson.getInteger("expiryNum"));
  183. // }
  184. // //设置供应商id
  185. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("supplierId"))) {
  186. // materialExtend.setSupplierId(tempInsertedJson.getLong("supplierId"));
  187. // }
  188. // //设置商品条码
  189. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("barCode"))) {
  190. // materialExtend.setBarCode(tempInsertedJson.getString("barCode"));
  191. // }
  192. // //设置批次号
  193. // String batchNumber = DateUtils.dateTimeNow("yyyyMMdd") + RandomHelper.getRandomStr(6);
  194. // materialExtend.setBatchNumber(batchNumber);
  195. // //设置库存
  196. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("inventory"))) {
  197. // materialExtend.setInventory(tempInsertedJson.getBigDecimal("inventory"));
  198. // }
  199. // //设置仓库id
  200. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("depotId"))) {
  201. // materialExtend.setDepotId(tempInsertedJson.getLong("depotId"));
  202. // }
  203. // //设置仓位货架
  204. // if (StringUtils.isNotEmpty(tempInsertedJson.getString("position"))) {
  205. // materialExtend.setPosition(tempInsertedJson.getString("position"));
  206. // }
  207. // //添加数据
  208. // this.insertMaterialExtend(materialExtend);
  209. // }
  210. // }
  211. // //修改拓展行
  212. // if (null != updatedJson) {
  213. // for (int i = 0; i < updatedJson.size(); i++) {
  214. // JSONObject tempUpdatedJson = JSONObject.parseObject(updatedJson.getString(i));
  215. // MaterialExtend materialExtend = new MaterialExtend();
  216. // //设置id
  217. // materialExtend.setId(tempUpdatedJson.getLong("id"));
  218. // materialExtend.setBarCode(tempUpdatedJson.getString("barCode"));
  219. // //Json里的数据赋值到对象
  220. // this.setMaterialExtend(tempUpdatedJson,materialExtend);
  221. // //修改商品拓展
  222. // this.updateMaterialExtend(materialExtend);
  223. // //如果金额为空,此处单独置空
  224. // materialExtendMapperEx.specialUpdatePrice(materialExtend);
  225. // }
  226. // }
  227. // //处理条码的排序,基本单位排第一个
  228. // if (null != sortJson && sortJson.size()>0) {
  229. // //此处为更新的逻辑
  230. // for (int i = 0; i < sortJson.size(); i++) {
  231. // JSONObject tempSortJson = JSONObject.parseObject(sortJson.getString(i));
  232. // MaterialExtend materialExtend = new MaterialExtend();
  233. // if(StringUtil.isExist(tempSortJson.get("id"))) {
  234. // materialExtend.setId(tempSortJson.getLong("id"));
  235. // }
  236. // if(StringUtil.isExist(tempSortJson.get("defaultFlag"))) {
  237. // materialExtend.setDefaultFlag(tempSortJson.getString("defaultFlag"));
  238. // }
  239. // this.updateMaterialExtend(materialExtend);
  240. // }
  241. // } else {
  242. // //新增的时候将第一条记录设置为默认基本单位
  243. // MaterialExtendExample example = new MaterialExtendExample();
  244. // example.createCriteria().andMaterialIdEqualTo(materialId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  245. // List<MaterialExtend> meList = materialExtendMapper.selectByExample(example);
  246. // if(meList!=null) {
  247. // for(int i=0; i<meList.size(); i++) {
  248. // MaterialExtend materialExtend = new MaterialExtend();
  249. // materialExtend.setId(meList.get(i).getId());
  250. // if(i==0) {
  251. // materialExtend.setDefaultFlag("1"); //默认
  252. // } else {
  253. // materialExtend.setDefaultFlag("0"); //非默认
  254. // }
  255. // this.updateMaterialExtend(materialExtend);
  256. // }
  257. // }
  258. // }
  259. // return null;
  260. // }
  261. @Override
  262. public String saveDetails(List<MaterialExtend> materialExtends, String sortList, Long materialId, String type) throws Exception {
  263. //添加商品条码集合
  264. List<MaterialExtend> insertedList = new ArrayList<>();
  265. //修改商品条码集合
  266. List<MaterialExtend> updatedList = new ArrayList<>();
  267. //添加/修改行数据处理
  268. if (null != materialExtends) {
  269. if("insert".equals(type)){
  270. for (int i = 0; i < materialExtends.size(); i++) {
  271. MaterialExtend materialExtend = materialExtends.get(i);
  272. insertedList.add(materialExtend);
  273. }
  274. } else if("update".equals(type)){
  275. for (int i = 0; i < materialExtends.size(); i++) {
  276. MaterialExtend materialExtend = materialExtends.get(i);
  277. Long id = materialExtend.getId();
  278. if(id != null && id > 0){
  279. //id不等于空并且大于0,属于修改数据
  280. updatedList.add(materialExtend);
  281. } else {
  282. insertedList.add(materialExtend);
  283. }
  284. }
  285. }
  286. }
  287. //添加条码
  288. if (!insertedList.isEmpty()) {
  289. for (MaterialExtend materialExtend : insertedList) {
  290. //设置商品id
  291. materialExtend.setMaterialId(materialId);
  292. //添加数据
  293. this.insertMaterialExtend(materialExtend);
  294. }
  295. }
  296. //修改拓展行
  297. if (!updatedList.isEmpty()) {
  298. for (MaterialExtend materialExtend : updatedList) {
  299. //修改商品拓展
  300. this.updateMaterialExtend(materialExtend);
  301. }
  302. }
  303. return null;
  304. }
  305. /**
  306. * 添加商品拓展
  307. * @param materialExtend 商品拓展
  308. */
  309. @Override
  310. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  311. public int insertMaterialExtend(MaterialExtend materialExtend)throws Exception {
  312. User user = userService.getCurrentUser();
  313. //设置是否为默认单位
  314. materialExtend.setDeleteFlag(BusinessConstants.DELETE_FLAG_EXISTS);
  315. //创建时间
  316. materialExtend.setCreateTime(new Date());
  317. //创建用户
  318. materialExtend.setCreateSerial(user.getLoginName());
  319. return materialExtendMapper.insertSelective(materialExtend);
  320. }
  321. /**
  322. * 修改商品拓展
  323. * @param materialExtend 商品拓展
  324. */
  325. @Override
  326. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  327. public int updateMaterialExtend(MaterialExtend materialExtend) throws Exception{
  328. User user = userService.getCurrentUser();
  329. materialExtend.setUpdateTime(System.currentTimeMillis());
  330. materialExtend.setUpdateSerial(user.getLoginName());
  331. return materialExtendMapper.updateByPrimaryKeySelective(materialExtend);
  332. }
  333. @Override
  334. public int checkIsBarCodeExist(Long id, String barCode)throws Exception {
  335. MaterialExtendExample example = new MaterialExtendExample();
  336. MaterialExtendExample.Criteria criteria = example.createCriteria();
  337. criteria.andBarCodeEqualTo(barCode);
  338. if (id > 0) {
  339. criteria.andIdNotEqualTo(id).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  340. } else {
  341. criteria.andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  342. }
  343. List<MaterialExtend> list =null;
  344. try{
  345. list = materialExtendMapper.selectByExample(example);
  346. }catch(Exception e){
  347. JshException.readFail(logger, e);
  348. }
  349. return list==null?0:list.size();
  350. }
  351. @Override
  352. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  353. public int deleteMaterialExtend(Long id, HttpServletRequest request)throws Exception {
  354. int result =0;
  355. MaterialExtend materialExtend = new MaterialExtend();
  356. materialExtend.setId(id);
  357. materialExtend.setDeleteFlag(BusinessConstants.DELETE_FLAG_DELETED);
  358. Long userId = Long.parseLong(redisService.getObjectFromSessionByKey(request,"userId").toString());
  359. User user = userService.getUser(userId);
  360. materialExtend.setUpdateTime(new Date().getTime());
  361. materialExtend.setUpdateSerial(user.getLoginName());
  362. try{
  363. result= materialExtendMapper.updateByPrimaryKeySelective(materialExtend);
  364. }catch(Exception e){
  365. JshException.writeFail(logger, e);
  366. }
  367. return result;
  368. }
  369. /**
  370. * 删除商品拓展行
  371. * @param ids 拓展行id集合
  372. */
  373. @Override
  374. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  375. public int batchDeleteMaterialExtendByIds(String ids, HttpServletRequest request) throws Exception{
  376. String [] idArray=ids.split(",");
  377. int result = 0;
  378. try{
  379. result = materialExtendMapperEx.batchDeleteMaterialExtendByIds(idArray);
  380. }catch(Exception e){
  381. JshException.writeFail(logger, e);
  382. }
  383. return result;
  384. }
  385. @Override
  386. public int insertMaterialExtend(JSONObject obj, HttpServletRequest request) throws Exception{
  387. MaterialExtend materialExtend = JSONObject.parseObject(obj.toJSONString(), MaterialExtend.class);
  388. int result=0;
  389. try{
  390. result = materialExtendMapper.insertSelective(materialExtend);
  391. }catch(Exception e){
  392. JshException.writeFail(logger, e);
  393. }
  394. return result;
  395. }
  396. @Override
  397. public int updateMaterialExtend(JSONObject obj, HttpServletRequest request)throws Exception {
  398. MaterialExtend materialExtend = JSONObject.parseObject(obj.toJSONString(), MaterialExtend.class);
  399. int result=0;
  400. try{
  401. result = materialExtendMapper.updateByPrimaryKeySelective(materialExtend);
  402. }catch(Exception e){
  403. JshException.writeFail(logger, e);
  404. }
  405. return result;
  406. }
  407. @Override
  408. public List<MaterialExtend> getMaterialExtendByTenantAndTime(Long tenantId, Long lastTime, Long syncNum)throws Exception {
  409. List<MaterialExtend> list=new ArrayList<MaterialExtend>();
  410. try{
  411. //先获取最大的时间戳,再查两个时间戳之间的数据,这样同步能够防止丢失数据(应为时间戳有重复)
  412. Long maxTime = materialExtendMapperEx.getMaxTimeByTenantAndTime(tenantId, lastTime, syncNum);
  413. if(tenantId!=null && lastTime!=null && maxTime!=null) {
  414. MaterialExtendExample example = new MaterialExtendExample();
  415. example.createCriteria().andTenantIdEqualTo(tenantId)
  416. .andUpdateTimeGreaterThan(lastTime)
  417. .andUpdateTimeLessThanOrEqualTo(maxTime);
  418. list=materialExtendMapper.selectByExample(example);
  419. }
  420. }catch(Exception e){
  421. JshException.readFail(logger, e);
  422. }
  423. return list;
  424. }
  425. @Override
  426. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  427. public Long selectIdByMaterialIdAndDefaultFlag(Long materialId, String defaultFlag) {
  428. Long id = 0L;
  429. MaterialExtendExample example = new MaterialExtendExample();
  430. example.createCriteria().andMaterialIdEqualTo(materialId).andDefaultFlagEqualTo(defaultFlag)
  431. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  432. List<MaterialExtend> list = materialExtendMapper.selectByExample(example);
  433. if(list!=null && list.size()>0) {
  434. id = list.get(0).getId();
  435. }
  436. return id;
  437. }
  438. @Override
  439. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  440. public Long selectIdByMaterialIdAndBarCode(Long materialId, String barCode) {
  441. Long id = 0L;
  442. MaterialExtendExample example = new MaterialExtendExample();
  443. example.createCriteria().andMaterialIdEqualTo(materialId).andBarCodeEqualTo(barCode)
  444. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  445. List<MaterialExtend> list = materialExtendMapper.selectByExample(example);
  446. if(list!=null && list.size()>0) {
  447. id = list.get(0).getId();
  448. }
  449. return id;
  450. }
  451. @Override
  452. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  453. public List<MaterialExtend> getListByMaterialIdAndDefaultFlagAndBarCode(Long materialId, String defaultFlag, String barCode) {
  454. MaterialExtendExample example = new MaterialExtendExample();
  455. example.createCriteria().andMaterialIdEqualTo(materialId).andDefaultFlagEqualTo(defaultFlag).andBarCodeNotEqualTo(barCode)
  456. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  457. return materialExtendMapper.selectByExample(example);
  458. }
  459. @Override
  460. public MaterialExtend getInfoByBarCode(String barCode)throws Exception {
  461. MaterialExtendExample example = new MaterialExtendExample();
  462. example.createCriteria().andBarCodeLike(barCode).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  463. List<MaterialExtend> list = materialExtendMapper.selectByExample(example);
  464. if(list!=null && list.size()>0) {
  465. return list.get(0);
  466. } else {
  467. return null;
  468. }
  469. }
  470. /**
  471. * 商品的副条码和数据库里面的商品条码存在重复(除自身商品之外)
  472. * @param manyBarCode
  473. * @param barCode
  474. * @return
  475. */
  476. @Override
  477. public int getCountByManyBarCodeWithoutUs(String manyBarCode, String barCode) {
  478. MaterialExtendExample example = new MaterialExtendExample();
  479. example.createCriteria().andBarCodeEqualTo(manyBarCode).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  480. List<MaterialExtend> list = materialExtendMapper.selectByExample(example);
  481. if(list!=null && list.size()>0) {
  482. for(MaterialExtend me: list) {
  483. List<MaterialExtend> basicMeList = materialExtendMapperEx.getBasicInfoByMid(me.getMaterialId());
  484. for(MaterialExtend basicMe: basicMeList) {
  485. if(basicMe!=null && !barCode.equals(basicMe.getBarCode())) {
  486. return 1;
  487. }
  488. }
  489. }
  490. }
  491. return 0;
  492. }
  493. @Override
  494. public MaterialExtend getInfoByBatchNumber(String batchNumber)throws Exception {
  495. MaterialExtend example = materialExtendMapper.selectByBatchNumber(batchNumber);
  496. return example;
  497. }
  498. /**
  499. * 设置当前库存
  500. * @param depotId 仓库id
  501. * @param mId 商品id
  502. * @param stock 库存数量
  503. * @param type 加减操作 add|subtract
  504. */
  505. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  506. public void insertCurrentStockByMaterialAndDepot(Long depotId, Long mId, BigDecimal stock,String type){
  507. //查询当前商品当前库存
  508. BigDecimal currentNumber = materialService.getCurrentStockByMaterialIdAndDepotId(mId,depotId);
  509. MaterialCurrentStock materialCurrentStock = new MaterialCurrentStock();
  510. materialCurrentStock.setDepotId(depotId);
  511. materialCurrentStock.setMaterialId(mId);
  512. materialCurrentStock.setCurrentNumber(stock);
  513. if (currentNumber == null || currentNumber.compareTo(BigDecimal.ZERO) == 0){
  514. materialCurrentStockMapper.insertSelective(materialCurrentStock); //存入当前库存
  515. }else {
  516. BigDecimal sumNumber = new BigDecimal("0");
  517. if ("add".equals(type)){
  518. sumNumber = currentNumber.add(materialCurrentStock.getCurrentNumber());
  519. }else if ("subtract".equals(type)){
  520. sumNumber = currentNumber.subtract(materialCurrentStock.getCurrentNumber());
  521. }
  522. materialCurrentStock.setCurrentNumber(sumNumber);
  523. materialCurrentStockMapper.updateByPrimaryKeySelective(materialCurrentStock);
  524. }
  525. }
  526. /**
  527. * 将Json对象的数据赋值到商品拓展对象中
  528. * @param tempJson
  529. * @param materialExtend
  530. */
  531. private void setMaterialExtend(JSONObject tempJson, MaterialExtend materialExtend){
  532. //设置商品单位
  533. if (StringUtils.isNotEmpty(tempJson.getString("commodityUnit"))) {
  534. materialExtend.setCommodityUnit(tempJson.getString("commodityUnit"));
  535. }
  536. //设置商品属性
  537. if (tempJson.get("sku")!=null) {
  538. materialExtend.setSku(tempJson.getString("sku"));
  539. }
  540. //设置采购价格
  541. if (StringUtils.isNotEmpty(tempJson.getString("purchaseDecimal"))) {
  542. materialExtend.setPurchaseDecimal(tempJson.getBigDecimal("purchaseDecimal"));
  543. }
  544. //设置零售价格
  545. if (StringUtils.isNotEmpty(tempJson.getString("commodityDecimal"))) {
  546. materialExtend.setCommodityDecimal(tempJson.getBigDecimal("commodityDecimal"));
  547. }
  548. //设置销售价格
  549. if (StringUtils.isNotEmpty(tempJson.getString("wholesaleDecimal"))) {
  550. materialExtend.setWholesaleDecimal(tempJson.getBigDecimal("wholesaleDecimal"));
  551. }
  552. //设置最低售价
  553. if (StringUtils.isNotEmpty(tempJson.getString("lowDecimal"))) {
  554. materialExtend.setLowDecimal(tempJson.getBigDecimal("lowDecimal"));
  555. }
  556. //设置生产日期
  557. if (StringUtils.isNotEmpty(tempJson.getString("productionDate"))) {
  558. materialExtend.setProductionDate(tempJson.getDate("productionDate"));
  559. }
  560. //设置保质期天数
  561. if (StringUtils.isNotEmpty(tempJson.getString("expiryNum"))) {
  562. materialExtend.setExpiryNum(tempJson.getInteger("expiryNum"));
  563. }
  564. //设置供应商id
  565. if (StringUtils.isNotEmpty(tempJson.getString("supplierId"))) {
  566. materialExtend.setSupplierId(tempJson.getLong("supplierId"));
  567. }
  568. //设置商品条码
  569. if (StringUtils.isNotEmpty(tempJson.getString("barCode"))) {
  570. materialExtend.setBarCode(tempJson.getString("barCode"));
  571. }
  572. //设置批次号
  573. String batchNumber = DateUtils.dateTimeNow("yyyyMMdd") + RandomHelper.getRandomStr(6);
  574. materialExtend.setBatchNumber(batchNumber);
  575. //设置库存
  576. if (StringUtils.isNotEmpty(tempJson.getString("inventory"))) {
  577. materialExtend.setInventory(tempJson.getBigDecimal("inventory"));
  578. }
  579. //设置仓库id
  580. if (StringUtils.isNotEmpty(tempJson.getString("depotId"))) {
  581. materialExtend.setDepotId(tempJson.getLong("depotId"));
  582. }
  583. //设置仓位货架
  584. if (StringUtils.isNotEmpty(tempJson.getString("position"))) {
  585. materialExtend.setPosition(tempJson.getString("position"));
  586. }
  587. }
  588. }