GoodsController.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. package com.kmall.admin.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.kmall.admin.dto.GoodsDto;
  4. import com.kmall.admin.dto.GoodsPanoramaDto;
  5. import com.kmall.admin.entity.GoodsEntity;
  6. import com.kmall.admin.entity.GoodsGalleryEntity;
  7. import com.kmall.admin.entity.SysOssEntity;
  8. import com.kmall.admin.haikong.constant.Constants;
  9. import com.kmall.admin.service.*;
  10. import com.kmall.admin.utils.ParamUtils;
  11. import com.kmall.admin.utils.ShiroUtils;
  12. import com.kmall.common.constant.Dict;
  13. import com.kmall.common.constant.JxlsXmlTemplateName;
  14. import com.kmall.admin.fromcomm.entity.SysUserEntity;
  15. import com.kmall.common.fileserver.util.FileManager;
  16. import com.kmall.common.utils.*;
  17. import com.kmall.common.utils.excel.ExcelExport;
  18. import com.kmall.common.utils.excel.ExcelUtil;
  19. import com.kmall.manager.manager.express.sf.ServiceException;
  20. import com.kmall.manager.manager.redis.JedisUtil;
  21. import org.apache.commons.fileupload.FileItem;
  22. import org.apache.commons.fileupload.FileItemFactory;
  23. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.shiro.authz.annotation.RequiresPermissions;
  26. import org.apache.tools.zip.ZipEntry;
  27. import org.apache.tools.zip.ZipFile;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.web.bind.annotation.*;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  34. import javax.servlet.http.HttpServletRequest;
  35. import javax.servlet.http.HttpServletResponse;
  36. import javax.sql.rowset.serial.SerialException;
  37. import java.io.*;
  38. import java.util.*;
  39. /**
  40. * Controller
  41. *
  42. * @author Scott
  43. * @email
  44. * @date 2017-08-21 21:19:49
  45. */
  46. @RestController
  47. @RequestMapping("goods")
  48. public class GoodsController {
  49. private final static Logger log = LoggerFactory.getLogger(GoodsController.class);
  50. @Autowired
  51. private GoodsService goodsService;
  52. @Autowired
  53. private GoodsGalleryService goodsGalleryService;
  54. @Autowired
  55. private OfflineCartService offlineCartService;
  56. @Autowired
  57. private ExcelUtil excelUtil;
  58. @Autowired
  59. private StoreService storeService;
  60. @Autowired
  61. private SysOssService sysOssService;
  62. /**
  63. * 查看列表
  64. */
  65. @RequestMapping("/list")
  66. @RequiresPermissions("goods:list")
  67. public R list(@RequestParam Map<String, Object> params) {
  68. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  69. String lastSaleTime = (String) params.get("lastSaleTime");
  70. if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) {
  71. try {
  72. lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8");
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. lastSaleTime = DateUtils.getDate(lastSaleTime);
  77. params.put("lastSaleTime", lastSaleTime + " 00:00:00");
  78. }
  79. //查询列表数据
  80. Query query = new Query(params);
  81. query.put("isDelete", 0);
  82. List<GoodsEntity> goodsList = goodsService.queryList(query);
  83. int total = goodsService.queryTotal(query);
  84. PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());
  85. return R.ok().put("page", pageUtil);
  86. }
  87. /**
  88. * 查看信息
  89. */
  90. @RequestMapping("/info/{id}")
  91. @RequiresPermissions("goods:info")
  92. public R info(@PathVariable("id") Integer id) {
  93. GoodsEntity goods = goodsService.queryObject(id);
  94. if(goods != null) {
  95. GoodsGalleryEntity goodsGalleryEntity =goodsGalleryService.queryVideoObjectByGoodId(goods.getId());
  96. if(goodsGalleryEntity != null){
  97. goods.setVideoUrl(goodsGalleryEntity.getImgUrl());
  98. }
  99. }
  100. return R.ok().put("goods", goods);
  101. }
  102. @RequestMapping("/queryGoodsName")
  103. public R queryGoodsName(@RequestParam String storeId, @RequestParam String goodsName) {
  104. List<GoodsEntity> goodsList = goodsService.queryByName(storeId, goodsName);
  105. return R.ok().put("goodsList", goodsList);
  106. }
  107. /**
  108. * 查看信息
  109. */
  110. @RequestMapping("/infoByQuery")
  111. public R infoByQuery(@RequestParam Map<String, Object> params) {
  112. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  113. ParamUtils.setName(params, "name");
  114. //查询列表数据
  115. Query query = new Query(params);
  116. query.put("isDelete", 0);
  117. List<GoodsEntity> goodsList = goodsService.queryList(query);
  118. if(goodsList != null && goodsList.size() != 0) {
  119. return R.ok().put("goods", goodsList.get(0));
  120. }
  121. return R.ok().put("goods", new GoodsEntity());
  122. }
  123. /**
  124. * 保存
  125. */
  126. @RequestMapping("/save")
  127. @RequiresPermissions("goods:save")
  128. public R save(@RequestBody GoodsEntity goods) {
  129. goodsService.save(goods);
  130. return R.ok();
  131. }
  132. /**
  133. * 修改
  134. */
  135. @RequestMapping("/update")
  136. @RequiresPermissions("goods:update")
  137. public R update(@RequestBody GoodsEntity goods) {
  138. goodsService.update(goods);
  139. return R.ok();
  140. }
  141. /**
  142. * 删除
  143. */
  144. @RequestMapping("/delete")
  145. @RequiresPermissions("goods:delete")
  146. public R delete(@RequestBody Integer[] ids) {
  147. goodsService.deleteBatch(ids);
  148. return R.ok();
  149. }
  150. /**
  151. * 查看所有列表
  152. */
  153. @RequestMapping("/queryAll")
  154. public R queryAll(@RequestParam Map<String, Object> params) {
  155. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  156. params.put("isDelete", Integer.parseInt(Dict.isDelete.item_0.getItem()));
  157. params.put("isOnSale", Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  158. List<GoodsEntity> list = goodsService.queryList(params);
  159. return R.ok().put("list", list);
  160. }
  161. /**
  162. * 商品回收站
  163. *
  164. * @param params
  165. * @return
  166. */
  167. @RequestMapping("/historyList")
  168. public R historyList(@RequestParam Map<String, Object> params) {
  169. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  170. //查询列表数据
  171. Query query = new Query(params);
  172. query.put("isDelete", 1);
  173. List<GoodsEntity> goodsList = goodsService.queryList(query);
  174. int total = goodsService.queryTotal(query);
  175. PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());
  176. return R.ok().put("page", pageUtil);
  177. }
  178. /**
  179. * 商品从回收站恢复
  180. */
  181. @RequestMapping("/back")
  182. @RequiresPermissions("goods:back")
  183. public R back(@RequestBody Integer[] ids) {
  184. goodsService.back(ids);
  185. return R.ok();
  186. }
  187. /**
  188. * 总计
  189. */
  190. @RequestMapping("/queryTotal")
  191. public R queryTotal(@RequestParam Map<String, Object> params) {
  192. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  193. params.put("isDelete", 0);
  194. int sum = goodsService.queryTotal(params);
  195. return R.ok().put("goodsSum", sum);
  196. }
  197. /**
  198. * 上架
  199. */
  200. @RequestMapping("/enSale")
  201. public R enSale(@RequestBody Integer id) {
  202. goodsService.enSale(id);
  203. return R.ok();
  204. }
  205. /**
  206. * 上架
  207. */
  208. @RequestMapping("/enSaleBatch")
  209. public R enSaleBatch(@RequestBody Integer[] ids) {
  210. goodsService.enSaleBatch(ids);
  211. return R.ok();
  212. }
  213. /**
  214. * 下架
  215. */
  216. @RequestMapping("/unSale")
  217. public R unSale(@RequestBody Integer id) {
  218. goodsService.unSale(id);
  219. return R.ok();
  220. }
  221. /**
  222. * 下架
  223. */
  224. @RequestMapping("/unSaleBatch")
  225. public R unSaleBatch(@RequestBody Integer[] ids) {
  226. goodsService.unSaleBatch(ids);
  227. return R.ok();
  228. }
  229. /**
  230. * 上传文件
  231. */
  232. @RequestMapping("/upload")
  233. @ResponseBody
  234. public R upload(@RequestParam("file") MultipartFile file) {
  235. List<GoodsDto> goodsDtoList = new ArrayList<>();//商品信息
  236. try {
  237. Map<String, Object> beans = new HashMap<String, Object>();
  238. beans.put("GoodsDtoList", goodsDtoList);
  239. if (file.isEmpty()) {
  240. return R.error("文件不能为空!");
  241. }
  242. excelUtil.readExcel(JxlsXmlTemplateName.GOODS_DTO_LIST, beans, file.getInputStream());
  243. } catch (Exception e) {
  244. e.printStackTrace();
  245. return R.error("导入失败!");
  246. }
  247. goodsService.uploadExcel(goodsDtoList,Integer.parseInt(Dict.exportDataType.item_1.getItem()));
  248. //上传文件
  249. return R.ok();
  250. }
  251. /**
  252. * 上传文件(修改库存版)
  253. */
  254. @RequestMapping("/uploadByCover")
  255. @ResponseBody
  256. public R uploadByCover(@RequestParam("file") MultipartFile file) {
  257. List<GoodsDto> goodsDtoList = new ArrayList<>();//商品信息
  258. try {
  259. Map<String, Object> beans = new HashMap<String, Object>();
  260. beans.put("GoodsDtoList", goodsDtoList);
  261. if (file.isEmpty()) {
  262. return R.error("文件不能为空!");
  263. }
  264. excelUtil.readExcel(JxlsXmlTemplateName.GOODS_DTO_LIST, beans, file.getInputStream());
  265. } catch (Exception e) {
  266. e.printStackTrace();
  267. return R.error("导入失败!");
  268. }
  269. goodsService.uploadExcelByCover(goodsDtoList,Integer.parseInt(Dict.exportDataType.item_1.getItem()));
  270. //上传文件
  271. return R.ok();
  272. }
  273. /**
  274. * 上传文件
  275. */
  276. @RequestMapping("/generalGoodsUpload")
  277. @ResponseBody
  278. public R generalGoodsUpload(@RequestParam("file") MultipartFile file) {
  279. List<GoodsDto> generalGoodsDtoList = new ArrayList<>();//商品信息
  280. try {
  281. Map<String, Object> beans = new HashMap<String, Object>();
  282. beans.put("GeneralGoodsDtoList", generalGoodsDtoList);
  283. if (file.isEmpty()) {
  284. return R.error("文件不能为空!");
  285. }
  286. excelUtil.readExcel(JxlsXmlTemplateName.GENERAL_GOODS_DTO_LIST, beans, file.getInputStream());
  287. } catch (Exception e) {
  288. e.printStackTrace();
  289. return R.error("导入失败!");
  290. }
  291. goodsService.uploadExcel(generalGoodsDtoList,Integer.parseInt(Dict.exportDataType.item_2.getItem()));
  292. //上传文件
  293. return R.ok();
  294. }
  295. @RequestMapping("/generalGoodsImgUploadByZip")
  296. @ResponseBody
  297. public R batchAddImgByZip(@RequestParam("file") MultipartFile file) throws IOException {
  298. //上传文件
  299. batchAdd(file,2);
  300. return R.ok();
  301. }
  302. @RequestMapping("/generalGoodsImgUpload")
  303. @ResponseBody
  304. public R batchAddImg(@RequestParam("file") MultipartFile file) throws IOException {
  305. //上传文件
  306. batchAdd(file,1);
  307. return R.ok();
  308. }
  309. private Map<String, Object> batchAdd(MultipartFile file, int type) throws IOException {
  310. /*
  311. *创建临时文件夹
  312. * 解压文件
  313. */
  314. String fileName = file.getOriginalFilename();
  315. String path = "/data/project/img/";
  316. File dir = new File(path);
  317. dir.mkdirs();
  318. String filePath = "/data/project/img2/";
  319. File fileDir = new File(filePath);
  320. fileDir.mkdirs();
  321. File saveFile = new File(fileDir, fileName);//将压缩包解析到指定位置
  322. List<String>list = new ArrayList<>();
  323. try {
  324. file.transferTo(saveFile);
  325. String newFilePath = filePath + fileName;
  326. File zipFile = new File(newFilePath);
  327. unZipFiles(zipFile, path,list,type);//解压文件,获取文件路径
  328. } catch (Exception e) {
  329. e.printStackTrace();
  330. log.info("解压执行失败");
  331. throw e;
  332. }finally {
  333. //程序结束时,删除临时文件
  334. deleteFiles(filePath);//删除压缩包文件夹
  335. deleteFiles(path);//删除解压文件夹**
  336. }
  337. log.info(JSON.toJSONString(list));
  338. Map<String, Object> jsonMap = new HashMap<>();
  339. jsonMap.put("ret",list);
  340. return jsonMap;
  341. }
  342. public void unZipFiles(File srcFile, String destDirPath, List<String> list, int type) throws RuntimeException {
  343. long start = System.currentTimeMillis();
  344. ZipFile zipFile = null;
  345. try {
  346. // 判断源文件是否存在
  347. if (!srcFile.exists()) {
  348. throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
  349. }
  350. // 开始解压
  351. zipFile = new ZipFile(srcFile);
  352. zipFile.getEncoding();
  353. Enumeration<?> entries = zipFile.getEntries();
  354. List<ZipEntry> entryList = new ArrayList<>();
  355. while (entries.hasMoreElements()) {
  356. ZipEntry entry = (ZipEntry) entries.nextElement();
  357. log.info("解压" + entry.getName());
  358. entryList.add(entry);
  359. // 如果是文件夹,就创建个文件夹
  360. }
  361. if(null==entryList){
  362. throw new RRException("文件夹内无图片信息,请检查后重试");
  363. }
  364. if(entryList.size()>100){
  365. throw new RRException("最多上传100张图片");
  366. }
  367. for(ZipEntry entry : entryList){
  368. if (entry.isDirectory()) {
  369. String dirPath = destDirPath + "/" + entry.getName();
  370. File dir = new File(dirPath);
  371. dir.mkdirs();
  372. } else {
  373. // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
  374. File targetFile = new File(destDirPath + "/" + entry.getName());
  375. // 保证这个文件的父文件夹必须要存在
  376. if(!targetFile.getParentFile().exists()){
  377. }
  378. targetFile.createNewFile();
  379. // 将压缩文件内容写入到这个文件中
  380. InputStream is = zipFile.getInputStream(entry);
  381. FileOutputStream fos = new FileOutputStream(targetFile);
  382. int len;
  383. byte[] buf = new byte[1024];
  384. while ((len = is.read(buf)) != -1) {
  385. fos.write(buf, 0, len);
  386. }
  387. MultipartFile mulFileByPath = getMulFileByPath(destDirPath + "/" + entry.getName());
  388. long fileSize = mulFileByPath.getSize();
  389. int maxSize = 1 * 1024 * 1024;
  390. if (fileSize > maxSize) {
  391. throw new RRException("商品图片过大(最大1M),请检查!");
  392. }
  393. //上传文件
  394. String url = FileManager.upload(mulFileByPath);
  395. list.add(url);
  396. if(type == 1){
  397. String barcode = entry.getName().split("\\.")[0];
  398. if (null==barcode||"".equals(barcode)) {
  399. throw new RRException("文件为:" + barcode + "的商品命名格式不正确,请检查!");
  400. }
  401. GoodsEntity goodsEntity = goodsService.queryByBarcode(barcode);
  402. goodsEntity.setPrimaryPicUrl(url);
  403. goodsEntity.setListPicUrl(url);
  404. goodsService.updateForImgUrl(goodsEntity);
  405. }
  406. //保存文件信息
  407. SysOssEntity ossEntity = new SysOssEntity();
  408. ossEntity.setUrl(url);
  409. ossEntity.setCreateDate(new Date());
  410. sysOssService.save(ossEntity);
  411. // 关流顺序,先打开的后关闭
  412. fos.close();
  413. is.close();
  414. }
  415. }
  416. long end = System.currentTimeMillis();
  417. System.out.println("解压完成,耗时:" + (end - start) +" ms");
  418. }catch (RRException e) {
  419. throw new RRException(e.getMessage(), e);
  420. }catch (Exception e) {
  421. throw new RuntimeException("unzip error from ZipUtils", e);
  422. } finally {
  423. if(zipFile != null){
  424. try {
  425. zipFile.close();
  426. } catch (IOException e) {
  427. e.printStackTrace();
  428. }
  429. }
  430. }
  431. }
  432. private static MultipartFile getMulFileByPath(String picPath) {
  433. FileItem fileItem = createFileItem(picPath);
  434. MultipartFile mfile = new CommonsMultipartFile(fileItem);
  435. return mfile;
  436. }
  437. private static FileItem createFileItem(String filePath)
  438. {
  439. FileItemFactory factory = new DiskFileItemFactory(16, null);
  440. String textFieldName = "textField";
  441. int num = filePath.lastIndexOf(".");
  442. String extFile = filePath.substring(num);
  443. FileItem item = factory.createItem(textFieldName, "text/plain", true,
  444. "MyFileName" + extFile);
  445. File newfile = new File(filePath);
  446. int bytesRead = 0;
  447. byte[] buffer = new byte[8192];
  448. try
  449. {
  450. FileInputStream fis = new FileInputStream(newfile);
  451. OutputStream os = item.getOutputStream();
  452. while ((bytesRead = fis.read(buffer, 0, 8192))
  453. != -1)
  454. {
  455. os.write(buffer, 0, bytesRead);
  456. }
  457. os.close();
  458. fis.close();
  459. }
  460. catch (IOException e)
  461. {
  462. e.printStackTrace();
  463. }
  464. return item;
  465. }
  466. public void deleteFiles(String filePath) {
  467. File file = new File(filePath);
  468. if ((!file.exists()) || (!file.isDirectory())) {
  469. System.out.println("file not exist");
  470. return;
  471. }
  472. String[] tempList = file.list();
  473. File temp = null;
  474. for (int i = 0; i < tempList.length; i++) {
  475. if (filePath.endsWith(File.separator)) {
  476. temp = new File(filePath + tempList[i]);
  477. }
  478. else {
  479. temp = new File(filePath + File.separator + tempList[i]);
  480. }
  481. if (temp.isFile()) {
  482. temp.delete();
  483. }
  484. if (temp.isDirectory()) {
  485. this.deleteFiles(filePath + "/" + tempList[i]);
  486. }
  487. }
  488. // 空文件的删除
  489. file.delete();
  490. }
  491. @RequestMapping("/scannInfo/{prodBarcode}")
  492. @RequiresPermissions("goods:scannInfo")
  493. public R scannInfo(@PathVariable("prodBarcode")String prodBarcode) {
  494. SysUserEntity user = ShiroUtils.getUserEntity();
  495. if(user == null) {
  496. return R.error("用户登录超时,请重新登录");
  497. }
  498. if (!user.getRoleType().equalsIgnoreCase("2")) {
  499. return R.error("该操作只允许店员账户操作");
  500. }
  501. GoodsEntity goods = goodsService.queryObjectByProdBarcodeAndBizType(prodBarcode, user.getStoreId());
  502. if(goods == null) {
  503. return R.error("商品信息不存在");
  504. }
  505. return R.ok().put("goods", goods);
  506. }
  507. @RequestMapping("/details/{prodBarcode}/{storeId}/{sku}")
  508. // @RequiresPermissions("goods:details") http://127.0.0.1:8080/goods/details/11111
  509. public R details(@PathVariable("prodBarcode")String prodBarcode,@PathVariable("storeId")String storeId,@PathVariable("sku")String sku) {
  510. SysUserEntity user = ShiroUtils.getUserEntity();
  511. if(user == null) {
  512. return R.error("用户登录超时,请重新登录");
  513. }
  514. if (!user.getRoleType().equalsIgnoreCase("2")) {
  515. return R.error("该操作只允许店员账户操作");
  516. }
  517. Map<String,Object> map = null;
  518. try {
  519. map = goodsService.calculateGoodsDetail(prodBarcode,storeId,sku);
  520. } catch (ServiceException e) {
  521. log.error("查询订单详情出现异常!", e);
  522. return R.error(e.getMessage());
  523. } catch (Exception e) {
  524. log.error("查询条码:【{}】详情出现异常!", prodBarcode, e);
  525. return R.error("系统异常,请联系管理员!e:"+e.getMessage());
  526. }
  527. if(map == null){
  528. return R.error("商品信息不存在");
  529. }
  530. return R.ok().put("goodsDetails", map.get("goods")).put("map",map);
  531. }
  532. @RequestMapping("/detailsOld/{prodBarcode}/{storeId}")
  533. // @RequiresPermissions("goods:details") http://127.0.0.1:8080/goods/details/11111
  534. public R details(@PathVariable("prodBarcode")String prodBarcode,@PathVariable("storeId")String storeId) {
  535. SysUserEntity user = ShiroUtils.getUserEntity();
  536. if(user == null) {
  537. return R.error("用户登录超时,请重新登录");
  538. }
  539. if (!user.getRoleType().equalsIgnoreCase("2")) {
  540. return R.error("该操作只允许店员账户操作");
  541. }
  542. Map<String,Object> map = null;
  543. try {
  544. map = goodsService.calculateGoodsDetail(prodBarcode,storeId,null);
  545. } catch (ServiceException e) {
  546. log.error("查询订单详情出现异常!", e);
  547. return R.error(e.getMessage());
  548. } catch (Exception e) {
  549. log.error("查询条码:【{}】价格出现异常!", prodBarcode, e);
  550. return R.error("系统异常,请联系管理员!e:"+e.getMessage());
  551. }
  552. if(map == null){
  553. return R.error("商品信息不存在");
  554. }
  555. return R.ok().put("goodsDetails", map.get("goods")).put("map",map);
  556. }
  557. // storeId + sku + prodBarcode
  558. @RequestMapping(value = "/number/minus/{storeId}/{sku}/{prodBarcode}", method = RequestMethod.GET)
  559. public R minus(@PathVariable("storeId") String storeId, @PathVariable("sku") String sku, @PathVariable("prodBarcode") String prodBarcode) {
  560. String key = Constants.WAREHOUSE_STOCK_MAP_KEY + "_" + storeId;
  561. String itemKey = storeId + sku + prodBarcode;
  562. try {
  563. String cartNumber = JedisUtil.hget(key, itemKey);
  564. int number = Integer.parseInt(cartNumber);
  565. JedisUtil.hset(key, itemKey, String.valueOf(++number));
  566. } catch (Exception e) {
  567. log.error("storeId:【{}】,sku:【{}】,prodBarcode:【{}】,扣减购物篮商品数量失败!", storeId, sku, prodBarcode);
  568. return R.error("扣减购物篮商品数量失败!请删除购物篮商品后重试!");
  569. }
  570. return R.ok();
  571. }
  572. @RequestMapping(value = "/number/add/{storeId}/{sku}/{prodBarcode}/{sellVolume}", method = RequestMethod.GET)
  573. public R add(@PathVariable("storeId") String storeId,
  574. @PathVariable("sku") String sku,
  575. @PathVariable("prodBarcode") String prodBarcode,
  576. @PathVariable("sellVolume") Integer sellVolume) {
  577. String key = Constants.WAREHOUSE_STOCK_MAP_KEY + "_" + storeId;
  578. String itemKey = storeId + sku + prodBarcode;
  579. try {
  580. GoodsEntity goodsEntity = goodsService.queryGoodsStockByBarcodeAndStoreIdAndSku(prodBarcode, Integer.parseInt(storeId), sku);
  581. Integer exitRegionNumber = goodsEntity.getExitRegionNumber();
  582. Integer stockNum = goodsEntity.getStockNum();
  583. String number = JedisUtil.hget(key, itemKey);
  584. int cartNumber = Integer.parseInt(number);
  585. if (stockNum + cartNumber - exitRegionNumber >= sellVolume) {
  586. JedisUtil.hset(key, itemKey, String.valueOf(--cartNumber));
  587. } else {
  588. log.error("增加商品数量失败!保税仓库存不足!storeId:【{}】,sku:【{}】,prodBarcode:【{}】,stockNum:【{}】,exitRegionNumber:【{}】,wareStockNumber:【{}】"
  589. , storeId, sku, prodBarcode, stockNum, exitRegionNumber, cartNumber);
  590. return R.error("增加商品数量失败!保税仓库存不足!");
  591. }
  592. } catch (Exception e) {
  593. log.error("storeId:【{}】,sku:【{}】,prodBarcode:【{}】,增加购物篮商品数量失败!", storeId, sku, prodBarcode);
  594. return R.error("增加购物篮商品数量失败!请删除购物篮商品后重试!");
  595. }
  596. return R.ok();
  597. }
  598. @RequestMapping(value = "/number/del/{storeId}/{sku}/{prodBarcode}", method = RequestMethod.GET)
  599. public R del(@PathVariable("storeId") String storeId, @PathVariable("sku") String sku, @PathVariable("prodBarcode") String prodBarcode) {
  600. String key = Constants.WAREHOUSE_STOCK_MAP_KEY + "_" + storeId;
  601. String itemKey = storeId + sku + prodBarcode;
  602. try {
  603. JedisUtil.hdel(key, itemKey);
  604. } catch (Exception e) {
  605. log.error("storeId:【{}】,sku:【{}】,prodBarcode:【{}】,删除购物篮商品失败!", storeId, sku, prodBarcode);
  606. return R.error("删除购物篮商品失败!请刷新收银端页面!");
  607. }
  608. return R.ok();
  609. }
  610. @RequestMapping(value = "/number/clear/{storeId}", method = RequestMethod.GET)
  611. public R clear(@PathVariable("storeId") String storeId) {
  612. String key = Constants.WAREHOUSE_STOCK_MAP_KEY + "_" + storeId;
  613. try {
  614. JedisUtil.del(key);
  615. } catch (Exception e) {
  616. log.error("storeId:【{}】,清空购物篮商品失败!", storeId);
  617. return R.error("清空购物篮商品失败!请刷新收银端页面!");
  618. }
  619. return R.ok();
  620. }
  621. /**
  622. * 多sku可选
  623. * @param prodBarcode
  624. * @param storeId
  625. * @return
  626. */
  627. @RequestMapping("/selectSkuDetails/{prodBarcode}/{storeId}")
  628. public R selectSkuDetails(@PathVariable("prodBarcode")String prodBarcode,@PathVariable("storeId")String storeId) {
  629. SysUserEntity user = ShiroUtils.getUserEntity();
  630. if(user == null) {
  631. return R.error("用户登录超时,请重新登录");
  632. }
  633. if (!user.getRoleType().equalsIgnoreCase("2")) {
  634. return R.error("该操作只允许店员账户操作");
  635. }
  636. List<Map<String,Object>> mapList = null;
  637. try {
  638. mapList = goodsService.selectSkuDetails(prodBarcode,storeId);
  639. } catch (ServiceException e) {
  640. log.error("查询sku详情出现异常!", e);
  641. return R.error(e.getMessage());
  642. } catch (Exception e) {
  643. return R.error("系统异常,请联系管理员!e:"+e.getMessage());
  644. }
  645. if(mapList == null){
  646. return R.error("商品信息不存在");
  647. }
  648. List<Object> objectList = new ArrayList<>();
  649. for(Map<String,Object> map : mapList){
  650. objectList.add(map.get("goods"));
  651. }
  652. return R.ok().put("goodsDetails", objectList).put("map",objectList);
  653. }
  654. /**
  655. * 根据商品编码或者条码查询商品信息(17.商品全景图)
  656. * @param keyword
  657. * @return
  658. */
  659. @GetMapping("/search/{keyword}")
  660. public R searchByKeyword(@PathVariable("keyword") String keyword){
  661. if (keyword == null || "".equals(keyword)){
  662. return R.error("请输入商品编码或者条码!");
  663. }
  664. GoodsPanoramaDto goodsPanoramaDto = goodsService.searchGoodsPanoramaDtoByKeyword(keyword);
  665. //GoodsEntity goods = goodsService.searchGoodsByKeyword(keyword);
  666. if (goodsPanoramaDto == null || "".equals(goodsPanoramaDto)) {
  667. return R.error("没有该商品!");
  668. }
  669. return R.ok().put("goodsPanoramaDto",goodsPanoramaDto);
  670. }
  671. /**
  672. * 所有商品模块导出
  673. * @param params 查询参数
  674. * @param response
  675. * @param request
  676. * @return
  677. */
  678. @RequiresPermissions("goods:export")
  679. @RequestMapping(value = "export")
  680. public R export(@RequestParam Map<String, Object> params, HttpServletResponse response, HttpServletRequest request) {
  681. ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
  682. params.put("isDelete", 0);
  683. String lastSaleTime = (String) params.get("lastSaleTime");
  684. if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) {
  685. try {
  686. lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8");
  687. } catch (Exception e) {
  688. e.printStackTrace();
  689. }
  690. lastSaleTime = DateUtils.getDate(lastSaleTime);
  691. params.put("lastSaleTime", lastSaleTime + " 00:00:00");
  692. }
  693. // 根据条件查询出列表
  694. List<GoodsEntity> goodsList = goodsService.queryExportList(params);
  695. ExcelExport ee = new ExcelExport("所有商品信息");
  696. String[] header = new String[]{"商户名称","第三方商户编号","商品编码","SKU","PLU","商品名称","商品英文名称","产品条码","货品业务类型","库存是否共享",
  697. "商品库存","日常价","成本价","是否上架","是否热销","录入日期","商品单位","商品税率","产品品牌","海关备案编号","计量单位","海关商品编码","国检规格型号",
  698. "原产国","海关申报要素","毛重(kg)","净重(kg)"};
  699. List<Map<String, Object>> list = new ArrayList<>();
  700. if (goodsList !=null && goodsList.size()>0){
  701. for (GoodsEntity goodsEntity : goodsList) {
  702. LinkedHashMap<String, Object> map = new LinkedHashMap<>();
  703. map.put("MerchName",goodsEntity.getMerchName());
  704. map.put("ThirdPartyMerchCode",goodsEntity.getThirdPartyMerchCode());
  705. map.put("GoodsSn",goodsEntity.getGoodsSn());
  706. map.put("Sku",goodsEntity.getSku());
  707. map.put("Plu",goodsEntity.getPlu());
  708. map.put("Name",goodsEntity.getName());
  709. map.put("EnglishName",goodsEntity.getEnglishName());
  710. String goodsBizType = goodsEntity.getGoodsBizType();
  711. Integer isStockShare = 0;
  712. if (goodsEntity.getIsStockShare()!=null){
  713. isStockShare = Integer.parseInt(goodsEntity.getIsStockShare());
  714. }
  715. map.put("ProdBarcode",goodsEntity.getProdBarcode());
  716. map.put("GoodsBizType",StringUtils.isEmpty(goodsBizType)?"":Dict.orderBizType.valueOf("item_"+goodsBizType).getItemName());
  717. map.put("IsStockShare",isStockShare==0?"否":"是");
  718. map.put("GoodsNumber",goodsEntity.getGoodsNumber());
  719. map.put("DailyPrice",goodsEntity.getDailyPrice());
  720. map.put("CostPrice",goodsEntity.getCostPrice());
  721. map.put("IsOnSale",goodsEntity.getIsOnSale()==0?"否":"是");
  722. map.put("IsHot",goodsEntity.getIsHot()==0?"否":"是");
  723. map.put("AddTime",goodsEntity.getAddTime());
  724. map.put("GoodsUnit",goodsEntity.getGoodsUnit());
  725. map.put("GoodsRate",goodsEntity.getGoodsRate());
  726. map.put("Brand",goodsEntity.getBrand());
  727. map.put("CusRecCode",goodsEntity.getCusRecCode());
  728. map.put("UnitCode",goodsEntity.getUnitCode());
  729. map.put("CusGoodsCode",goodsEntity.getCusGoodsCode());
  730. map.put("CiqProdModel",goodsEntity.getCiqProdModel());
  731. map.put("OriCntName",goodsEntity.getOriCntName());
  732. map.put("CusDeclEle",goodsEntity.getCusDeclEle());
  733. map.put("GrossWeight",goodsEntity.getGrossWeight());
  734. map.put("NetWeight",goodsEntity.getNetWeight());
  735. list.add(map);
  736. }
  737. }
  738. ee.addSheetByMap("所有商品信息", list, header);
  739. ee.export(response);
  740. return R.ok();
  741. }
  742. /**
  743. * 选择同步海关编号和商品税率
  744. * @return
  745. */
  746. @PostMapping("/syncGoodsRate")
  747. public R syncGoodsRate(@RequestBody Integer[] ids){
  748. // 先同步海关商品编码,再同步税率
  749. try {
  750. goodsService.syncOmsHsCodeGoode(Arrays.asList(ids));
  751. }catch (Exception e){
  752. e.printStackTrace();
  753. return R.error("同步海关商品编码失败,请联系管理员");
  754. }
  755. try {
  756. goodsService.syncGoodsRateGoode(Arrays.asList(ids));
  757. }catch (Exception e){
  758. e.printStackTrace();
  759. return R.error("同步商品税率失败,请联系管理员");
  760. }
  761. return R.ok();
  762. }
  763. /**
  764. * 全量同步海关编号和商品税率
  765. * @return
  766. */
  767. @PostMapping("/syncGoodsRateAll")
  768. public R syncGoodsRateAll(){
  769. // 先同步海关商品编码,再同步税率
  770. try {
  771. goodsService.syncOmsHsCodeTask();
  772. }catch (Exception e){
  773. e.printStackTrace();
  774. return R.error("同步海关商品编码失败,请联系管理员");
  775. }
  776. try {
  777. goodsService.syncGoodsRateTask();
  778. }catch (Exception e){
  779. e.printStackTrace();
  780. return R.error("同步商品税率失败,请联系管理员");
  781. }
  782. return R.ok();
  783. }
  784. /**
  785. * 校验系统中的商品价格是否有问题
  786. * @return
  787. */
  788. @RequestMapping("/checkGoodsPrice")
  789. public R checkGoodsPrice(){
  790. SysUserEntity user = ShiroUtils.getUserEntity();
  791. // 先同步海关商品编码,再同步税率
  792. try {
  793. goodsService.checkGoodsPrice(user);
  794. }catch (Exception e){
  795. e.printStackTrace();
  796. return R.error("校验失败,请联系管理员");
  797. }
  798. return R.ok("校验成功");
  799. }
  800. }