OpenService.java 15.5 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.contants.Contants;
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.response.*;
import cn.com.glsx.merchants.remote.MerchantRemote;
import cn.com.glsx.merchants.remote.MerchantStaffRemote;
import cn.com.glsx.merchants.remote.MerchantUserRemote;
import cn.com.glsx.merchants.request.file.FileSaveRequest;
import cn.com.glsx.merchants.request.merchant.MerchantSaveOpenRequest;
import cn.com.glsx.merchants.request.merchant.OpenMerchantPageRequest;
import cn.com.glsx.merchants.request.open.*;
import cn.com.glsx.merchants.request.open.MerchantListOpenRequest;
import cn.com.glsx.merchants.response.*;
import cn.com.glsx.merchants.util.ListUtils;
import cn.com.glsx.merchants.util.Pagination;
import com.alibaba.fastjson.JSONObject;
import com.rop.RopRequestContext;
import com.rop.session.SimpleSession;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.oreframework.boot.autoconfigure.rop.session.RopSessionManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: TODO
 * @date 2021/9/17 9:52
 */
@Service
public class OpenService {

    @Autowired
    private MerchantRemote merchantRemote;

    @Autowired
    private MerchantUserRemote merchantUserRemote;

    @Autowired
    private MerchantStaffRemote merchantStaffRemote;

    @Autowired
    private RopSessionManager ropSessionManager;

