ANDRU-PC\Andru
2022-12-22 eb4b67d05beba01a088dc4d2ad46d7c319f3d92b
phi_platform_user/src/main/java/com/hx/phip/service/userMoney/impl/UserMoneyUnclaimedServiceImpl.java
@@ -3,13 +3,16 @@
import javax.annotation.Resource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hx.common.service.CommonService;
import com.hx.phiappt.common.OperationReasonConstants;
import com.hx.phiappt.common.OriginChannelConstants;
import com.hx.phiappt.constants.tool.UserInfoTool;
import com.hx.phiappt.constants.tool.money.UserMoneyTool;
import com.hx.phiappt.model.*;
import com.hx.phip.common.wx.corp.WeiXinMpUtil;
import com.hx.phip.config.CustomParameter;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.model.ApplyParameter;
import com.hx.phip.model.SysParameter;
@@ -20,10 +23,14 @@
import com.hx.phip.tool.SendSMSTool;
import com.hx.util.AesUtil;
import com.hx.util.StringUtils;
import com.platform.constants.PlatformPattern;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
import com.platform.resultTool.SystemCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.hx.exception.TipsException;
@@ -34,17 +41,22 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Transactional
@Service
public class UserMoneyUnclaimedServiceImpl implements UserMoneyUnclaimedService {
    private static final Logger logger = LoggerFactory.getLogger(UserMoneyUnclaimedServiceImpl.class);
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommonService commonService;
    @Resource
    private CustomParameter customParameter;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserMoneyService userMoneyService;
    @Resource
@@ -103,13 +115,125 @@
    }
    /**
     * 获取操作用户资金信息消息
     * @param userMoneyUnclaimed 参数
     * @param type 是否领取资金
     * @return 返回
     */
    private JSONObject getOpInfo(UserMoneyUnclaimed userMoneyUnclaimed, Integer type) {
        BigDecimal before = BigDecimal.ZERO;
        BigDecimal after = BigDecimal.ZERO;
        JSONObject jsonObject = new JSONObject();
        // 获取处理信息
        StringBuilder info = new StringBuilder();
        info.append(userMoneyUnclaimed.getOriginSubject());
        // 查询用户资金信息
        UserMoney userMoney = userMoneyService.selectUserIdDecrypt(userMoneyUnclaimed.getUserId());
        switch (userMoneyUnclaimed.getFundType()) {
            case UserMoneyUnclaimed.FUND_TYPE_STORED_VALUE_FUND:
                info.append(", 储值金");
                before = new BigDecimal(userMoney.getStoredValueFund());
                after = before.add(userMoneyUnclaimed.getOpNumber());
                break;
            case UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND:
                info.append(", 增值金");
                before = new BigDecimal(userMoney.getValueAddedFund());
                after = before.add(userMoneyUnclaimed.getOpNumber());
                break;
            case UserMoneyUnclaimed.FUND_TYPE_INTEGRAL:
                info.append(", 积分");
                before = new BigDecimal(userMoney.getIntegral());
                after = before.add(userMoneyUnclaimed.getOpNumber());
                break;
            case UserMoneyUnclaimed.FUND_TYPE_DEPOSIT:
                info.append(", 预定金");
                before = new BigDecimal(userMoney.getDeposit());
                after = before.add(userMoneyUnclaimed.getOpNumber());
                break;
        }
        info.append(", 操作数量:").append(userMoneyUnclaimed.getOpNumber());
        if (UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND == userMoneyUnclaimed.getFundType()) {
            // 增值金添加是否需要领取
            int opType = userMoneyUnclaimed.getOpType() == null ? BaseEntity.YES : userMoneyUnclaimed.getOpType();
            info.append(", 是否需要领取:").append(BaseEntity.YES.equals(opType) ? "是" : "否");
            if (BaseEntity.YES.equals(opType)){
                before = null;
                after = null;
            }
        }
        if (BaseEntity.YES.equals(type)){
            info.append(", 领取资金: ").append(userMoneyUnclaimed.getOpNumber());
        }
        if (3 == type){
            info.append(", 作废待领取资金: ").append(userMoneyUnclaimed.getOpNumber());
            before = null;
            after = null;
        }
        jsonObject.put("key", info.toString());
        jsonObject.put("before", before);
        jsonObject.put("after", after);
        return jsonObject;
    }
    /**
     * 添加用户日志
     * @param userMoneyUnclaimed 用户资金对象
     * @param type 是否领取资金
     */
    private void addUserLog(UserMoneyUnclaimed userMoneyUnclaimed, Integer type){
        // 处理用户日志
        User user = userMapper.selectOneByKey(userMoneyUnclaimed.getUserId());
        if (user != null) {
            // 用户日志
            JSONArray logArray = new JSONArray();
            JSONObject jsonObject = getOpInfo(userMoneyUnclaimed, type);
            logArray.add(jsonObject);
            // 操作人转换
            UserUpdateLog userUpdateLog = new UserUpdateLog();
            switch (userMoneyUnclaimed.getOperatorType()) {
                case UserMoneyUnclaimed.OPERATOR_TYPE_USER:
                    userUpdateLog.setOperatorType(UserUpdateLog.OPERATOR_TYPE_USER);
                    userUpdateLog.setOperatorId(userMoneyUnclaimed.getOperatorId());
                    userUpdateLog.setOperatorName(userMoneyUnclaimed.getOperatorName());
                    break;
                case UserMoneyUnclaimed.OPERATOR_TYPE_EMPLOYEE:
                    userUpdateLog.setOperatorType(UserUpdateLog.OPERATOR_TYPE_EMPLOYEE);
                    userUpdateLog.setOperatorId(userMoneyUnclaimed.getOperatorId());
                    userUpdateLog.setOperatorName(userMoneyUnclaimed.getOperatorName());
                    break;
                case UserMoneyUnclaimed.OPERATOR_TYPE_ADMIN:
                    userUpdateLog.setOperatorType(UserUpdateLog.OPERATOR_TYPE_ADMIN);
                    userUpdateLog.setOperatorId(userMoneyUnclaimed.getOperatorId());
                    userUpdateLog.setOperatorName(userMoneyUnclaimed.getOperatorName());
                    break;
                case UserMoneyUnclaimed.OPERATOR_TYPE_SYSTEM:
                    userUpdateLog.setOperatorType(UserUpdateLog.OPERATOR_TYPE_SYSTEM);
                    break;
            }
            // 添加用户日志
            UserInfoTool.addUserUpdateLogOp(commonService, user, user.getShopId(), userUpdateLog, "用户资金调整", logArray, userMoneyUnclaimed.getRemarks());
        }
    }
    /**
     * 新增
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insert(UserMoneyUnclaimed userMoneyUnclaimed) {
        int count = userMoneyUnclaimedMapper.insert(userMoneyUnclaimed);
        if (count != 1) {
            throw new TipsException("新增失败!");
        } else {
            // 重新获取下数据
            UserMoneyUnclaimed checkUserMoneyUnclaimed = userMoneyUnclaimedMapper.selectOneByKey(userMoneyUnclaimed.getId());
            if(checkUserMoneyUnclaimed != null) {
                // 过滤待领取的数据
                this.handlerReceiveData(checkUserMoneyUnclaimed, BaseEntity.YES);
                // 添加用户日志
                addUserLog(userMoneyUnclaimed, BaseEntity.NO);
            }
        }
    }
@@ -118,6 +242,10 @@
     */
    @Override
    public void updateAll(UserMoneyUnclaimed userMoneyUnclaimed) {
        if(BaseEntity.YES.equals(userMoneyUnclaimed.getIsDel())){
            // 添加用户日志
            addUserLog(userMoneyUnclaimed, 3);
        }
        int count = userMoneyUnclaimedMapper.updateAll(userMoneyUnclaimed);
        if (count != 1) {
            throw new TipsException("保存失败!");
@@ -161,6 +289,7 @@
     * @param userMoneyUnclaimed 用户待领取列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void checkParam(UserMoneyUnclaimed userMoneyUnclaimed) {
        // 用户校验
        if (StringUtils.isEmpty(userMoneyUnclaimed.getUserId())) {
@@ -193,7 +322,7 @@
        }
        // 操作原因
        if(StringUtils.isEmpty(userMoneyUnclaimed.getOperationReason())){
        if (StringUtils.isEmpty(userMoneyUnclaimed.getOperationReason())) {
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "操作原因不能为空");
        } else {
            if (checkConstantNotHaveValue(OperationReasonConstants.class, userMoneyUnclaimed.getOperationReason())) {
@@ -208,16 +337,34 @@
            // 增值金默认需要领取
            if (UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND == userMoneyUnclaimed.getFundType()) {
                // 增值金默认需要领取 负数不用用户领取和判断是否需要发送短信提醒领取
                if(BigDecimal.ZERO.compareTo(userMoneyUnclaimed.getOpNumber()) < 0) {
                if (BigDecimal.ZERO.compareTo(userMoneyUnclaimed.getOpNumber()) < 0) {
                    userMoneyUnclaimed.setOpType(UserMoneyUnclaimed.OP_TYPE_RECEIVE);
                    // 校验是否要发送短信
                    checkIsNeedSentSMS(userMoneyUnclaimed);
                }
            }
        } else {
            // 判断是否是支持的类型
            if (UserMoneyUnclaimed.OP_TYPE_SYSTEM != userMoneyUnclaimed.getOpType() && UserMoneyUnclaimed.OP_TYPE_RECEIVE != userMoneyUnclaimed.getOpType()) {
                throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "操作类型错误");
            }
        }
        // 是否发送短信
        if (UserMoneyUnclaimed.OP_TYPE_RECEIVE == userMoneyUnclaimed.getOpType()) {
            boolean flag = true;
            // 查询用户设置是否领取状态
            UserInfo userInfo = userInfoMapper.selectOneByUserId(userMoneyUnclaimed.getUserId());
            if (userInfo != null) {
                // 不需要领取处理数据
                if (userInfo.getIsReceiveValueAddedFund() != null && BaseEntity.NO.equals(userInfo.getIsReceiveValueAddedFund())) {
                    flag = false;
                    userMoneyUnclaimed.setOpType(UserMoneyUnclaimed.OP_TYPE_SYSTEM);
                    // 用户设置了不用再领取增值金
                    logger.info("操作用户资金(待领取记录)-用户设置是否需要领取状态:{}", userInfo.getIsReceiveValueAddedFund());
                }
            }
            // 校验是否要发送短信
            if (flag) {
                checkIsNeedSentSMS(userMoneyUnclaimed);
            }
        }
@@ -252,8 +399,8 @@
                            break;
                        case UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND:
                            // 金额操作 不做校验可以减成负数
                            /*BigDecimal valueAddedFund = new BigDecimal(userMoney.getValueAddedFund());
                            checkCanOperation(valueAddedFund, userMoneyUnclaimed.getOpNumber());*/
                            BigDecimal valueAddedFund = new BigDecimal(userMoney.getValueAddedFund());
                            checkCanOperation(valueAddedFund, userMoneyUnclaimed.getOpNumber());
                            break;
                        case UserMoneyUnclaimed.FUND_TYPE_INTEGRAL:
                            // 积分操作
@@ -324,8 +471,8 @@
            }
        }
        // 判断订单 先不判断订单
        /*if(!StringUtils.isEmpty(userMoneyUnclaimed.getOrderId())){
        /*// 判断订单 先不判断订单
        if(!StringUtils.isEmpty(userMoneyUnclaimed.getOrderId())){
            OrdersTotal ordersTotal = commonService.selectOneByKey(OrdersTotalMapper.class, userMoneyUnclaimed.getOrderId());
            if(ordersTotal == null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "主订单的信息不存在");
@@ -359,7 +506,8 @@
     * @param value 校验值
     * @return 返回
     */
    private boolean checkConstantNotHaveValue(Class classObj, String value) {
    @Override
    public boolean checkConstantNotHaveValue(Class classObj, String value) {
        boolean flag = true;
        // 判断是否存在这个来源渠道
        Field[] fields = classObj.getDeclaredFields();
@@ -405,45 +553,50 @@
     * 根据平台判断是否需要发送短信
     * @param userMoneyUnclaimed 用户待领取信息
     */
    private void checkIsNeedSentSMS(UserMoneyUnclaimed userMoneyUnclaimed){
        if(!StringUtils.isEmpty(userMoneyUnclaimed.getOriginChannel()) && userMoneyUnclaimed.getOpType() == UserMoneyUnclaimed.OP_TYPE_RECEIVE) {
            // CRM不用发送短信领取其他平台需要发送短信领取
            if(!OriginChannelConstants.ORIGIN_CHANNEL_CRM.equals(userMoneyUnclaimed.getOriginChannel())) {
                userMoneyUnclaimed.setIsSentSMS(BaseEntity.YES);
                String url = getSMSUrl();
                if(StringUtils.isEmpty(url)){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS, "获取发送短信链接地址出现异常!");
                }
                userMoneyUnclaimed.setSMSUrl(url);
            }
    private void checkIsNeedSentSMS(UserMoneyUnclaimed userMoneyUnclaimed) {
        // CRM不用发送短信领取其他平台需要发送短信领取
        // if(!OriginChannelConstants.ORIGIN_CHANNEL_CRM.equals(userMoneyUnclaimed.getOriginChannel())) {
        userMoneyUnclaimed.setIsSentSMS(BaseEntity.YES);
        String url = getSMSUrl();
        if (StringUtils.isEmpty(url)) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "获取发送短信链接地址出现异常!");
        }
        userMoneyUnclaimed.setSMSUrl(url);
        //}
    }
    /**
     * 生成需要发送短信的地址
     * @return 返回
     */
    private String getSMSUrl(){
        //获取用户CRM小程序token
        ApplyParameter applyParameter = ApplyParameterTool.getMpParameter("wx71e6babac80abcee", commonService);
        if (applyParameter == null) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "mpId is error");
        }
        SysParameter sysParameter = WeiXinMpUtil.getAccessToken(commonService, applyParameter.getMpAppId(), AesUtil.aesDecryp(applyParameter.getMpSecretKey()));
        if (sysParameter == null) {
            throw new PlatTipsException(PlatformCode.ERROR_SYSTEM, "服务错误" + SystemCode.ERROR_GET_CORP_TOKEN);
        }
        //生成小程序链接
    private String getSMSUrl() {
        // 生成小程序链接
        String urlLink = null;
        String result = SendSMSTool.getAooLetUrl(sysParameter.getParamValue(), "/pages/recharge/incrementInfo", "", "release");
        if(!StringUtils.isEmpty(result)) {
            JSONObject jsonObject = JSON.parseObject(result);
            if (jsonObject.getInteger("errcode") != 0) {
                throw new PlatTipsException(PlatformCode.ERROR_SYSTEM, "生成短信小程序路径错误:" + jsonObject.getString("msg") + SystemCode.ERROR_GET_CORP_TOKEN);
        // 正式区在拿取链接
        if (PlatformPattern.PROD.equals(customParameter.getPlatformPatternMoney())) {
            //获取用户CRM小程序token
            ApplyParameter applyParameter = ApplyParameterTool.getMpParameter("wx71e6babac80abcee", commonService);
            if (applyParameter == null) {
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "mpId is error");
            }
            urlLink = jsonObject.getString("url_link");
            SysParameter sysParameter = WeiXinMpUtil.getAccessToken(commonService, applyParameter.getMpAppId(), AesUtil.aesDecryp(applyParameter.getMpSecretKey()));
            if (sysParameter == null) {
                throw new PlatTipsException(PlatformCode.ERROR_SYSTEM, "服务错误" + SystemCode.ERROR_GET_CORP_TOKEN);
            }
            // 更换链接 原CRM 1.0 /pages/recharge/incrementInfo
            String result = SendSMSTool.getAooLetUrl(sysParameter.getParamValue(), "/pages/portClient_me/incrementValue/incrementValue", "", "release");
            if (!StringUtils.isEmpty(result)) {
                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject.getInteger("errcode") != 0) {
                    throw new PlatTipsException(PlatformCode.ERROR_SYSTEM, "生成短信小程序路径错误:" + jsonObject.getString("msg") + SystemCode.ERROR_GET_CORP_TOKEN);
                }
                urlLink = jsonObject.getString("url_link");
            }
        } else {
            // 测试区跳转百度
            urlLink = "https://wxaurl.cn/6666";
        }
        return urlLink;
    }
@@ -461,15 +614,18 @@
                // 判断是不是要领取的数据
                if(BaseEntity.YES.equals(isReceive)) {
                    if (UserMoneyUnclaimed.OP_TYPE_RECEIVE == userMoneyUnclaimed.getOpType()) {
                        logger.info("待领取资产处理跳过,数据id:{},原因:需要领取数据", userMoneyUnclaimed.getId());
                        return;
                    }
                }
                // 判断是否处理了
                if(UserMoneyUnclaimed.STATUS_SUCCEED == userMoneyUnclaimed.getStatus()){
                    logger.info("待领取资产处理跳过,数据id:{},原因:数据已领取不需要再处理", userMoneyUnclaimed.getId());
                    return;
                }
                // 判断数据是否有效
                if(UserMoneyUnclaimed.NO.equals(userMoneyUnclaimed.getIsValid())){
                    logger.info("待领取资产处理跳过,数据id:{},原因:数据已失效无法领取", userMoneyUnclaimed.getId());
                    return;
                }
                // 操作资金处理
@@ -501,6 +657,11 @@
                sqlValue.put("nowTime",new Date());
                sqlSentence.sqlSentence(sql, sqlValue);
                commonService.updateWhere(UserMoneyUnclaimedMapper.class, sqlSentence);
                // 添加用户日志
                if(BaseEntity.NO.equals(isReceive)) {
                    addUserLog(userMoneyUnclaimed, BaseEntity.YES);
                }
            }
        }
    }