BackendGoodsController.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. package com.fuint.module.backendApi.controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.fuint.common.Constants;
  5. import com.fuint.common.dto.*;
  6. import com.fuint.common.enums.GoodsTypeEnum;
  7. import com.fuint.common.enums.StatusEnum;
  8. import com.fuint.common.enums.YesOrNoEnum;
  9. import com.fuint.common.service.*;
  10. import com.fuint.common.util.CommonUtil;
  11. import com.fuint.common.util.TokenUtil;
  12. import com.fuint.framework.exception.BusinessCheckException;
  13. import com.fuint.framework.pagination.PaginationRequest;
  14. import com.fuint.framework.pagination.PaginationResponse;
  15. import com.fuint.framework.web.BaseController;
  16. import com.fuint.framework.web.ResponseObject;
  17. import com.fuint.repository.mapper.MtGoodsSkuMapper;
  18. import com.fuint.repository.mapper.MtGoodsSpecMapper;
  19. import com.fuint.repository.model.*;
  20. import com.fuint.utils.StringUtil;
  21. import io.swagger.annotations.Api;
  22. import io.swagger.annotations.ApiOperation;
  23. import lombok.AllArgsConstructor;
  24. import org.springframework.beans.BeanUtils;
  25. import org.springframework.security.access.prepost.PreAuthorize;
  26. import org.springframework.web.bind.annotation.*;
  27. import javax.servlet.http.HttpServletRequest;
  28. import java.lang.reflect.InvocationTargetException;
  29. import java.math.BigDecimal;
  30. import java.util.*;
  31. /**
  32. * 商品管理controller
  33. *
  34. * Created by FSQ
  35. * CopyRight https://www.fuint.cn
  36. */
  37. @Api(tags="管理端-商品相关接口")
  38. @RestController
  39. @AllArgsConstructor
  40. @RequestMapping(value = "/backendApi/goods/goods")
  41. public class BackendGoodsController extends BaseController {
  42. private MtGoodsSpecMapper mtGoodsSpecMapper;
  43. private MtGoodsSkuMapper mtGoodsSkuMapper;
  44. /**
  45. * 商品服务接口
  46. */
  47. private GoodsService goodsService;
  48. /**
  49. * 商品分类服务接口
  50. */
  51. private CateService cateService;
  52. /**
  53. * 店铺服务接口
  54. */
  55. private StoreService storeService;
  56. /**
  57. * 后台账户服务接口
  58. */
  59. private AccountService accountService;
  60. /**
  61. * 系统设置服务接口
  62. * */
  63. private SettingService settingService;
  64. /**
  65. * 分页查询商品列表
  66. *
  67. * @param request
  68. * @return
  69. * @throws BusinessCheckException
  70. */
  71. @ApiOperation(value = "分页查询商品列表")
  72. @RequestMapping(value = "/list", method = RequestMethod.GET)
  73. @CrossOrigin
  74. @PreAuthorize("@pms.hasPermission('goods:goods:index')")
  75. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  76. String token = request.getHeader("Access-Token");
  77. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  78. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  79. String name = request.getParameter("name");
  80. String goodsNo = request.getParameter("goodsNo");
  81. String isSingleSpec = request.getParameter("isSingleSpec");
  82. String type = request.getParameter("type");
  83. String status = request.getParameter("status");
  84. String searchStoreId = request.getParameter("storeId");
  85. String stock = request.getParameter("stock");
  86. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  87. if (accountInfo == null) {
  88. return getFailureResult(1001, "请先登录");
  89. }
  90. TAccount account = accountService.getAccountInfoById(accountInfo.getId());
  91. Integer storeId = account.getStoreId() == null ? 0 : account.getStoreId();
  92. Integer merchantId = account.getMerchantId() == null ? 0 : account.getMerchantId();
  93. PaginationRequest paginationRequest = new PaginationRequest();
  94. paginationRequest.setCurrentPage(page);
  95. paginationRequest.setPageSize(pageSize);
  96. Map<String, Object> params = new HashMap<>();
  97. if (StringUtil.isNotEmpty(searchStoreId)) {
  98. params.put("storeId", searchStoreId);
  99. }
  100. if (merchantId > 0) {
  101. params.put("merchantId", merchantId);
  102. }
  103. if (storeId > 0) {
  104. params.put("storeId", storeId);
  105. }
  106. if (StringUtil.isNotEmpty(name)) {
  107. params.put("name", name);
  108. }
  109. if (StringUtil.isNotEmpty(type)) {
  110. params.put("type", type);
  111. }
  112. if (StringUtil.isNotEmpty(goodsNo)) {
  113. params.put("goodsNo", goodsNo);
  114. }
  115. if (StringUtil.isNotEmpty(isSingleSpec)) {
  116. params.put("isSingleSpec", isSingleSpec);
  117. }
  118. if (StringUtil.isNotEmpty(status)) {
  119. params.put("status", status);
  120. }
  121. if (StringUtil.isNotEmpty(stock)) {
  122. params.put("stock", stock);
  123. }
  124. paginationRequest.setSearchParams(params);
  125. PaginationResponse<GoodsDto> paginationResponse = goodsService.queryGoodsListByPagination(paginationRequest);
  126. // 商品类型列表
  127. GoodsTypeEnum[] typeListEnum = GoodsTypeEnum.values();
  128. List<ParamDto> typeList = new ArrayList<>();
  129. for (GoodsTypeEnum enumItem : typeListEnum) {
  130. ParamDto paramDto = new ParamDto();
  131. paramDto.setKey(enumItem.getKey());
  132. paramDto.setName(enumItem.getValue());
  133. paramDto.setValue(enumItem.getKey());
  134. typeList.add(paramDto);
  135. }
  136. Map<String, Object> paramsStore = new HashMap<>();
  137. paramsStore.put("status", StatusEnum.ENABLED.getKey());
  138. if (storeId != null && storeId > 0) {
  139. paramsStore.put("storeId", storeId.toString());
  140. }
  141. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  142. paramsStore.put("merchantId", accountInfo.getMerchantId());
  143. }
  144. List<MtStore> storeList = storeService.queryStoresByParams(paramsStore);
  145. Map<String, Object> result = new HashMap<>();
  146. result.put("paginationResponse", paginationResponse);
  147. result.put("typeList", typeList);
  148. result.put("storeList", storeList);
  149. return getSuccessResult(result);
  150. }
  151. /**
  152. * 删除商品
  153. *
  154. * @param request
  155. * @return
  156. */
  157. @ApiOperation(value = "删除商品")
  158. @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
  159. @CrossOrigin
  160. @PreAuthorize("@pms.hasPermission('goods:goods:edit')")
  161. public ResponseObject delete(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException {
  162. String token = request.getHeader("Access-Token");
  163. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  164. if (accountInfo == null) {
  165. return getFailureResult(1001, "请先登录");
  166. }
  167. String operator = accountInfo.getAccountName();
  168. goodsService.deleteGoods(id, operator);
  169. return getSuccessResult(true);
  170. }
  171. /**
  172. * 更新商品状态
  173. *
  174. * @return
  175. */
  176. @ApiOperation(value = "更新商品状态")
  177. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  178. @CrossOrigin
  179. @PreAuthorize("@pms.hasPermission('goods:goods:edit')")
  180. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  181. String token = request.getHeader("Access-Token");
  182. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  183. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  184. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  185. if (accountInfo == null) {
  186. return getFailureResult(1001, "请先登录");
  187. }
  188. MtGoods mtGoods = goodsService.queryGoodsById(id);
  189. if (mtGoods == null) {
  190. return getFailureResult(201, "该商品不存在");
  191. }
  192. String operator = accountInfo.getAccountName();
  193. MtGoods goodsInfo = new MtGoods();
  194. goodsInfo.setOperator(operator);
  195. goodsInfo.setId(id);
  196. goodsInfo.setStatus(status);
  197. goodsService.saveGoods(goodsInfo);
  198. return getSuccessResult(true);
  199. }
  200. /**
  201. * 获取商品详情
  202. *
  203. * @param request
  204. * @param goodsId
  205. * @return
  206. */
  207. @ApiOperation(value = "获取商品详情")
  208. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  209. @CrossOrigin
  210. @PreAuthorize("@pms.hasPermission('goods:goods:index')")
  211. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer goodsId) throws BusinessCheckException, InvocationTargetException, IllegalAccessException {
  212. String token = request.getHeader("Access-Token");
  213. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  214. if (accountInfo == null) {
  215. return getFailureResult(1001, "请先登录");
  216. }
  217. Integer storeId = accountInfo.getStoreId();
  218. GoodsDto goods = goodsService.getGoodsDetail(goodsId, false);
  219. Map<String, Object> result = new HashMap<>();
  220. result.put("goods", goods);
  221. List<String> images = new ArrayList<>();
  222. if (goods != null) {
  223. images = JSONArray.parseArray(goods.getImages(), String.class);
  224. }
  225. result.put("images", images);
  226. // 商品规格列表
  227. List<String> specNameArr = new ArrayList<>();
  228. List<Integer> specIdArr = new ArrayList<>();
  229. List<GoodsSpecItemDto> specArr = new ArrayList<>();
  230. // sku列表
  231. List<GoodsSkuDto> skuArr = new ArrayList<>();
  232. if (goods != null) {
  233. // 处理规格列表
  234. for (MtGoodsSpec mtGoodsSpec : goods.getSpecList()) {
  235. if (!specNameArr.contains(mtGoodsSpec.getName())) {
  236. specNameArr.add(mtGoodsSpec.getName());
  237. specIdArr.add(mtGoodsSpec.getId());
  238. }
  239. }
  240. for (int i = 0; i < specNameArr.size(); i++) {
  241. GoodsSpecItemDto item = new GoodsSpecItemDto();
  242. List<GoodsSpecChildDto> child = new ArrayList<>();
  243. Integer specId = specIdArr.get(i) == null ? (i+1) : specIdArr.get(i);
  244. String name = specNameArr.get(i);
  245. for (MtGoodsSpec mtGoodsSpec : goods.getSpecList()) {
  246. if (mtGoodsSpec.getName().equals(name)) {
  247. GoodsSpecChildDto goodsSpecChildDto = new GoodsSpecChildDto();
  248. goodsSpecChildDto.setId(mtGoodsSpec.getId());
  249. goodsSpecChildDto.setName(mtGoodsSpec.getValue());
  250. goodsSpecChildDto.setChecked(true);
  251. child.add(goodsSpecChildDto);
  252. }
  253. }
  254. item.setId(specId);
  255. item.setName(name);
  256. item.setChild(child);
  257. specArr.add(item);
  258. }
  259. // 处理sku列表
  260. for (MtGoodsSku mtGoodsSku : goods.getSkuList()) {
  261. GoodsSkuDto skuDto = new GoodsSkuDto();
  262. BeanUtils.copyProperties(mtGoodsSku, skuDto);
  263. List<MtGoodsSpec> specList = new ArrayList<>();
  264. String[] specIds = skuDto.getSpecIds().split("-");
  265. for (String specId : specIds) {
  266. MtGoodsSpec spec = goodsService.getSpecDetail(Integer.parseInt(specId));
  267. if (spec != null) {
  268. specList.add(spec);
  269. }
  270. }
  271. skuDto.setSpecList(specList);
  272. skuArr.add(skuDto);
  273. }
  274. }
  275. result.put("specData", specArr);
  276. result.put("skuData", skuArr);
  277. Map<String, Object> param = new HashMap<>();
  278. param.put("status", StatusEnum.ENABLED.getKey());
  279. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  280. param.put("merchantId", accountInfo.getMerchantId());
  281. }
  282. List<MtGoodsCate> cateList = cateService.queryCateListByParams(param);
  283. result.put("cateList", cateList);
  284. String imagePath = settingService.getUploadBasePath();
  285. result.put("imagePath", imagePath);
  286. Map<String, Object> paramsStore = new HashMap<>();
  287. paramsStore.put("status", StatusEnum.ENABLED.getKey());
  288. if (storeId != null && storeId > 0) {
  289. paramsStore.put("storeId", storeId.toString());
  290. }
  291. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  292. paramsStore.put("merchantId", accountInfo.getMerchantId());
  293. }
  294. List<MtStore> storeList = storeService.queryStoresByParams(paramsStore);
  295. // 商品类型列表
  296. GoodsTypeEnum[] typeListEnum = GoodsTypeEnum.values();
  297. List<ParamDto> typeList = new ArrayList<>();
  298. for (GoodsTypeEnum enumItem : typeListEnum) {
  299. ParamDto paramDto = new ParamDto();
  300. paramDto.setKey(enumItem.getKey());
  301. paramDto.setName(enumItem.getValue());
  302. paramDto.setValue(enumItem.getKey());
  303. typeList.add(paramDto);
  304. }
  305. result.put("typeList", typeList);
  306. result.put("storeId", storeId);
  307. result.put("storeList", storeList);
  308. return getSuccessResult(result);
  309. }
  310. /**
  311. * 保存商品信息
  312. *
  313. * @param request HttpServletRequest对象
  314. * @return
  315. */
  316. @ApiOperation(value = "保存商品信息")
  317. @RequestMapping(value = "/save", method = RequestMethod.POST)
  318. @CrossOrigin
  319. @PreAuthorize("@pms.hasPermission('goods:goods:add')")
  320. public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  321. String token = request.getHeader("Access-Token");
  322. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  323. if (accountInfo == null) {
  324. return getFailureResult(1001, "请先登录");
  325. }
  326. String goodsId = param.get("goodsId") == null ? "0" : param.get("goodsId").toString();
  327. if (StringUtil.isEmpty(goodsId)) {
  328. goodsId = "0";
  329. }
  330. String name = param.get("name") == null ? null : CommonUtil.replaceXSS(param.get("name").toString());
  331. String description = param.get("description") == null ? "" : param.get("description").toString();
  332. List<String> images = param.get("images") == null ? new ArrayList<>() : (List) param.get("images");
  333. String sort = param.get("sort") == null ? "" : param.get("sort").toString();
  334. String stock = param.get("stock") == null ? "" : param.get("stock").toString();
  335. String status = param.get("status") == null ? "" : param.get("status").toString();
  336. String goodsNo = param.get("goodsNo") == null ? "" : param.get("goodsNo").toString();
  337. String price = param.get("price") == null ? "" : param.get("price").toString();
  338. String linePrice = param.get("linePrice") == null ? "" : param.get("linePrice").toString();
  339. String weight = param.get("weight") == null ? "" : param.get("weight").toString();
  340. Integer initSale = param.get("initSale") == null ? 0 : Integer.parseInt(param.get("initSale").toString());
  341. String salePoint = param.get("salePoint") == null ? "" : param.get("salePoint").toString();
  342. String canUsePoint = param.get("canUsePoint") == null ? "" : param.get("canUsePoint").toString();
  343. String isMemberDiscount = param.get("isMemberDiscount") == null ? "" : param.get("isMemberDiscount").toString();
  344. String isSingleSpec = param.get("isSingleSpec") == null ? "" : param.get("isSingleSpec").toString();
  345. Integer cateId = (param.get("cateId") == null || StringUtil.isEmpty(param.get("cateId").toString())) ? 0 : Integer.parseInt(param.get("cateId").toString());
  346. Integer storeId = (param.get("storeId") == null || StringUtil.isEmpty(param.get("storeId").toString())) ? 0 : Integer.parseInt(param.get("storeId").toString());
  347. String type = param.get("type") == null ? "" : param.get("type").toString();
  348. String couponIds = param.get("couponIds") == null ? "" : param.get("couponIds").toString();
  349. String serviceTime = param.get("serviceTime") == null ? "0" : param.get("serviceTime").toString();
  350. List<LinkedHashMap> skuList = param.get("skuData") == null ? new ArrayList<>() : (List) param.get("skuData");
  351. List<LinkedHashMap> specList = param.get("specData") == null ? new ArrayList<>() : (List) param.get("specData");
  352. // 保存规格名称
  353. if (specList.size() > 0) {
  354. for (LinkedHashMap specDto : specList) {
  355. String specId = specDto.get("id") == null ? "" : specDto.get("id").toString();
  356. String specName = specDto.get("name") == null ? "" : specDto.get("name").toString();
  357. if (StringUtil.isNotEmpty(specId) && StringUtil.isNotEmpty(specName)) {
  358. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(Integer.parseInt(specId));
  359. String oldName = mtGoodsSpec.getName();
  360. Map<String, Object> paramSearch = new HashMap<>();
  361. paramSearch.put("goods_id", goodsId);
  362. paramSearch.put("name", oldName);
  363. List<MtGoodsSpec> dataList = mtGoodsSpecMapper.selectByMap(paramSearch);
  364. if (dataList.size() > 0 && !specName.equals(oldName)) {
  365. for (MtGoodsSpec mtSpec : dataList) {
  366. mtSpec.setName(specName);
  367. mtGoodsSpecMapper.updateById(mtSpec);
  368. }
  369. }
  370. }
  371. }
  372. }
  373. // 全部规格
  374. List<String> specIdList = new ArrayList<>();
  375. if (skuList.size() > 0) {
  376. for (LinkedHashMap skuDto : skuList) {
  377. specIdList.add(skuDto.get("specIds").toString());
  378. }
  379. }
  380. // 保存新规格或或单规格商品,要先删除旧的sku数据
  381. if (skuList.size() > 0 || isSingleSpec.equals(YesOrNoEnum.YES.getKey())) {
  382. Map<String, Object> param0 = new HashMap<>();
  383. param0.put("goods_id", goodsId);
  384. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param0);
  385. if (goodsSkuList.size() > 0) {
  386. for (MtGoodsSku mtGoodsSku : goodsSkuList) {
  387. if (!specIdList.contains(mtGoodsSku.getSpecIds())) {
  388. mtGoodsSkuMapper.deleteById(mtGoodsSku.getId());
  389. }
  390. }
  391. }
  392. }
  393. for (LinkedHashMap skuDto : skuList) {
  394. Map<String, Object> params = new HashMap<>();
  395. params.put("goods_id", goodsId);
  396. params.put("spec_ids", skuDto.get("specIds"));
  397. // 是否已存在
  398. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(params);
  399. MtGoodsSku sku = new MtGoodsSku();
  400. if (goodsSkuList.size() > 0) {
  401. sku = goodsSkuList.get(0);
  402. }
  403. sku.setSkuNo(skuDto.get("skuNo").toString());
  404. sku.setLogo(skuDto.get("logo").toString());
  405. sku.setGoodsId(Integer.parseInt(goodsId));
  406. sku.setSpecIds(skuDto.get("specIds").toString());
  407. String skuStock = skuDto.get("stock").toString();
  408. if (StringUtil.isEmpty(skuStock)) {
  409. skuStock = "0";
  410. }
  411. sku.setStock(Integer.parseInt(skuStock));
  412. BigDecimal skuPrice = new BigDecimal("0");
  413. if (skuDto.get("price") != null && StringUtil.isNotEmpty(skuDto.get("price").toString())) {
  414. skuPrice = new BigDecimal(skuDto.get("price").toString());
  415. }
  416. sku.setPrice(skuPrice);
  417. BigDecimal skuLinePrice = new BigDecimal("0");
  418. if (skuDto.get("linePrice") != null && StringUtil.isNotEmpty(skuDto.get("linePrice").toString())) {
  419. skuLinePrice = new BigDecimal(skuDto.get("linePrice").toString());
  420. }
  421. sku.setLinePrice(skuLinePrice);
  422. BigDecimal skuWeight = new BigDecimal("0");
  423. if (skuDto.get("weight") != null && StringUtil.isNotEmpty(skuDto.get("weight").toString())) {
  424. skuWeight = new BigDecimal(skuDto.get("weight").toString());
  425. }
  426. sku.setWeight(skuWeight);
  427. sku.setStatus(StatusEnum.ENABLED.getKey());
  428. if (sku.getId() != null && sku.getId() > 0) {
  429. mtGoodsSkuMapper.updateById(sku);
  430. } else {
  431. mtGoodsSkuMapper.insert(sku);
  432. }
  433. }
  434. // 多规格商品,价格默认取第一个sku
  435. if (skuList.size() > 0 && isSingleSpec.equals(YesOrNoEnum.NO.getKey())) {
  436. price = skuList.get(0).get("price").toString();
  437. linePrice = skuList.get(0).get("linePrice").toString();
  438. weight = skuList.get(0).get("weight").toString();
  439. // 库存等于所有sku库存相加
  440. Integer allStock = 0;
  441. for (LinkedHashMap item : skuList) {
  442. allStock = allStock + Integer.parseInt(item.get("stock").toString());
  443. }
  444. stock = allStock.toString();
  445. }
  446. Integer myStoreId = accountInfo.getStoreId();
  447. if (myStoreId > 0) {
  448. storeId = myStoreId;
  449. }
  450. MtGoods mtGoods = new MtGoods();
  451. mtGoods.setId(Integer.parseInt(goodsId));
  452. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  453. mtGoods.setMerchantId(accountInfo.getMerchantId());
  454. }
  455. if (StringUtil.isNotEmpty(type)) {
  456. mtGoods.setType(type);
  457. }
  458. mtGoods.setCateId(cateId);
  459. mtGoods.setName(name);
  460. mtGoods.setGoodsNo(goodsNo);
  461. if (StringUtil.isNotEmpty(serviceTime)) {
  462. mtGoods.setServiceTime(Integer.parseInt(serviceTime));
  463. }
  464. if (StringUtil.isNotEmpty(couponIds)) {
  465. mtGoods.setCouponIds(couponIds);
  466. }
  467. mtGoods.setIsSingleSpec(isSingleSpec);
  468. if (StringUtil.isNotEmpty(stock)) {
  469. mtGoods.setStock(Integer.parseInt(stock));
  470. }
  471. if (StringUtil.isNotEmpty(description)) {
  472. mtGoods.setDescription(description);
  473. }
  474. if (storeId != null && storeId > 0) {
  475. mtGoods.setStoreId(storeId);
  476. }
  477. if (images.size() > 0) {
  478. mtGoods.setLogo(images.get(0));
  479. }
  480. if (StringUtil.isNotEmpty(sort)) {
  481. mtGoods.setSort(Integer.parseInt(sort));
  482. }
  483. if (StringUtil.isNotEmpty(status)) {
  484. mtGoods.setStatus(status);
  485. }
  486. if (StringUtil.isNotEmpty(price)) {
  487. mtGoods.setPrice(new BigDecimal(price));
  488. }
  489. if (StringUtil.isNotEmpty(linePrice)) {
  490. mtGoods.setLinePrice(new BigDecimal(linePrice));
  491. }
  492. if (StringUtil.isNotEmpty(weight)) {
  493. mtGoods.setWeight(new BigDecimal(weight));
  494. }
  495. if (initSale > 0) {
  496. mtGoods.setInitSale(initSale);
  497. }
  498. if (StringUtil.isNotEmpty(salePoint)) {
  499. mtGoods.setSalePoint(salePoint);
  500. }
  501. if (StringUtil.isNotEmpty(canUsePoint)) {
  502. mtGoods.setCanUsePoint(canUsePoint);
  503. }
  504. if (StringUtil.isNotEmpty(isMemberDiscount)) {
  505. mtGoods.setIsMemberDiscount(isMemberDiscount);
  506. }
  507. if (images.size() > 0) {
  508. String imagesJson = JSONObject.toJSONString(images);
  509. mtGoods.setImages(imagesJson);
  510. }
  511. mtGoods.setOperator(accountInfo.getAccountName());
  512. MtGoods goodsInfo = goodsService.saveGoods(mtGoods);
  513. Map<String, Object> result = new HashMap();
  514. result.put("goodsInfo", goodsInfo);
  515. return getSuccessResult(result);
  516. }
  517. /**
  518. * 保存商品规格
  519. *
  520. * @param request HttpServletRequest对象
  521. */
  522. @ApiOperation(value = "保存商品规格")
  523. @RequestMapping(value = "/saveSpecName", method = RequestMethod.POST)
  524. @CrossOrigin
  525. @PreAuthorize("@pms.hasPermission('goods:goods:add')")
  526. public ResponseObject saveSpecName(HttpServletRequest request, @RequestBody Map<String, Object> param) {
  527. String token = request.getHeader("Access-Token");
  528. String goodsId = param.get("goodsId") == null ? "0" : param.get("goodsId").toString();
  529. String name = param.get("name") == null ? "" : param.get("name").toString();
  530. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  531. if (accountInfo == null) {
  532. return getFailureResult(1001, "请先登录");
  533. }
  534. if (StringUtil.isEmpty(goodsId)) {
  535. return getFailureResult(201, "请先保存商品基础信息");
  536. }
  537. Map<String, Object> paramSearch = new HashMap<>();
  538. paramSearch.put("goods_id", goodsId);
  539. paramSearch.put("name", name);
  540. List<MtGoodsSpec> dataList = mtGoodsSpecMapper.selectByMap(paramSearch);
  541. Integer targetId;
  542. MtGoodsSpec mtGoodsSpec;
  543. if (dataList.size() < 1) {
  544. mtGoodsSpec = new MtGoodsSpec();
  545. mtGoodsSpec.setGoodsId(Integer.parseInt(goodsId));
  546. mtGoodsSpec.setName(name);
  547. mtGoodsSpec.setValue("");
  548. mtGoodsSpec.setStatus(StatusEnum.ENABLED.getKey());
  549. mtGoodsSpecMapper.insert(mtGoodsSpec);
  550. } else {
  551. mtGoodsSpec = dataList.get(0);
  552. if (!mtGoodsSpec.getStatus().equals(StatusEnum.ENABLED.getKey())) {
  553. mtGoodsSpec.setStatus(StatusEnum.ENABLED.getKey());
  554. mtGoodsSpec.setValue("");
  555. mtGoodsSpecMapper.updateById(mtGoodsSpec);
  556. }
  557. }
  558. targetId = mtGoodsSpec.getId();
  559. Map<String, Object> outParams = new HashMap();
  560. outParams.put("id", targetId);
  561. return getSuccessResult(outParams);
  562. }
  563. /**
  564. * 保存商品规格值
  565. *
  566. * @param request HttpServletRequest对象
  567. * @return
  568. */
  569. @ApiOperation(value = "保存商品规格值")
  570. @RequestMapping(value = "/saveSpecValue", method = RequestMethod.POST)
  571. @CrossOrigin
  572. @PreAuthorize("@pms.hasPermission('goods:goods:add')")
  573. public ResponseObject saveSpecValue(HttpServletRequest request, @RequestBody Map<String, Object> param) {
  574. String token = request.getHeader("Access-Token");
  575. String specName = param.get("specName") == null ? "" : param.get("specName").toString();
  576. String goodsId = param.get("goodsId") == null ? "" : param.get("goodsId").toString();
  577. String value = param.get("value") == null ? "" : param.get("value").toString();
  578. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  579. if (accountInfo == null) {
  580. return getFailureResult(1001, "请先登录");
  581. }
  582. if (StringUtil.isEmpty(goodsId)) {
  583. return getFailureResult(201, "请先保存商品基础信息");
  584. }
  585. if (StringUtil.isEmpty(specName)) {
  586. return getFailureResult(201, "规格名称不能为空");
  587. }
  588. if ( StringUtil.isEmpty(value)) {
  589. return getFailureResult(201, "规格值不能为空");
  590. }
  591. Map<String, Object> paramSearch = new HashMap<>();
  592. paramSearch.put("goods_id", goodsId);
  593. paramSearch.put("name", specName);
  594. paramSearch.put("status", StatusEnum.ENABLED.getKey());
  595. List<MtGoodsSpec> dataList = mtGoodsSpecMapper.selectByMap(paramSearch);
  596. // 1.先修改空值
  597. Integer id = 0;
  598. if (dataList.size() > 0) {
  599. for (MtGoodsSpec mtGoodsSpec : dataList) {
  600. if (StringUtil.isEmpty(mtGoodsSpec.getValue())) {
  601. mtGoodsSpec.setValue(value);
  602. id = mtGoodsSpec.getId();
  603. mtGoodsSpecMapper.updateById(mtGoodsSpec);
  604. break;
  605. }
  606. }
  607. }
  608. // 2.没有空值再新增
  609. if (id < 1) {
  610. MtGoodsSpec mtGoodsSpec = new MtGoodsSpec();
  611. mtGoodsSpec.setGoodsId(Integer.parseInt(goodsId));
  612. mtGoodsSpec.setName(specName);
  613. mtGoodsSpec.setValue(value);
  614. mtGoodsSpec.setStatus(StatusEnum.ENABLED.getKey());
  615. mtGoodsSpecMapper.insert(mtGoodsSpec);
  616. id = mtGoodsSpec.getId();
  617. }
  618. // 3.更新已存在的sku的规格ID
  619. Map<String, Object> skuParams = new HashMap<>();
  620. skuParams.put("goods_id", goodsId);
  621. skuParams.put("status", StatusEnum.ENABLED.getKey());
  622. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(skuParams);
  623. List<MtGoodsSpec> dataCountList = mtGoodsSpecMapper.getGoodsSpecCountList(Integer.parseInt(goodsId));
  624. if (goodsSkuList.size() > 0) {
  625. for (MtGoodsSku mtGoodsSku : goodsSkuList) {
  626. String specIds = mtGoodsSku.getSpecIds();
  627. String[] specIdArr = specIds.split("-");
  628. if (specIdArr.length < dataCountList.size()) {
  629. specIds = specIds + "-" + id;
  630. mtGoodsSku.setSpecIds(specIds);
  631. mtGoodsSkuMapper.updateById(mtGoodsSku);
  632. }
  633. }
  634. }
  635. // 4.返回新增的ID
  636. Map<String, Object> result = new HashMap();
  637. result.put("id", id);
  638. return getSuccessResult(result);
  639. }
  640. /**
  641. * 删除商品规格
  642. *
  643. * @param request HttpServletRequest对象
  644. * @return
  645. */
  646. @ApiOperation(value = "删除商品规格")
  647. @RequestMapping(value = "/deleteSpec", method = RequestMethod.GET)
  648. @CrossOrigin
  649. @PreAuthorize("@pms.hasPermission('goods:goods:add')")
  650. public ResponseObject deleteSpec(HttpServletRequest request) {
  651. String specName = request.getParameter("specName") == null ? "" : request.getParameter("specName");
  652. String goodsId = request.getParameter("goodsId") == null ? "0" : request.getParameter("goodsId");
  653. if (StringUtil.isEmpty(specName) || StringUtil.isEmpty(goodsId)) {
  654. return getFailureResult(201, "请求参数错误");
  655. }
  656. Map<String, Object> param = new HashMap<>();
  657. param.put("goods_id", goodsId);
  658. param.put("name", specName);
  659. List<MtGoodsSpec> dataList = mtGoodsSpecMapper.selectByMap(param);
  660. if (dataList.size() > 0) {
  661. for (MtGoodsSpec mtGoodsSpec : dataList) {
  662. mtGoodsSpec.setStatus(StatusEnum.DISABLE.getKey());
  663. mtGoodsSpecMapper.updateById(mtGoodsSpec);
  664. }
  665. }
  666. return getSuccessResult(true);
  667. }
  668. /**
  669. * 删除商品规格值
  670. *
  671. * @param request HttpServletRequest对象
  672. */
  673. @ApiOperation(value = "删除商品规格值")
  674. @RequestMapping(value = "/deleteSpecValue", method = RequestMethod.GET)
  675. @CrossOrigin
  676. @PreAuthorize("@pms.hasPermission('goods:goods:add')")
  677. public ResponseObject deleteSpecValue(HttpServletRequest request) {
  678. Integer specId = request.getParameter("id") == null ? 0 : Integer.parseInt(request.getParameter("id"));
  679. if (specId < 1) {
  680. return getFailureResult(201, "请求参数错误");
  681. }
  682. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(specId);
  683. if (mtGoodsSpec == null) {
  684. return getFailureResult(201, "该规格值不存在");
  685. }
  686. mtGoodsSpec.setStatus(StatusEnum.DISABLE.getKey());
  687. mtGoodsSpecMapper.updateById(mtGoodsSpec);
  688. // 把对应的sku删掉
  689. Map<String, Object> param = new HashMap<>();
  690. param.put("goods_id", mtGoodsSpec.getGoodsId().toString());
  691. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param);
  692. for(MtGoodsSku mtGoodsSku : goodsSkuList) {
  693. String[] ss = mtGoodsSku.getSpecIds().split("-");
  694. for (int i = 0; i < ss.length; i++) {
  695. if (ss[i].equals(specId+"")) {
  696. mtGoodsSku.setStatus(StatusEnum.DISABLE.getKey());
  697. mtGoodsSkuMapper.updateById(mtGoodsSku);
  698. }
  699. }
  700. }
  701. return getSuccessResult(true);
  702. }
  703. /**
  704. * 获取选择商品列表
  705. *
  706. * @param request HttpServletRequest对象
  707. * @return
  708. */
  709. @ApiOperation(value = "获取选择商品列表")
  710. @RequestMapping(value = "/selectGoods", method = RequestMethod.POST)
  711. @CrossOrigin
  712. public ResponseObject selectGoods(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  713. String token = request.getHeader("Access-Token");
  714. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  715. if (accountInfo == null) {
  716. return getFailureResult(1001, "请先登录");
  717. }
  718. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  719. params.put("merchantId", accountInfo.getMerchantId());
  720. }
  721. PaginationResponse<GoodsDto> paginationResponse = goodsService.selectGoodsList(params);
  722. String imagePath = settingService.getUploadBasePath();
  723. Map<String, Object> result = new HashMap();
  724. result.put("paginationResponse", paginationResponse);
  725. result.put("imagePath", imagePath);
  726. return getSuccessResult(result);
  727. }
  728. }