GoodsController.java 33 KB

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