package com.chinacreator.process.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.chinacreator.process.bean.UserFakeidBean;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;

@Component
public class UserFakeidDao{
	
  private static Logger logger = Logger.getLogger(UserFakeidDao.class);

  @SuppressWarnings("unchecked")
  public List<UserFakeidBean> queryNeedQuery(){
	StringBuffer sb = new StringBuffer(" select * from net3g.TB_USERID_FAKEID where status='1' and ( ");
	sb.append(" (querycount=1 and querytime<sysdate-1/144) ");
	sb.append(" or (querycount=2 and querytime<sysdate-1/36) ");
	sb.append(" or (querycount=3 and querytime<sysdate-1/24) ");
	sb.append(" or (querycount=4 and querytime<sysdate-1/6) ");
	sb.append(" or (querycount=5 and querytime<sysdate-5/12) ");
	sb.append(" or (querycount=6 and querytime<sysdate-1) ");
	sb.append(" or (querycount=7 and querytime<sysdate-2) ");
	sb.append(" ) order by orderid");
	
    PreparedDBUtil pdb = new PreparedDBUtil();
    try {
      return pdb.executeSelectForList("net3g", sb.toString(), 0L, 500, UserFakeidBean.class);
    } catch (Exception e) {
      logger.error("查询需要重新取电商伪码的数据失败", e);
    }
    return null;
  }

  public void fakeidSuccess(String orderid,String userid,String fakeid) {
    String sql = "update net3g.TB_USERID_FAKEID set status='0',fakeid=?,querytime=sysdate,querycount=querycount+1 where orderid=? and userid=? and status='1'";
    try {
      SQLExecutor.updateWithDBName("net3g", sql, new Object[] { fakeid, orderid,userid });
    } catch (SQLException e) {
      logger.error("更改TB_USERID_FAKEID失败", e);
    }
  }
  
  public void fakeidFail(String orderid,String userid) {
	  String sql = "update net3g.TB_USERID_FAKEID set querytime=sysdate,querycount=querycount+1 where orderid=? and userid=? and status='1'";
	  try {
		  SQLExecutor.updateWithDBName("net3g", sql, new Object[] { orderid,userid });
	  } catch (SQLException e) {
		  logger.error("更改TB_USERID_FAKEID失败", e);
	  }
  }
  
  /**
   * 更新同步状态，重新同步
   * 
   * @param orderid
   * @param userid
   * @param fakeid
   * @author 科创·毛燕龙
   * @datetime 2019年4月12日 上午11:36:43
   */
  public void updateSyncFakeid(String orderid,String userid,String fakeid) {
	  String sql = "update net3g.TD_ORDER_RELATIONS set fakeid=?,synccount=0,syncsucceed='1' where id=? and userid=?";
	  try {
		  SQLExecutor.updateWithDBName("net3g", sql, new Object[] { fakeid,orderid,userid });
	  } catch (SQLException e) {
		  logger.error("更改TD_ORDER_RELATIONS失败", e);
	  }
  }
  
  
}