BackendGoodsController.java 32 KB

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