package com.topsun.posclient.common.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.codec.digest.DigestUtils;
import com.topsun.posclient.common.AppConstants;
import com.topsun.posclient.common.LoggerUtil;
import com.topsun.posclient.common.POSClientApp;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.common.ProjectUtil;
import com.topsun.posclient.common.core.CommonCoreActivator;
import com.topsun.posclient.common.dao.BaseDao;
import com.topsun.posclient.common.service.IBaseService;
import com.topsun.posclient.datamodel.CardRule;
import com.topsun.posclient.datamodel.CashierModel;
import com.topsun.posclient.datamodel.GoldPrice;
import com.topsun.posclient.datamodel.Invoice;
import com.topsun.posclient.datamodel.Item;
import com.topsun.posclient.datamodel.Material;
import com.topsun.posclient.datamodel.Member;
import com.topsun.posclient.datamodel.PointData;
import com.topsun.posclient.datamodel.Shop;
import com.topsun.posclient.datamodel.User;
import com.topsun.posclient.datamodel.dto.CardRuleDTO;
import com.topsun.posclient.datamodel.dto.CashierModeDTO;
import com.topsun.posclient.datamodel.dto.GoldPriceDTO;
import com.topsun.posclient.datamodel.dto.InvoiceDTO;
import com.topsun.posclient.datamodel.dto.ItemDTO;
import com.topsun.posclient.datamodel.dto.MaterialDTO;
import com.topsun.posclient.datamodel.dto.MemberDTO;
import com.topsun.posclient.datamodel.dto.PointDataDTO;
import com.topsun.posclient.datamodel.dto.ShopDTO;
import com.topsun.posclient.datamodel.dto.UserDTO;
/**
* @author Dong
*
*/
public class BaseServiceImpl implements IBaseService {
BaseDao baseDao = new BaseDao();
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getUserData()
*/
public UserDTO getUserData() throws Exception {
return baseDao.getUserData();
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getUserById(int)
*/
public UserDTO getUserById(int userId) throws Exception {
return baseDao.getUserById(userId);
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getEmployeeNameById(int)
*/
public String getEmployeeNameById(int userId) throws Exception {
return baseDao.getEmployeeNameById(userId);
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getAllCashierMode()
*/
public CashierModeDTO getAllCashierMode() throws POSException {
try {
return baseDao.getAllCashierMode();
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询结算方式出错", e);
throw new POSException("查询结算方式出错" + e.getStackTrace());
}
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getAllItem()
*/
public ItemDTO getAllItem() throws Exception {
return baseDao.getAllItem();
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getAllShop()
*/
public ShopDTO getAllShop() throws Exception {
return baseDao.getAllShop();
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getShopNameById(int)
*/
public String getShopNameById(int shopId) throws Exception {
return baseDao.getShopNameById(shopId);
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getItemNameById(int)
*/
public String getItemNameById(int itemId) throws Exception {
return baseDao.getItemNameById(itemId);
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getShopNameByUserId(int)
*/
public String getShopNameByUserId(int userId) throws POSException {
UserDTO userDto = null;
try {
userDto = baseDao.getUserById(userId);
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询店铺名称出错", e);
throw new POSException("查询店铺名称出错");
}
List<User> userList = userDto.getUserList();
if (null == userList) {
return "";
}
User user = userList.get(0);
if (null == user) {
return "";
}
return user.getDeptName();
}
/**
* 获取所有用户
* @return
* @throws POSException
*/
public List<User> getAllUser() throws POSException {
UserDTO userDTO = null;
List<User> userList = null;
try {
userDTO = baseDao.getUserData();
if (null != userDTO) {
userList = userDTO.getUserList();
}
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询用户出错", e);
throw new POSException("查询用户出错");
}
return userList;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#createNo(int)
*/
public String createNo(int type) throws POSException {
String docNum = "";
switch (type) {
case AppConstants.DOC_TYPE_SALES:
docNum = "L" + POSClientApp.get().getSysConfig().getOwnerShop()
+ POSClientApp.get().getSysConfig().getPosNo()
+ ProjectUtil.generateDocNum(AppConstants.DOC_TYPE_SALES);//
break;
case AppConstants.DOC_TYPE_OLDGOLD:
docNum = "J" + POSClientApp.get().getSysConfig().getOwnerShop()
+ POSClientApp.get().getSysConfig().getPosNo()
+ ProjectUtil.generateDocNum(AppConstants.DOC_TYPE_OLDGOLD);//
break;
case AppConstants.DOC_TYPE_BOOKINGGOLD:
docNum = "B" + POSClientApp.get().getSysConfig().getOwnerShop()
+ POSClientApp.get().getSysConfig().getPosNo()
+ ProjectUtil.generateDocNum(AppConstants.DOC_TYPE_BOOKINGGOLD);//
break;
case AppConstants.DOC_TYPE_GOLDBUYBACK:
docNum = "G" + POSClientApp.get().getSysConfig().getOwnerShop()
+ POSClientApp.get().getSysConfig().getPosNo()
+ ProjectUtil.generateDocNum(AppConstants.DOC_TYPE_GOLDBUYBACK);//
break;
case AppConstants.DOC_TYPE_RETURNED:
docNum = "R" + POSClientApp.get().getSysConfig().getOwnerShop()
+ POSClientApp.get().getSysConfig().getPosNo()
+ ProjectUtil.generateDocNum(AppConstants.DOC_TYPE_RETURNED);//
break;
}
return docNum;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getShopById(int)
*/
public Shop getShopById(int shopId) throws Exception {
return baseDao.getShopById(shopId);
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getGoldPriceByMtartCode(java.lang.String)
*/
public BigDecimal getGoldPriceByMtartCode(String mtartCode) throws POSException {
GoldPrice returnGoldPrice = null;
GoldPriceDTO goldPriceDTO = null;
MaterialDTO materialDTO = null;
BigDecimal retPrice = null;
String matkl = "";
try {
materialDTO = baseDao.getAllMaterial();
if(null != materialDTO && null != materialDTO.getMaterialList() && materialDTO.getMaterialList().size() > 0){
for (Material material : materialDTO.getMaterialList()) {
if (material.getMatnr().equals(mtartCode)) {
matkl = material.getMatkl();
}
}
}
if("".equals(matkl)){
throw new POSException("没有相应的物料组数据");
}
goldPriceDTO = baseDao.getAllGoldPrice();
if (null != goldPriceDTO) {
List<GoldPrice> goldPriceList = goldPriceDTO.getGoldPriceList();
if (null == goldPriceList || goldPriceList.size() <= 0) {
return null;
}
for (GoldPrice goldPrice : goldPriceList) {
if (goldPrice.getMtartCode().equals(matkl)) {
returnGoldPrice = goldPrice;
}
}
}
if (null != returnGoldPrice) {
if(null == returnGoldPrice.getPrice() || "".equals(returnGoldPrice.getPrice())){
retPrice = new BigDecimal(0);
}else{
retPrice = new BigDecimal(returnGoldPrice.getPrice());
}
}
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询实时金价信息出错", e);
throw new POSException("查询实时金价信息出错");
}
if(null == retPrice){
throw new POSException("没有相应的实时金价数据");
}
return retPrice;
}
/*
* (non-Javadoc)
*
* @see com.topsun.posclient.common.service.IBaseService#queryAllGoldPrice()
*/
public GoldPriceDTO queryAllGoldPrice() throws POSException {
GoldPriceDTO goldPriceDTO = null;
try {
goldPriceDTO = baseDao.getAllGoldPrice();
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询实时金价信息出错", e);
throw new POSException("查询实时金价信息出错");
}
return goldPriceDTO;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getItemByBarCode(java.lang.String)
*/
public Item getItemByBarCode(String barCode) throws POSException {
Item item = null;
try {
item = baseDao.getItemByBarCode(barCode);
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "单品查询出错,条形码【"+barCode+"】", e);
throw new POSException("单品查询出错,条形码【"+barCode+"】");
}
if(null == item){
throw new POSException("没有相应的单品数据");
}
return item;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getNameByPayModeId(int)
*/
public String getNameByPayModeId(int payModeId) throws POSException {
String name = "";
CashierModeDTO cmDTO = this.getAllCashierMode();
if(null == cmDTO){
return name;
}
List<CashierModel> cmList = cmDTO.getCashierModeList();
if(null == cmList || cmList.size() == 0){
return name;
}
for(CashierModel cm : cmList){
if(cm.getID() != payModeId){
continue;
}
name = cm.getName();
}
return name;
}
public CashierModel getCashierModelById(int payModeId) throws POSException {
CashierModel ret = null;
CashierModeDTO cmDTO = this.getAllCashierMode();
if(null == cmDTO){
return ret;
}
List<CashierModel> cmList = cmDTO.getCashierModeList();
if(null == cmList || cmList.size() == 0){
return ret;
}
for(CashierModel cm : cmList){
if(cm.getID() != payModeId){
continue;
}
ret = cm;
}
return ret;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getNameByMatkl(java.lang.String)
*/
public String getNameByMatkl(String matkl) throws POSException {
String name = "";
MaterialDTO materialDTO = null;
try {
materialDTO = baseDao.getAllMaterial();
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询物料信息出错", e);
throw new POSException("查询物料信息出错!");
}
List<Material> list = materialDTO.getMaterialList();
if(null == list || list.size() == 0){
return name;
}
for(Material m : list){
if(m.getMatkl().equals(matkl)){
name = m.getMaktx();
}
}
return name;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getNameByMatnr(java.lang.String)
*/
public String getNameByMatnr(String matnr) throws POSException {
String name = "";
MaterialDTO materialDTO = null;
try {
materialDTO = baseDao.getAllMaterial();
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询物料信息出错", e);
throw new POSException("查询物料信息出错!");
}
List<Material> list = materialDTO.getMaterialList();
if(null == list || list.size() == 0){
return name;
}
for(Material m : list){
if(m.getMatnr().equals(matnr)){
name = m.getMaktx();
}
}
return name;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getMaterialByMatnr(java.lang.String)
*/
public Material getMaterialByMatnr(String matnrCode) throws POSException {
Material material = null;
MaterialDTO materialDTO = null;
try {
materialDTO = baseDao.getAllMaterial();
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询物料信息出错", e);
throw new POSException("查询物料信息出错");
}
List<Material> list = materialDTO.getMaterialList();
if(null == list || list.size() == 0){
return material;
}
for(Material m : list){
if(m.getMatnr().equals(matnrCode)){
material = m;
}
}
if(null == material){
throw new POSException("没有相应的物料组");
}
return material;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getMatklByMatnr(java.lang.String)
*/
public String getMatklByMatnr(String matnr) throws POSException {
Material material = getMaterialByMatnr(matnr);
if(null != material){
return material.getMatkl();
}else{
throw new POSException("没有找到对应的物料组");
}
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getMemberByCardNo(java.lang.String)
*/
public Member getMemberByCardNo(String cardNo) throws POSException {
MemberDTO memberDTO = null;
List<Member> memberList = null;
try {
memberDTO = baseDao.getAllMember();
if (null != memberDTO) {
memberList = memberDTO.getMemberList();
}
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "获取会员信息出错", e);
throw new POSException("获取会员信息出错");
}
if(null != memberList && memberList.size() > 0){
return memberList.get(0);
}else{
return null;
}
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getAmountByPoint(java.math.BigDecimal)
*/
public BigDecimal getAmountByPoint(BigDecimal inputPoint) throws POSException {
List<PointData> ret = new ArrayList<PointData>();
PointDataDTO pointDataDTO = null;
try {
pointDataDTO = baseDao.getAllPointData();
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "获取积分兑换数据出错", e);
throw new POSException("获取积分兑换数据出错");
}
if(null == pointDataDTO){
throw new POSException("本地没有积分兑换数据,请同步数据");
}
List<PointData> pointDataList = pointDataDTO.getPointDataList();
if(null == pointDataList || pointDataList.size() <= 0){
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "本地没有积分兑换数据,请同步数据");
throw new POSException("本地没有积分兑换数据,请同步数据");
}
for(PointData pd : pointDataList){
Date nowdate=new Date();
Date startDate = ProjectUtil.getDateByFormat(pd.getStartDate(), "yyyy-MM-dd");
Date endDate = ProjectUtil.getDateByFormat(pd.getEndDate(), "yyyy-MM-dd");
boolean isStart = nowdate.after(startDate);
boolean isEnd = nowdate.before(endDate);
if(isStart && isEnd){
ret.add(pd);
}
}
if(ret.size() <= 0 || null == ret.get(0)){
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "没有有效的积分兑换数据");
throw new POSException("没有有效的积分兑换数据");
}
PointData retPd = ret.get(0);
BigDecimal ratio = new BigDecimal(retPd.getRatio()); //积分数
BigDecimal exchangeMoney = new BigDecimal(retPd.getExchangeMoney());//兑换金额
BigDecimal onePointExchangMoney = inputPoint.multiply(exchangeMoney).divide(ratio, 4, BigDecimal.ROUND_HALF_UP);//1积分兑换金额;
onePointExchangMoney = onePointExchangMoney.setScale(2, BigDecimal.ROUND_HALF_UP);
return onePointExchangMoney;//1积分兑换金额;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getCardRuleByCardType(java.lang.String)
*/
public CardRule getCardRuleByCardType(String cardType) throws POSException {
CardRuleDTO cardRuleDTO = null;
List<CardRule> cardRuleList = null;
try {
cardRuleDTO = baseDao.getAllCardRule();
if (null != cardRuleDTO) {
cardRuleList = cardRuleDTO.getCardRuleList();
}
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "获取卡类型信息出错", e);
throw new POSException("获取卡类型信息出错");
}
if(null == cardRuleList || cardRuleList.size() == 0){
return null;
}
for(CardRule cardRule : cardRuleList){
//卡类型匹配,并且打折的物料组在范围内
if(cardRule.getCardTypeNM().equals(cardType)){
return cardRule;
}
}
return null;
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getVipDiscountByCardType(java.lang.String, java.lang.String)
*/
public BigDecimal getVipDiscountByCardType(String cardTypeNM, String matkl) throws POSException {
CardRuleDTO cardRuleDTO = null;
List<CardRule> cardRuleList = null;
try {
cardRuleDTO = baseDao.getAllCardRule();
if (null != cardRuleDTO) {
cardRuleList = cardRuleDTO.getCardRuleList();
}
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "获取卡类型信息出错", e);
throw new POSException("获取卡类型信息出错");
}
if(null == cardRuleList || cardRuleList.size() == 0){
return null;
}
for(CardRule cardRule : cardRuleList){
//卡类型匹配,并且打折的物料组在范围内
if(cardRule.getCardTypeNM().equals(cardTypeNM) && matkl.startsWith(cardRule.getMatnr())){
return cardRule.getDiscount();
}
}
return new BigDecimal(1);
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getInvoiceNumber()
*/
public Invoice getInvoice() throws POSException {
InvoiceDTO dto = null;
try {
dto = baseDao.getAllInvoice();
} catch (POSException e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "错误", e);
throw new POSException(e.getErrorMessage());
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询发票数据出错", e);
throw new POSException("查询发票数据出错");
}
if(null == dto){
throw new POSException("没有发票数据");
}
List<Invoice> invoiceList = dto.getInvoiceList();
if(null == invoiceList || invoiceList.size() <= 0){
throw new POSException("没有发票数据");
}else{
return invoiceList.get(0);
}
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#deleteInvoiceNumber(java.lang.String)
*/
public void deleteInvoiceNumber(String invoiceNo) throws POSException {
try {
// baseDao.deleteInvoice(invoiceNo);
baseDao.deleteInvoice1(invoiceNo);
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "删除发票号码出错", e);
throw new POSException("删除发票号码出错");
}
}
/* (non-Javadoc)
* @see com.topsun.posclient.common.service.IBaseService#getInvoice(java.lang.String)
*/
public Invoice getInvoice(String invoiceNumber) throws POSException {
Invoice ret = null;
if(null == invoiceNumber){
return ret;
}
InvoiceDTO invoiceDTO = null;
try {
invoiceDTO = baseDao.getAllInvoice();
if(null == invoiceDTO){
return null;
}
List<Invoice> invoiceList = invoiceDTO.getInvoiceList();
if(null == invoiceList || invoiceList.size() == 0){
return null;
}
for(Invoice invoice : invoiceList){
if(invoice.getFPH().equals(invoiceNumber)){
ret = invoice;
}
}
return ret;
} catch (Exception e) {
e.printStackTrace();
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询发票数据出错", e);
throw new POSException("查询发票数据出错");
}
}
/**
* @param invoiceNum
* @param inputInvoiceCode
* @return
* @throws POSException
*/
public Invoice getInvoiceByNum(String invoiceNum, String inputInvoiceCode) throws POSException {
try {
return baseDao.getInvoiceByNum(invoiceNum, inputInvoiceCode);
} catch (Exception e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "查询发票数据出错", e);
throw new POSException("查询发票数据出错");
}
}
}