CommonBiz.java 7.79 KB
package com.cjs.site.biz.pub;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cjs.site.dao.pub.SysDateDao;
import com.cjs.site.dao.pub.SysParamValueDao;
import com.cjs.site.util.lang.DateEnum;
import com.cjs.site.util.lang.DateUtil;
import com.cjs.site.util.lang.JsonUtil;
import com.cjs.site.util.lang.StringUtil;
import com.cjs.site.util.t2.T2Result;
import com.cjs.site.util.t2.T2Util;
import com.cjs.site.util.web.DesUtil;

import chinapnr.SecureLink;

@Service
public class CommonBiz {

    Logger                   log = LogManager.getLogger();
    @Autowired
    private SysDateDao       sysDao;
    @Autowired
    private SysParamValueDao sysParamValue;
    @Value("${${env}.pnr.operId}")
    private String           operId;
    @Value("${${env}.pnr.pbKey}")
    private String           pbKey;

    public boolean checkTime(boolean checkDate, int beginTimeId, int endTimeId) { if (checkDate) {
        Map params = new HashMap();
        params.put("function_id", "619824");
        T2Result queryResult = T2Util.request(params, "619824");
        if ("0".equals(((Map)queryResult.getData().get(0)).get("business_flag"))) {
          return false;
        }
      }

      int nowtime = Integer.parseInt(DateUtil.getNow(DateEnum.UNSIGNED_TIME));
      String beginTime = this.sysParamValue.getValueById(beginTimeId);
      String endTime = this.sysParamValue.getValueById(endTimeId);
      boolean flag = (Integer.parseInt(beginTime) <= nowtime) && (
        Integer.parseInt(endTime) >= nowtime);
      return flag;
    }
    
    /**
     * 检查开户时间
     * 
     * @return true:开户时间;false:非开户时间
     */
    public boolean checkOpenTime() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("function_id", "619824");
        T2Result queryResult = T2Util.request(params, "619824");
        if ("0".equals(queryResult.getData().get(0).get("business_flag"))) {
            return false;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
        int nowtime = Integer.parseInt(sdf.format(new Date()).substring(11));
        String beginTime = sysParamValue.getParamValue("begin_time_am");
        String endTime = sysParamValue.getParamValue("end_time_pm");
        boolean flag = Integer.parseInt(beginTime) <= nowtime
                       && Integer.parseInt(endTime) >= nowtime;

        if (!flag) {
            return false;
        }
        return true;
    }

    public boolean checkKitingTime() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("function_id", "619824");
        T2Result queryResult = T2Util.request(params, "619824");
        if ("0".equals(queryResult.getData().get(0).get("business_flag"))) {
            return false;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
        int nowtime = Integer.parseInt(sdf.format(new Date()).substring(11));
        String beginTime = sysParamValue.getParamValue("begin_time_fund");
        String endTime = sysParamValue.getParamValue("end_time_fund");
        boolean flag = Integer.parseInt(beginTime) <= nowtime
                       && Integer.parseInt(endTime) >= nowtime;

        if (!flag) {
            return false;
        }
        return true;
    }

    public boolean checkChargeTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
        int nowtime = Integer.parseInt(sdf.format(new Date()).substring(11));
        String beginTime = sysParamValue.getParamValue("begin_time_charge");
        String endTime = sysParamValue.getParamValue("end_time_charge");
        boolean flag = Integer.parseInt(beginTime) <= nowtime
                       && Integer.parseInt(endTime) >= nowtime;
        if (!flag) {
            return false;
        }
        return true;
    }

    /**
     * 检查交易所交易状态
     * 
     * @return business_flag:1:初始化;2:连续竞价;3:闭市
     */
    public Integer checkMarketStatus() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("mchexch_type", "O1"); //交易撮合市场
        T2Result result = T2Util.request(params, "649013");
        log.debug("检查交易所交易状态:" + result);
        return Integer.parseInt(result.getData().get(0).get("business_flag").toString());
    }

    /**
     * 汇付天下接口参数加签
     * 
     * @param params 请求参数
     * @return
     */
    public String encrypt(Map<String, String> params) {
        StringBuilder build = new StringBuilder();
        for (String value : params.values()) {
            build.append(value);
        }
        log.info(build.toString());
        String MsgBuf = DesUtil.MD5(build.toString());
        SecureLink secureLink = new SecureLink();
        String path = CommonBiz.class.getResource("/pay/").getFile();
        try {
            secureLink.SignMsg(operId, path + "MerPrK" + operId + ".key", MsgBuf.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return secureLink.getChkValue();
    }

    @SuppressWarnings("unchecked")
    public String decrypt(String retStr) {
        Map<String, String> params = JsonUtil.fromJson(retStr, HashMap.class);
        String[] str = retStr.split("\r\n");
        StringBuilder build = new StringBuilder();
        String ChkValue = "";
        for (int i = 0; i < str.length; i++) {
            if (!str[i].substring(1, str[i].indexOf("=")).equals("check_value")) {
                build.append(str[i].substring(str[i].indexOf("=") + 1));
            } else {
                ChkValue = str[i].substring(str[i].indexOf("=") + 1);
            }
            params.put(str[i].substring(0, str[i].indexOf("=")),
                str[i].substring(str[i].indexOf("=") + 1));
        }
        //        SecureLink secureLink = new SecureLink();
        //        String PgKeyFile = CommonBiz.class.getResource("/pay/").getFile() + pbKey;
        //        int ret = secureLink.VeriSignMsg(PgKeyFile, build.toString(), ChkValue);
        //        if (ret != 0) {
        //            log.info("签名错误 ret=" + ret);
        //            throw new RuntimeException("签名错误");
        //        } else {
        //            return params.get("RespCode");
        //        }
        return params.get("RespCode");
    }

    /**
     * 获得数据库的当前时间
     */
    public Date getSysdate() {
        return sysDao.getSysdate();//获得数据库当前日期
    }

    public boolean isWorkingDay(String date) {
        return false;

    }

    /** 格式化日期为恒生的格式 */
    public void formatDate(Map<String, Object> params) {
        String startDate = StringUtil.castString(params.get("startDate"));
        String endDate = StringUtil.castString(params.get("endDate"));
        params.put("startDate", this.formatDate(startDate));
        params.put("endDate", this.formatDate(endDate));
    }

    /** 格式化日期为恒生的格式 */
    public String formatDate(String date) {
        if (StringUtils.isBlank(date)) {
            return "";
        }
        if (date.length() > 8) {
            Date date2 = DateUtil.parseDate(date, DateEnum.DATE);
            return DateUtil.parseDate(date2, DateEnum.UNSIGNED_DATE);
        } else {
            Date date2 = DateUtil.parseDate(date, DateEnum.UNSIGNED_DATE);
            return DateUtil.parseDate(date2, DateEnum.DATE);
        }
    }
}