StockMngService.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package com.oqpo.api.service.stockmng;
  2. import com.oqpo.api.entity.oper.UserMngEntity;
  3. import com.oqpo.api.entity.stinfo.ItemEntity;
  4. import com.oqpo.api.entity.stinfo.StoreBaseInfoEntity;
  5. import com.oqpo.api.entity.stockmng.*;
  6. import com.oqpo.api.enums.ChgDvsn;
  7. import com.oqpo.api.enums.IfCode;
  8. import com.oqpo.api.enums.InOutCd;
  9. import com.oqpo.api.enums.SystemMessageCode;
  10. import com.oqpo.api.exception.GlobalException;
  11. import com.oqpo.api.mapper.stinfo.ItemMapper;
  12. import com.oqpo.api.mapper.stockmng.StockMngMapper;
  13. import com.oqpo.api.service.CommonService;
  14. import com.oqpo.api.util.DateUtil;
  15. import com.oqpo.api.web.dto.request.GridRequest;
  16. import com.oqpo.api.web.dto.request.ifmng.BoSaleStoreInfoRequest;
  17. import com.oqpo.api.web.dto.request.ifmng.BoStoreChgInfoRequest;
  18. import com.oqpo.api.web.dto.request.stockmng.ProcStockRequest;
  19. import com.oqpo.api.web.dto.request.stockmng.StockMngCddGridRequest;
  20. import com.oqpo.api.web.dto.request.stockmng.StockMngPassQtyGridRequest;
  21. import com.oqpo.api.web.dto.response.GridResponse;
  22. import com.oqpo.api.web.dto.response.ifmng.BoStoreChgInfoResponse;
  23. import com.oqpo.api.web.dto.response.stockmng.*;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import java.math.BigDecimal;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.stream.Collectors;
  32. @Service
  33. @Slf4j
  34. public class StockMngService extends CommonService {
  35. @Autowired
  36. private StockMngMapper stockMngMapper;
  37. @Autowired
  38. private ItemMapper itemMapper;
  39. /*
  40. 재고관리 그리드 리스트 조회
  41. */
  42. public GridResponse selectStockMngGridList(String sBrandId, String sStoreId, String sWhsId, String sLocation,
  43. String sItemStCd, String sItemType, String sItemGubun,
  44. String sItemClass1, String sItemClass2, String sItemClass3,
  45. String sItemClass4, String sItemId, String sItemNm, GridRequest gridRequest) throws Exception {
  46. int gridPage = gridRequest.getGridPage();
  47. int gridSize = gridRequest.getGridSize();
  48. int gridRecords = stockMngMapper.selectStockMngGridCnt(sBrandId, sStoreId, sWhsId, sLocation,
  49. sItemStCd, sItemType, sItemGubun,
  50. sItemClass1, sItemClass2, sItemClass3, sItemClass4, sItemId, sItemNm);
  51. int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
  52. List<StckMgntBaseInfoEntity> entities = stockMngMapper.selectStockMngGridList(sBrandId, sStoreId, sWhsId, sLocation,
  53. sItemStCd, sItemType, sItemGubun,
  54. sItemClass1, sItemClass2, sItemClass3, sItemClass4, sItemId, sItemNm, gridRequest);
  55. List<Object> gridRows = entities.stream()
  56. .map(m -> StockMngListResponse.builder()
  57. .viewCd("R")
  58. .brandId(m.getBrandId() == null ? "" : m.getBrandId())
  59. .brandNm(m.getBrandNm() == null ? "" : m.getBrandNm())
  60. .storeId(m.getStoreId() == null ? "" : m.getStoreId())
  61. .storeNm(m.getStoreNm() == null ? "" : m.getStoreNm())
  62. .whsId(m.getWhsId() == null ? "" : m.getWhsId())
  63. .whsNm(m.getWhsNm() == null ? "" : m.getWhsNm())
  64. .location(m.getLocation() == null ? "" : m.getLocation())
  65. .locationNm(m.getLocationNm() == null ? "" : m.getLocationNm())
  66. .itemId(m.getItemId() == null ? "" : m.getItemId())
  67. .itemNm(m.getItemNm() == null ? "" : m.getItemNm())
  68. .unit(m.getUnit() == null ? "" : m.getUnit())
  69. .stckQty(m.getStckQty())
  70. .prpStckQty(m.getPrpStckQty())
  71. .sftStckRt(m.getSftStckRt())
  72. .curStckRt(m.getCurStckRt())
  73. .build())
  74. .collect(Collectors.toList());
  75. return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
  76. }
  77. /*
  78. 재고실사 그리드 리스트 - 엑셀업로드 체크
  79. */
  80. public GridResponse selectStockCddGridList(StockMngCddGridRequest stockMngCddGridRequest, GridRequest gridRequest) throws Exception {
  81. int gridPage = gridRequest.getGridPage();
  82. int gridSize = gridRequest.getGridSize();
  83. List<StckMgntBaseInfoEntity> entities = stockMngMapper.selectStockCddGridList(stockMngCddGridRequest.getBrandId(), stockMngCddGridRequest.getStoreId(),
  84. stockMngCddGridRequest.getWhsId(), stockMngCddGridRequest.getLocation(), stockMngCddGridRequest.getGridItemData());
  85. int gridRecords = entities.size();
  86. int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
  87. List<Object> gridRows = entities.stream()
  88. .map(m -> StockMngListResponse.builder()
  89. .viewCd("R")
  90. .brandId(m.getBrandId())
  91. .brandNm(m.getBrandNm())
  92. .storeId(m.getStoreId())
  93. .storeNm(m.getStoreNm())
  94. .whsId(m.getWhsId())
  95. .whsNm(m.getWhsNm())
  96. .location(m.getLocation())
  97. .locationNm(m.getLocationNm())
  98. .itemId(m.getItemId())
  99. .itemNm(m.getItemNm())
  100. .unit(m.getUnit())
  101. .stckQty(m.getStckQty())
  102. .prpStckQty(m.getPrpStckQty())
  103. .sftStckRt(m.getSftStckRt())
  104. .curStckRt(m.getCurStckRt())
  105. .build())
  106. .collect(Collectors.toList());
  107. return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
  108. }
  109. /* 재고관리 정보 조회 */
  110. public StockMngInfoResponse selectStockMngInfo(String brandId, String whsId, String location, String itemId) throws Exception {
  111. StckMgntBaseInfoEntity entity = stockMngMapper.selectStckMgntBaseInfo(brandId, whsId, location, itemId);
  112. return StockMngInfoResponse.toDTO(entity);
  113. }
  114. /*
  115. 재고이력 그리드 리스트 조회
  116. */
  117. public GridResponse selectStockHistGridList(String sBrandId, String sWhsId, String sLocation,
  118. String sItemId, String sInoutCd, String fromDt, String toDt, GridRequest gridRequest) throws Exception {
  119. int gridPage = gridRequest.getGridPage();
  120. int gridSize = gridRequest.getGridSize();
  121. fromDt = fromDt == null ? null : fromDt.replace(".", "");
  122. toDt = toDt == null ? null : toDt.replace(".", "");
  123. int gridRecords = stockMngMapper.selectStockHistGridCnt(sBrandId, sWhsId, sLocation, sItemId, sInoutCd, fromDt, toDt);
  124. int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
  125. List<StckMgntDtlHistEntity> entities = stockMngMapper.selectStockHistGridList(sBrandId, sWhsId, sLocation, sItemId, sInoutCd, fromDt, toDt, gridRequest);
  126. List<Object> gridRows = entities.stream()
  127. .map(m -> StockHistListResponse.builder()
  128. .viewCd("R")
  129. .inoutCd(m.getInoutCd())
  130. .inoutNm(m.getInoutNm())
  131. .inoutQty(m.getInoutQty())
  132. .chgDvsn(m.getChgDvsn())
  133. .chgDvsnNm(m.getChgDvsnNm())
  134. .relationNo(m.getRelationNo())
  135. .sysRegDttm(m.getSysRegDttm())
  136. .build())
  137. .collect(Collectors.toList());
  138. return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
  139. }
  140. /* 재고 처리 */
  141. @Transactional
  142. public void procStock(String userId, ProcStockRequest procStockRequest) throws Exception {
  143. try {
  144. // 1-1. 소스 재고 정보 (stck_mgnt_base_info) 업데이트
  145. StckMgntBaseInfoEntity source = stockMngMapper.selectStckMgntBaseInfo(procStockRequest.getBrandId(), procStockRequest.getSourceWhsId(),
  146. procStockRequest.getSourceLocation(), procStockRequest.getItemId());
  147. source.setStckQty(source.getStckQty() - procStockRequest.getMoveQty()); // 재고수량
  148. //double curStckRt = Math.round(source.getStckQty() * 100 / source.getPrpStckQty() * 100) / 100.0;
  149. //source.setCurStckRt(BigDecimal.valueOf(curStckRt)); // 현재고비율((재고수량 * 100) / 적정재고)
  150. stockMngMapper.updateStckMgntBaseInfo4StckProc(userId, source);
  151. // 1-2. 소스 이력 등록 (stck_mgnt_dtl_hist)
  152. StckMgntDtlHistEntity sourceHist = new StckMgntDtlHistEntity();
  153. sourceHist.setStckMgntDtlNo(fnGetDealNo(32, "")); // 재고관리상세번호
  154. sourceHist.setBrandId(source.getBrandId());
  155. sourceHist.setWhsId(source.getWhsId());
  156. sourceHist.setLocation(source.getLocation());
  157. sourceHist.setItemId(source.getItemId());
  158. sourceHist.setItemNm(source.getItemNm());
  159. sourceHist.setInoutCd(InOutCd.OUT.getCd()); // 출고
  160. sourceHist.setInoutQty(procStockRequest.getMoveQty());
  161. sourceHist.setChgDvsn(ChgDvsn.STOCK_MOVE.getCd()); // 재고이동
  162. stockMngMapper.insertStckMgntDtlHist(userId, sourceHist);
  163. // 2-1. 타겟 재고 정보 (stck_mgnt_base_info) 업데이트
  164. StckMgntBaseInfoEntity target = stockMngMapper.selectStckMgntBaseInfo(procStockRequest.getBrandId(), procStockRequest.getTargetWhsId(),
  165. procStockRequest.getTargetLocation(), procStockRequest.getItemId());
  166. if (target == null) {
  167. target = new StckMgntBaseInfoEntity();
  168. target.setBrandId(procStockRequest.getBrandId());
  169. target.setWhsId(procStockRequest.getTargetWhsId());
  170. target.setLocation(procStockRequest.getTargetLocation());
  171. target.setItemId(procStockRequest.getItemId());
  172. target.setItemNm(source.getItemNm()); // 소스와 동일하게
  173. target.setUnit(source.getUnit()); // 소스와 동일하게
  174. target.setStckQty(procStockRequest.getMoveQty()); // 재고수량
  175. target.setPrpStckQty(source.getPrpStckQty()); // 소스와 동일하게
  176. target.setSftStckRt(source.getSftStckRt()); // 소스와 동일하게
  177. //curStckRt = Math.round(target.getStckQty() * 100 / target.getPrpStckQty() * 100) / 100.0;
  178. //target.setCurStckRt(BigDecimal.valueOf(curStckRt)); // 현재고비율((재고수량 * 100) / 적정재고)
  179. stockMngMapper.insertStckMgntBaseInfo(userId, target);
  180. } else {
  181. target.setStckQty(target.getStckQty() + procStockRequest.getMoveQty()); // 재고수량
  182. //curStckRt = Math.round(target.getStckQty() * 100 / target.getPrpStckQty() * 100) / 100.0;
  183. //target.setCurStckRt(BigDecimal.valueOf(curStckRt)); // 현재고비율((재고수량 * 100) / 적정재고)
  184. stockMngMapper.updateStckMgntBaseInfo4StckProc(userId, target);
  185. }
  186. // 2-2. 타겟 이력 등록 (stck_mgnt_dtl_hist)
  187. StckMgntDtlHistEntity targetHist = new StckMgntDtlHistEntity();
  188. targetHist.setStckMgntDtlNo(fnGetDealNo(32, "")); // 재고관리상세번호
  189. targetHist.setBrandId(target.getBrandId());
  190. targetHist.setWhsId(target.getWhsId());
  191. targetHist.setLocation(target.getLocation());
  192. targetHist.setItemId(target.getItemId());
  193. targetHist.setItemNm(target.getItemNm());
  194. targetHist.setInoutCd(InOutCd.IN.getCd()); // 입고
  195. targetHist.setInoutQty(procStockRequest.getMoveQty());
  196. targetHist.setChgDvsn(ChgDvsn.STOCK_MOVE.getCd()); // 재고이동
  197. stockMngMapper.insertStckMgntDtlHist(userId, targetHist);
  198. } catch (GlobalException e) {
  199. e.getStackTrace();
  200. throw new GlobalException(e.getSystemMessageCode());
  201. } catch (Exception e) {
  202. e.getStackTrace();
  203. throw new RuntimeException();
  204. }
  205. }
  206. public StockMngPassqtyListResponse selectWhsLocOutPassQty(String userId, StockMngPassQtyGridRequest stockMngPassQtyGridRequest) throws Exception {
  207. List<StckMgntBaseInfoEntity> gridItemList = stockMngPassQtyGridRequest.toCodeEntities(stockMngPassQtyGridRequest.getGridUpdateData());
  208. List<StckMgntBaseInfoEntity> entities = stockMngMapper.selectWhsLocOutPassQty(stockMngPassQtyGridRequest.getBrandId(),
  209. stockMngPassQtyGridRequest.getWhsId(), stockMngPassQtyGridRequest.getLocation(), gridItemList);
  210. return StockMngPassqtyListResponse.toDTO(entities);
  211. }
  212. /*
  213. 재고관리 그리드 리스트 조회
  214. */
  215. public GridResponse selectPopItemStockMngGridList(String sBrandId, String sWhsId, String sLocation,
  216. String sItemNm, GridRequest gridRequest) throws Exception {
  217. int gridPage = gridRequest.getGridPage();
  218. int gridSize = gridRequest.getGridSize();
  219. int gridRecords = stockMngMapper.selectPopItemStockMngGridCnt(sBrandId, sWhsId, sLocation, sItemNm);
  220. int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
  221. List<StckMgntBaseInfoEntity> entities = stockMngMapper.selectPopItemStockMngGridList(sBrandId, sWhsId, sLocation, sItemNm, gridRequest);
  222. List<Object> gridRows = entities.stream()
  223. .map(m -> PopItemWhsStockListResponse.builder()
  224. .viewCd("R")
  225. .brandId(m.getBrandId())
  226. .brandNm(m.getBrandNm())
  227. .itemId(m.getItemId())
  228. .itemNm(m.getItemNm())
  229. .unit(m.getUnit())
  230. .stckQty(m.getStckQty())
  231. .waitQty(m.getWaitQty())
  232. .build())
  233. .collect(Collectors.toList());
  234. return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
  235. }
  236. /* 바코드 품목 정보 조회 */
  237. public BarcodeWhsItemInfoResponse selectBarcodeWhsItemMngInfo(String sBrandId, String sWhsId, String sLocation, String sBarcode) throws Exception {
  238. StckMgntBaseInfoEntity entity = stockMngMapper.selectPopItemStockInfo(sBrandId, sWhsId, sLocation, sBarcode);
  239. return BarcodeWhsItemInfoResponse.toDTO(entity);
  240. }
  241. /* 창고 품목 정보 조회 */
  242. public GridResponse selectWhsLocationItemList(String brandId, String whsId, String location, GridRequest gridRequest) throws Exception {
  243. int gridPage = gridRequest.getGridPage();
  244. int gridSize = gridRequest.getGridSize();
  245. List<StckMgntBaseInfoEntity> entities = stockMngMapper.selectWhsLocationItemList(brandId, whsId, location);
  246. int gridRecords = entities.size();
  247. int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
  248. List<Object> gridRows = entities.stream()
  249. .map(m -> WhsItemInfoResponse.builder()
  250. .itemId(m.getItemId())
  251. .itemNm(m.getItemNm())
  252. .excelQty(m.getExcelQty())
  253. .build())
  254. .collect(Collectors.toList());
  255. return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
  256. }
  257. /* 창고 품목 정보 조회 */
  258. public GridResponse selectWhsLocationItemList4Excel(String brandId, String whsId, String location, GridRequest gridRequest) throws Exception {
  259. int gridPage = gridRequest.getGridPage();
  260. int gridSize = gridRequest.getGridSize();
  261. List<StckMgntBaseInfoEntity> entities = stockMngMapper.selectWhsLocationItemList(brandId, whsId, location);
  262. if (entities.size() < 1) {
  263. entities = new ArrayList<>();
  264. StckMgntBaseInfoEntity entity = new StckMgntBaseInfoEntity();
  265. entities.add(entity);
  266. }
  267. int gridRecords = entities.size();
  268. int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
  269. List<Object> gridRows = entities.stream()
  270. .map(m -> WhsItemInfoExcelResponse.builder()
  271. .품목번호(m.getItemId())
  272. .품목명(m.getItemNm())
  273. .실사수량(m.getExcelQty())
  274. .build())
  275. .collect(Collectors.toList());
  276. return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
  277. }
  278. /*
  279. 백오피스 판매정보 수신
  280. */
  281. @Transactional
  282. public BoSaleStoreStockResponse ifBoSaveStoreStock(String userId, BoSaleStoreInfoRequest boSaleStoreInfoRequest) throws Exception {
  283. try {
  284. String brandCd = boSaleStoreInfoRequest.getBrandCd();
  285. String storeNo = boSaleStoreInfoRequest.getStoreNo();
  286. String saleDt = boSaleStoreInfoRequest.getSaleDt() ;
  287. String baseYm = "";
  288. if (saleDt == null || saleDt.equals("")) {
  289. baseYm = DateUtil.getCurrentYearMonth() + "01";
  290. } else {
  291. baseYm = saleDt.substring(0,6) + "01";
  292. }
  293. StckMgntBaseInfoEntity whsInfoEntity = stockMngMapper.selectBoWhsInfo(brandCd, storeNo);
  294. if (whsInfoEntity == null) {
  295. throw new GlobalException(SystemMessageCode.ERR_BO_STORE);
  296. }
  297. List<StckSaleMgntDtlHistEntity> hisList = new ArrayList<StckSaleMgntDtlHistEntity>();
  298. List<StckMgntBaseInfoEntity> stckList = new ArrayList<StckMgntBaseInfoEntity>();
  299. List<BoSaleInfoEntity> saleList = boSaleStoreInfoRequest.toCodeEntities(boSaleStoreInfoRequest.getSaleList());
  300. BoSaleInfoResultEntity boSaleInfo = new BoSaleInfoResultEntity();
  301. List<BoSaleProcResultEntity > resultList = new ArrayList<BoSaleProcResultEntity>();
  302. boSaleInfo.setBrandCd(brandCd);
  303. boSaleInfo.setStoreNo(storeNo);
  304. int notFindItemCnt = 0 ;
  305. if (saleList.size() > 0) {
  306. for (int i = 0 ; i < saleList.size() ; i++ ) {
  307. BoSaleProcResultEntity retEntity = new BoSaleProcResultEntity();
  308. BoSaleInfoEntity saleEntity = saleList.get(i);
  309. ItemEntity itemInfo = itemMapper.selectOqToItemId(whsInfoEntity.getBrandId(), saleEntity.getMenuCd());
  310. if (itemInfo == null || "".equals(itemInfo.getItemId())) {
  311. notFindItemCnt = notFindItemCnt + 1 ;
  312. retEntity.setMenuCd(saleEntity.getMenuCd());
  313. retEntity.setItemId("");
  314. retEntity.setProcCd("01");
  315. } else {
  316. StckSaleMgntDtlHistEntity stckInfo = new StckSaleMgntDtlHistEntity();
  317. stckInfo.setBrandId(whsInfoEntity.getBrandId());
  318. stckInfo.setWhsId(whsInfoEntity.getWhsId());
  319. stckInfo.setLocation(whsInfoEntity.getLocation());
  320. stckInfo.setItemId(itemInfo.getItemId());
  321. stckInfo.setItemNm(itemInfo.getItemNm());
  322. stckInfo.setBaseYm(baseYm);
  323. stckInfo.setSaleQty(saleEntity.getSaleCnt());
  324. stckInfo.setOqBrandId(brandCd);
  325. stckInfo.setOqMenuCd(saleEntity.getMenuCd());
  326. hisList.add(stckInfo);
  327. StckMgntBaseInfoEntity stckBaseInfo = new StckMgntBaseInfoEntity();
  328. stckBaseInfo.setBrandId(whsInfoEntity.getBrandId());
  329. stckBaseInfo.setWhsId(whsInfoEntity.getWhsId());
  330. stckBaseInfo.setLocation(whsInfoEntity.getLocation());
  331. stckBaseInfo.setItemId(itemInfo.getItemId());
  332. stckBaseInfo.setStckQty(saleEntity.getSaleCnt());
  333. stckList.add(stckBaseInfo);
  334. retEntity.setMenuCd(saleEntity.getMenuCd());
  335. retEntity.setItemId(itemInfo.getItemId());
  336. retEntity.setProcCd("00");
  337. }
  338. resultList.add(retEntity);
  339. }
  340. if (hisList.size() > 0) {
  341. stockMngMapper.insertStckSaleMgntDtlHist(userId,hisList );
  342. stockMngMapper.insertInOutStckMgntBaseInfo(userId,stckList );
  343. }
  344. }
  345. if (notFindItemCnt > 0 ) {
  346. if (notFindItemCnt == saleList.size() ) {
  347. boSaleInfo.setCode(IfCode.S03.getCd());
  348. boSaleInfo.setMessage(IfCode.S03.getName());
  349. } else {
  350. boSaleInfo.setCode(IfCode.S02.getCd());
  351. boSaleInfo.setMessage(IfCode.S02.getName());
  352. }
  353. } else {
  354. boSaleInfo.setCode(IfCode.S01.getCd());
  355. boSaleInfo.setMessage(IfCode.S01.getName());
  356. }
  357. return BoSaleStoreStockResponse.toDTO(boSaleInfo, resultList);
  358. } catch (GlobalException e) {
  359. e.getStackTrace();
  360. throw new GlobalException(e.getSystemMessageCode());
  361. } catch (Exception e) {
  362. e.getStackTrace();
  363. throw new RuntimeException();
  364. }
  365. }
  366. }