BatchEntrustBiz.java 5.09 KB
package com.cjs.cms.biz.trade;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

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

import com.cjs.cms.dao.trade.OtcEntrustBatchDao;
import com.cjs.cms.dao.trade.OtcEntrustDao;
import com.cjs.cms.model.trade.OtcEntrustBatchInfo;
import com.cjs.cms.util.t2.T2Result;
import com.cjs.cms.util.t2.T2Util;

/**
 * 批量委托
 * 
 * @author tongyufu
 *
 */
@Service
public class BatchEntrustBiz {

    Logger                     log       = LogManager.getLogger(BatchEntrustBiz.class);
    private AtomicInteger      entrustNo = new AtomicInteger(0);
    @Autowired
    private OtcEntrustDao      otcEntrustDao;
    @Autowired
    private OtcEntrustBatchDao otcEntrustBatchDao;
    ReentrantLock              lock      = new ReentrantLock();

    public void entrust(OtcEntrustBatchInfo entrust, CountDownLatch latch) {
        try {
            latch.countDown();
            int entrustAmount = entrust.getEntrustAmount();
            if (entrust.getBusinessAmount() != null) {
                entrustAmount -= entrust.getBusinessAmount();
            }
            //客户自主下单的op_station为12位
            if (StringUtils.isBlank(entrust.getOpStation())
                || entrust.getOpStation().length() < 20) {
                entrust.setOpStation(entrust.getPositionStr());
            }
            entrust.setEntrustNo(this.getEntrustNo());
            T2Result result = this.entrustService(entrust.getFundAccount(), entrust.getOtcAccount(),
                entrust.getOtcProp(), entrust.getOtcCode(), entrust.getEntrustPrice(),
                entrustAmount, entrust.getPositionStr(), entrust.getEntrustNo());
            entrust.setT2Result(result.toString());
            if (result.getCode() == 0) { //成功
                entrust.setSuccess("1");
            } else {
                entrust.setSuccess("0");
            }
        } catch (Exception e) {
            entrust.setSuccess("0");
            entrust.setT2Result(e.getMessage());
            log.error("批量委托出错:" + entrust.toString(), e);
        } finally {
            try {
                otcEntrustBatchDao.save(entrust);
            } catch (Exception e) {
                log.error("批量委托出错:" + entrust.toString(), e);
            }
        }
    }

    /**
     * 普通委托接口
     * 
     * @param fundAccount 资产账号
     * @param otcAccount 权益账号
     * @param otcProp 0B0:买单;0S0:卖单
     * @param otcCode 权益代码
     * @param entrustPrice 委托价格
     * @param entrustAmount 委托数量
     * @return
     */
    public T2Result entrustService(String fundAccount, String otcAccount, String otcProp,
                                   String otcCode, Double entrustPrice, Integer entrustAmount,
                                   String opStation, Integer entrustNO) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("op_branch_no", "11");
        params.put("operator_no", "2001");
        params.put("user_type", "1");
        params.put("op_password", "");
        params.put("op_station", opStation);
        params.put("op_entrust_way", "4");
        params.put("menu_id", "350311");
        params.put("function_id", "611014");
        params.put("audit_action", "0");
        params.put("action_in", "0");
        params.put("branch_no", "11");
        params.put("client_id", fundAccount);
        params.put("fund_account", fundAccount);
        params.put("password", "");
        params.put("otcexch_type", "O1");
        params.put("otc_account", otcAccount);
        params.put("otc_code", otcCode);
        params.put("entrust_amount", entrustAmount);
        params.put("entrust_price", entrustPrice);
        params.put("otc_prop", otcProp);
        params.put("op_remark", "");
        params.put("entrust_no", entrustNO);
        params.put("batch_no", entrustNO);
        T2Result result = T2Util.request(params, "611014");
        log.info("普通委托单参数:" + params);
        log.info("普通委托单结果:" + result);
        return result;
    }

    public int getEntrustNo() {
        if (entrustNo.get() == 0) {
            lock.lock();
            try {
                if (entrustNo.get() != 0) {
                    return entrustNo.getAndIncrement();
                }
                Integer maxEntrustNo = otcEntrustDao.queryMaxEntrustNo();
                if (maxEntrustNo == null || maxEntrustNo < 1) {
                    entrustNo = new AtomicInteger(1);
                } else {
                    entrustNo = new AtomicInteger(maxEntrustNo + 1);
                }
            } finally {
                lock.unlock();
            }
        }
        return entrustNo.getAndIncrement();
    }

    public void setEntrustNo(AtomicInteger entrustNo) {
        this.entrustNo = entrustNo;
    }

}