GoodsController.java 33 KB

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