GoodsSkuService.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package com.dk.mdm.service.mst;
  2. import com.dk.common.exception.BaseBusinessException;
  3. import com.dk.common.infrastructure.annotaiton.Pagination;
  4. import com.dk.common.infrastructure.constant.Constant;
  5. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  6. import com.dk.common.model.pojo.PageList;
  7. import com.dk.common.response.ResponseResultUtil;
  8. import com.dk.common.response.ResponseResultVO;
  9. import com.dk.common.util.ExcelUtils;
  10. import com.dk.mdm.infrastructure.convert.mst.GoodsSkuConvert;
  11. import com.dk.mdm.infrastructure.util.AuthUtils;
  12. import com.dk.mdm.mapper.mst.*;
  13. import com.dk.mdm.model.pojo.mst.GoodsBrand;
  14. import com.dk.mdm.model.pojo.mst.GoodsCategory;
  15. import com.dk.mdm.model.pojo.mst.GoodsSku;
  16. import com.dk.common.service.BaseService;
  17. import com.dk.common.mapper.BaseMapper;
  18. import com.dk.mdm.model.pojo.mst.Role;
  19. import com.dk.mdm.model.query.mac.OtherReceivableItemQuery;
  20. import com.dk.mdm.model.query.mst.*;
  21. import com.dk.mdm.model.response.mac.OtherReceivableItemResponse;
  22. import com.dk.mdm.model.response.mst.*;
  23. import com.dk.mdm.model.vo.ivt.InOutRecordVO;
  24. import com.dk.mdm.model.vo.ivt.InboundItemVO;
  25. import com.dk.mdm.model.vo.ivt.InboundVO;
  26. import com.dk.mdm.model.vo.mst.GoodsSkuVO;
  27. import com.dk.mdm.model.vo.mst.RoleVo;
  28. import com.dk.mdm.service.common.CommonService;
  29. import com.dk.mdm.service.ivt.inbound.InboundCheckService;
  30. import com.dk.mdm.service.ivt.inventory.InventoryService;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import org.springframework.web.bind.annotation.PathVariable;
  36. import javax.servlet.http.HttpServletResponse;
  37. import java.io.File;
  38. import java.io.FileOutputStream;
  39. import java.io.IOException;
  40. import java.math.BigDecimal;
  41. import java.time.LocalDate;
  42. import java.time.LocalDateTime;
  43. import java.util.*;
  44. @Service
  45. @Transactional
  46. public class GoodsSkuService extends BaseService<GoodsSku> {
  47. @Override
  48. public BaseMapper<GoodsSku> getRepository() {
  49. return goodsSkuMapper;
  50. }
  51. @Autowired
  52. private GoodsSkuMapper goodsSkuMapper;
  53. @Autowired
  54. private GoodsSkuConvert goodsSkuConvert;
  55. @Autowired
  56. private InboundCheckService inboundCheckService;
  57. @Autowired
  58. InventoryService inventoryService;
  59. @Autowired
  60. private GoodsBrandMapper goodsBrandMapper;
  61. @Autowired
  62. private GoodsCategoryMapper goodsCategoryMapper;
  63. @Autowired
  64. private UnitMapper unitMapper;
  65. @Autowired
  66. private GoodsSeriesMapper goodsSeriesMapper;
  67. /**
  68. * @desc : 重写组件
  69. * @author : 王英杰
  70. * @date : 2024/3/4 14:51
  71. */
  72. @Override
  73. public String getPrimaryKey() {
  74. return "sku_id";
  75. }
  76. @Autowired
  77. private CommonService commonService;
  78. @Autowired
  79. private AuthUtils authUtils;
  80. /**
  81. * @desc : 查询
  82. * @author : 王英杰
  83. * @date : 2023/1/5 9:39
  84. */
  85. @Pagination
  86. public ResponseResultVO<PageList<GoodsSkuResponse>> selectByCond(GoodsSkuQuery goodsSkuQuery) {
  87. return super.mergeListWithCount(goodsSkuQuery, goodsSkuMapper.selectByCond(goodsSkuQuery),
  88. goodsSkuMapper.countByCond(goodsSkuQuery));
  89. }
  90. /**
  91. * @desc : 新建
  92. * @author : 王英杰
  93. * @date : 2023/1/5 9:39
  94. */
  95. @Transactional(
  96. rollbackFor = {Exception.class}
  97. )
  98. public ResponseResultVO<?> insert(GoodsSkuVO goodsSkuVO) {
  99. GoodsSku goodsSku = goodsSkuConvert.convertToPo(goodsSkuVO);
  100. // 获取编码和主键UuId
  101. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.GOODSSKU.getName(), true);
  102. goodsSku.setSkuId(codeMap.get("outId").toString());
  103. goodsSku.setSkuCode(codeMap.get("outNote").toString());
  104. super.insert(goodsSku);
  105. if(goodsSkuVO.getWhId()!=null){ //生成入库单数据 插入存货表
  106. String StaffId = authUtils.getStaff().getStaffId();
  107. //总单
  108. InboundVO inboundVO = new InboundVO();
  109. inboundVO.setIntoType(Constant.IntoType.BEGIN.getName());
  110. inboundVO.setIntoingQty(goodsSkuVO.getIntoQty());
  111. inboundVO.setIntoingAmt(goodsSkuVO.getIntoQty().multiply(goodsSkuVO.getPriceInto()));
  112. inboundVO.setStaffId(StaffId);
  113. inboundVO.setMakeStaff(StaffId);
  114. inboundVO.setOrgId(goodsSkuVO.getOrgId());
  115. inboundVO.setMakeTime(LocalDateTime.now());
  116. inboundVO.setIntoDate(LocalDate.now());
  117. inboundVO.setWhId(goodsSkuVO.getWhId());
  118. inboundVO.setFlgAutoHandle(true);
  119. //明细
  120. List<InboundItemVO> inboundItemVOList = new ArrayList<>();
  121. InboundItemVO inboundItemVO = new InboundItemVO();
  122. inboundItemVO.setIntoingQty(goodsSkuVO.getIntoQty());
  123. inboundItemVO.setIntoingAmt(goodsSkuVO.getIntoQty().multiply(goodsSkuVO.getPriceInto()));
  124. inboundItemVO.setPriceInto(goodsSkuVO.getPriceInto());
  125. inboundItemVO.setIntoQty(goodsSkuVO.getIntoQty());
  126. inboundItemVO.setItemIndex(0);
  127. inboundItemVO.setSkuId(goodsSku.getSkuId());
  128. inboundItemVO.setWhId(goodsSkuVO.getWhId());
  129. inboundItemVO.setNonStdCode(goodsSkuVO.getNonStdCode());
  130. inboundItemVO.setIntoingBox(goodsSkuVO.getBox());
  131. inboundItemVO.setIntoingPiece(goodsSkuVO.getPiece());
  132. inboundItemVOList.add(inboundItemVO);
  133. Map<String, Object> map = new HashMap<>();
  134. map.put("total",inboundVO);
  135. map.put("detail",inboundItemVOList);
  136. inboundCheckService.checkInboundInsert(map);
  137. }
  138. return ResponseResultUtil.success();
  139. }
  140. /**
  141. * @desc : 新建
  142. * @author : 王英杰
  143. * @date : 2023/1/5 9:39
  144. */
  145. @Transactional(
  146. rollbackFor = {Exception.class}
  147. )
  148. public ResponseResultVO<?> update(GoodsSkuVO goodsSkuVO) {
  149. GoodsSku goodsSku = goodsSkuConvert.convertToPo(goodsSkuVO);
  150. // 获取编码和主键UuId
  151. super.updateByUuid(goodsSku);
  152. return ResponseResultUtil.success();
  153. }
  154. /**
  155. * @desc : 导入
  156. * @author : 常皓宁
  157. * @date : 2024/6/7 11:41
  158. */
  159. @Transactional(rollbackFor = {Exception.class})
  160. public ResponseResultVO<String> importSKU(List<GoodsSku> list, HttpServletResponse response,@PathVariable Integer cpId) {
  161. List<GoodsSku> messageList = new ArrayList<>();
  162. Workbook workbook = null;
  163. Boolean importMessage = false;
  164. //规则校验
  165. for (GoodsSku goodsSku : list) {
  166. goodsSku.setCpId(cpId);
  167. goodsSku.setErrorMessage("");
  168. //TODO 校验规则
  169. //商品编号不能为空
  170. if(goodsSku.getSkuCode()==null){
  171. goodsSku.setErrorMessage("商品编号不能为空,");
  172. importMessage = true;
  173. }
  174. //商品型号不能为空
  175. if(goodsSku.getSkuModel()==null){
  176. goodsSku.setErrorMessage("商品型号不能为空,");
  177. importMessage = true;
  178. }
  179. //品牌
  180. if (goodsSku.getBrandName()!=null) {
  181. String brandName = goodsSku.getBrandName();
  182. GoodsBrandResponse goodsBrandResponse = goodsBrandMapper.selectByUuId(new GoodsBrandQuery().setBrandName(brandName).setCpId(goodsSku.getCpId()));
  183. if(goodsBrandResponse == null){
  184. goodsSku.setErrorMessage("商品品牌填写错误,");
  185. importMessage = true;
  186. }else{
  187. goodsSku.setBrandId(goodsBrandResponse.getBrandId());
  188. }
  189. }else{
  190. goodsSku.setErrorMessage("商品品牌不能为空,");
  191. importMessage = true;
  192. }
  193. //种类
  194. if (goodsSku.getCategoryName()!=null) {
  195. String CatagoryName = goodsSku.getCategoryName();
  196. GoodsCategoryResponse goodsCategoryResponse = goodsCategoryMapper.selectByUuId(new GoodsCategoryQuery().setCatName(CatagoryName).setCpId(goodsSku.getCpId()));
  197. if(goodsCategoryResponse == null){
  198. goodsSku.setErrorMessage(goodsSku.getErrorMessage() + "商品种类填写错误,");
  199. importMessage = true;
  200. }else{
  201. goodsSku.setCategoryId(goodsCategoryResponse.getCatId());
  202. }
  203. }else{
  204. goodsSku.setErrorMessage(goodsSku.getErrorMessage() + "商品种类不能为空,");
  205. importMessage = true;
  206. }
  207. //计量单位
  208. if (goodsSku.getUnitName()!=null) {
  209. String unintName = goodsSku.getUnitName();
  210. UnitResponse unitResponse = unitMapper.selectByUuId(new UnitQuery().setUnitName(unintName).setCpId(goodsSku.getCpId()));
  211. if(unitResponse == null){
  212. goodsSku.setErrorMessage(goodsSku.getErrorMessage() + "计量单位填写错误,");
  213. importMessage = true;
  214. }else{
  215. goodsSku.setUnitId(unitResponse.getUnitId());
  216. }
  217. }else{
  218. goodsSku.setErrorMessage(goodsSku.getErrorMessage() + "计量单位不能为空,");
  219. importMessage = true;
  220. }
  221. //辅助单位
  222. if (goodsSku.getSubUnitName()!=null) {
  223. String subUnintName = goodsSku.getSubUnitName();
  224. UnitResponse unitResponse = unitMapper.selectByUuId(new UnitQuery().setUnitName(subUnintName).setCpId(goodsSku.getCpId()));
  225. if(unitResponse == null){
  226. goodsSku.setErrorMessage(goodsSku.getErrorMessage() + "辅助单位填写错误,");
  227. importMessage = true;
  228. }else{
  229. goodsSku.setSubUnitId(unitResponse.getUnitId());
  230. }
  231. }
  232. //商品系列
  233. if (goodsSku.getSeriesName()!=null) {
  234. String seriesName = goodsSku.getSeriesName();
  235. GoodsSeriesResponse goodsSeriesResponse = goodsSeriesMapper.selectByUuId(new GoodsSeriesQuery().setSeriesName(seriesName).setCpId(goodsSku.getCpId()));
  236. if(goodsSeriesResponse == null){
  237. goodsSku.setErrorMessage(goodsSku.getErrorMessage() + "商品系列填写错误,");
  238. importMessage = true;
  239. }else{
  240. goodsSku.setSubUnitId(goodsSeriesResponse.getSeriesId());
  241. }
  242. }
  243. messageList.add(goodsSku);
  244. }
  245. //导出错误提示
  246. if (importMessage) {
  247. workbook = ExcelUtils.exportExcelUrl(messageList, "错误数据反馈", "错误数据反馈", GoodsSku.class, "错误数据反馈" + ".xls", response);
  248. UUID uuid = UUID.randomUUID();
  249. String randomUUIDString = uuid.toString();
  250. //TODO 导出文件位置
  251. String fileName = "D:/练习/" + randomUUIDString;
  252. File outputFile = new File(fileName);
  253. //创建文件夹
  254. if (!outputFile.exists()) {
  255. outputFile.mkdir();
  256. }
  257. File uploadFile = new File(fileName, "错误数据反馈.xls");
  258. try (FileOutputStream fos = new FileOutputStream(uploadFile)) {
  259. workbook.write(fos);
  260. } catch (IOException e) {
  261. e.printStackTrace();
  262. }
  263. return ResponseResultUtil.error(-300,"D:/练习/" + randomUUIDString + "/错误数据反馈.xls");
  264. }
  265. else{
  266. //插入数据
  267. for (GoodsSku goodsSku : messageList) {
  268. super.insert(goodsSku);
  269. }
  270. return ResponseResultUtil.success();
  271. }
  272. }
  273. }