    public MerchantRopResponse findMerchantDetailByMerchantNo(OpenMerchantDetailRequest openMerchantRequest) {
        RpcResponse<MerchantResponse> merchantDetailByMerchantNoRemote = merchantRemote.findMerchantDetailByMerchantNoRemote(openMerchantRequest.getMerchantId());
        if (merchantDetailByMerchantNoRemote.isSuccess()) {
            return ConvertResponse.convertObject(merchantDetailByMerchantNoRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(merchantDetailByMerchantNoRemote.getException().getErrId(), merchantDetailByMerchantNoRemote.getMessage());
    }

    public Integer findSuperlativeMerchant(OpenMerchantDetailRequest openMerchantRequest) {
        RpcResponse<Integer> superlativeMerchantRemote = merchantRemote.findSuperlativeMerchantRemote(ConvertRequest.convertRequest(openMerchantRequest, new MerchantOpenRequest()));
        if (superlativeMerchantRemote.isSuccess()) {
            return superlativeMerchantRemote.getResult();
        }
        throw new RpcServiceException(superlativeMerchantRemote.getException().getErrId(), superlativeMerchantRemote.getMessage());
    }

    public List<MerchantRopResponse> findSuperiorList(OpenMerchantLevelRequest openMerchantRequest) {
        RpcResponse<List<MerchantResponse>> merchantListRemote = merchantRemote.findSuperiorListRemote(ConvertRequest.convertRequest(openMerchantRequest, new MerchantOpenRequest()));
        if (merchantListRemote.isSuccess()) {
            return ConvertResponse.convertList(merchantListRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(merchantListRemote.getException().getErrId(), merchantListRemote.getMessage());
    }

    public List<MerchantRopResponse> findMerchantList(OpenMerchantListRequest openMerchantRequest) {
        RpcResponse<List<MerchantResponse>> merchantListRemote = merchantRemote.findMerchantListRemote(ConvertRequest.convertRequest(openMerchantRequest, new MerchantOpenRequest()));
        if (merchantListRemote.isSuccess()) {
            return ConvertResponse.convertList(merchantListRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(merchantListRemote.getException().getErrId(), merchantListRemote.getMessage());
    }

    public Pagination<MerchantRopResponse> findSubordinateListByPage(OpenMerchantPageRequest openMerchantRequest) {
        RpcResponse<RpcPagination<MerchantResponse>> subordinateListByPageRemote = merchantRemote.findSubordinateListByPageRemote(ConvertRequest.convertPage(openMerchantRequest, new MerchantOpenRequest()));
        if (subordinateListByPageRemote.isSuccess()) {
            return ConvertResponse.convertPage(subordinateListByPageRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(subordinateListByPageRemote.getException().getErrId(), subordinateListByPageRemote.getMessage());
    }

    /**
     * 用户登录保存登录信息
     *
     * @param openUserRequest
     * @return
     */
    public List<OpenUserResponse> checkBussinessAccount(OpenCheckUserRequest openUserRequest) {
        RpcResponse<List<MerchantUserResponse>> merchantUserResponseRpcResponse = merchantUserRemote.checkBussinessAccount(ConvertRequest.convertRequest(openUserRequest, new MerchantUserRequest()));
        if (merchantUserResponseRpcResponse.isSuccess()) {
            List<MerchantUserResponse> result = merchantUserResponseRpcResponse.getResult();
            SimpleSession session = new SimpleSession();
            session.setAttribute(Contants.USER_ACCOUNT, openUserRequest.getLoginAccout());
            session.setAttribute(Contants.USER_NAME, result.get(0).getUserName());
            RopRequestContext ropRequestContext = openUserRequest.getRopRequestContext();
            ropRequestContext.addSession(openUserRequest.getSessionId(), session);
            ropSessionManager.addSession(openUserRequest.getSessionId(), session);
            return ConvertResponse.convertList(merchantUserResponseRpcResponse.getResult(), OpenUserResponse.class);
        }
        throw new RpcServiceException(merchantUserResponseRpcResponse.getException().getErrId(), merchantUserResponseRpcResponse.getMessage());
    }

    public List<Integer> findDataPermission(OpenUserDataPermissionRequest openUserRequest) {
        RpcResponse<List<Integer>> dataPermission = merchantUserRemote.findDataPermission(ConvertRequest.convertRequest(openUserRequest, new MerchantUserRequest()));
        if (dataPermission.isSuccess()) {
            return dataPermission.getResult();
        }
        throw new RpcServiceException(dataPermission.getException().getErrId(), dataPermission.getMessage());
    }

    public List<AccountTypeTreeRopResponse> findMenuFunPermission(OpenUserMenuPermissionRequest openUserRequest) {
        RpcResponse<List<AccountTypeTreeResponse>> menuFunPermission = merchantUserRemote.findMenuFunPermission(ConvertRequest.convertRequest(openUserRequest, new MerchantUserRequest()));
        if (menuFunPermission.isSuccess()) {
            return ConvertResponse.convertList(menuFunPermission.getResult(), AccountTypeTreeRopResponse.class);
        }
        throw new RpcServiceException(menuFunPermission.getException().getErrId(), menuFunPermission.getMessage());
    }

    public Integer saveMerchant(MerchantSaveOpenRequest merchantSaveRequest) {
        List<FileSaveRequest> merchantFiles = merchantSaveRequest.getMerchantFiles();
        MerchantRequest merchantRequest = ConvertRequest.convertRequest(merchantSaveRequest, new MerchantRequest());
        if (CollectionUtils.isNotEmpty(merchantFiles)) {
            List<MerchantFileRequest> collect = merchantFiles.stream().map(index -> {
                MerchantFileRequest merchantFileRequest = new MerchantFileRequest();
                BeanUtils.copyProperties(index, merchantFileRequest);
                return merchantFileRequest;
            }).collect(Collectors.toList());
            merchantRequest.setMerchantFiles(collect);
        }
        RpcResponse<Integer> booleanRpcResponse = merchantRemote.saveOrUpdateMerchantRemote(merchantRequest);
        if (booleanRpcResponse.isSuccess()) {
            return booleanRpcResponse.getResult();
        }
        throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
    }

    public Boolean relevanceMerchant(RelaevanceMerRequest relaevanceMerRequest) {
        RpcResponse<Boolean> booleanRpcResponse = merchantRemote.relevanceMerchantRemote(ConvertRequest.convertRequest(relaevanceMerRequest, new MerchantOpenRequest()));
        if (booleanRpcResponse.isSuccess()) {
            return booleanRpcResponse.getResult();
        }
        throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
    }

    public Integer saveStaff(StaffSaveOpenRequest staffSaveOpenRequest) {
        MerchantStaffRequest merchantStaffRequest = ConvertRequest.convertRequest(staffSaveOpenRequest, new MerchantStaffRequest());
        if (StringUtils.isNotEmpty(staffSaveOpenRequest.getMerchantFileRequest())) {
            MerchantFileRequest merchantFileRequest = new MerchantFileRequest();
            merchantFileRequest.setFileUrl(staffSaveOpenRequest.getMerchantFileRequest());
            merchantStaffRequest.setMerchantFileRequest(merchantFileRequest);
        }
        RpcResponse<Integer> booleanRpcResponse = merchantStaffRemote.saveOrUpdateStaffRemote(merchantStaffRequest);
        if (booleanRpcResponse.isSuccess()) {
            return booleanRpcResponse.getResult();
        }
        throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
    }

    public List<StaffResponse> syncEntmicroStaff(SyncStaffOpenRequest staffSaveOpenRequest) {
        List<StaffSaveOpenRequest> list = null;
        String data = staffSaveOpenRequest.getData();
        if (StringUtils.isNotEmpty(data)) {
            list = JSONObject.parseArray(data, StaffSaveOpenRequest.class);
        }
        RpcResponse<List<MerchantStaffResponse>> listRpcResponse = merchantStaffRemote.syncEntmicroStaff(ConvertRequest.convertListRequest(list, MerchantStaffRequest.class));
        if (listRpcResponse.isSuccess()) {
            return ConvertResponse.convertList(listRpcResponse.getResult(), StaffResponse.class);
        }
        throw new RpcServiceException(listRpcResponse.getException().getErrId(), listRpcResponse.getMessage());
    }

    public List<MerchantRopResponse> proximityMerchantList(OpenProximityMerchantRequest openProximityMerchantRequest) {
        RpcResponse<List<MerchantResponse>> subordinateListByPageRemote = merchantRemote.findProximityMerchantListRemote(ConvertRequest.convertRequest(openProximityMerchantRequest, new MerchantOpenRequest()));
        if (subordinateListByPageRemote.isSuccess()) {
            return ConvertResponse.convertList(subordinateListByPageRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(subordinateListByPageRemote.getException().getErrId(), subordinateListByPageRemote.getMessage());
    }

    public Pagination<MerchantRopResponse> findMerchantListByPage(MerchantListOpenRequest merchantListOpenRequest) {
        RpcResponse<RpcPagination<MerchantResponse>> merchantListByPageRemote = merchantRemote.findMerchantListByPageRemote(ConvertRequest.convertPage(merchantListOpenRequest, new cn.com.glsx.merchants.model.request.MerchantListOpenRequest()));
        if (merchantListByPageRemote.isSuccess()) {
            return ConvertResponse.convertPage(merchantListByPageRemote.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(merchantListByPageRemote.getException().getErrId(), merchantListByPageRemote.getMessage());
    }

    public StaffResponse findStaffByAccount(StaffSearchOpenRequest staffSearchOpenRequest) {
        RpcResponse<MerchantStaffResponse> staffByAccount = merchantStaffRemote.findStaffByAccount(ConvertRequest.convertRequest(staffSearchOpenRequest, new StaffOpenRequest()));
        if (staffByAccount.isSuccess()) {
            return ConvertResponse.convertObject(staffByAccount.getResult(), StaffResponse.class);
        }
        throw new RpcServiceException(staffByAccount.getException().getErrId(), staffByAccount.getMessage());
    }

    public List<MerchantTreeOpenResponse> findMerchantTreeByMerchantNo(MerchantTreeOpenRequest merchantTreeOpenRequest) {
        RpcResponse<List<MerchantTreeResponse>> merchantTreeByMerchantNoRemote = merchantRemote.findMerchantTreeByMerchantNoRemote(merchantTreeOpenRequest.getMerchantId(), merchantTreeOpenRequest.getFlag());
        if (merchantTreeByMerchantNoRemote.isSuccess()) {
            return ConvertResponse.convertList(merchantTreeByMerchantNoRemote.getResult(), MerchantTreeOpenResponse.class);
        }
        throw new RpcServiceException(merchantTreeByMerchantNoRemote.getException().getErrId(), merchantTreeByMerchantNoRemote.getMessage());
    }

    public List<OpenUserResponse> checkPhone(OpenCheckPhoneRequest openCheckPhoneRequest) {
        RpcResponse<List<MerchantUserResponse>> merchantUserResponseRpcResponse = merchantUserRemote.checkPhone(ConvertRequest.convertRequest(openCheckPhoneRequest, new MerchantUserRequest()));
        if (merchantUserResponseRpcResponse.isSuccess()) {
            return ConvertResponse.convertList(merchantUserResponseRpcResponse.getResult(), OpenUserResponse.class);
        }
        throw new RpcServiceException(merchantUserResponseRpcResponse.getException().getErrId(), merchantUserResponseRpcResponse.getMessage());
    }

    public MerchantRopResponse findBySapCode(OpenSapSearchRequest openSapSearchRequest) {
        RpcResponse<MerchantResponse> bySapCode = merchantRemote.findBySapCode(openSapSearchRequest.getSapCode());
        if (bySapCode.isSuccess()) {
            return ConvertResponse.convertObject(bySapCode.getResult(), MerchantRopResponse.class);
        }
        throw new RpcServiceException(bySapCode.getException().getErrId(), bySapCode.getMessage());
    }

    public void userLogin(UserLoginRequest userLoginRequest) {
        SimpleSession session = new SimpleSession();
        session.setAttribute(Contants.USER_ACCOUNT, userLoginRequest.getLoginAccout());
        session.setAttribute(Contants.MERCHANT_ID, userLoginRequest.getMerchantId());
        RopRequestContext ropRequestContext = userLoginRequest.getRopRequestContext();
        ropRequestContext.addSession(userLoginRequest.getSessionId(), session);
        ropSessionManager.addSession(userLoginRequest.getSessionId(), session);
    }

    public Boolean updateMd5Password() {
        RpcResponse<Boolean> booleanRpcResponse = merchantUserRemote.updateMd5Password();
        if (booleanRpcResponse.isSuccess()) {
            return booleanRpcResponse.getResult();
        }
        throw new RpcServiceException(booleanRpcResponse.getException().getErrId(), booleanRpcResponse.getMessage());
    }

    public List<OpenUserResponse> findUserById(UserRequest userRequest) {
        RpcResponse<List<MerchantUserResponse>> bySapCode = merchantUserRemote.findUserById(ListUtils.stringToList(userRequest.getCreatedBy(), ','));
        if (bySapCode.isSuccess()) {
            return ConvertResponse.convertList(bySapCode.getResult(), OpenUserResponse.class);
        }
        throw new RpcServiceException(bySapCode.getException().getErrId(), bySapCode.getMessage());
    }
}