GoodsServiceImpl.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. package com.fuint.common.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.fuint.common.Constants;
  6. import com.fuint.common.dto.GoodsDto;
  7. import com.fuint.common.dto.GoodsSpecValueDto;
  8. import com.fuint.common.dto.GoodsTopDto;
  9. import com.fuint.common.enums.GoodsTypeEnum;
  10. import com.fuint.common.enums.StatusEnum;
  11. import com.fuint.common.enums.YesOrNoEnum;
  12. import com.fuint.common.service.*;
  13. import com.fuint.framework.annoation.OperationServiceLog;
  14. import com.fuint.framework.exception.BusinessCheckException;
  15. import com.fuint.framework.pagination.PaginationRequest;
  16. import com.fuint.framework.pagination.PaginationResponse;
  17. import com.fuint.repository.bean.GoodsBean;
  18. import com.fuint.repository.bean.GoodsTopBean;
  19. import com.fuint.repository.mapper.MtGoodsMapper;
  20. import com.fuint.repository.mapper.MtGoodsSkuMapper;
  21. import com.fuint.repository.mapper.MtGoodsSpecMapper;
  22. import com.fuint.repository.model.*;
  23. import com.fuint.utils.StringUtil;
  24. import com.github.pagehelper.Page;
  25. import com.github.pagehelper.PageHelper;
  26. import lombok.AllArgsConstructor;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.springframework.beans.BeanUtils;
  29. import org.springframework.data.domain.PageImpl;
  30. import org.springframework.data.domain.PageRequest;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.transaction.annotation.Transactional;
  33. import java.math.BigDecimal;
  34. import java.util.*;
  35. /**
  36. * 商品业务实现类
  37. *
  38. * Created by FSQ
  39. * CopyRight https://www.fuint.cn
  40. */
  41. @Service
  42. @AllArgsConstructor
  43. public class GoodsServiceImpl extends ServiceImpl<MtGoodsMapper, MtGoods> implements GoodsService {
  44. private MtGoodsMapper mtGoodsMapper;
  45. private MtGoodsSpecMapper mtGoodsSpecMapper;
  46. private MtGoodsSkuMapper mtGoodsSkuMapper;
  47. /**
  48. * 系统设置服务接口
  49. * */
  50. private SettingService settingService;
  51. /**
  52. * 商品分类服务接口
  53. * */
  54. private CateService cateService;
  55. /**
  56. * 店铺服务接口
  57. * */
  58. private StoreService storeService;
  59. /**
  60. * 卡券服务接口
  61. * */
  62. private CouponService couponService;
  63. /**
  64. * 分页查询商品列表
  65. *
  66. * @param paginationRequest
  67. * @return
  68. */
  69. @Override
  70. public PaginationResponse<GoodsDto> queryGoodsListByPagination(PaginationRequest paginationRequest) throws BusinessCheckException {
  71. Page<MtGoods> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  72. LambdaQueryWrapper<MtGoods> lambdaQueryWrapper = Wrappers.lambdaQuery();
  73. lambdaQueryWrapper.ne(MtGoods::getStatus, StatusEnum.DISABLE.getKey());
  74. String name = paginationRequest.getSearchParams().get("name") == null ? "" : paginationRequest.getSearchParams().get("name").toString();
  75. if (StringUtils.isNotBlank(name)) {
  76. lambdaQueryWrapper.like(MtGoods::getName, name);
  77. }
  78. String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
  79. if (StringUtils.isNotBlank(status)) {
  80. lambdaQueryWrapper.eq(MtGoods::getStatus, status);
  81. }
  82. String goodsNo = paginationRequest.getSearchParams().get("goodsNo") == null ? "" : paginationRequest.getSearchParams().get("goodsNo").toString();
  83. if (StringUtils.isNotBlank(goodsNo)) {
  84. lambdaQueryWrapper.eq(MtGoods::getGoodsNo, goodsNo);
  85. }
  86. String isSingleSpec = paginationRequest.getSearchParams().get("isSingleSpec") == null ? "" : paginationRequest.getSearchParams().get("isSingleSpec").toString();
  87. if (StringUtils.isNotBlank(isSingleSpec)) {
  88. lambdaQueryWrapper.eq(MtGoods::getIsSingleSpec, isSingleSpec);
  89. }
  90. String merchantId = paginationRequest.getSearchParams().get("merchantId") == null ? "" : paginationRequest.getSearchParams().get("merchantId").toString();
  91. if (StringUtils.isNotBlank(merchantId)) {
  92. lambdaQueryWrapper.eq(MtGoods::getMerchantId, merchantId);
  93. }
  94. String storeId = paginationRequest.getSearchParams().get("storeId") == null ? "" : paginationRequest.getSearchParams().get("storeId").toString();
  95. if (StringUtils.isNotBlank(storeId)) {
  96. lambdaQueryWrapper.and(wq -> wq
  97. .eq(MtGoods::getStoreId, 0)
  98. .or()
  99. .eq(MtGoods::getStoreId, storeId));
  100. }
  101. String type = paginationRequest.getSearchParams().get("type") == null ? "" : paginationRequest.getSearchParams().get("type").toString();
  102. if (StringUtils.isNotBlank(type)) {
  103. lambdaQueryWrapper.eq(MtGoods::getType, type);
  104. }
  105. String cateId = paginationRequest.getSearchParams().get("cateId") == null ? "" : paginationRequest.getSearchParams().get("cateId").toString();
  106. if (StringUtils.isNotBlank(cateId)) {
  107. lambdaQueryWrapper.eq(MtGoods::getCateId, cateId);
  108. }
  109. String hasStock = paginationRequest.getSearchParams().get("stock") == null ? "" : paginationRequest.getSearchParams().get("stock").toString();
  110. if (StringUtils.isNotBlank(hasStock)) {
  111. if (hasStock.equals(YesOrNoEnum.YES.getKey())) {
  112. lambdaQueryWrapper.gt(MtGoods::getStock, 0);
  113. } else {
  114. lambdaQueryWrapper.lt(MtGoods::getStock, 1);
  115. }
  116. }
  117. String hasPrice = paginationRequest.getSearchParams().get("hasPrice") == null ? "" : paginationRequest.getSearchParams().get("hasPrice").toString();
  118. if (StringUtils.isNotBlank(hasPrice)) {
  119. if (hasPrice.equals(YesOrNoEnum.YES.getKey())) {
  120. lambdaQueryWrapper.gt(MtGoods::getPrice, 0);
  121. }
  122. }
  123. lambdaQueryWrapper.orderByAsc(MtGoods::getSort);
  124. List<MtGoods> goodsList = mtGoodsMapper.selectList(lambdaQueryWrapper);
  125. List<GoodsDto> dataList = new ArrayList<>();
  126. String basePath = settingService.getUploadBasePath();
  127. for (MtGoods mtGoods : goodsList) {
  128. MtGoodsCate cateInfo = null;
  129. if (mtGoods.getCateId() != null) {
  130. cateInfo = cateService.queryCateById(mtGoods.getCateId());
  131. }
  132. GoodsDto item = new GoodsDto();
  133. item.setId(mtGoods.getId());
  134. item.setInitSale(mtGoods.getInitSale());
  135. if (StringUtil.isNotEmpty(mtGoods.getLogo())) {
  136. item.setLogo(basePath + mtGoods.getLogo());
  137. }
  138. item.setStoreId(mtGoods.getStoreId());
  139. if (mtGoods.getStoreId() != null) {
  140. MtStore storeInfo = storeService.queryStoreById(mtGoods.getStoreId());
  141. item.setStoreInfo(storeInfo);
  142. }
  143. item.setName(mtGoods.getName());
  144. item.setGoodsNo(mtGoods.getGoodsNo());
  145. item.setCateId(mtGoods.getCateId());
  146. item.setStock(mtGoods.getStock());
  147. item.setCateInfo(cateInfo);
  148. item.setType(mtGoods.getType());
  149. item.setPrice(mtGoods.getPrice());
  150. item.setLinePrice(mtGoods.getLinePrice());
  151. item.setSalePoint(mtGoods.getSalePoint());
  152. item.setDescription(mtGoods.getDescription());
  153. item.setCreateTime(mtGoods.getCreateTime());
  154. item.setUpdateTime(mtGoods.getUpdateTime());
  155. item.setStatus(mtGoods.getStatus());
  156. item.setOperator(mtGoods.getOperator());
  157. item.setWeight(mtGoods.getWeight());
  158. dataList.add(item);
  159. }
  160. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  161. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  162. PaginationResponse<GoodsDto> paginationResponse = new PaginationResponse(pageImpl, GoodsDto.class);
  163. paginationResponse.setTotalPages(pageHelper.getPages());
  164. paginationResponse.setTotalElements(pageHelper.getTotal());
  165. paginationResponse.setContent(dataList);
  166. return paginationResponse;
  167. }
  168. /**
  169. * 保存商品信息
  170. *
  171. * @param reqDto 商品参数
  172. * @throws BusinessCheckException
  173. * @return
  174. */
  175. @Override
  176. @Transactional(rollbackFor = Exception.class)
  177. @OperationServiceLog(description = "保存商品信息")
  178. public MtGoods saveGoods(MtGoods reqDto) throws BusinessCheckException {
  179. MtGoods mtGoods = new MtGoods();
  180. if (reqDto.getId() > 0) {
  181. mtGoods = queryGoodsById(reqDto.getId());
  182. reqDto.setMerchantId(mtGoods.getMerchantId());
  183. }
  184. if (reqDto.getMerchantId() != null) {
  185. mtGoods.setMerchantId(reqDto.getMerchantId() >= 0 ? reqDto.getMerchantId() : 0);
  186. }
  187. if (reqDto.getStoreId() != null) {
  188. mtGoods.setStoreId(reqDto.getStoreId() >= 0 ? reqDto.getStoreId() : 0);
  189. }
  190. Integer storeId = reqDto.getStoreId() == null ? 0 : reqDto.getStoreId();
  191. if (reqDto.getMerchantId() == null || reqDto.getMerchantId() <= 0) {
  192. MtStore mtStore = storeService.queryStoreById(storeId);
  193. if (mtStore != null) {
  194. mtGoods.setMerchantId(mtStore.getMerchantId());
  195. }
  196. }
  197. if (mtGoods.getId() == null && (mtGoods.getMerchantId() == null || mtGoods.getMerchantId() < 1)) {
  198. throw new BusinessCheckException("平台方帐号无法执行该操作,请使用商户帐号操作");
  199. }
  200. if (StringUtil.isNotEmpty(reqDto.getIsSingleSpec())) {
  201. mtGoods.setIsSingleSpec(reqDto.getIsSingleSpec());
  202. }
  203. if (reqDto.getId() <= 0 && StringUtil.isEmpty(reqDto.getIsSingleSpec())) {
  204. mtGoods.setIsSingleSpec(YesOrNoEnum.YES.getKey());
  205. }
  206. if (StringUtil.isNotEmpty(reqDto.getName())) {
  207. mtGoods.setName(reqDto.getName());
  208. }
  209. if (StringUtil.isNotEmpty(reqDto.getStatus())) {
  210. mtGoods.setStatus(reqDto.getStatus());
  211. }
  212. if (StringUtil.isNotEmpty(reqDto.getLogo())) {
  213. mtGoods.setLogo(reqDto.getLogo());
  214. }
  215. if (StringUtil.isNotEmpty(reqDto.getIsSingleSpec())) {
  216. mtGoods.setIsSingleSpec(reqDto.getIsSingleSpec());
  217. }
  218. if (StringUtil.isNotEmpty(reqDto.getDescription())) {
  219. mtGoods.setDescription(reqDto.getDescription());
  220. }
  221. if (StringUtil.isNotEmpty(reqDto.getOperator())) {
  222. mtGoods.setOperator(reqDto.getOperator());
  223. }
  224. if (StringUtil.isNotEmpty(reqDto.getType())) {
  225. mtGoods.setType(reqDto.getType());
  226. }
  227. if (reqDto.getCateId() != null && reqDto.getCateId() > 0) {
  228. mtGoods.setCateId(reqDto.getCateId());
  229. }
  230. if (reqDto.getServiceTime() != null && reqDto.getServiceTime() > 0) {
  231. mtGoods.setServiceTime(reqDto.getServiceTime());
  232. }
  233. if (StringUtil.isNotEmpty(reqDto.getGoodsNo())) {
  234. mtGoods.setGoodsNo(reqDto.getGoodsNo());
  235. }
  236. if (reqDto.getSort() != null) {
  237. mtGoods.setSort(reqDto.getSort());
  238. }
  239. if (reqDto.getId() == null && (mtGoods.getSort().equals("") || mtGoods.getSort() == null )) {
  240. mtGoods.setSort(0);
  241. }
  242. if (reqDto.getPrice() != null) {
  243. mtGoods.setPrice(reqDto.getPrice());
  244. }
  245. if (reqDto.getPrice() == null && reqDto.getId() <= 0) {
  246. mtGoods.setPrice(new BigDecimal("0.00"));
  247. }
  248. if (reqDto.getLinePrice() != null) {
  249. mtGoods.setLinePrice(reqDto.getLinePrice());
  250. }
  251. if (reqDto.getLinePrice() == null && reqDto.getId() <= 0) {
  252. mtGoods.setLinePrice(new BigDecimal("0.00"));
  253. }
  254. if (StringUtil.isNotEmpty(reqDto.getCouponIds())) {
  255. mtGoods.setCouponIds(reqDto.getCouponIds());
  256. }
  257. if (reqDto.getWeight() != null) {
  258. mtGoods.setWeight(reqDto.getWeight());
  259. }
  260. if (reqDto.getInitSale() != null) {
  261. mtGoods.setInitSale(reqDto.getInitSale());
  262. }
  263. if (reqDto.getStock() != null) {
  264. mtGoods.setStock(reqDto.getStock());
  265. }
  266. if (StringUtil.isNotEmpty(reqDto.getSalePoint())) {
  267. mtGoods.setSalePoint(reqDto.getSalePoint());
  268. }
  269. if (StringUtil.isEmpty(reqDto.getSalePoint()) && reqDto.getId() <= 0) {
  270. reqDto.setSalePoint("");
  271. }
  272. if (StringUtil.isNotEmpty(reqDto.getCanUsePoint())) {
  273. mtGoods.setCanUsePoint(reqDto.getCanUsePoint());
  274. }
  275. if (StringUtil.isNotEmpty(reqDto.getIsMemberDiscount())) {
  276. mtGoods.setIsMemberDiscount(reqDto.getIsMemberDiscount());
  277. }
  278. if (StringUtil.isNotEmpty(reqDto.getImages())) {
  279. mtGoods.setImages(reqDto.getImages());
  280. }
  281. if (!mtGoods.getType().equals(GoodsTypeEnum.COUPON.getKey())) {
  282. mtGoods.setCouponIds("");
  283. }
  284. if (mtGoods.getCouponIds() != null && StringUtil.isNotEmpty(mtGoods.getCouponIds())) {
  285. String couponIds[] = mtGoods.getCouponIds().split(",");
  286. if (couponIds.length > 0) {
  287. for (int i = 0; i < couponIds.length; i++) {
  288. MtCoupon mtCoupon = couponService.queryCouponById(Integer.parseInt(couponIds[i]));
  289. if (mtCoupon == null) {
  290. throw new BusinessCheckException("卡券ID等于“"+couponIds[i]+"”的虚拟卡券不存在.");
  291. }
  292. }
  293. }
  294. }
  295. mtGoods.setUpdateTime(new Date());
  296. if (reqDto.getId() == null || reqDto.getId() <= 0) {
  297. mtGoods.setCreateTime(new Date());
  298. this.save(mtGoods);
  299. } else {
  300. this.updateById(mtGoods);
  301. }
  302. return mtGoods;
  303. }
  304. /**
  305. * 根据ID获取商品信息
  306. *
  307. * @param id 商品ID
  308. * @throws BusinessCheckException
  309. * @return
  310. */
  311. @Override
  312. public MtGoods queryGoodsById(Integer id) {
  313. MtGoods mtGoods = mtGoodsMapper.selectById(id);
  314. if (mtGoods == null) {
  315. return null;
  316. }
  317. return mtGoods;
  318. }
  319. /**
  320. * 根据编码获取商品信息
  321. *
  322. * @param merchantId 商户ID
  323. * @param goodsNo 商品编码
  324. * @throws BusinessCheckException
  325. * @return
  326. */
  327. @Override
  328. public MtGoods queryGoodsByGoodsNo(Integer merchantId, String goodsNo) {
  329. return mtGoodsMapper.getByGoodsNo(merchantId, goodsNo);
  330. }
  331. /**
  332. * 根据条码获取sku信息
  333. *
  334. * @param skuNo skuNo
  335. * @throws BusinessCheckException
  336. * */
  337. @Override
  338. public MtGoodsSku getSkuInfoBySkuNo(String skuNo) {
  339. List<MtGoodsSku> mtGoodsSkuList = mtGoodsSkuMapper.getBySkuNo(skuNo);
  340. if (mtGoodsSkuList.size() > 0) {
  341. return mtGoodsSkuList.get(0);
  342. }
  343. return null;
  344. }
  345. /**
  346. * 根据ID获取商品详情
  347. *
  348. * @param id 商品ID
  349. * @throws BusinessCheckException
  350. */
  351. @Override
  352. public GoodsDto getGoodsDetail(Integer id, boolean getDeleteSpec) {
  353. if (id == null || id < 1) {
  354. return null;
  355. }
  356. MtGoods mtGoods = mtGoodsMapper.selectById(id);
  357. GoodsDto goodsInfo = new GoodsDto();
  358. if (mtGoods != null) {
  359. try {
  360. BeanUtils.copyProperties(mtGoods, goodsInfo);
  361. } catch (Exception e) {
  362. goodsInfo.setId(mtGoods.getId());
  363. goodsInfo.setType(mtGoods.getType());
  364. goodsInfo.setStoreId(mtGoods.getStoreId());
  365. goodsInfo.setName(mtGoods.getName());
  366. goodsInfo.setCateId(mtGoods.getCateId());
  367. goodsInfo.setGoodsNo(mtGoods.getGoodsNo());
  368. goodsInfo.setIsSingleSpec(mtGoods.getIsSingleSpec());
  369. goodsInfo.setLogo(mtGoods.getLogo());
  370. goodsInfo.setImages(mtGoods.getImages());
  371. goodsInfo.setStatus(mtGoods.getStatus());
  372. goodsInfo.setSort(mtGoods.getSort());
  373. goodsInfo.setPrice(mtGoods.getPrice());
  374. goodsInfo.setLinePrice(mtGoods.getLinePrice());
  375. goodsInfo.setServiceTime(mtGoods.getServiceTime());
  376. goodsInfo.setCouponIds(mtGoods.getCouponIds());
  377. }
  378. }
  379. String basePath = settingService.getUploadBasePath();
  380. if (StringUtil.isNotEmpty(goodsInfo.getLogo())) {
  381. goodsInfo.setLogo(basePath + goodsInfo.getLogo());
  382. }
  383. // 规格列表
  384. Map<String, Object> param = new HashMap<>();
  385. param.put("goods_id", id.toString());
  386. if (getDeleteSpec == false) {
  387. param.put("status", StatusEnum.ENABLED.getKey());
  388. }
  389. List<MtGoodsSpec> goodsSpecList = mtGoodsSpecMapper.selectByMap(param);
  390. goodsInfo.setSpecList(goodsSpecList);
  391. // sku列表
  392. if (goodsInfo.getIsSingleSpec().equals(YesOrNoEnum.NO.getKey())) {
  393. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param);
  394. goodsInfo.setSkuList(goodsSkuList);
  395. // 多规格商品的价格、库存数量
  396. if (goodsSkuList.size() > 0) {
  397. goodsInfo.setPrice(goodsSkuList.get(0).getPrice());
  398. goodsInfo.setLinePrice(goodsSkuList.get(0).getLinePrice());
  399. Integer stock = 0;
  400. for (MtGoodsSku mtGoodsSku : goodsSkuList) {
  401. stock = stock + mtGoodsSku.getStock();
  402. }
  403. goodsInfo.setStock(stock);
  404. } else {
  405. goodsInfo.setStock(0);
  406. }
  407. } else {
  408. goodsInfo.setSkuList(new ArrayList<>());
  409. }
  410. return goodsInfo;
  411. }
  412. /**
  413. * 根据ID删除商品信息
  414. *
  415. * @param id ID
  416. * @param operator 操作人
  417. * @throws BusinessCheckException
  418. * @return
  419. */
  420. @Override
  421. @OperationServiceLog(description = "删除商品信息")
  422. @Transactional(rollbackFor = Exception.class)
  423. public void deleteGoods(Integer id, String operator) throws BusinessCheckException {
  424. MtGoods cateInfo = queryGoodsById(id);
  425. if (null == cateInfo) {
  426. throw new BusinessCheckException("该商品不存在");
  427. }
  428. cateInfo.setStatus(StatusEnum.DISABLE.getKey());
  429. cateInfo.setUpdateTime(new Date());
  430. mtGoodsMapper.updateById(cateInfo);
  431. }
  432. /**
  433. * 获取店铺的商品列表
  434. *
  435. * @param storeId 店铺ID
  436. * @param keyword 关键字
  437. * @param cateId 分类ID
  438. * @param page 当前页码
  439. * @param pageSize 每页页数
  440. * @throws BusinessCheckException
  441. * @return
  442. * */
  443. @Override
  444. public Map<String, Object> getStoreGoodsList(Integer storeId, String keyword, Integer cateId, Integer page, Integer pageSize) throws BusinessCheckException {
  445. MtStore mtStore = storeService.queryStoreById(storeId);
  446. if (mtStore == null) {
  447. Map<String, Object> result = new HashMap<>();
  448. result.put("goodsList", new ArrayList<>());
  449. result.put("total", 0);
  450. return result;
  451. }
  452. Integer merchantId = mtStore.getMerchantId() == null ? 0 : mtStore.getMerchantId();
  453. Page<MtGoods> pageHelper = PageHelper.startPage(page, pageSize);
  454. List<MtGoods> goodsList = new ArrayList<>();
  455. List<MtGoodsSku> skuList = new ArrayList<>();
  456. if (StringUtil.isNotEmpty(keyword)) {
  457. skuList = mtGoodsSkuMapper.getBySkuNo(keyword);
  458. }
  459. if (skuList != null && skuList.size() > 0) {
  460. MtGoods goods = mtGoodsMapper.selectById(skuList.get(0).getGoodsId());
  461. goodsList.add(goods);
  462. } else {
  463. pageHelper = PageHelper.startPage(page, pageSize);
  464. if (keyword != null && StringUtil.isNotEmpty(keyword)) {
  465. goodsList = mtGoodsMapper.searchStoreGoodsList(merchantId, storeId, keyword);
  466. } else {
  467. goodsList = mtGoodsMapper.getStoreGoodsList(merchantId, storeId, cateId);
  468. }
  469. }
  470. List<MtGoods> dataList = new ArrayList<>();
  471. if (goodsList.size() > 0) {
  472. for (MtGoods mtGoods : goodsList) {
  473. // 多规格商品价格、库存数量
  474. if (mtGoods != null && mtGoods.getIsSingleSpec().equals(YesOrNoEnum.NO.getKey())) {
  475. Map<String, Object> param = new HashMap<>();
  476. param.put("goods_id", mtGoods.getId().toString());
  477. param.put("status", StatusEnum.ENABLED.getKey());
  478. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param);
  479. if (goodsSkuList.size() > 0) {
  480. mtGoods.setPrice(goodsSkuList.get(0).getPrice());
  481. mtGoods.setLinePrice(goodsSkuList.get(0).getLinePrice());
  482. Integer stock = 0;
  483. for (MtGoodsSku mtGoodsSku : goodsSkuList) {
  484. stock = stock + mtGoodsSku.getStock();
  485. }
  486. mtGoods.setStock(stock);
  487. } else {
  488. mtGoods.setStock(0);
  489. }
  490. }
  491. dataList.add(mtGoods);
  492. }
  493. }
  494. Map<String, Object> data = new HashMap<>();
  495. data.put("goodsList", dataList);
  496. data.put("total", pageHelper.getTotal());
  497. return data;
  498. }
  499. /**
  500. * 通过SKU获取规格列表
  501. *
  502. * @param skuId skuID
  503. * @return
  504. * */
  505. @Override
  506. public List<GoodsSpecValueDto> getSpecListBySkuId(Integer skuId) {
  507. if (skuId < 0 || skuId == null) {
  508. return new ArrayList<>();
  509. }
  510. List<GoodsSpecValueDto> result = new ArrayList<>();
  511. MtGoodsSku goodsSku = mtGoodsSkuMapper.selectById(skuId);
  512. if (goodsSku == null) {
  513. return result;
  514. }
  515. String specIds = goodsSku.getSpecIds();
  516. String specIdArr[] = specIds.split("-");
  517. for (String specId : specIdArr) {
  518. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(Integer.parseInt(specId));
  519. GoodsSpecValueDto dto = new GoodsSpecValueDto();
  520. dto.setSpecValueId(mtGoodsSpec.getId());
  521. dto.setSpecName(mtGoodsSpec.getName());
  522. dto.setSpecValue(mtGoodsSpec.getValue());
  523. result.add(dto);
  524. }
  525. return result;
  526. }
  527. /**
  528. * 获取商品规格详情
  529. *
  530. * @param specId 规格ID
  531. * @return
  532. * */
  533. @Override
  534. public MtGoodsSpec getSpecDetail(Integer specId) {
  535. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(specId);
  536. return mtGoodsSpec;
  537. }
  538. /**
  539. * 更新已售数量
  540. *
  541. * @param goodsId 商品ID
  542. * @return
  543. * */
  544. @Override
  545. @Transactional(rollbackFor = Exception.class)
  546. public Boolean updateInitSale(Integer goodsId) {
  547. return mtGoodsMapper.updateInitSale(goodsId);
  548. }
  549. /**
  550. * 获取选择商品列表
  551. *
  552. * @param params 查询参数
  553. * @return
  554. */
  555. @Override
  556. public PaginationResponse<GoodsDto> selectGoodsList(Map<String, Object> params) throws BusinessCheckException {
  557. Integer page = params.get("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(params.get("page").toString());
  558. Integer pageSize = params.get("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(params.get("pageSize").toString());
  559. Integer merchantId = (params.get("merchantId") == null || StringUtil.isEmpty(params.get("merchantId").toString())) ? 0 : Integer.parseInt(params.get("merchantId").toString());
  560. Integer storeId = (params.get("storeId") == null || StringUtil.isEmpty(params.get("storeId").toString())) ? 0 : Integer.parseInt(params.get("storeId").toString());
  561. Integer cateId = (params.get("cateId") == null || StringUtil.isEmpty(params.get("cateId").toString())) ? 0 : Integer.parseInt(params.get("cateId").toString());
  562. String keyword = params.get("keyword") == null ? "" : params.get("keyword").toString();
  563. MtStore mtStore = storeService.queryStoreById(storeId);
  564. if (mtStore != null && mtStore.getMerchantId() != null) {
  565. merchantId = mtStore.getMerchantId();
  566. }
  567. Page<MtGoods> pageHelper = PageHelper.startPage(page, pageSize);
  568. List<GoodsDto> dataList = new ArrayList<>();
  569. List<GoodsBean> goodsList = mtGoodsMapper.selectGoodsList(merchantId, storeId, cateId, keyword);
  570. for (GoodsBean goodsBean : goodsList) {
  571. GoodsDto goodsDto = new GoodsDto();
  572. goodsDto.setId(goodsBean.getGoodsId());
  573. goodsDto.setLogo(goodsBean.getLogo());
  574. goodsDto.setName(goodsBean.getName());
  575. goodsDto.setGoodsNo(goodsBean.getGoodsNo());
  576. goodsDto.setStoreId(goodsBean.getStoreId());
  577. goodsDto.setPrice(goodsBean.getPrice());
  578. goodsDto.setCateId(goodsBean.getCateId());
  579. goodsDto.setStock(goodsBean.getStock());
  580. if (goodsBean.getSpecIds() != null) {
  581. Map<String, Object> param = new HashMap<>();
  582. param.put("GOODS_ID", goodsBean.getGoodsId());
  583. param.put("SPEC_IDS", goodsBean.getSpecIds());
  584. param.put("STATUS", StatusEnum.ENABLED.getKey());
  585. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param);
  586. if (goodsSkuList != null && goodsSkuList.size() > 0) {
  587. goodsDto.setSkuId(goodsSkuList.get(0).getId());
  588. goodsDto.setPrice(goodsSkuList.get(0).getPrice());
  589. if (goodsSkuList.get(0).getLogo() != null && StringUtil.isNotEmpty(goodsSkuList.get(0).getLogo())) {
  590. goodsDto.setLogo(goodsSkuList.get(0).getLogo());
  591. }
  592. goodsDto.setStock(goodsSkuList.get(0).getStock());
  593. List<MtGoodsSpec> specList = new ArrayList<>();
  594. String[] specIds = goodsBean.getSpecIds().split("-");
  595. if (specIds.length > 0) {
  596. for (String specId : specIds) {
  597. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(Integer.parseInt(specId));
  598. if (mtGoodsSpec != null) {
  599. specList.add(mtGoodsSpec);
  600. }
  601. }
  602. }
  603. goodsDto.setSpecList(specList);
  604. }
  605. }
  606. dataList.add(goodsDto);
  607. }
  608. PageRequest pageRequest = PageRequest.of(page, pageSize);
  609. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  610. PaginationResponse<GoodsDto> paginationResponse = new PaginationResponse(pageImpl, GoodsDto.class);
  611. paginationResponse.setTotalPages(pageHelper.getPages());
  612. paginationResponse.setTotalElements(pageHelper.getTotal());
  613. paginationResponse.setContent(dataList);
  614. return paginationResponse;
  615. }
  616. /**
  617. * 获取商品销售排行榜
  618. *
  619. * @param merchantId 商户ID
  620. * @param storeId 店铺ID
  621. * @param startTime 开始时间
  622. * @param endTime 结束时间
  623. * @return
  624. * */
  625. @Override
  626. public List<GoodsTopDto> getGoodsSaleTopList(Integer merchantId, Integer storeId, Date startTime, Date endTime) {
  627. List<GoodsTopBean> dataList = mtGoodsMapper.getGoodsSaleTopList(merchantId, storeId, startTime, endTime);
  628. List<GoodsTopDto> goodsList = new ArrayList<>();
  629. if (dataList != null && dataList.size() > 0) {
  630. for (GoodsTopBean bean : dataList) {
  631. GoodsTopDto dto = new GoodsTopDto();
  632. BeanUtils.copyProperties(bean, dto);
  633. goodsList.add(dto);
  634. }
  635. }
  636. return goodsList;
  637. }
  638. }