MerchantServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.dto.MerchantDto;
  6. import com.fuint.common.enums.StatusEnum;
  7. import com.fuint.common.service.MerchantService;
  8. import com.fuint.common.service.StoreService;
  9. import com.fuint.common.util.CommonUtil;
  10. import com.fuint.framework.annoation.OperationServiceLog;
  11. import com.fuint.framework.exception.BusinessCheckException;
  12. import com.fuint.framework.pagination.PaginationRequest;
  13. import com.fuint.framework.pagination.PaginationResponse;
  14. import com.fuint.repository.mapper.MtGoodsMapper;
  15. import com.fuint.repository.mapper.MtMerchantMapper;
  16. import com.fuint.repository.mapper.MtStoreMapper;
  17. import com.fuint.repository.model.MtGoods;
  18. import com.fuint.repository.model.MtMerchant;
  19. import com.fuint.repository.model.MtStore;
  20. import com.fuint.utils.StringUtil;
  21. import com.github.pagehelper.Page;
  22. import com.github.pagehelper.PageHelper;
  23. import lombok.AllArgsConstructor;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.beans.BeanUtils;
  28. import org.springframework.context.annotation.Lazy;
  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.util.*;
  34. /**
  35. * 商户业务实现类
  36. *
  37. * Created by FSQ
  38. * CopyRight https://www.fuint.cn
  39. */
  40. @Service
  41. @AllArgsConstructor(onConstructor_= {@Lazy})
  42. public class MerchantServiceImpl extends ServiceImpl<MtMerchantMapper, MtMerchant> implements MerchantService {
  43. private static final Logger logger = LoggerFactory.getLogger(MerchantServiceImpl.class);
  44. private MtMerchantMapper mtMerchantMapper;
  45. private MtStoreMapper mtStoreMapper;
  46. private MtGoodsMapper mtGoodsMapper;
  47. /**
  48. * 店铺服务接口
  49. * */
  50. private StoreService storeService;
  51. /**
  52. * 分页查询商户列表
  53. *
  54. * @param paginationRequest
  55. * @return
  56. */
  57. @Override
  58. public PaginationResponse<MerchantDto> queryMerchantListByPagination(PaginationRequest paginationRequest) {
  59. Page<MtMerchant> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  60. LambdaQueryWrapper<MtMerchant> lambdaQueryWrapper = Wrappers.lambdaQuery();
  61. lambdaQueryWrapper.ne(MtMerchant::getStatus, StatusEnum.DISABLE.getKey());
  62. String name = paginationRequest.getSearchParams().get("name") == null ? "" : paginationRequest.getSearchParams().get("name").toString();
  63. if (StringUtils.isNotBlank(name)) {
  64. lambdaQueryWrapper.like(MtMerchant::getName, name);
  65. }
  66. String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
  67. if (StringUtils.isNotBlank(status)) {
  68. lambdaQueryWrapper.eq(MtMerchant::getStatus, status);
  69. }
  70. String id = paginationRequest.getSearchParams().get("id") == null ? "" : paginationRequest.getSearchParams().get("id").toString();
  71. if (StringUtils.isNotBlank(id)) {
  72. lambdaQueryWrapper.eq(MtMerchant::getId, id);
  73. }
  74. lambdaQueryWrapper.orderByAsc(MtMerchant::getStatus).orderByDesc(MtMerchant::getId);
  75. List<MtMerchant> merchantList = mtMerchantMapper.selectList(lambdaQueryWrapper);
  76. List<MerchantDto> dataList = new ArrayList<>();
  77. if (merchantList != null && merchantList.size() > 0) {
  78. for (MtMerchant mtMerchant : merchantList) {
  79. MerchantDto merchantDto = new MerchantDto();
  80. BeanUtils.copyProperties(mtMerchant, merchantDto);
  81. merchantDto.setPhone(CommonUtil.hidePhone(mtMerchant.getPhone()));
  82. dataList.add(merchantDto);
  83. }
  84. }
  85. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  86. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  87. PaginationResponse<MerchantDto> paginationResponse = new PaginationResponse(pageImpl, MtMerchant.class);
  88. paginationResponse.setTotalPages(pageHelper.getPages());
  89. paginationResponse.setTotalElements(pageHelper.getTotal());
  90. paginationResponse.setContent(dataList);
  91. return paginationResponse;
  92. }
  93. /**
  94. * 保存商户信息
  95. *
  96. * @param merchant 商户信息
  97. * @throws BusinessCheckException
  98. * @return
  99. */
  100. @Override
  101. @Transactional
  102. @OperationServiceLog(description = "保存商户信息")
  103. public MtMerchant saveMerchant(MtMerchant merchant) {
  104. MtMerchant mtMerchant = new MtMerchant();
  105. // 编辑商户
  106. if (merchant.getId() != null) {
  107. mtMerchant = queryMerchantById(merchant.getId());
  108. }
  109. if (merchant.getNo() == null || StringUtil.isEmpty(merchant.getNo())) {
  110. mtMerchant.setNo(CommonUtil.createMerchantNo());
  111. } else {
  112. mtMerchant.setNo(merchant.getNo());
  113. }
  114. if (merchant.getType() != null) {
  115. mtMerchant.setType(merchant.getType());
  116. }
  117. mtMerchant.setName(merchant.getName());
  118. mtMerchant.setLogo(merchant.getLogo());
  119. mtMerchant.setContact(merchant.getContact());
  120. mtMerchant.setOperator(merchant.getOperator());
  121. mtMerchant.setUpdateTime(new Date());
  122. if (merchant.getId() == null) {
  123. mtMerchant.setCreateTime(new Date());
  124. }
  125. mtMerchant.setWxAppId(merchant.getWxAppId());
  126. mtMerchant.setWxAppSecret(merchant.getWxAppSecret());
  127. mtMerchant.setWxOfficialAppId(merchant.getWxOfficialAppId());
  128. mtMerchant.setWxOfficialAppSecret(merchant.getWxOfficialAppSecret());
  129. mtMerchant.setDescription(merchant.getDescription());
  130. mtMerchant.setPhone(merchant.getPhone());
  131. mtMerchant.setAddress(merchant.getAddress());
  132. mtMerchant.setStatus(merchant.getStatus());
  133. if (mtMerchant.getStatus() == null) {
  134. mtMerchant.setStatus(StatusEnum.ENABLED.getKey());
  135. }
  136. if (mtMerchant.getId() == null || mtMerchant.getId() < 1) {
  137. this.save(mtMerchant);
  138. } else {
  139. mtMerchantMapper.updateById(mtMerchant);
  140. }
  141. return mtMerchant;
  142. }
  143. /**
  144. * 根据ID获取商户信息
  145. *
  146. * @param id 商户ID
  147. * @throws BusinessCheckException
  148. * @return
  149. */
  150. @Override
  151. public MtMerchant queryMerchantById(Integer id) {
  152. if (id == null || id < 1) {
  153. return null;
  154. }
  155. return mtMerchantMapper.selectById(id);
  156. }
  157. /**
  158. * 根据名称获取商户信息
  159. *
  160. * @param name 商户名称
  161. * @throws BusinessCheckException
  162. * @return
  163. */
  164. @Override
  165. public MtMerchant queryMerchantByName(String name) {
  166. return mtMerchantMapper.queryMerchantByName(name);
  167. }
  168. /**
  169. * 根据商户号获取商户信息
  170. *
  171. * @param merchantNo 商户号
  172. * @return
  173. */
  174. @Override
  175. public MtMerchant queryMerchantByNo(String merchantNo) {
  176. return mtMerchantMapper.queryMerchantByNo(merchantNo);
  177. }
  178. /**
  179. * 根据商户号获取商户ID
  180. *
  181. * @param merchantNo 商户号
  182. * @return
  183. */
  184. @Override
  185. public Integer getMerchantId(String merchantNo) {
  186. if (merchantNo == null || StringUtil.isEmpty(merchantNo)) {
  187. return 0;
  188. }
  189. MtMerchant mtMerchant = queryMerchantByNo(merchantNo);
  190. if (mtMerchant != null) {
  191. return mtMerchant.getId();
  192. } else {
  193. return 0;
  194. }
  195. }
  196. /**
  197. * 更新商户状态
  198. *
  199. * @param id 商户ID
  200. * @param operator 操作人
  201. * @param status 状态
  202. * @throws BusinessCheckException
  203. * @return
  204. */
  205. @Override
  206. @Transactional
  207. @OperationServiceLog(description = "修改商户状态")
  208. public void updateStatus(Integer id, String operator, String status) throws BusinessCheckException {
  209. MtMerchant mtMerchant = queryMerchantById(id);
  210. if (null == mtMerchant) {
  211. throw new BusinessCheckException("该商户不存在.");
  212. }
  213. // 如果是删除,检查是否有商品等数据
  214. if (status.equals(StatusEnum.DISABLE.getKey())) {
  215. // 删除店铺
  216. storeService.deleteStoreByMerchant(id);
  217. // 删除商品
  218. Map<String, Object> params = new HashMap<>();
  219. params.put("status", StatusEnum.ENABLED.getKey());
  220. params.put("merchant_id", id);
  221. List<MtGoods> goodsList = mtGoodsMapper.selectByMap(params);
  222. if (goodsList != null && goodsList.size() > 0) {
  223. logger.info("删除商户,连同商品一起删除", mtMerchant.getId());
  224. mtGoodsMapper.removeMerchantGoods(mtMerchant.getId());
  225. }
  226. }
  227. mtMerchant.setStatus(status);
  228. mtMerchant.setUpdateTime(new Date());
  229. mtMerchant.setOperator(operator);
  230. mtMerchantMapper.updateById(mtMerchant);
  231. }
  232. /**
  233. * 根据条件查询商户列表
  234. *
  235. * @param params 查询参数
  236. * @return
  237. * */
  238. @Override
  239. public List<MtMerchant> queryMerchantByParams(Map<String, Object> params) {
  240. LambdaQueryWrapper<MtMerchant> lambdaQueryWrapper = Wrappers.lambdaQuery();
  241. lambdaQueryWrapper.ne(MtMerchant::getStatus, StatusEnum.DISABLE.getKey());
  242. String merchantId = params.get("merchantId") == null ? "" : params.get("merchantId").toString();
  243. if (StringUtils.isNotBlank(merchantId)) {
  244. lambdaQueryWrapper.eq(MtMerchant::getId, merchantId);
  245. }
  246. String storeId = params.get("storeId") == null ? "" : params.get("storeId").toString();
  247. if (StringUtils.isNotBlank(storeId) && StringUtil.isEmpty(merchantId)) {
  248. MtStore mtStore = mtStoreMapper.selectById(storeId);
  249. if (mtStore != null && mtStore.getMerchantId() > 0) {
  250. lambdaQueryWrapper.eq(MtMerchant::getId, mtStore.getMerchantId());
  251. }
  252. }
  253. String name = params.get("name") == null ? "" : params.get("name").toString();
  254. if (StringUtils.isNotBlank(name)) {
  255. lambdaQueryWrapper.like(MtMerchant::getName, name);
  256. }
  257. String status = params.get("status") == null ? "" : params.get("status").toString();
  258. if (StringUtils.isNotBlank(status)) {
  259. lambdaQueryWrapper.eq(MtMerchant::getStatus, status);
  260. }
  261. lambdaQueryWrapper.orderByAsc(MtMerchant::getStatus).orderByDesc(MtMerchant::getId);
  262. return mtMerchantMapper.selectList(lambdaQueryWrapper);
  263. }
  264. /**
  265. * 查询我的商户列表
  266. *
  267. * @param merchantId 商户ID
  268. * @param storeId 店铺ID
  269. * @param status 状态
  270. * @return
  271. * */
  272. @Override
  273. public List<MtMerchant> getMyMerchantList(Integer merchantId, Integer storeId, String status) {
  274. Map<String, Object> param = new HashMap<>();
  275. if (merchantId != null && merchantId > 0) {
  276. param.put("merchantId", merchantId);
  277. }
  278. if (storeId != null && storeId > 0) {
  279. param.put("storeId", storeId);
  280. }
  281. if (StringUtils.isNotBlank(status)) {
  282. param.put("status", status);
  283. }
  284. return queryMerchantByParams(param);
  285. }
  286. }