GoodsServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. dataList.add(item);
  158. }
  159. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  160. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  161. PaginationResponse<GoodsDto> paginationResponse = new PaginationResponse(pageImpl, GoodsDto.class);
  162. paginationResponse.setTotalPages(pageHelper.getPages());
  163. paginationResponse.setTotalElements(pageHelper.getTotal());
  164. paginationResponse.setContent(dataList);
  165. return paginationResponse;
  166. }
  167. /**
  168. * 保存商品信息
  169. *
  170. * @param reqDto 商品参数
  171. * @throws BusinessCheckException
  172. * @return
  173. */
  174. @Override
  175. @Transactional(rollbackFor = Exception.class)
  176. @OperationServiceLog(description = "保存商品信息")
  177. public MtGoods saveGoods(MtGoods reqDto) throws BusinessCheckException {
  178. MtGoods mtGoods = new MtGoods();
  179. if (reqDto.getId() > 0) {
  180. mtGoods = queryGoodsById(reqDto.getId());
  181. reqDto.setMerchantId(mtGoods.getMerchantId());
  182. }
  183. if (reqDto.getMerchantId() != null) {
  184. mtGoods.setMerchantId(reqDto.getMerchantId() >= 0 ? reqDto.getMerchantId() : 0);
  185. }
  186. if (reqDto.getStoreId() != null) {
  187. mtGoods.setStoreId(reqDto.getStoreId() >= 0 ? reqDto.getStoreId() : 0);
  188. }
  189. Integer storeId = reqDto.getStoreId() == null ? 0 : reqDto.getStoreId();
  190. if (reqDto.getMerchantId() == null || reqDto.getMerchantId() <= 0) {
  191. MtStore mtStore = storeService.queryStoreById(storeId);
  192. if (mtStore != null) {
  193. mtGoods.setMerchantId(mtStore.getMerchantId());
  194. }
  195. }
  196. if (StringUtil.isNotEmpty(reqDto.getIsSingleSpec())) {
  197. mtGoods.setIsSingleSpec(reqDto.getIsSingleSpec());
  198. }
  199. if (reqDto.getId() <= 0 && StringUtil.isEmpty(reqDto.getIsSingleSpec())) {
  200. mtGoods.setIsSingleSpec(YesOrNoEnum.YES.getKey());
  201. }
  202. if (StringUtil.isNotEmpty(reqDto.getName())) {
  203. mtGoods.setName(reqDto.getName());
  204. }
  205. if (StringUtil.isNotEmpty(reqDto.getStatus())) {
  206. mtGoods.setStatus(reqDto.getStatus());
  207. }
  208. if (StringUtil.isNotEmpty(reqDto.getLogo())) {
  209. mtGoods.setLogo(reqDto.getLogo());
  210. }
  211. if (StringUtil.isNotEmpty(reqDto.getIsSingleSpec())) {
  212. mtGoods.setIsSingleSpec(reqDto.getIsSingleSpec());
  213. }
  214. if (StringUtil.isNotEmpty(reqDto.getDescription())) {
  215. mtGoods.setDescription(reqDto.getDescription());
  216. }
  217. if (StringUtil.isNotEmpty(reqDto.getOperator())) {
  218. mtGoods.setOperator(reqDto.getOperator());
  219. }
  220. if (StringUtil.isNotEmpty(reqDto.getType())) {
  221. mtGoods.setType(reqDto.getType());
  222. }
  223. if (reqDto.getCateId() != null && reqDto.getCateId() > 0) {
  224. mtGoods.setCateId(reqDto.getCateId());
  225. }
  226. if (reqDto.getServiceTime() != null && reqDto.getServiceTime() > 0) {
  227. mtGoods.setServiceTime(reqDto.getServiceTime());
  228. }
  229. if (StringUtil.isNotEmpty(reqDto.getGoodsNo())) {
  230. mtGoods.setGoodsNo(reqDto.getGoodsNo());
  231. }
  232. if (reqDto.getSort() != null) {
  233. mtGoods.setSort(reqDto.getSort());
  234. }
  235. if (reqDto.getId() == null && (mtGoods.getSort().equals("") || mtGoods.getSort() == null )) {
  236. mtGoods.setSort(0);
  237. }
  238. if (reqDto.getPrice() != null) {
  239. mtGoods.setPrice(reqDto.getPrice());
  240. }
  241. if (reqDto.getPrice() == null && reqDto.getId() <= 0) {
  242. mtGoods.setPrice(new BigDecimal("0.00"));
  243. }
  244. if (reqDto.getLinePrice() != null) {
  245. mtGoods.setLinePrice(reqDto.getLinePrice());
  246. }
  247. if (reqDto.getLinePrice() == null && reqDto.getId() <= 0) {
  248. mtGoods.setLinePrice(new BigDecimal("0.00"));
  249. }
  250. if (StringUtil.isNotEmpty(reqDto.getCouponIds())) {
  251. mtGoods.setCouponIds(reqDto.getCouponIds());
  252. }
  253. if (reqDto.getWeight() != null) {
  254. mtGoods.setWeight(reqDto.getWeight());
  255. }
  256. if (reqDto.getInitSale() != null) {
  257. mtGoods.setInitSale(reqDto.getInitSale());
  258. }
  259. if (reqDto.getStock() != null) {
  260. mtGoods.setStock(reqDto.getStock());
  261. }
  262. if (StringUtil.isNotEmpty(reqDto.getSalePoint())) {
  263. mtGoods.setSalePoint(reqDto.getSalePoint());
  264. }
  265. if (StringUtil.isEmpty(reqDto.getSalePoint()) && reqDto.getId() <= 0) {
  266. reqDto.setSalePoint("");
  267. }
  268. if (StringUtil.isNotEmpty(reqDto.getCanUsePoint())) {
  269. mtGoods.setCanUsePoint(reqDto.getCanUsePoint());
  270. }
  271. if (StringUtil.isNotEmpty(reqDto.getIsMemberDiscount())) {
  272. mtGoods.setIsMemberDiscount(reqDto.getIsMemberDiscount());
  273. }
  274. if (StringUtil.isNotEmpty(reqDto.getImages())) {
  275. mtGoods.setImages(reqDto.getImages());
  276. }
  277. if (!mtGoods.getType().equals(GoodsTypeEnum.COUPON.getKey())) {
  278. mtGoods.setCouponIds("");
  279. }
  280. if (mtGoods.getCouponIds() != null && StringUtil.isNotEmpty(mtGoods.getCouponIds())) {
  281. String couponIds[] = mtGoods.getCouponIds().split(",");
  282. if (couponIds.length > 0) {
  283. for (int i = 0; i < couponIds.length; i++) {
  284. MtCoupon mtCoupon = couponService.queryCouponById(Integer.parseInt(couponIds[i]));
  285. if (mtCoupon == null) {
  286. throw new BusinessCheckException("卡券ID等于“"+couponIds[i]+"”的虚拟卡券不存在.");
  287. }
  288. }
  289. }
  290. }
  291. mtGoods.setUpdateTime(new Date());
  292. if (reqDto.getId() == null || reqDto.getId() <= 0) {
  293. mtGoods.setCreateTime(new Date());
  294. this.save(mtGoods);
  295. } else {
  296. this.updateById(mtGoods);
  297. }
  298. return mtGoods;
  299. }
  300. /**
  301. * 根据ID获取商品信息
  302. *
  303. * @param id 商品ID
  304. * @throws BusinessCheckException
  305. * @return
  306. */
  307. @Override
  308. public MtGoods queryGoodsById(Integer id) {
  309. MtGoods mtGoods = mtGoodsMapper.selectById(id);
  310. if (mtGoods == null) {
  311. return null;
  312. }
  313. return mtGoods;
  314. }
  315. /**
  316. * 根据编码获取商品信息
  317. *
  318. * @param merchantId 商户ID
  319. * @param goodsNo 商品编码
  320. * @throws BusinessCheckException
  321. * @return
  322. */
  323. @Override
  324. public MtGoods queryGoodsByGoodsNo(Integer merchantId, String goodsNo) {
  325. return mtGoodsMapper.getByGoodsNo(merchantId, goodsNo);
  326. }
  327. /**
  328. * 根据条码获取sku信息
  329. *
  330. * @param skuNo skuNo
  331. * @throws BusinessCheckException
  332. * */
  333. @Override
  334. public MtGoodsSku getSkuInfoBySkuNo(String skuNo) {
  335. List<MtGoodsSku> mtGoodsSkuList = mtGoodsSkuMapper.getBySkuNo(skuNo);
  336. if (mtGoodsSkuList.size() > 0) {
  337. return mtGoodsSkuList.get(0);
  338. }
  339. return null;
  340. }
  341. /**
  342. * 根据ID获取商品详情
  343. *
  344. * @param id 商品ID
  345. * @throws BusinessCheckException
  346. */
  347. @Override
  348. public GoodsDto getGoodsDetail(Integer id, boolean getDeleteSpec) {
  349. if (id == null || id < 1) {
  350. return null;
  351. }
  352. MtGoods mtGoods = mtGoodsMapper.selectById(id);
  353. GoodsDto goodsInfo = new GoodsDto();
  354. if (mtGoods != null) {
  355. try {
  356. BeanUtils.copyProperties(mtGoods, goodsInfo);
  357. } catch (Exception e) {
  358. goodsInfo.setId(mtGoods.getId());
  359. goodsInfo.setType(mtGoods.getType());
  360. goodsInfo.setStoreId(mtGoods.getStoreId());
  361. goodsInfo.setName(mtGoods.getName());
  362. goodsInfo.setCateId(mtGoods.getCateId());
  363. goodsInfo.setGoodsNo(mtGoods.getGoodsNo());
  364. goodsInfo.setIsSingleSpec(mtGoods.getIsSingleSpec());
  365. goodsInfo.setLogo(mtGoods.getLogo());
  366. goodsInfo.setImages(mtGoods.getImages());
  367. goodsInfo.setStatus(mtGoods.getStatus());
  368. goodsInfo.setSort(mtGoods.getSort());
  369. goodsInfo.setPrice(mtGoods.getPrice());
  370. goodsInfo.setLinePrice(mtGoods.getLinePrice());
  371. goodsInfo.setServiceTime(mtGoods.getServiceTime());
  372. goodsInfo.setCouponIds(mtGoods.getCouponIds());
  373. }
  374. }
  375. String basePath = settingService.getUploadBasePath();
  376. if (StringUtil.isNotEmpty(goodsInfo.getLogo())) {
  377. goodsInfo.setLogo(basePath + goodsInfo.getLogo());
  378. }
  379. // 规格列表
  380. Map<String, Object> param = new HashMap<>();
  381. param.put("goods_id", id.toString());
  382. if (getDeleteSpec == false) {
  383. param.put("status", StatusEnum.ENABLED.getKey());
  384. }
  385. List<MtGoodsSpec> goodsSpecList = mtGoodsSpecMapper.selectByMap(param);
  386. goodsInfo.setSpecList(goodsSpecList);
  387. // sku列表
  388. if (goodsInfo.getIsSingleSpec().equals(YesOrNoEnum.NO.getKey())) {
  389. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param);
  390. goodsInfo.setSkuList(goodsSkuList);
  391. // 多规格商品的价格、库存数量
  392. if (goodsSkuList.size() > 0) {
  393. goodsInfo.setPrice(goodsSkuList.get(0).getPrice());
  394. goodsInfo.setLinePrice(goodsSkuList.get(0).getLinePrice());
  395. Integer stock = 0;
  396. for (MtGoodsSku mtGoodsSku : goodsSkuList) {
  397. stock = stock + mtGoodsSku.getStock();
  398. }
  399. goodsInfo.setStock(stock);
  400. } else {
  401. goodsInfo.setStock(0);
  402. }
  403. } else {
  404. goodsInfo.setSkuList(new ArrayList<>());
  405. }
  406. return goodsInfo;
  407. }
  408. /**
  409. * 根据ID删除商品信息
  410. *
  411. * @param id ID
  412. * @param operator 操作人
  413. * @throws BusinessCheckException
  414. * @return
  415. */
  416. @Override
  417. @OperationServiceLog(description = "删除商品信息")
  418. @Transactional(rollbackFor = Exception.class)
  419. public void deleteGoods(Integer id, String operator) throws BusinessCheckException {
  420. MtGoods cateInfo = queryGoodsById(id);
  421. if (null == cateInfo) {
  422. throw new BusinessCheckException("该商品不存在");
  423. }
  424. cateInfo.setStatus(StatusEnum.DISABLE.getKey());
  425. cateInfo.setUpdateTime(new Date());
  426. mtGoodsMapper.updateById(cateInfo);
  427. }
  428. /**
  429. * 获取店铺的商品列表
  430. *
  431. * @param storeId 店铺ID
  432. * @param keyword 关键字
  433. * @param cateId 分类ID
  434. * @param page 当前页码
  435. * @param pageSize 每页页数
  436. * @throws BusinessCheckException
  437. * @return
  438. * */
  439. @Override
  440. public Map<String, Object> getStoreGoodsList(Integer storeId, String keyword, Integer cateId, Integer page, Integer pageSize) throws BusinessCheckException {
  441. MtStore mtStore = storeService.queryStoreById(storeId);
  442. if (mtStore == null) {
  443. Map<String, Object> result = new HashMap<>();
  444. result.put("goodsList", new ArrayList<>());
  445. result.put("total", 0);
  446. return result;
  447. }
  448. Integer merchantId = mtStore.getMerchantId() == null ? 0 : mtStore.getMerchantId();
  449. Page<MtGoods> pageHelper = PageHelper.startPage(page, pageSize);
  450. List<MtGoods> goodsList = new ArrayList<>();
  451. List<MtGoodsSku> skuList = new ArrayList<>();
  452. if (StringUtil.isNotEmpty(keyword)) {
  453. skuList = mtGoodsSkuMapper.getBySkuNo(keyword);
  454. }
  455. if (skuList != null && skuList.size() > 0) {
  456. MtGoods goods = mtGoodsMapper.selectById(skuList.get(0).getGoodsId());
  457. goodsList.add(goods);
  458. } else {
  459. pageHelper = PageHelper.startPage(page, pageSize);
  460. if (keyword != null && StringUtil.isNotEmpty(keyword)) {
  461. goodsList = mtGoodsMapper.searchStoreGoodsList(merchantId, storeId, keyword);
  462. } else {
  463. goodsList = mtGoodsMapper.getStoreGoodsList(merchantId, storeId, cateId);
  464. }
  465. }
  466. List<MtGoods> dataList = new ArrayList<>();
  467. if (goodsList.size() > 0) {
  468. for (MtGoods mtGoods : goodsList) {
  469. // 多规格商品价格、库存数量
  470. if (mtGoods != null && mtGoods.getIsSingleSpec().equals(YesOrNoEnum.NO.getKey())) {
  471. Map<String, Object> param = new HashMap<>();
  472. param.put("goods_id", mtGoods.getId().toString());
  473. param.put("status", StatusEnum.ENABLED.getKey());
  474. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param);
  475. if (goodsSkuList.size() > 0) {
  476. mtGoods.setPrice(goodsSkuList.get(0).getPrice());
  477. mtGoods.setLinePrice(goodsSkuList.get(0).getLinePrice());
  478. Integer stock = 0;
  479. for (MtGoodsSku mtGoodsSku : goodsSkuList) {
  480. stock = stock + mtGoodsSku.getStock();
  481. }
  482. mtGoods.setStock(stock);
  483. } else {
  484. mtGoods.setStock(0);
  485. }
  486. }
  487. dataList.add(mtGoods);
  488. }
  489. }
  490. Map<String, Object> data = new HashMap<>();
  491. data.put("goodsList", dataList);
  492. data.put("total", pageHelper.getTotal());
  493. return data;
  494. }
  495. /**
  496. * 通过SKU获取规格列表
  497. *
  498. * @param skuId skuID
  499. * @return
  500. * */
  501. @Override
  502. public List<GoodsSpecValueDto> getSpecListBySkuId(Integer skuId) {
  503. if (skuId < 0 || skuId == null) {
  504. return new ArrayList<>();
  505. }
  506. List<GoodsSpecValueDto> result = new ArrayList<>();
  507. MtGoodsSku goodsSku = mtGoodsSkuMapper.selectById(skuId);
  508. if (goodsSku == null) {
  509. return result;
  510. }
  511. String specIds = goodsSku.getSpecIds();
  512. String specIdArr[] = specIds.split("-");
  513. for (String specId : specIdArr) {
  514. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(Integer.parseInt(specId));
  515. GoodsSpecValueDto dto = new GoodsSpecValueDto();
  516. dto.setSpecValueId(mtGoodsSpec.getId());
  517. dto.setSpecName(mtGoodsSpec.getName());
  518. dto.setSpecValue(mtGoodsSpec.getValue());
  519. result.add(dto);
  520. }
  521. return result;
  522. }
  523. /**
  524. * 获取商品规格详情
  525. *
  526. * @param specId 规格ID
  527. * @return
  528. * */
  529. @Override
  530. public MtGoodsSpec getSpecDetail(Integer specId) {
  531. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(specId);
  532. return mtGoodsSpec;
  533. }
  534. /**
  535. * 更新已售数量
  536. *
  537. * @param goodsId 商品ID
  538. * @return
  539. * */
  540. @Override
  541. @Transactional(rollbackFor = Exception.class)
  542. public Boolean updateInitSale(Integer goodsId) {
  543. return mtGoodsMapper.updateInitSale(goodsId);
  544. }
  545. /**
  546. * 获取选择商品列表
  547. *
  548. * @param params 查询参数
  549. * @return
  550. */
  551. @Override
  552. public PaginationResponse<GoodsDto> selectGoodsList(Map<String, Object> params) throws BusinessCheckException {
  553. Integer page = params.get("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(params.get("page").toString());
  554. Integer pageSize = params.get("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(params.get("pageSize").toString());
  555. Integer merchantId = (params.get("merchantId") == null || StringUtil.isEmpty(params.get("merchantId").toString())) ? 0 : Integer.parseInt(params.get("merchantId").toString());
  556. Integer storeId = (params.get("storeId") == null || StringUtil.isEmpty(params.get("storeId").toString())) ? 0 : Integer.parseInt(params.get("storeId").toString());
  557. Integer cateId = (params.get("cateId") == null || StringUtil.isEmpty(params.get("cateId").toString())) ? 0 : Integer.parseInt(params.get("cateId").toString());
  558. String keyword = params.get("keyword") == null ? "" : params.get("keyword").toString();
  559. MtStore mtStore = storeService.queryStoreById(storeId);
  560. if (mtStore != null && mtStore.getMerchantId() != null) {
  561. merchantId = mtStore.getMerchantId();
  562. }
  563. Page<MtGoods> pageHelper = PageHelper.startPage(page, pageSize);
  564. List<GoodsDto> dataList = new ArrayList<>();
  565. List<GoodsBean> goodsList = mtGoodsMapper.selectGoodsList(merchantId, storeId, cateId, keyword);
  566. for (GoodsBean goodsBean : goodsList) {
  567. GoodsDto goodsDto = new GoodsDto();
  568. goodsDto.setId(goodsBean.getGoodsId());
  569. goodsDto.setLogo(goodsBean.getLogo());
  570. goodsDto.setName(goodsBean.getName());
  571. goodsDto.setGoodsNo(goodsBean.getGoodsNo());
  572. goodsDto.setStoreId(goodsBean.getStoreId());
  573. goodsDto.setPrice(goodsBean.getPrice());
  574. goodsDto.setCateId(goodsBean.getCateId());
  575. goodsDto.setStock(goodsBean.getStock());
  576. if (goodsBean.getSpecIds() != null) {
  577. Map<String, Object> param = new HashMap<>();
  578. param.put("GOODS_ID", goodsBean.getGoodsId());
  579. param.put("SPEC_IDS", goodsBean.getSpecIds());
  580. param.put("STATUS", StatusEnum.ENABLED.getKey());
  581. List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param);
  582. if (goodsSkuList != null && goodsSkuList.size() > 0) {
  583. goodsDto.setSkuId(goodsSkuList.get(0).getId());
  584. goodsDto.setPrice(goodsSkuList.get(0).getPrice());
  585. if (goodsSkuList.get(0).getLogo() != null && StringUtil.isNotEmpty(goodsSkuList.get(0).getLogo())) {
  586. goodsDto.setLogo(goodsSkuList.get(0).getLogo());
  587. }
  588. goodsDto.setStock(goodsSkuList.get(0).getStock());
  589. List<MtGoodsSpec> specList = new ArrayList<>();
  590. String[] specIds = goodsBean.getSpecIds().split("-");
  591. if (specIds.length > 0) {
  592. for (String specId : specIds) {
  593. MtGoodsSpec mtGoodsSpec = mtGoodsSpecMapper.selectById(Integer.parseInt(specId));
  594. if (mtGoodsSpec != null) {
  595. specList.add(mtGoodsSpec);
  596. }
  597. }
  598. }
  599. goodsDto.setSpecList(specList);
  600. }
  601. }
  602. dataList.add(goodsDto);
  603. }
  604. PageRequest pageRequest = PageRequest.of(page, pageSize);
  605. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  606. PaginationResponse<GoodsDto> paginationResponse = new PaginationResponse(pageImpl, GoodsDto.class);
  607. paginationResponse.setTotalPages(pageHelper.getPages());
  608. paginationResponse.setTotalElements(pageHelper.getTotal());
  609. paginationResponse.setContent(dataList);
  610. return paginationResponse;
  611. }
  612. /**
  613. * 获取商品销售排行榜
  614. *
  615. * @param merchantId 商户ID
  616. * @param storeId 店铺ID
  617. * @param startTime 开始时间
  618. * @param endTime 结束时间
  619. * @return
  620. * */
  621. @Override
  622. public List<GoodsTopDto> getGoodsSaleTopList(Integer merchantId, Integer storeId, Date startTime, Date endTime) {
  623. List<GoodsTopBean> dataList = mtGoodsMapper.getGoodsSaleTopList(merchantId, storeId, startTime, endTime);
  624. List<GoodsTopDto> goodsList = new ArrayList<>();
  625. if (dataList != null && dataList.size() > 0) {
  626. for (GoodsTopBean bean : dataList) {
  627. GoodsTopDto dto = new GoodsTopDto();
  628. BeanUtils.copyProperties(bean, dto);
  629. goodsList.add(dto);
  630. }
  631. }
  632. return goodsList;
  633. }
  634. }