GoodsController.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. // ParamUtils.setName(params, "name");
  70. String lastSaleTime = (String) params.get("lastSaleTime");
  71. if(org.apache.commons.lang.StringUtils.isNotEmpty(lastSaleTime)) {
  72. try {
  73. lastSaleTime = new String(lastSaleTime.getBytes("iso-8859-1"), "utf-8");
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. lastSaleTime = DateUtils.getDate(lastSaleTime);
  78. params.put("lastSaleTime", lastSaleTime + " 00:00:00");
  79. }
  80. //查询列表数据
  81. Query query = new Query(params);
  82. query.put("isDelete", 0);
  83. List<GoodsEntity> goodsList = goodsService.queryList(query);
  84. int total = goodsService.queryTotal(query);
  85. PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());
  86. return R.ok().put("page", pageUtil);
  87. }
  88. /**
  89. * 查看信息
  90. */
  91. @RequestMapping("/info/{id}")
  92. @RequiresPermissions("goods:info")
  93. public R info(@PathVariable("id") Integer id) {
  94. GoodsEntity goods = goodsService.queryObject(id);
  95. if(goods != null) {
  96. GoodsGalleryEntity goodsGalleryEntity =goodsGalleryService.queryVideoObjectByGoodId(goods.getId());
  97. if(goodsGalleryEntity != null){
  98. goods.setVideoUrl(goodsGalleryEntity.getImgUrl());
  99. }
  100. }
  101. return R.ok().put("goods", goods);
  102. }
  103. @RequestMapping("/queryGoodsName")
  104. public R queryGoodsName(@RequestParam String storeId, @RequestParam String goodsName) {
  105. List<GoodsEntity> goodsList = goodsService.queryByName(storeId, goodsName);
  106. return R.ok().put("goodsList", goodsList);
  107. }
  108. /**
  109. * 查看信息
  110. */
  111. @RequestMapping("/infoByQuery")
  112. public R infoByQuery(@RequestParam Map<String, Object> params) {
  113. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  114. ParamUtils.setName(params, "name");
  115. //查询列表数据
  116. Query query = new Query(params);
  117. query.put("isDelete", 0);
  118. List<GoodsEntity> goodsList = goodsService.queryList(query);
  119. if(goodsList != null && goodsList.size() != 0) {
  120. return R.ok().put("goods", goodsList.get(0));
  121. }
  122. return R.ok().put("goods", new GoodsEntity());
  123. }
  124. /**
  125. * 保存
  126. */
  127. @RequestMapping("/save")
  128. @RequiresPermissions("goods:save")
  129. public R save(@RequestBody GoodsEntity goods) {
  130. goodsService.save(goods);
  131. return R.ok();
  132. }
  133. /**
  134. * 修改
  135. */
  136. @RequestMapping("/update")
  137. @RequiresPermissions("goods:update")
  138. public R update(@RequestBody GoodsEntity goods) {
  139. goodsService.update(goods);
  140. return R.ok();
  141. }
  142. /**
  143. * 删除
  144. */
  145. @RequestMapping("/delete")
  146. @RequiresPermissions("goods:delete")
  147. public R delete(@RequestBody Integer[] ids) {
  148. goodsService.deleteBatch(ids);
  149. return R.ok();
  150. }
  151. /**
  152. * 查看所有列表
  153. */
  154. @RequestMapping("/queryAll")
  155. public R queryAll(@RequestParam Map<String, Object> params) {
  156. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  157. params.put("isDelete", Integer.parseInt(Dict.isDelete.item_0.getItem()));
  158. params.put("isOnSale", Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  159. List<GoodsEntity> list = goodsService.queryList(params);
  160. return R.ok().put("list", list);
  161. }
  162. /**
  163. * 商品回收站
  164. *
  165. * @param params
  166. * @return
  167. */
  168. @RequestMapping("/historyList")
  169. public R historyList(@RequestParam Map<String, Object> params) {
  170. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  171. //查询列表数据
  172. Query query = new Query(params);
  173. query.put("isDelete", 1);
  174. List<GoodsEntity> goodsList = goodsService.queryList(query);
  175. int total = goodsService.queryTotal(query);
  176. PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getPage());
  177. return R.ok().put("page", pageUtil);
  178. }
  179. /**
  180. * 商品从回收站恢复
  181. */
  182. @RequestMapping("/back")
  183. @RequiresPermissions("goods:back")
  184. public R back(@RequestBody Integer[] ids) {
  185. goodsService.back(ids);
  186. return R.ok();
  187. }
  188. /**
  189. * 总计
  190. */
  191. @RequestMapping("/queryTotal")
  192. public R queryTotal(@RequestParam Map<String, Object> params) {
  193. ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
  194. params.put("isDelete", 0);
  195. int sum = goodsService.queryTotal(params);
  196. return R.ok().put("goodsSum", sum);
  197. }
  198. /**
  199. * 上架
  200. */
  201. @RequestMapping("/enSale")
  202. public R enSale(@RequestBody Integer id) {
  203. goodsService.enSale(id);
  204. return R.ok();
  205. }
  206. /**
  207. * 上架
  208. */
  209. @RequestMapping("/enSaleBatch")
  210. public R enSaleBatch(@RequestBody Integer[] ids) {
  211. goodsService.enSaleBatch(ids);
  212. return R.ok();
  213. }
  214. /**
  215. * 下架
  216. */
  217. @RequestMapping("/unSale")
  218. public R unSale(@RequestBody Integer id) {
  219. goodsService.unSale(id);
  220. return R.ok();
  221. }
  222. /**
  223. * 下架
  224. */
  225. @RequestMapping("/unSaleBatch")
  226. public R unSaleBatch(@RequestBody Integer[] ids) {
  227. goodsService.unSaleBatch(ids);
  228. return R.ok();
  229. }
  230. /**
  231. * 上传文件
  232. */
  233. @RequestMapping("/upload")
  234. @ResponseBody
  235. public R upload(@RequestParam("file") MultipartFile file) {
  236. List<GoodsDto> goodsDtoList = new ArrayList<>();//商品信息
  237. try {
  238. Map<String, Object> beans = new HashMap<String, Object>();
  239. beans.put("GoodsDtoList", goodsDtoList);
  240. if (file.isEmpty()) {
  241. return R.error("文件不能为空!");
  242. }
  243. excelUtil.readExcel(JxlsXmlTemplateName.GOODS_DTO_LIST, beans, file.getInputStream());
  244. } catch (Exception e) {
  245. e.printStackTrace();
  246. return R.error("导入失败!");
  247. }
  248. goodsService.uploadExcel(goodsDtoList,Integer.parseInt(Dict.exportDataType.item_1.getItem()));
  249. //上传文件
  250. return R.ok();
  251. }
  252. /**
  253. * 上传文件(修改库存版)
  254. */
  255. @RequestMapping("/uploadByCover")
  256. @ResponseBody
  257. public R uploadByCover(@RequestParam("file") MultipartFile file) {
  258. List<GoodsDto> goodsDtoList = new ArrayList<>();//商品信息
  259. try {
  260. Map<String, Object> beans = new HashMap<String, Object>();
  261. beans.put("GoodsDtoList", goodsDtoList);
  262. if (file.isEmpty()) {
  263. return R.error("文件不能为空!");
  264. }
  265. excelUtil.readExcel(JxlsXmlTemplateName.GOODS_DTO_LIST, beans, file.getInputStream());
  266. } catch (Exception e) {
  267. e.printStackTrace();
  268. return R.error("导入失败!");
  269. }
  270. goodsService.uploadExcelByCover(goodsDtoList,Integer.parseInt(Dict.exportDataType.item_1.getItem()));
  271. //上传文件
  272. return R.ok();
  273. }
  274. /**
  275. * 上传文件
  276. */
  277. @RequestMapping("/generalGoodsUpload")
  278. @ResponseBody
  279. public R generalGoodsUpload(@RequestParam("file") MultipartFile file) {
  280. List<GoodsDto> generalGoodsDtoList = new ArrayList<>();//商品信息
  281. try {
  282. Map<String, Object> beans = new HashMap<String, Object>();
  283. beans.put("GeneralGoodsDtoList", generalGoodsDtoList);
  284. if (file.isEmpty()) {
  285. return R.error("文件不能为空!");
  286. }
  287. excelUtil.readExcel(JxlsXmlTemplateName.GENERAL_GOODS_DTO_LIST, beans, file.getInputStream());
  288. } catch (Exception e) {
  289. e.printStackTrace();
  290. return R.error("导入失败!");
  291. }
  292. goodsService.uploadExcel(generalGoodsDtoList,Integer.parseInt(Dict.exportDataType.item_2.getItem()));
  293. //上传文件
  294. return R.ok();
  295. }
  296. @RequestMapping("/generalGoodsImgUploadByZip")
  297. @ResponseBody
  298. public R batchAddImgByZip(@RequestParam("file") MultipartFile file) throws IOException {
  299. //上传文件
  300. batchAdd(file,2);
  301. return R.ok();
  302. }
  303. @RequestMapping("/generalGoodsImgUpload")
  304. @ResponseBody
  305. public R batchAddImg(@RequestParam("file") MultipartFile file) throws IOException {
  306. //上传文件
  307. batchAdd(file,1);
  308. return R.ok();
  309. }
  310. private Map<String, Object> batchAdd(MultipartFile file, int type) throws IOException {
  311. /*
  312. *创建临时文件夹
  313. * 解压文件
  314. */
  315. String fileName = file.getOriginalFilename();
  316. String path = "/data/project/img/";
  317. File dir = new File(path);
  318. dir.mkdirs();
  319. String filePath = "/data/project/img2/";
  320. File fileDir = new File(filePath);
  321. fileDir.mkdirs();
  322. File saveFile = new File(fileDir, fileName);//将压缩包解析到指定位置
  323. List<String>list = new ArrayList<>();
  324. try {
  325. file.transferTo(saveFile);
  326. String newFilePath = filePath + fileName;
  327. File zipFile = new File(newFilePath);
  328. unZipFiles(zipFile, path,list,type);//解压文件,获取文件路径
  329. System.out.println(JSON.toJSONString(list));
  330. } catch (Exception e) {
  331. e.printStackTrace();
  332. System.out.println("解压执行失败");
  333. throw e ;
  334. }
  335. //程序结束时,删除临时文件
  336. deleteFiles(filePath);//删除压缩包文件夹
  337. deleteFiles(path);//删除解压文件夹**
  338. Map<String, Object> jsonMap = new HashMap<String, Object>();
  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. // 判断源文件是否存在
  345. if (!srcFile.exists()) {
  346. throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
  347. }
  348. // 开始解压
  349. ZipFile zipFile = null;
  350. try {
  351. zipFile = new ZipFile(srcFile);
  352. Enumeration<?> entries = zipFile.getEntries();
  353. while (entries.hasMoreElements()) {
  354. ZipEntry entry = (ZipEntry) entries.nextElement();
  355. System.out.println("解压" + entry.getName());
  356. // 如果是文件夹,就创建个文件夹
  357. if (entry.isDirectory()) {
  358. String dirPath = destDirPath + "/" + entry.getName();
  359. File dir = new File(dirPath);
  360. dir.mkdirs();
  361. } else {
  362. // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
  363. File targetFile = new File(destDirPath + "/" + entry.getName());
  364. // 保证这个文件的父文件夹必须要存在
  365. if(!targetFile.getParentFile().exists()){
  366. }
  367. targetFile.createNewFile();
  368. // 将压缩文件内容写入到这个文件中
  369. InputStream is = zipFile.getInputStream(entry);
  370. FileOutputStream fos = new FileOutputStream(targetFile);
  371. int len;
  372. byte[] buf = new byte[1024];
  373. while ((len = is.read(buf)) != -1) {
  374. fos.write(buf, 0, len);
  375. }
  376. MultipartFile mulFileByPath = getMulFileByPath(destDirPath + "/" + entry.getName());
  377. //上传文件
  378. String url = FileManager.upload(mulFileByPath);
  379. list.add(url);
  380. if(type == 1){
  381. String sku = entry.getName().split("/")[1].split("\\.")[0];
  382. GoodsEntity goodsEntity = goodsService.queryBySku(sku);
  383. goodsEntity.setPrimaryPicUrl(url);
  384. goodsEntity.setListPicUrl(url);
  385. goodsService.updateForImgUrl(goodsEntity);
  386. }else if(type == 2){
  387. String barCode = entry.getName().split("/")[1];
  388. GoodsEntity goodsEntity = goodsService.queryByBarcode(barCode);
  389. goodsEntity.setPrimaryPicUrl(url);
  390. goodsEntity.setListPicUrl(url);
  391. goodsService.updateForImgUrl(goodsEntity);
  392. }
  393. //保存文件信息
  394. SysOssEntity ossEntity = new SysOssEntity();
  395. ossEntity.setUrl(url);
  396. ossEntity.setCreateDate(new Date());
  397. sysOssService.save(ossEntity);
  398. // 关流顺序,先打开的后关闭
  399. fos.close();
  400. is.close();
  401. }
  402. }
  403. long end = System.currentTimeMillis();
  404. System.out.println("解压完成,耗时:" + (end - start) +" ms");
  405. } catch (Exception e) {
  406. throw new RuntimeException("unzip error from ZipUtils", e);
  407. } finally {
  408. if(zipFile != null){
  409. try {
  410. zipFile.close();
  411. } catch (IOException e) {
  412. e.printStackTrace();
  413. }
  414. }
  415. }
  416. }
  417. private static MultipartFile getMulFileByPath(String picPath) {
  418. FileItem fileItem = createFileItem(picPath);
  419. MultipartFile mfile = new CommonsMultipartFile(fileItem);
  420. return mfile;
  421. }
  422. private static FileItem createFileItem(String filePath)
  423. {
  424. FileItemFactory factory = new DiskFileItemFactory(16, null);
  425. String textFieldName = "textField";
  426. int num = filePath.lastIndexOf(".");
  427. String extFile = filePath.substring(num);
  428. FileItem item = factory.createItem(textFieldName, "text/plain", true,
  429. "MyFileName" + extFile);
  430. File newfile = new File(filePath);
  431. int bytesRead = 0;
  432. byte[] buffer = new byte[8192];
  433. try
  434. {
  435. FileInputStream fis = new FileInputStream(newfile);
  436. OutputStream os = item.getOutputStream();
  437. while ((bytesRead = fis.read(buffer, 0, 8192))
  438. != -1)
  439. {
  440. os.write(buffer, 0, bytesRead);
  441. }
  442. os.close();
  443. fis.close();
  444. }
  445. catch (IOException e)
  446. {
  447. e.printStackTrace();
  448. }
  449. return item;
  450. }
  451. public void deleteFiles(String filePath) {
  452. File file = new File(filePath);
  453. if ((!file.exists()) || (!file.isDirectory())) {
  454. System.out.println("file not exist");
  455. return;
  456. }
  457. String[] tempList = file.list();
  458. File temp = null;
  459. for (int i = 0; i < tempList.length; i++) {
  460. if (filePath.endsWith(File.separator)) {
  461. temp = new File(filePath + tempList[i]);
  462. }
  463. else {
  464. temp = new File(filePath + File.separator + tempList[i]);
  465. }
  466. if (temp.isFile()) {
  467. temp.delete();
  468. }
  469. if (temp.isDirectory()) {
  470. this.deleteFiles(filePath + "/" + tempList[i]);
  471. }
  472. }
  473. // 空文件的删除
  474. file.delete();
  475. }
  476. /*@RequestMapping("/scannInfo")
  477. @RequiresPermissions("goods:scannInfo")
  478. public R scannInfo(@RequestParam Map<String, Object> params) {
  479. String goodsSn = (String)params.get("goodsSn");
  480. GoodsEntity goods = goodsService.queryObjectByGoodsSnAndBizType(goodsSn);
  481. if(goods == null) {
  482. return R.error("商品信息不存在");
  483. }
  484. List<OfflineCartEntity> cartEntityList = offlineCartService.offlineGoodsCart(goods);
  485. return R.ok().put("cartEntityList", cartEntityList);
  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","PLU","商品名称","商品英文名称","产品条码","货品业务类型","库存是否共享",
  693. "商品库存","日常价","成本价","是否上架","是否热销","录入日期","商品单位","商品税率","产品品牌","海关备案编号","计量单位","海关商品编码","国检规格型号",
  694. "原产国","海关申报要素","毛重(kg)","净重(kg)"};
  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("Plu",goodsEntity.getPlu());
  704. map.put("Name",goodsEntity.getName());
  705. map.put("EnglishName",goodsEntity.getEnglishName());
  706. String goodsBizType = goodsEntity.getGoodsBizType();
  707. Integer isStockShare = 0;
  708. if (goodsEntity.getIsStockShare()!=null){
  709. isStockShare = Integer.parseInt(goodsEntity.getIsStockShare());
  710. }
  711. map.put("ProdBarcode",goodsEntity.getProdBarcode());
  712. map.put("GoodsBizType",StringUtils.isEmpty(goodsBizType)?"":Dict.orderBizType.valueOf("item_"+goodsBizType).getItemName());
  713. map.put("IsStockShare",isStockShare==0?"否":"是");
  714. map.put("GoodsNumber",goodsEntity.getGoodsNumber());
  715. map.put("DailyPrice",goodsEntity.getDailyPrice());
  716. map.put("CostPrice",goodsEntity.getCostPrice());
  717. map.put("IsOnSale",goodsEntity.getIsOnSale()==0?"否":"是");
  718. map.put("IsHot",goodsEntity.getIsHot()==0?"否":"是");
  719. map.put("AddTime",goodsEntity.getAddTime());
  720. map.put("GoodsUnit",goodsEntity.getGoodsUnit());
  721. map.put("GoodsRate",goodsEntity.getGoodsRate());
  722. map.put("Brand",goodsEntity.getBrand());
  723. map.put("CusRecCode",goodsEntity.getCusRecCode());
  724. map.put("UnitCode",goodsEntity.getUnitCode());
  725. map.put("CusGoodsCode",goodsEntity.getCusGoodsCode());
  726. map.put("CiqProdModel",goodsEntity.getCiqProdModel());
  727. map.put("OriCntName",goodsEntity.getOriCntName());
  728. map.put("CusDeclEle",goodsEntity.getCusDeclEle());
  729. map.put("GrossWeight",goodsEntity.getGrossWeight());
  730. map.put("NetWeight",goodsEntity.getNetWeight());
  731. list.add(map);
  732. }
  733. }
  734. ee.addSheetByMap("所有商品信息", list, header);
  735. ee.export(response);
  736. return R.ok();
  737. }
  738. /**
  739. * 选择同步海关编号和商品税率
  740. * @return
  741. */
  742. @PostMapping("/syncGoodsRate")
  743. public R syncGoodsRate(@RequestBody Integer[] ids){
  744. // 先同步海关商品编码,再同步税率
  745. try {
  746. goodsService.syncOmsHsCodeGoode(Arrays.asList(ids));
  747. }catch (Exception e){
  748. e.printStackTrace();
  749. return R.error("同步海关商品编码失败,请联系管理员");
  750. }
  751. try {
  752. goodsService.syncGoodsRateGoode(Arrays.asList(ids));
  753. }catch (Exception e){
  754. e.printStackTrace();
  755. return R.error("同步商品税率失败,请联系管理员");
  756. }
  757. return R.ok();
  758. }
  759. /**
  760. * 全量同步海关编号和商品税率
  761. * @return
  762. */
  763. @PostMapping("/syncGoodsRateAll")
  764. public R syncGoodsRateAll(){
  765. // 先同步海关商品编码,再同步税率
  766. try {
  767. goodsService.syncOmsHsCodeTask();
  768. }catch (Exception e){
  769. e.printStackTrace();
  770. return R.error("同步海关商品编码失败,请联系管理员");
  771. }
  772. try {
  773. goodsService.syncGoodsRateTask();
  774. }catch (Exception e){
  775. e.printStackTrace();
  776. return R.error("同步商品税率失败,请联系管理员");
  777. }
  778. return R.ok();
  779. }
  780. /**
  781. * 校验系统中的商品价格是否有问题
  782. * @return
  783. */
  784. @RequestMapping("/checkGoodsPrice")
  785. public R checkGoodsPrice(){
  786. SysUserEntity user = ShiroUtils.getUserEntity();
  787. // 先同步海关商品编码,再同步税率
  788. try {
  789. goodsService.checkGoodsPrice(user);
  790. }catch (Exception e){
  791. e.printStackTrace();
  792. return R.error("校验失败,请联系管理员");
  793. }
  794. return R.ok("校验成功");
  795. }
  796. }