GoodsController.java 30 KB

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