package com.lgx8.gateway.dao.impl;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.springframework.transaction.annotation.Transactional;
import com.lgx8.common.dao.impl.BaseDao;
import com.lgx8.gateway.dao.ICardDao;
import com.lgx8.gateway.entities.Card;
import com.lgx8.gateway.entities.MemberRecord;
import com.lgx8.right.common.EncryptUtil;
import com.lgx8.right.entities.User;
/**
*
* @author lihui
*
*/
public class CardDao extends BaseDao implements ICardDao {
public Card createCard() {
Card card = new Card();
card.setId(getNewCardId());
getHibernateTemplate().persist(card);
return card;
}
public void updateCard(Card card) {
getHibernateTemplate().update(card);
}
public Card findCard(String id) {
return getHibernateTemplate().get(Card.class, id);
}
private String getNewCardId() {
String hql = " from Card c where c.id like '9%' order by c.id desc";
@SuppressWarnings("unchecked")
List<Card> cards = this.getHibernateTemplate().find(hql);
if(cards.size() == 0)
{
return "900000000001";
}else
{
return (Long.parseLong(cards.get(0).getId())+1l)+"";
}
}
public Card createCard(String cardNum) {
Card card = new Card();
card.setId(cardNum);
getHibernateTemplate().persist(card);
return card;
}
@Transactional
public Card createUserByCard(String card, Double score, long parenttype,
long usertype, long organizationid, User parent, User opener,String mobile,String email) {
String password = createRandomPassowrd(5);
Card car= new Card();
car.setId(card);
car.setScore(score);
car.setPassword(password);
this.getHibernateTemplate().persist(car);
String md5Passwd = EncryptUtil.toMD5(password);
User user = new User();
user.setPassword(md5Passwd);
user.setLocked(false);
user.setEnabled(true);
user.setOpener(opener);
user.setParent(parent);
user.setUsertype(usertype);
user.setParenttype(parenttype);
user.setOrganizationid(organizationid);
user.setRegistertime(new Date());
user.setCard(car);
user.setUsername(card);
user.setLastlogin(new Date());
if(mobile != null && !"".equals(mobile))
user.setMobile(mobile);
if(email != null && !"".equals(email))
user.setEmail(email);
this.getHibernateTemplate().persist(user);
return car;
}
/**
* 生成密码,密码动态生成
* @param point
* @return
*/
private String createRandomPassowrd(int point)
{
String pws = "";
String numberChars = "0,1,2,3,4,5,6,7,8,9";
String lowerChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
String upperChars = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String otherChars = "!,@,#,$,%,&,*,?";
String[] ncs = {numberChars,lowerChars,upperChars,otherChars};
Random random = new Random();
int p = random.nextInt(lowerChars.split(",").length-1);
pws += lowerChars.split(",")[p];
for(int i=1; i <= point ; i++)
{
int p1 = random.nextInt(ncs.length-1);
int p2 = random.nextInt(numberChars.split(",").length-1);
int p3 = random.nextInt(lowerChars.split(",").length-1);
int p4 = random.nextInt(upperChars.split(",").length-1);
int p5 = random.nextInt(otherChars.split(",").length-1);
if(p1 == 0)
{
pws += numberChars.split(",")[p2];
}else if(p1 == 1)
{
pws += lowerChars.split(",")[p3];
}else if(p1 == 2)
{
pws += upperChars.split(",")[p4];
}else if(p1 == 3)
{
pws += otherChars.split(",")[p5];
}
}
return pws;
}
public Boolean checkCardExists(String card) {
String hql = " from Card c where c.id = '"+card+"'";
@SuppressWarnings("unchecked")
List<Card> cards = this.getHibernateTemplate().find(hql);
if(cards.size() > 0)
{
return true;
}
return false;
}
@SuppressWarnings("unchecked")
public List<Card> findCardByConditions(Long userId,String cardStart, String cardEnd) {
String hql = "select c from Card c,User u where c.id = u.card.id";
if(userId != null && userId != 0)
{
hql += " and u.parent.id = " + userId;
}
if(cardStart != null && !"".equals(cardStart))
{
hql += " and c.id >= '"+cardStart+"'";
}
if(cardEnd != null && !"".equals(cardEnd))
{
hql += " and c.id <= '"+cardEnd+"'";
}
hql += " order by c.id asc";
return this.getHibernateTemplate().find(hql);
}
public String findLastCardByOrg() {
String hql = " from Card c where c.id like '9%' order by c.id desc";
@SuppressWarnings("unchecked")
List<Card> cards = this.getHibernateTemplate().find(hql);
if(cards.size() == 0)
{
return "900000000001";
}else
{
return (Long.parseLong(cards.get(0).getId())+1l)+"";
}
}
@Transactional
public MemberRecord createRecord(MemberRecord record) {
this.getHibernateTemplate().persist(record);
return record;
}
public String checkCardExists(String card, User user,Boolean sign) {
String startCard = user.getCardStart();
String endCard = user.getCardEnd();
if(sign && user.getParent() != null&&(user.getParent().getUsertype() == 2 || user.getParent().getUsertype() == 3 ))
{
startCard = user.getParent() .getCardStart();
endCard = user.getParent() .getCardEnd();
}
Boolean flag = false;
for(int i = 0 ; i < startCard.split(",").length;i++)
{
if(Long.parseLong(startCard.split(",")[i]) <= Long.parseLong(card) && Long.parseLong(endCard.split(",")[i]) >= Long.parseLong(card) )
{
flag = true;
break;
}
}
String result= "2";
if(flag)
{
String hql = " from Card c where c.id = '"+card+"'";
@SuppressWarnings("unchecked")
List<Card> cards = this.getHibernateTemplate().find(hql);
if(cards.size() > 0)
{
result= "3";
}else
{
result= "4";
}
}
return result;
}
}