123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- package com.fuint.common.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fuint.common.dto.MerchantDto;
- import com.fuint.common.enums.StatusEnum;
- import com.fuint.common.service.MerchantService;
- import com.fuint.common.service.StoreService;
- import com.fuint.common.util.CommonUtil;
- import com.fuint.framework.annoation.OperationServiceLog;
- import com.fuint.framework.exception.BusinessCheckException;
- import com.fuint.framework.pagination.PaginationRequest;
- import com.fuint.framework.pagination.PaginationResponse;
- import com.fuint.repository.mapper.MtGoodsMapper;
- import com.fuint.repository.mapper.MtMerchantMapper;
- import com.fuint.repository.mapper.MtStoreMapper;
- import com.fuint.repository.model.MtGoods;
- import com.fuint.repository.model.MtMerchant;
- import com.fuint.repository.model.MtStore;
- import com.fuint.utils.StringUtil;
- import com.github.pagehelper.Page;
- import com.github.pagehelper.PageHelper;
- import lombok.AllArgsConstructor;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.BeanUtils;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.*;
- /**
- * 商户业务实现类
- *
- * Created by FSQ
- * CopyRight https://www.fuint.cn
- */
- @Service
- @AllArgsConstructor(onConstructor_= {@Lazy})
- public class MerchantServiceImpl extends ServiceImpl<MtMerchantMapper, MtMerchant> implements MerchantService {
- private static final Logger logger = LoggerFactory.getLogger(MerchantServiceImpl.class);
- private MtMerchantMapper mtMerchantMapper;
- private MtStoreMapper mtStoreMapper;
- private MtGoodsMapper mtGoodsMapper;
- /**
- * 店铺服务接口
- * */
- private StoreService storeService;
- /**
- * 分页查询商户列表
- *
- * @param paginationRequest
- * @return
- */
- @Override
- public PaginationResponse<MerchantDto> queryMerchantListByPagination(PaginationRequest paginationRequest) {
- Page<MtMerchant> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
- LambdaQueryWrapper<MtMerchant> lambdaQueryWrapper = Wrappers.lambdaQuery();
- lambdaQueryWrapper.ne(MtMerchant::getStatus, StatusEnum.DISABLE.getKey());
- String name = paginationRequest.getSearchParams().get("name") == null ? "" : paginationRequest.getSearchParams().get("name").toString();
- if (StringUtils.isNotBlank(name)) {
- lambdaQueryWrapper.like(MtMerchant::getName, name);
- }
- String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
- if (StringUtils.isNotBlank(status)) {
- lambdaQueryWrapper.eq(MtMerchant::getStatus, status);
- }
- String id = paginationRequest.getSearchParams().get("id") == null ? "" : paginationRequest.getSearchParams().get("id").toString();
- if (StringUtils.isNotBlank(id)) {
- lambdaQueryWrapper.eq(MtMerchant::getId, id);
- }
- lambdaQueryWrapper.orderByAsc(MtMerchant::getStatus).orderByDesc(MtMerchant::getId);
- List<MtMerchant> merchantList = mtMerchantMapper.selectList(lambdaQueryWrapper);
- List<MerchantDto> dataList = new ArrayList<>();
- if (merchantList != null && merchantList.size() > 0) {
- for (MtMerchant mtMerchant : merchantList) {
- MerchantDto merchantDto = new MerchantDto();
- BeanUtils.copyProperties(mtMerchant, merchantDto);
- merchantDto.setPhone(CommonUtil.hidePhone(mtMerchant.getPhone()));
- dataList.add(merchantDto);
- }
- }
- PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
- PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
- PaginationResponse<MerchantDto> paginationResponse = new PaginationResponse(pageImpl, MtMerchant.class);
- paginationResponse.setTotalPages(pageHelper.getPages());
- paginationResponse.setTotalElements(pageHelper.getTotal());
- paginationResponse.setContent(dataList);
- return paginationResponse;
- }
- /**
- * 保存商户信息
- *
- * @param merchant 商户信息
- * @throws BusinessCheckException
- * @return
- */
- @Override
- @Transactional
- @OperationServiceLog(description = "保存商户信息")
- public MtMerchant saveMerchant(MtMerchant merchant) {
- MtMerchant mtMerchant = new MtMerchant();
- // 编辑商户
- if (merchant.getId() != null) {
- mtMerchant = queryMerchantById(merchant.getId());
- }
- if (merchant.getNo() == null || StringUtil.isEmpty(merchant.getNo())) {
- mtMerchant.setNo(CommonUtil.createMerchantNo());
- } else {
- mtMerchant.setNo(merchant.getNo());
- }
- if (merchant.getType() != null) {
- mtMerchant.setType(merchant.getType());
- }
- mtMerchant.setName(merchant.getName());
- mtMerchant.setLogo(merchant.getLogo());
- mtMerchant.setContact(merchant.getContact());
- mtMerchant.setOperator(merchant.getOperator());
- mtMerchant.setUpdateTime(new Date());
- if (merchant.getId() == null) {
- mtMerchant.setCreateTime(new Date());
- }
- mtMerchant.setWxAppId(merchant.getWxAppId());
- mtMerchant.setWxAppSecret(merchant.getWxAppSecret());
- mtMerchant.setWxOfficialAppId(merchant.getWxOfficialAppId());
- mtMerchant.setWxOfficialAppSecret(merchant.getWxOfficialAppSecret());
- mtMerchant.setDescription(merchant.getDescription());
- mtMerchant.setPhone(merchant.getPhone());
- mtMerchant.setAddress(merchant.getAddress());
- mtMerchant.setStatus(merchant.getStatus());
- if (mtMerchant.getStatus() == null) {
- mtMerchant.setStatus(StatusEnum.ENABLED.getKey());
- }
- if (mtMerchant.getId() == null || mtMerchant.getId() < 1) {
- this.save(mtMerchant);
- } else {
- mtMerchantMapper.updateById(mtMerchant);
- }
- return mtMerchant;
- }
- /**
- * 根据ID获取商户信息
- *
- * @param id 商户ID
- * @throws BusinessCheckException
- * @return
- */
- @Override
- public MtMerchant queryMerchantById(Integer id) {
- if (id == null || id < 1) {
- return null;
- }
- return mtMerchantMapper.selectById(id);
- }
- /**
- * 根据名称获取商户信息
- *
- * @param name 商户名称
- * @throws BusinessCheckException
- * @return
- */
- @Override
- public MtMerchant queryMerchantByName(String name) {
- return mtMerchantMapper.queryMerchantByName(name);
- }
- /**
- * 根据商户号获取商户信息
- *
- * @param merchantNo 商户号
- * @return
- */
- @Override
- public MtMerchant queryMerchantByNo(String merchantNo) {
- return mtMerchantMapper.queryMerchantByNo(merchantNo);
- }
- /**
- * 根据商户号获取商户ID
- *
- * @param merchantNo 商户号
- * @return
- */
- @Override
- public Integer getMerchantId(String merchantNo) {
- if (merchantNo == null || StringUtil.isEmpty(merchantNo)) {
- return 0;
- }
- MtMerchant mtMerchant = queryMerchantByNo(merchantNo);
- if (mtMerchant != null) {
- return mtMerchant.getId();
- } else {
- return 0;
- }
- }
- /**
- * 更新商户状态
- *
- * @param id 商户ID
- * @param operator 操作人
- * @param status 状态
- * @throws BusinessCheckException
- * @return
- */
- @Override
- @Transactional
- @OperationServiceLog(description = "修改商户状态")
- public void updateStatus(Integer id, String operator, String status) throws BusinessCheckException {
- MtMerchant mtMerchant = queryMerchantById(id);
- if (null == mtMerchant) {
- throw new BusinessCheckException("该商户不存在.");
- }
- // 如果是删除,检查是否有商品等数据
- if (status.equals(StatusEnum.DISABLE.getKey())) {
- // 删除店铺
- storeService.deleteStoreByMerchant(id);
- // 删除商品
- Map<String, Object> params = new HashMap<>();
- params.put("status", StatusEnum.ENABLED.getKey());
- params.put("merchant_id", id);
- List<MtGoods> goodsList = mtGoodsMapper.selectByMap(params);
- if (goodsList != null && goodsList.size() > 0) {
- logger.info("删除商户,连同商品一起删除", mtMerchant.getId());
- mtGoodsMapper.removeMerchantGoods(mtMerchant.getId());
- }
- }
- mtMerchant.setStatus(status);
- mtMerchant.setUpdateTime(new Date());
- mtMerchant.setOperator(operator);
- mtMerchantMapper.updateById(mtMerchant);
- }
- /**
- * 根据条件查询商户列表
- *
- * @param params 查询参数
- * @return
- * */
- @Override
- public List<MtMerchant> queryMerchantByParams(Map<String, Object> params) {
- LambdaQueryWrapper<MtMerchant> lambdaQueryWrapper = Wrappers.lambdaQuery();
- lambdaQueryWrapper.ne(MtMerchant::getStatus, StatusEnum.DISABLE.getKey());
- String merchantId = params.get("merchantId") == null ? "" : params.get("merchantId").toString();
- if (StringUtils.isNotBlank(merchantId)) {
- lambdaQueryWrapper.eq(MtMerchant::getId, merchantId);
- }
- String storeId = params.get("storeId") == null ? "" : params.get("storeId").toString();
- if (StringUtils.isNotBlank(storeId) && StringUtil.isEmpty(merchantId)) {
- MtStore mtStore = mtStoreMapper.selectById(storeId);
- if (mtStore != null && mtStore.getMerchantId() > 0) {
- lambdaQueryWrapper.eq(MtMerchant::getId, mtStore.getMerchantId());
- }
- }
- String name = params.get("name") == null ? "" : params.get("name").toString();
- if (StringUtils.isNotBlank(name)) {
- lambdaQueryWrapper.like(MtMerchant::getName, name);
- }
- String status = params.get("status") == null ? "" : params.get("status").toString();
- if (StringUtils.isNotBlank(status)) {
- lambdaQueryWrapper.eq(MtMerchant::getStatus, status);
- }
- lambdaQueryWrapper.orderByAsc(MtMerchant::getStatus).orderByDesc(MtMerchant::getId);
- return mtMerchantMapper.selectList(lambdaQueryWrapper);
- }
- /**
- * 查询我的商户列表
- *
- * @param merchantId 商户ID
- * @param storeId 店铺ID
- * @param status 状态
- * @return
- * */
- @Override
- public List<MtMerchant> getMyMerchantList(Integer merchantId, Integer storeId, String status) {
- Map<String, Object> param = new HashMap<>();
- if (merchantId != null && merchantId > 0) {
- param.put("merchantId", merchantId);
- }
- if (storeId != null && storeId > 0) {
- param.put("storeId", storeId);
- }
- if (StringUtils.isNotBlank(status)) {
- param.put("status", status);
- }
- return queryMerchantByParams(param);
- }
- }
|