package com.topsun.posclient.sales.core.dao;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.List;
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.dao.BaseDao;
import com.topsun.posclient.datamodel.dto.OldGoldDTO;
import com.topsun.posclient.sales.core.SalesCoreActivator;
import com.topsun.posclient.webservice.POSServerCaller;
import com.topsun.posclient.webservice.dto.ArrayOfoldGold;
import com.topsun.posclient.webservice.dto.OldGold;
import com.topsun.posclient.webservice.dto.OldGoldMain;
import com.topsun.posclient.webservice.dto.ProcessResult;
import com.topsun.posclient.webservice.dto.SaveOldGoldData;
import com.topsun.posclient.webservice.dto.SaveOldGoldDataReq;
import com.topsun.posclient.webservice.dto.SaveOldGoldDataResponse;
import com.topsun.posclient.webservice.dto.SaveOldGoldDataResult;
/**
* 旧金数据访问
* @author Dong
*
*/
public class OldGoldDao extends BaseDao {
/**
* 保存旧金数据,在线则上传到服务端
* @param oldGoldDTO 旧金数据
* @throws Exception
*/
public void saveOldGoldData(OldGoldDTO oldGoldDTO) throws POSException {
try{
ProcessResult result = this.uploadOldGoldData(oldGoldDTO);
if(result.getFlag().equals(POSServerCaller.SUCCESS)){
this.getLocalProcessor().createXmlFileFromObject(oldGoldDTO, "data_oldGold", AppConstants.DATA_OLDGOLD_HIS_PATH);
this.updateOgDocNum(oldGoldDTO.getDocNum());
return;
}else if(result.getFlag().equals(POSServerCaller.DOUBLEDOCNUM_ERROR)){
throw new POSException(result.getFlag(), "联机失败【"+result.getMsg()+"】");
}else{
throw new POSException("联机失败【"+result.getMsg()+"】,旧金数据已成功保存在本地");
}
}catch(POSException e){
try {
this.getLocalProcessor().createXmlFileFromObject(oldGoldDTO, "data_oldGold", AppConstants.DATA_OLDGOLD_PATH_BACK);
this.updateOgDocNum(oldGoldDTO.getDocNum());
} catch (Exception ee) {
LoggerUtil.logError(SalesCoreActivator.PLUGIN_ID, "旧金数据本地保存出错", ee);
throw new POSException("旧金数据本地保存出错");
}
throw new POSException(e.getErrorCode(), e.getErrorMessage());
} catch (Exception e) {
throw new POSException("联机失败,数据保存在本地");
}
}
/**
* 上传旧金鉴定数据
* @param oldGoldDTO
* @return
* @throws Exception
* @throws RemoteException
*/
private ProcessResult uploadOldGoldData(OldGoldDTO oldGoldDTO) throws POSException{
if(!POSServerCaller.isConnected()){
try {
this.getLocalProcessor().createXmlFileFromObject(oldGoldDTO, "data_oldGold", AppConstants.DATA_OLDGOLD_PATH);
this.updateOgDocNum(oldGoldDTO.getDocNum());
} catch (Exception e) {
}
throw new POSException("联机失败,数据已保存在本地!");
}
List<com.topsun.posclient.datamodel.OldGold> ogList = oldGoldDTO.getOldGoldList();
OldGold[] oldGoldArray = new OldGold[ogList.size()];
for(int i=0; i<ogList.size(); i++){
com.topsun.posclient.datamodel.OldGold og = ogList.get(i);
OldGold olgGold = new OldGold();
olgGold.setIdfCode(oldGoldDTO.getDocNum());
olgGold.setItemCode(og.getItemCode());
olgGold.setLoss(og.getLoss());
olgGold.setMtartCode(og.getMtartCode());
olgGold.setNeatness(og.getNeatness());
olgGold.setPrice_G(og.getPrice_G());
olgGold.setRefine(og.getRefine());
olgGold.setService(og.getService());
BigDecimal sum = og.getSum().setScale(2, BigDecimal.ROUND_HALF_UP);
olgGold.setSum(sum);
olgGold.setSuppliser(og.getSuppliser());
olgGold.setType(og.getType());
olgGold.setWeight(og.getWeight());
olgGold.setWeight_F(og.getWeight_F());
olgGold.setWeight_G(og.getWeight_G());
olgGold.setWeight_R(og.getWeight_R());
olgGold.setShopID(Integer.valueOf(POSClientApp.get().getSysConfig().getOwnerShop()));
oldGoldArray[i] = olgGold;
}
ArrayOfoldGold aog = new ArrayOfoldGold();
aog.setOldGold(oldGoldArray);
OldGoldMain main = new OldGoldMain();
String employeeName = POSClientApp.get().getLoginUser().getEmployeeName();
main.setDocNum(oldGoldDTO.getDocNum());
main.setEmployeeID(POSClientApp.get().getLoginUser().getId());
main.setEmployeeName(null == employeeName?POSClientApp.get().getLoginUser().getUserName():employeeName);//雇员名称为空取用户名称
main.setStatus("0");
main.setOldGolds(aog);
SaveOldGoldDataReq req = new SaveOldGoldDataReq();
req.setOldGoldMain(main);
req.setUserCredential(POSServerCaller.getDefaultUserCredential());
SaveOldGoldData saveOldGoldData92 = new SaveOldGoldData();
saveOldGoldData92.setSaveOldGoldDataReq(req);
SaveOldGoldDataResponse response = null;
try {
response = POSServerCaller.getWebService().saveOldGoldData(saveOldGoldData92);
} catch (Exception e) {
throw new POSException("旧金数据上传出错");
}
SaveOldGoldDataResult result = response.getSaveOldGoldDataResult();
return result.getResult();
}
}