📄 accountdao.java
字号:
package com.ufmobile.business.account.dao;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import com.ufmobile.business.account.entity.AccountEntity;
import com.ufmobile.business.exception.AccountException;
import com.ufmobile.common.dao.UFMobileDAO;
import com.ufmobile.mstreet.util.SqlUtil;
/**
* <p>
* 账户dao
* <p>
* 创建日期:Dec 15, 2006
*
* @author msf
* @since v3.0
*/
public class AccountDAO extends UFMobileDAO {
private static String sCodeFormate = "0000000000";
public AccountDAO(EntityManager manager) {
super(manager);
}
public AccountDAO() {
super();
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 15, 2006
*
* @param account 要创建账户值
* @return
* @throws AccountException
*/
public AccountEntity add(AccountEntity account) throws AccountException {
if (account == null) {
account = new AccountEntity();
}
//
synchronized (sCodeFormate) {
account.setId(getUUID());
account.setCode(getNextCode());
account.setAccountstart(Boolean.TRUE);
account.setCreatetime(new java.sql.Date(System.currentTimeMillis()));
getManager().persist(account);
}
return account;
}
public void delete(String accountid) throws AccountException {
getManager().createQuery("delete from AccountEntity where id=:id").setParameter("id", accountid).executeUpdate();
// AccountEntity enti = getManager().find(AccountEntity.class,
// accountid);
// if (enti != null)
// getManager().remove(accountid);
}
public void modify(AccountEntity account) throws AccountException {
getManager().merge(account);
}
public List querybyAgent(Long agentid, int type) throws AccountException {
return null;
}
public void start(String accountid) throws AccountException {
updateState(accountid, Boolean.TRUE);
}
public void stop(String accountid) throws AccountException {
updateState(accountid, Boolean.FALSE);
}
private void updateState(String accountid, Boolean flag) {
// getManager().createNamedQuery("")
getManager().createQuery("update AccountEntity set accountstart=:state and id=:id").setParameter("state", flag).setParameter("id", accountid).executeUpdate();
}
private String getNextCode() {
String Sql = "select max(code) from tb_business_account ";
List s = getManager().createNativeQuery(Sql).getResultList();
if (s == null || s.size() == 0 || s.get(0) == null) {
return sCodeFormate.substring(0, sCodeFormate.length() - 1) + 1;
} else {
String scode = s.get(0).toString();
int istart = 0;
for (; istart < scode.length(); istart++) {
if (scode.charAt(istart) > '0')
break;
}
int x = Integer.parseInt(scode.substring(istart)) + 1;
return sCodeFormate.substring(0, sCodeFormate.length() - ("" + x).length()) + x;
}
}
// 根据id查询
public AccountEntity querybyAccountID(String sID) {
return getManager().find(AccountEntity.class, sID);
}
/**
* <p>
* 查询账户总记录数
* <p>
* 作者:Janet Feng <br>
* 日期:Dec 21, 2006
*
* @param accountCode 账户编码
* @param accountName 账户名称
* @param from 账户创建时间,开始
* @param to 账户创建时间,结束
* @return
*/
public int queryAccountTotal(String accountCode, String accountName, Date from, Date to, int isStarted) throws AccountException {
StringBuffer buf = constructQueryString(true, accountCode, accountName, from, to, isStarted);
Query query = this.getManager().createQuery(buf.toString());
if (from != null && to != null && from.before(to)) {
query.setParameter("from", from, TemporalType.DATE);
query.setParameter("to", to, TemporalType.DATE);
}
return ((Long) query.getSingleResult()).intValue();
}
/**
* <p>
* 分页查询账户
* <p>
* 作者:Janet Feng <br>
* 日期:Dec 21, 2006
*
* @param beginIndex 记录开始索引值
* @param maxNumber 查询的记录总数
* @param accountCode 账户编码
* @param accountName 账户名称
* @param from 账户创建时间,开始
* @param to 账户创建时间,结束
* @return
* @throws AccountException
*/
public List<AccountEntity> queryAccountByPage(int beginIndex, int maxNumber, String accountCode, String accountName, Date from, Date to, int isStarted) throws AccountException {
StringBuffer buf = constructQueryString(false, accountCode, accountName, from, to, isStarted);
Query query = this.getManager().createQuery(buf.toString());
if (from != null && to != null && from.before(to)) {
query.setParameter("from", from, TemporalType.DATE);
query.setParameter("to", to, TemporalType.DATE);
}
if (beginIndex > -1)
query.setFirstResult(beginIndex);
if (maxNumber > -1)
query.setMaxResults(maxNumber);
return query.getResultList();
}
/**
* <p>
* <p>
* 作者:Janet Feng <br>
* 日期:Dec 21, 2006
*
* @param bCountTotal
* @param accountCode
* @param accountName
* @param from
* @param to
* @return
*/
private StringBuffer constructQueryString(boolean bCountTotal, String accountCode, String accountName, Date from, Date to, int isStarted) {
StringBuffer buf = new StringBuffer();
if (bCountTotal)
buf.append("select count(*) from AccountEntity where 1=1");
else
buf.append("from AccountEntity where 1=1");
if (accountCode != null && accountCode.trim().length() != 0)
buf.append(" and code like '%").append(SqlUtil.convert2SqlStr(accountCode)).append("%'");
if (accountName != null && accountName.trim().length() != 0)
buf.append(" and name like '%").append(SqlUtil.convert2SqlStr(accountName)).append("%'");
if (isStarted > -1)
buf.append(" and accountstart = ").append(isStarted);
if (from != null && to != null && from.before(to)) {
buf.append(" and ( createtime>= :from and createtime<= :to )");
}
return buf;
}
/**
* <p>
* 批量更改状态
* <p>
* 作者:Janet Feng <br>
* 日期:Jan 6, 2007
*
* @param isStarted
* @param accountIds
*/
public void changeAccountState(Boolean isStarted, String... accountIds) {
if (isStarted == null || accountIds == null || accountIds.length == 0)
return;
StringBuffer buf = new StringBuffer();
buf.append("update AccountEntity set accountstart=").append(isStarted);
buf.append(" where id in ('");
for (int i = 0; i < accountIds.length; i++) {
buf.append(accountIds[i]);
if (i < accountIds.length - 1)
buf.append("','");
else
buf.append("')");
}
getManager().createQuery(buf.toString()).executeUpdate();
}
/**
* <p>
* 批量删除
* <p>
* 作者:Janet Feng <br>
* 日期:Jan 6, 2007
*
* @param accountIds
*/
public void batchDelete(String... accountIds) {
if (accountIds == null || accountIds.length == 0)
return;
StringBuffer buf = new StringBuffer();
buf.append("delete from AccountEntity ");
buf.append(" where id in ('");
for (int i = 0; i < accountIds.length; i++) {
buf.append(accountIds[i]);
if (i < accountIds.length - 1)
buf.append("','");
else
buf.append("')");
}
getManager().createQuery(buf.toString()).executeUpdate();
}
/**
* <p>
* 修改代理商编号
* <p>
* 作者:msf <br>
* 日期:Feb 28, 2007
* @param agentCode
* @param accountId
*/
public void changeAccountAgent(String agentCode, String accountId) {
if ( accountId== null )
return;
StringBuffer buf = new StringBuffer();
buf.append("update AccountEntity set AGENT='").append(agentCode).append("' where id='").append(accountId).append("'");
getManager().createQuery(buf.toString()).executeUpdate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -