📄 accountmoneydao.java
字号:
package com.ufmobile.business.account.dao;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.ufmobile.business.account.bo.Account.AccountType;
import com.ufmobile.business.account.entity.AccmoneyEntity;
import com.ufmobile.business.exception.AccountException;
import com.ufmobile.common.dao.UFMobileDAO;
public class AccountMoneyDAO extends UFMobileDAO {
private static String mutex = "Lock";
public AccountMoneyDAO(EntityManager manager) {
super(manager);
// TODO Auto-generated constructor stub
}
public AccountMoneyDAO() {
super();
// TODO Auto-generated constructor stub
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 18, 2006
*
* @param accountid
* @return
* @throws AccountException
*/
public List<AccmoneyEntity> add(String accountid) throws AccountException {
List<AccmoneyEntity> ret = new ArrayList<AccmoneyEntity>();
for (int i = 0; i < 2; i++) {
AccmoneyEntity moneyEntity = new AccmoneyEntity();
switch (i) {
case 0:
moneyEntity.setAcctype(AccountType.Flow);
break;
case 1:
moneyEntity.setAcctype(AccountType.Service);
break;
}
moneyEntity.setAccountid(accountid);
getManager().persist(moneyEntity);
ret.add(moneyEntity);
}
return ret;
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 18, 2006
*
* @param accountid
* @param Type
* @param money
* @return
* @throws AccountException
*/
public AccmoneyEntity deduct(String accountid, AccountType Type, BigDecimal money) throws AccountException {
AccmoneyEntity moneyEntity=null;
synchronized (mutex) {
Query query = getManager().createQuery("select c from AccmoneyEntity c where acctype=:itype and accountid=:accountid").setParameter("itype", Type).setParameter("accountid", accountid);
List l = query.getResultList();
// 校验账户是否充值
if (l == null || l.size() == 0) {
throw new AccountException("该账户不存在");
}
// 校验余额
moneyEntity = (AccmoneyEntity) l.get(0);
if (moneyEntity.getAccin().add(moneyEntity.getBack()).subtract(moneyEntity.getAccout()).subtract(money).doubleValue() < -0.001) {
throw new AccountException("店铺余额不足无法使用,请及时充值");
}
// 扣费
moneyEntity.setAccout(moneyEntity.getAccout().add(money));
getManager().merge(moneyEntity);
getManager().flush();
}
return moneyEntity;
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 18, 2006
*
* @param accountid
* @param Type
* @param money
* @return
* @throws AccountException
*/
public AccmoneyEntity fillMoney(String accountid, AccountType Type, BigDecimal money) throws AccountException {
Query query = getManager().createQuery(" from AccmoneyEntity where acctype=:type and accountid=:accountid").setParameter("type", Type).setParameter("accountid", accountid);
List l = query.getResultList();
AccmoneyEntity moneyEntity = null;
if (l == null || l.size() == 0) {
moneyEntity = new AccmoneyEntity();
moneyEntity.setAcctype(Type);
moneyEntity.setAccin(money);
moneyEntity.setAccountid(accountid);
getManager().persist(moneyEntity);
} else {
moneyEntity = (AccmoneyEntity) l.get(0);
moneyEntity.setAccin(moneyEntity.getAccin().add(money));
getManager().merge(moneyEntity);
}
return moneyEntity;
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 18, 2006
*
* @param accountid
* @param Type
* @param money
* @return
* @throws AccountException
*/
public AccmoneyEntity back(String accountid, AccountType Type, BigDecimal money) throws AccountException {
Query query = getManager().createQuery("select c from AccmoneyEntity c where acctype=:itype and accountid=:accountid").setParameter("itype", Type).setParameter("accountid", accountid);
List l = query.getResultList();
// 校验账户是否充值
if (l == null || l.size() == 0) {
throw new AccountException("该账户还没有充值");
}
AccmoneyEntity moneyEntity = (AccmoneyEntity) l.get(0);
// 返款
moneyEntity.setBack(moneyEntity.getBack().add(money));
getManager().merge(moneyEntity);
return moneyEntity;
}
/**
* <p>
* <p>
* 作者:msf <br>
* 日期:Dec 18, 2006
*
* @param accountid
* @param Type
* @return
*/
public AccmoneyEntity getAccmoneyEngtity(String accountid, AccountType Type) {
return (AccmoneyEntity) getManager().createQuery("from AccmoneyEntity where acctype=:itype and accountid=:accountid").setParameter("itype", Type).setParameter("accountid", accountid).getSingleResult();
}
public List<AccmoneyEntity> getAccmoneyEngtity(String accountid) {
return getManager().createQuery("from AccmoneyEntity where accountid=:accountid").setParameter("accountid", accountid).getResultList();
}
public void deleteByaccountid(String accountid) {
getManager().createQuery("delete from AccmoneyEntity where accountid=:id").setParameter("id", accountid).executeUpdate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -