MerchantService.java 15.8 KB
package cn.com.glsx.merchants.service;

import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.merchants.convert.ConvertRequest;
import cn.com.glsx.merchants.convert.ConvertResponse;
import cn.com.glsx.merchants.model.request.*;
import cn.com.glsx.merchants.model.request.MerchantBrandRequest;
import cn.com.glsx.merchants.model.response.*;
import cn.com.glsx.merchants.remote.MerchantRemote;
import cn.com.glsx.merchants.request.accounttype.AccountTypeSearchRequest;
import cn.com.glsx.merchants.request.merchant.*;
import cn.com.glsx.merchants.request.role.RoleSearchRequest;
import cn.com.glsx.merchants.response.*;
import cn.com.glsx.merchants.util.Pagination;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: 商户服务
 * @date 2021/7/21 17:06
 */
@Service
public class MerchantService {

    @Autowired
    private MerchantRemote merchantRemote;

    @Autowired
    private MerchantUserService merchantUserService;

    /**
     * 保存商户
     *
     * @param merchantSaveRequest 入参
     */
    public Integer saveMerchant(MerchantSaveRequest merchantSaveRequest) {
        RpcResponse<Integer> booleanRpcResponse = merchantRemote.saveMerchantRemote(converMerchant(merchantSaveRequest));
        if (booleanRpcResponse.isSuccess()) {
            return booleanRpcResponse.getResult();
        }
        throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
    }

    private MerchantRequest converMerchant(MerchantSaveRequest merchantSaveRequest) {
        MerchantRequest merchantRequest = ConvertRequest.convertRequest(merchantSaveRequest, new MerchantRequest());
        String merchantFiles = merchantSaveRequest.getMerchantFiles();
        String merchantBrandListJson = merchantSaveRequest.getMerchantBrandListJson();
        if(StringUtils.isNotEmpty(merchantBrandListJson))
            merchantRequest.setMerchantBrandList(JSONObject.parseArray(merchantBrandListJson, MerchantBrandRequest.class));
        if (StringUtils.isNotEmpty(merchantFiles)) {
            merchantRequest.setMerchantFiles(JSONObject.parseArray(merchantFiles, MerchantFileRequest.class));
        }
        return merchantRequest;
    }

    private MerchantRequest converUpdateMerchant(MerchantUpdateRequest merchantUpdateRequest) {
        MerchantRequest merchantRequest = ConvertRequest.convertRequest(merchantUpdateRequest, new MerchantRequest());
        String merchantFiles = merchantUpdateRequest.getMerchantFiles();
        String merchantBrandListJson = merchantUpdateRequest.getMerchantBrandListJson();
        if(StringUtils.isNotEmpty(merchantBrandListJson))
            merchantRequest.setMerchantBrandList(JSONObject.parseArray(merchantBrandListJson, MerchantBrandRequest.class));
        if (StringUtils.isNotEmpty(merchantFiles)) {
            merchantRequest.setMerchantFiles(JSONObject.parseArray(merchantFiles, MerchantFileRequest.class));
        }
        return merchantRequest;
    }


    /**
     * 更新商户
     *
     * @param merchantUpdateRequest 入参
     */
    public void updateMerchant(MerchantUpdateRequest merchantUpdateRequest) {
        RpcResponse<Boolean> booleanRpcResponse = merchantRemote.updateMerchantRemote(converUpdateMerchant(merchantUpdateRequest));
        if (booleanRpcResponse.isFailed()) {
            throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
        }
    }

    /**
     * 保存商户权限
     *
     * @param merchantTreeRequest 入参
     */
    public void savePermissions(MerchantTreeRopRequest merchantTreeRequest) {
        MerchantTreeRequest merchantTree = ConvertRequest.convertRequest(merchantTreeRequest, new MerchantTreeRequest());
        String accountTypes = merchantTreeRequest.getAccountTypes();
        if (StringUtils.isNotEmpty(accountTypes)) {
            merchantTree.setAccountTypes(JSONObject.parseArray(accountTypes, AccountTypeTreeRequest.class));
        }
        RpcResponse<Boolean> booleanRpcResponse = merchantRemote.savePermissionsRemote(merchantTree);
        if (booleanRpcResponse.isFailed()) {
            throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
        }
    }


    /**
     * 更新商户权限
     *
     * @param merchantTreeRequest 入参
     */
    public void updatePermissions(MerchantTreeRopRequest merchantTreeRequest) {
        MerchantTreeRequest merchantTree = ConvertRequest.convertRequest(merchantTreeRequest, new MerchantTreeRequest());
        String accountTypes = merchantTreeRequest.getAccountTypes();
        if (StringUtils.isNotEmpty(accountTypes)) {
            merchantTree.setAccountTypes(JSONObject.parseArray(accountTypes, AccountTypeTreeRequest.class));
        }
        RpcResponse<Boolean> booleanRpcResponse = merchantRemote.updatePermissionsRemote(merchantTree);
        if (booleanRpcResponse.isFailed()) {
            throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
        }
    }

    /**
     * 查询商户详情
     *
     * @param merchantDetailRequest
     * @return
     */
    public MerchantRopResponse findMerchantDetail(MerchantDetailRequest merchantDetailRequest) {
        RpcResponse<MerchantResponse> merchantDetailRemote = merchantRemote.findMerchantDetailRemote(ConvertRequest.convertRequest(merchantDetailRequest, new MerchantRequest()));
        if (merchantDetailRemote.isSuccess()) {
            return ConvertResponse.convertObject(merchantDetailRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(merchantDetailRemote.getException().getErrId(), merchantDetailRemote.getMessage());
    }

    /**
     * 查询商户树
     *
     * @param merchantDetailRequest 入参
     * @return
     */
    public List<MerchantTreeRopResponse> findMerchantTree(MerchantDetailRequest merchantDetailRequest) {
        RpcResponse<List<MerchantTreeResponse>> merchantDetailRemote = merchantRemote.findMerchantTreeRemote(ConvertRequest.convertRequest(merchantDetailRequest, new MerchantRequest()));
        if (merchantDetailRemote.isSuccess()) {
            return ConvertResponse.convertList(merchantDetailRemote.getResult(), MerchantTreeRopResponse.class);
        }
        throw new RpcServiceException(merchantDetailRemote.getException().getErrId(), merchantDetailRemote.getMessage());
    }

    /**
     * 查询商户的菜单权限
     *
     * @param merchantDetailRequest 入参
     * @return
     */
    public List<AccountTypeTreeRopResponse> findPermissionsByMerId(MerchantDetailRequest merchantDetailRequest) {
        RpcResponse<List<AccountTypeTreeResponse>> merchantDetailRemote = merchantRemote.findPermissionsByMerId(merchantDetailRequest.getId());
        if (merchantDetailRemote.isSuccess()) {
            return ConvertResponse.convertList(merchantDetailRemote.getResult(), AccountTypeTreeRopResponse.class);
        }
        throw new RpcServiceException(merchantDetailRemote.getException().getErrId(), merchantDetailRemote.getMessage());
    }

    /**
     * 查询商户的菜单权限树
     *
     * @param merchantDetailRequest 入参
     * @return
     */
    public List<AccountTypeTreeRopResponse> findMenuFunTreeByMerId(MerchantDetailRequest merchantDetailRequest) {
        RpcResponse<List<AccountTypeTreeResponse>> merchantDetailRemote = merchantRemote.findMenuFunTreeByMerId(merchantDetailRequest.getId(), merchantDetailRequest.getAccountlevel(), merchantDetailRequest.getParentId());
        if (merchantDetailRemote.isSuccess()) {
            return ConvertResponse.convertList(merchantDetailRemote.getResult(), AccountTypeTreeRopResponse.class);
        }
        throw new RpcServiceException(merchantDetailRemote.getException().getErrId(), merchantDetailRemote.getMessage());
    }

    /**
     * 更新商户的有效期
     *
     * @param merchantUpdateValRequest 入参
     * @return
     */
    public void updateValPerByMerId(MerchantUpdateValRequest merchantUpdateValRequest) {
        RpcResponse<Boolean> booleanRpcResponse = merchantRemote.updateValPerByMerId(ConvertRequest.convertRequest(merchantUpdateValRequest, new MerchantRequest()));
        if (booleanRpcResponse.isFailed()) {
            throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
        }
    }

    /**
     * 查询商户的列表
     *
     * @param merchantSearchResquest 入参
     * @return
     */
    public Pagination<MerchantRopResponse> findMerchantByPage(MerchantSearchResquest merchantSearchResquest) {
        RpcPagination<MerchantRequest> merchantRequestRpcPagination = ConvertRequest.convertPage(merchantSearchResquest, new MerchantRequest());
        MerchantRequest condition = merchantRequestRpcPagination.getCondition();
        Integer superMerchantNo = merchantSearchResquest.getSuperMerchantNo();
        condition.setSuperMerchantNo(superMerchantNo);
        RpcResponse<RpcPagination<MerchantResponse>> merProductByPageRemote = merchantRemote.findMerchantByPageRemote(merchantRequestRpcPagination);
        if (merProductByPageRemote.isSuccess()) {
            return ConvertResponse.convertPage(merProductByPageRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(merProductByPageRemote.getException().getErrId(), merProductByPageRemote.getMessage());
    }

    /**
     * 查询所有商户
     *
     * @return
     */
    public List<MerchantRopListRes> findAllMerchant(MerchantFindRequest merchantFindRequest) {
        RpcResponse<List<MerchantResponse>> allMerchantRemote = merchantRemote.findAllMerchantRemote(ConvertRequest.convertRequest(merchantFindRequest, new MerchantRequest()));
        if (allMerchantRemote.isSuccess()) {
            return ConvertResponse.convertList(allMerchantRemote.getResult(), MerchantRopListRes.class);
        }
        throw new RpcServiceException(allMerchantRemote.getException().getErrId(), allMerchantRemote.getMessage());
    }

    /**
     * 查询上下级商户
     *
     * @return
     */
    public MerchantRopResponse findSubordinateRelationship(MerchantDetailRequest merchantDetailRequest) {
        RpcResponse<MerchantResponse> allMerchantRemote = merchantRemote.findSubordinateRelationshipRemote(ConvertRequest.convertRequest(merchantDetailRequest, new MerchantRequest()));
        if (allMerchantRemote.isSuccess()) {
            return ConvertResponse.convertObject(allMerchantRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(allMerchantRemote.getException().getErrId(), allMerchantRemote.getMessage());
    }

    public List<AccountTypeTreeRopResponse> findMenuFunTreeByAccountType(AccountTypeSearchRequest accountTypeSearchRequest) {
        String[] split = accountTypeSearchRequest.getIds().split(",");
        List<Integer> strings = new ArrayList<>();
        for (String s : split) {
            strings.add(Integer.parseInt(s));
        }
        RpcResponse<List<AccountTypeTreeResponse>> response =
                merchantRemote.findMenuFunTreeByAccountType(strings, accountTypeSearchRequest.getAccountLevel(), accountTypeSearchRequest.getMerchantId());
        if (response.isSuccess()) {
            return ConvertResponse.convertList(response.getResult(), AccountTypeTreeRopResponse.class);
        }
        throw new RpcServiceException(response.getException().getErrId(), response.getMessage());
    }

    public List<AccountTypeTreeRopResponse> findMenuFunTreeByRoleId(RoleSearchRequest roleSearchRequest) {
        RpcResponse<List<AccountTypeTreeResponse>> response =
                merchantRemote.findMenuFunTreeByRoleId(roleSearchRequest.getId());
        if (response.isSuccess()) {
            return ConvertResponse.convertList(response.getResult(), AccountTypeTreeRopResponse.class);
        }
        throw new RpcServiceException(response.getException().getErrId(), response.getMessage());
    }

    public MerchantBaseInfoRopResponse findMerchantBaseInfo(MerchantBaseInfoRequest merchantBaseInfoRequest) {
        RpcResponse<MerchantBaseInfoResponse> merchantBaseInfo = merchantRemote.findMerchantBaseInfo(merchantBaseInfoRequest.getMerchantId());
        if (merchantBaseInfo.isSuccess()) {
            return ConvertResponse.convertObject(merchantBaseInfo.getResult(), MerchantBaseInfoRopResponse.class);
        }
        throw new RpcServiceException(merchantBaseInfo.getException().getErrId(), merchantBaseInfo.getMessage());
    }

    public void updateMerchantPath(MerchantUpdatePathRequest merchantUpdatePathRequest) {
        MerchantUpdatePathApiRequest pathApiRequest = new MerchantUpdatePathApiRequest();
        BeanUtils.copyProperties(merchantUpdatePathRequest, pathApiRequest);
        RpcResponse<Boolean> rpcResponse = merchantRemote.merchantUpdatePath(pathApiRequest);
        if(!rpcResponse.isSuccess()){
            RpcServiceException exception = rpcResponse.getException();
            if(null != exception)
                throw new RpcServiceException(rpcResponse.getException().getErrId(), rpcResponse.getMessage());
            throw new RpcServiceException(rpcResponse.getError().getValue(), rpcResponse.getError().getDisplayName());

        }
    }

    public void updateAccessMode(MerchantAccessMode merchantAccessMode) {
         MerchantApiAccessMode apiAccessMode = new MerchantApiAccessMode();
         BeanUtils.copyProperties(merchantAccessMode, apiAccessMode);
         RpcResponse<Boolean> rpcResponse = merchantRemote.updateAccessMode(apiAccessMode);
        if (rpcResponse.isFailed()) {
            throw new RpcServiceException(rpcResponse.getException().getErrId(), rpcResponse.getMessage());
        }
    }

    public MerchantInfoDjRopResponse findMerchantInfoToDj(MerchantBaseInfoRequest merchantBaseInfoRequest) {
        RpcResponse<MerchantInfoApiDjRopResponse> rpcResponse = merchantRemote.findMerchantInfoToDj(merchantBaseInfoRequest.getMerchantId());
        if (rpcResponse.isSuccess()) {
            return ConvertResponse.convertObject(rpcResponse.getResult(), MerchantInfoDjRopResponse.class);
        }
        throw new RpcServiceException(rpcResponse.getException().getErrId(), rpcResponse.getMessage());
    }


    public Pagination<MerchantSearchEntMicroResponse> findEntMicroMerchantsByPage(MerchantSearchEntMicroRequest merchantSearchEntMicroRequest) {
        RpcPagination<MerchantEntMicroPageRequest> remoteRequest = ConvertRequest.convertPage(merchantSearchEntMicroRequest, new MerchantEntMicroPageRequest());
        RpcResponse<RpcPagination<MerchantSearchEntMicroResponse>> response = merchantRemote.findEntMicroMerchantsByPage(remoteRequest);
        if (response.isSuccess()) {
            return ConvertResponse.convertPage(response.getResult(), MerchantSearchEntMicroResponse.class);
        }
        throw new RpcServiceException(response.getException().getErrId(), response.getMessage());
    }

    public Boolean checkMerchantNameRepeat(MerchantNameRopRequest request) {
        RpcResponse<Boolean> rpcResponse = merchantRemote.checkMerchantNameRepeat(request.getMerchantName());
        if (rpcResponse.isSuccess()) {
            return rpcResponse.getResult();
        }
        throw new RpcServiceException(rpcResponse.getException().getErrId(), rpcResponse.getMessage());
    }
}