package com.topsun.posclient.common.dao; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBException; import com.topsun.posclient.common.AppConstants; import com.topsun.posclient.common.LocalDataProcessor; import com.topsun.posclient.common.POSClientApp; import com.topsun.posclient.common.ProjectUtil; import com.topsun.posclient.datamodel.GoldPrice; import com.topsun.posclient.datamodel.Invoice; import com.topsun.posclient.datamodel.Item; import com.topsun.posclient.datamodel.ItemIndex; import com.topsun.posclient.datamodel.SettingData; 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.ItemIndexDTO; 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 BaseDao { LocalDataProcessor localProcessor = new LocalDataProcessor(); public LocalDataProcessor getLocalProcessor() { return localProcessor; } /** * 获取用户 * @return * @throws Exception */ public UserDTO getUserData() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_USER_FILENAME); if(file.exists()){ UserDTO operatorDTO = (UserDTO) getLocalProcessor().getObjectFromXml( this.getLocalProcessor().getDataFileContent(file), UserDTO.class); return operatorDTO; } return null; } /** * 根据ID获取用户信息 * @return * @throws Exception */ public UserDTO getUserById(int userId) throws Exception { File userDatafile = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_USER_FILENAME); UserDTO userDTO = (UserDTO) getLocalProcessor().getObjectFromXml( getLocalProcessor().getDataFileContent(userDatafile), UserDTO.class); return userDTO; } /** * 根据用户ID获取雇员名称 * @param userId * @return * @throws Exception */ public String getEmployeeNameById(int userId) throws Exception { File userDatafile = new File(ProjectUtil.getRuntimeClassPath()+ AppConstants.DATA_USER_FILENAME); UserDTO userDTO = (UserDTO) getLocalProcessor().getObjectFromXml( getLocalProcessor().getDataFileContent(userDatafile), UserDTO.class); List<User> userList = userDTO.getUserList(); for(User s : userList){ if(s.getId() == userId){ return s.getEmployeeName(); } } return ""; } /** * 获取所有结算方式 * @return * @throws Exception */ public CashierModeDTO getAllCashierMode() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_CASHIERMODE_FILENAME); CashierModeDTO cashierModeDTO = (CashierModeDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file),CashierModeDTO.class); return cashierModeDTO; } /** * 获取所有物料信息 * @return * @throws Exception */ public MaterialDTO getAllMaterial() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_MATERIAL_FILENAME); MaterialDTO materialDTO = (MaterialDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file),MaterialDTO.class); return materialDTO; } /** * 获取所有会员 * @return * @throws Exception */ public MemberDTO getAllMember() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_MEMBER_FILENAME); MemberDTO memberDTO = (MemberDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file), MemberDTO.class); return memberDTO; } /** * 获取所有卡类型对象 * @return * @throws Exception */ public CardRuleDTO getAllCardRule() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_CARDRULE_FILENAME); CardRuleDTO cardRuleDTO = (CardRuleDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file), CardRuleDTO.class); return cardRuleDTO; } /** * 获取所有积分消费信息 * @return * @throws Exception */ public PointDataDTO getAllPointData() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_POINTDATA_FILENAME); PointDataDTO pointDataDTO = (PointDataDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file), PointDataDTO.class); return pointDataDTO; } /** * 获取所有商品信息 * @return * @throws Exception */ public ItemDTO getAllItem() throws Exception { ItemDTO itemDTO = new ItemDTO(); List<Item> items = new ArrayList<Item>(); File path = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_ITEM_PATH); File[] files = path.listFiles(); for(File f : files){ ItemDTO dto = (ItemDTO) getLocalProcessor().getObjectFromXml(getLocalProcessor().getDataFileContent(f), ItemDTO.class); if(null != dto && null != dto.getItemList()){ items.addAll(dto.getItemList()); } } itemDTO.setItemList(items); return itemDTO; } public Item getItemByBarCode(String barCode) throws Exception { //条形码大于10长度按旧品编码查找,从所有单品数据文件中查找 if(barCode.length() > 10){ ItemDTO itemDto = this.getAllItem(); if(null == itemDto){ return null; } if(null == itemDto.getItemList() || itemDto.getItemList().size() == 0){ return null; } List<Item> items = itemDto.getItemList(); for(Item i : items){ // 如果是10位的数字还是通过itemCode去匹配 如果是大于10位的数字用ZYTM这个字段去匹配 查找出销售商品信息 if(barCode.length() > 10){ if(i.getZYTM().equals(barCode)){ return i; } }else{ if(i.getItemCode().equals(barCode)){ return i; } } } } if(null == barCode || "".equals(barCode)){ return null; } String path = this.getIndexFilePathByBarCode(barCode); if(null == path || "".equals(path)){ return null; } List<Item> items = new ArrayList<Item>(); File itemDataFileName = new File(ProjectUtil.getRuntimeClassPath() + path); ItemDTO dto = (ItemDTO) getLocalProcessor().getObjectFromXml(getLocalProcessor().getDataFileContent(itemDataFileName), ItemDTO.class); if(null != dto && null != dto.getItemList()){ items.addAll(dto.getItemList()); } for(Item i : items){ // 如果是10位的数字还是通过itemCode去匹配 如果是大于10位的数字用ZYTM这个字段去匹配 查找出销售商品信息 if(barCode.length() > 10){ if(i.getZYTM().equals(barCode)){ return i; } }else{ if(i.getItemCode().equals(barCode)){ return i; } } } return null; } public String getIndexFilePathByBarCode(String barCode) throws Exception { File indexFile = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_ITEM_INDEX_FILENAME); ItemIndexDTO dto = (ItemIndexDTO) getLocalProcessor().getObjectFromXml(getLocalProcessor().getDataFileContent(indexFile), ItemIndexDTO.class); List<ItemIndex> iindexList = dto.getItemIndexList(); for(ItemIndex ii : iindexList){ List<String> barCodes = new ArrayList<String>(); String[] bcArray = ii.getBarCodes().split(","); for(int i=0; i<bcArray.length; i++){ barCodes.add(bcArray[i]); } if(barCodes.contains(barCode)){ return ii.getFileName(); } } return null; } /** * 获取所有店铺信息 * @return * @throws Exception */ public ShopDTO getAllShop() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_SHOP_FILENAME); ShopDTO shopDTO = (ShopDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file), ShopDTO.class); return shopDTO; } /** * 根据ID获取店铺名称 * @param shopId * @return * @throws Exception */ public String getShopNameById(int shopId) throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath()+ AppConstants.DATA_SHOP_FILENAME); ShopDTO shopDTO = (ShopDTO) getLocalProcessor().getObjectFromXml(getLocalProcessor().getDataFileContent(file), ShopDTO.class); List<Shop> shopList = shopDTO.getShopList(); for(Shop s : shopList){ if(s.getId() == shopId){ return s.getShpName(); } } return ""; } /** * 根据ID获取店铺 * @param shopId * @return * @throws Exception */ public Shop getShopById(int shopId) throws Exception{ File file = new File(ProjectUtil.getRuntimeClassPath()+ AppConstants.DATA_SHOP_FILENAME); ShopDTO shopDTO = (ShopDTO) getLocalProcessor().getObjectFromXml(getLocalProcessor().getDataFileContent(file), ShopDTO.class); List<Shop> shopList = shopDTO.getShopList(); if(null != shopList){ for(Shop s : shopList){ if(s.getId() == shopId){ return s; } } } return null; } /** * 根据ID获取单品名称 * @param itemId * @return * @throws Exception */ public String getItemNameById(int itemId) throws Exception { for(Item i : this.getAllItem().getItemList()){ if(i.getId() == itemId){ return i.getItemName(); } } return ""; } /** * 获取所有实时金价信息 * @return 实时金价 * @throws Exception */ public GoldPriceDTO getAllGoldPrice() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_GOLDPRICE_FILENAME); GoldPriceDTO goldPriceDTO = (GoldPriceDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file), GoldPriceDTO.class); return goldPriceDTO; } /** * 根据物料编码修改实时金价 * @param mtartCode 物料编码 * @param gprice 实时金价 * @throws Exception */ public void updateGoldPriceByMtartCode(String mtartCode, String gprice) throws Exception { GoldPriceDTO goldPriceDTO = getAllGoldPrice(); if(null == goldPriceDTO){ return; } List<GoldPrice> goldPriceList = goldPriceDTO.getGoldPriceList(); if(null == goldPriceList || goldPriceList.size() <= 0){ return; } for(GoldPrice gp : goldPriceList){ if(gp.getMtartCode().equals(mtartCode)){ gp.setPrice(gprice); } } this.getLocalProcessor().createXmlFileFromObject(goldPriceDTO, AppConstants.DATA_GOLDPRICE_FILENAME); } /** * 更新发票号码 * @param docNum * @throws Exception */ public void updateInvoiceNumber(String invoiceNumber) throws Exception{ SettingData settingData = POSClientApp.get().getSysConfig(); settingData.setInvoiceNumber(invoiceNumber); POSClientApp.get().updateSysConfig(settingData); } /** * 更新单据编号 * @param docNum * @throws Exception */ public void updateBDocNum(String docNum) throws Exception{ docNum = docNum.substring((docNum.length()-10), docNum.length()); SettingData settingData = POSClientApp.get().getSysConfig(); settingData.setbDocNum(docNum); POSClientApp.get().updateSysConfig(settingData); } /** * 更新单据编号 * @param docNum * @throws Exception */ public void updateDocNum(String docNum) throws Exception{ docNum = docNum.substring((docNum.length()-10), docNum.length()); SettingData settingData = POSClientApp.get().getSysConfig(); settingData.setDocNum(docNum); POSClientApp.get().updateSysConfig(settingData); } public void updateRDocNum(String docNum) throws Exception{ docNum = docNum.substring((docNum.length()-10), docNum.length()); SettingData settingData = POSClientApp.get().getSysConfig(); settingData.setrDocNum(docNum); POSClientApp.get().updateSysConfig(settingData); } /** * 更新旧金编码 * @param docNum * @throws Exception */ public void updateOgDocNum(String docNum) throws Exception{ docNum = docNum.substring((docNum.length()-10), docNum.length()); SettingData settingData = POSClientApp.get().getSysConfig(); settingData.setOgDocNum(docNum); POSClientApp.get().updateSysConfig(settingData); } /** * 更新回购旧金编码 * @param docNum * @throws Exception */ public void updateGDocNum(String docNum) throws Exception{ docNum = docNum.substring((docNum.length()-10), docNum.length()); SettingData settingData = POSClientApp.get().getSysConfig(); settingData.setgDocNum(docNum); POSClientApp.get().updateSysConfig(settingData); } /** * 获取所有发票数据 * @return * @throws JAXBException * @throws Exception */ public InvoiceDTO getAllInvoice() throws Exception { File file = new File(ProjectUtil.getRuntimeClassPath() + AppConstants.DATA_INVOICE_FILENAME); InvoiceDTO invoiceDTO = (InvoiceDTO) getLocalProcessor() .getObjectFromXml(getLocalProcessor().getDataFileContent(file), InvoiceDTO.class); return invoiceDTO; } /** * 根据发票号码和发票代码获取发票 * @param invoiceNum 发票号码 * @param inputInvoiceCode 发票代码 * @return * @throws Exception */ public Invoice getInvoiceByNum(String invoiceNum, String inputInvoiceCode) throws Exception { Invoice ret = null; InvoiceDTO invoiceDTO = this.getAllInvoice(); if(null == invoiceDTO || null == invoiceDTO.getInvoiceList() || invoiceDTO.getInvoiceList().size() < 0){ return ret; } for(Invoice invoice : invoiceDTO.getInvoiceList()){ if(invoice.getFPH().equals(invoiceNum) && invoice.getFPDM().equals(inputInvoiceCode)){ ret = invoice; } } return ret; } /** * 根據發票號碼刪除發票 * @param invoiceNum * @return * @throws Exception */ public void deleteInvoice(String invoiceNum) throws Exception { InvoiceDTO invoiceDTO = getAllInvoice(); if(null == invoiceDTO || null == invoiceDTO.getInvoiceList()){ return; } List<Invoice> invoiceList = invoiceDTO.getInvoiceList(); List<Invoice> invoiceListNew = new ArrayList<Invoice>(); for(Invoice invoice : invoiceList){ invoiceListNew.add(invoice); } for(Invoice invoice : invoiceList){ if(invoice.getFPH().equals(invoiceNum)){ invoiceListNew.remove(invoice); break; } } invoiceDTO.setInvoiceList(invoiceListNew); getLocalProcessor().createXmlFileFromObject(invoiceDTO, AppConstants.DATA_INVOICE_FILENAME); } /** * 根据发票号删除发票,并删除该发票之前的发票 * @param invoiceNum 发票号码 * @throws Exception */ public void deleteInvoice1(String invoiceNum) throws Exception { InvoiceDTO invoiceDTO = getAllInvoice(); if(null == invoiceDTO || null == invoiceDTO.getInvoiceList()){ return; } List<Invoice> invoiceList = invoiceDTO.getInvoiceList(); List<Invoice> invoiceListNew = new ArrayList<Invoice>(); for(Invoice invoice : invoiceList){ invoiceListNew.add(invoice); } for(Invoice delInvoice : invoiceList){ if(delInvoice.getFPH().equals(invoiceNum)){ invoiceListNew.remove(delInvoice); if(null != delInvoice.getLastFPH() && !"".equals(delInvoice.getLastFPH())){ if(null != delInvoice.getLastFPH() && !"".equals(delInvoice.getLastFPH())){ for(Invoice invoice : invoiceListNew){ //设置上一张发票的next if(invoice.getFPH().equals(delInvoice.getLastFPH())){ if(null != delInvoice.getNextFPH() && !"".equals(delInvoice.getNextFPH())){ Invoice nextInvoice = this.getInvoiceByNum(delInvoice.getNextFPH(),delInvoice.getNextFPDM()); invoice.setNextFPH(null == nextInvoice?"":nextInvoice.getFPH()); invoice.setNextFPDM(null == nextInvoice?"":nextInvoice.getFPDM()); } } //设置下一张发票的last if(invoice.getFPH().equals(delInvoice.getNextFPH())){ if(null != delInvoice.getNextFPH() && !"".equals(delInvoice.getNextFPH())){ Invoice lastInvoice = this.getInvoiceByNum(delInvoice.getLastFPH(), delInvoice.getLastFPDM()); invoice.setLastFPH(null == lastInvoice?"":delInvoice.getLastFPH()); invoice.setLastFPDM(null == lastInvoice?"":delInvoice.getLastFPDM()); } } } } } } } invoiceDTO.setInvoiceList(invoiceListNew); getLocalProcessor().createXmlFileFromObject(invoiceDTO, AppConstants.DATA_INVOICE_FILENAME); } }