UserKitingBiz.java 7.89 KB
package com.cjs.site.biz.user.fund;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cjs.site.biz.pub.CommonBiz;
import com.cjs.site.biz.pub.SendMessageBiz;
import com.cjs.site.dao.pub.SysParamValueDao;
import com.cjs.site.dao.user.account.ClientInfoDao;
import com.cjs.site.dao.user.fund.FundRealDao;
import com.cjs.site.dao.user.fund.UserKitingDao;
import com.cjs.site.model.pub.ConstInfo;
import com.cjs.site.model.user.account.UserInfo;
import com.cjs.site.model.user.fund.UserKitingInfo;
import com.cjs.site.util.lang.DateEnum;
import com.cjs.site.util.lang.DateUtil;
import com.cjs.site.util.t2.T2Result;
import com.cjs.site.util.t2.T2Util;
import com.cjs.site.util.web.ActionUtil;

/**
 * 客户提现
 * 
 * @author tongyufu
 *
 */
@Service
public class UserKitingBiz {

    Logger                   log = LogManager.getLogger();
    @Autowired
    private FundRealDao      fundRealDao;
    @Autowired
    private UserKitingDao    userKitingDao;
    @Autowired
    private ClientInfoDao    clientInfoDao;
    @Autowired
    private SendMessageBiz   sendMessageBiz;
    @Autowired
    private CommonBiz        commonBiz;
    @Autowired
    private SysParamValueDao sysParamDao;

    /**
     * 取现
     * @param userId
     * @param amount 
     * @param validCode 短信验证码
     */
    public String kiting(String subBranch, Double amount, String validCode, HttpSession session) {
        UserInfo userInfo = ActionUtil.getUser();
        String userId = userInfo.getClient_id();
        if (!this.commonBiz.checkTime(true, 10, 11)) {
            String beginTime = DateUtil.parseDate(
              DateUtil.parseDate(this.sysParamDao.getParamValue("begin_time_fund"), DateEnum.UNSIGNED_TIME), 
              DateEnum.TIME);
            String endTime = DateUtil.parseDate(DateUtil.parseDate(
              this.sysParamDao.getParamValue("end_time_fund"), DateEnum.UNSIGNED_TIME), DateEnum.TIME);
            return "请于交易日的" + beginTime + " - " + endTime + "进行提现操作。";
          }
        //验证短信验证码
        if (!sendMessageBiz.compareValidCode(validCode, session)) {
            return "短信验证码不正确";
        }
        Map<String, Object> balance = fundRealDao.queryByUserId(userId);
        Double enableBalance = Double.valueOf(balance.get("ENABLE_BALANCE").toString());
        if (StringUtils.isBlank(subBranch)) {
            return "支行名称不能为空";
        }
        if (amount < 10) {
            return "提现金额不能小于10元";
        }
        if (amount > 2000000) {
            return "提现金额不能大于2,000,000元";
        }
        if (enableBalance < amount) {
            return "提现金额不能超过可用金额,当前可用金额:" + enableBalance;
        }

        Map<String, Object> params = new HashMap<String, Object>();
        if (amount > 0) {
            amount *= -1;
        }
        Map<String, String> bankInfo = clientInfoDao.queryForPnr(userId);
        params.put("op_branch_no", "11");
        params.put("operator_no", "2001");
        params.put("user_type", "");
        params.put("op_password", "111111");
        params.put("op_station", "");
        params.put("op_entrust_way", "7");
        params.put("menu_id", "");
        params.put("function_id", "");
        params.put("branch_no", "11");
        params.put("audit_action", "1");
        params.put("action_in", "");
        params.put("fund_account", userId);
        params.put("money_type", "0");
        params.put("occur_date", DateUtil.getNow(DateEnum.UNSIGNED_DATE));
        params.put("occur_balance", amount);
        params.put("bank_no", "");
        //params.put("join_info", "存管内部附加信息");
        params.put("remark", ConstInfo.Fund.USER_KITING_REMARK);
        T2Result result = null;
        String msg = "提现申请成功";
        try {
          //  result = T2Util.request(params, "202101");
        	result = this.ducureFund(userId, amount);
        } finally {
            UserKitingInfo kitInfo = new UserKitingInfo();
            kitInfo.setUserId(userId);
            kitInfo.setUserName(bankInfo.get("CLIENT_NAME").toString());
            kitInfo.setMobile(bankInfo.get("MOBILE_TEL").toString());
            kitInfo.setBankName(bankInfo.get("OPEN_BANK_NAME").toString());
            kitInfo.setBankNo(bankInfo.get("BANK_ACCOUNT").toString());
            kitInfo.setProvince(bankInfo.get("PROVINCE_NAME"));
            kitInfo.setCity(bankInfo.get("CITY_NAME"));
            kitInfo.setSubBranch(subBranch);
            amount *= -1;
            kitInfo.setKitAmount(amount);
            double kitFare = amount * 0.3 / 100;
            if (kitFare < 2) {
                kitFare = 2;
            } else if (kitFare > 200) {
                kitFare = 200;
            }
            kitInfo.setKitFare(kitFare);
            kitInfo.setPayableAmount(amount - kitFare);
            if (result == null) {
                kitInfo.setKitStatus("0");
                msg = "提现失败";
            } else if (result.getCode() != 0) {
                kitInfo.setKitStatus("0");
                msg = "提现失败:" + result.getError().getErrorInfo();
            } else {
                kitInfo.setKitStatus("1");
            }
            log.info("客户[" + userId + "]提现结果:" + result);
            userKitingDao.save(kitInfo);
            session.removeAttribute(SendMessageBiz.RANDOM_NUM);
        }
        log.info(msg);
        return msg;
    }

    
    private T2Result ducureFund(String userId,Double amount){
    	T2Result result = new T2Result();
    	fundRealDao.freezeAmount(userId, Math.abs(amount));
    	result.setCode(0);
    	return result;
    }
    /**
     * 查询用户提现列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> search(Map<String, Object> params) {
        List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
        params.put("userId", ActionUtil.getUser().getClient_id());
        List<UserKitingInfo> list = userKitingDao.searchByUserId(params);
        for (UserKitingInfo userKitingInfo : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("kitAt", DateUtil.parseDate(userKitingInfo.getKitAt(), DateEnum.DATETIME));
            map.put("kitAmount", userKitingInfo.getKitAmount());
            map.put("kitFare", userKitingInfo.getKitFare());
            map.put("payableAmount", userKitingInfo.getPayableAmount());
            switch (Integer.parseInt(userKitingInfo.getKitStatus())) {
                case 0:
                    map.put("kitStatus", "申请失败");
                    break;
                case 1:
                    map.put("kitStatus", "已申请");
                    break;
                case 2:
                    map.put("kitStatus", "处理中");
                    break;
                case 3:
                    map.put("kitStatus", "已成功");
                    break;
                case 4:
                    map.put("kitStatus", "申请否决");
                    break;
                default:
                    break;
            }
            returnList.add(map);
        }
        return returnList;
    }

    /**
     * 查询最近一条提现记录的支行名称
     * @return
     */
    public String querySubBranch() {
        UserKitingInfo kitingInfo = userKitingDao
            .queryLastKitingInfo(ActionUtil.getUser().getClient_id());
        if (kitingInfo == null) {
            return "";
        }
        if (StringUtils.isBlank(kitingInfo.getSubBranch())) {
            return "";
        }
        return kitingInfo.getSubBranch();
    }
}