package com.topsun.posclient.sales.core.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.co.lane.card.jni.RFCardManager;
import com.topsun.posclient.common.LoggerUtil;
import com.topsun.posclient.common.POSException;
import com.topsun.posclient.common.ProjectUtil;
import com.topsun.posclient.datamodel.Item;
import com.topsun.posclient.datamodel.PartSales;
import com.topsun.posclient.datamodel.Promotion;
import com.topsun.posclient.datamodel.Retail;
import com.topsun.posclient.datamodel.RetailM;
import com.topsun.posclient.datamodel.dto.PromotionDTO;
import com.topsun.posclient.datamodel.dto.RetailDTO;
import com.topsun.posclient.sales.core.PromotionMath;
import com.topsun.posclient.sales.core.SalesCoreActivator;
import com.topsun.posclient.sales.core.SalesDataUtil;
import com.topsun.posclient.sales.core.dao.PartSaleDao;
import com.topsun.posclient.sales.core.service.IPartSaleService;
import com.topsun.posclient.webservice.POSServerCaller;
import com.topsun.posclient.webservice.dto.CheckMember;
import com.topsun.posclient.webservice.dto.CheckMemberReq;
import com.topsun.posclient.webservice.dto.CheckMemberReqMemberData;
import com.topsun.posclient.webservice.dto.CheckMemberResponse;
import com.topsun.posclient.webservice.dto.CheckMemberResult;
import com.topsun.posclient.webservice.dto.Member;
/**
* 零售服务实现
* @author Dong
*
*/
public class PartSaleServiceImpl extends SalesServiceImpl implements IPartSaleService {
/**
* 零售数据访问
*/
private PartSaleDao partSaleDao = new PartSaleDao();
/* (non-Javadoc)
* @see com.topsun.posclient.sales.core.service.IPartSaleService#savePartSaleData(com.topsun.posclient.datamodel.PartSales)
*/
public void savePartSaleData(PartSales partSales) throws POSException {
RetailDTO retailDTO = new RetailDTO();
Retail retail = convertPartSalesToRetail(partSales);
List<Retail> retailList = new ArrayList<Retail>();
retailList.add(retail);
retailDTO.setRetailList(retailList);
partSaleDao.saveRetail(retailDTO, partSales.getDocNum());//保存零售数据
}
/* (non-Javadoc)
* @see com.topsun.posclient.sales.core.service.IPartSaleService#applyPromotion(com.topsun.posclient.datamodel.PartSales)
*/
public List<Item> applyPromotion(PartSales partSales) throws POSException{
if(null == partSales){
return null;
}
PromotionDTO promotionDTO = null;
List<RetailM> retailMs = null;
try {
retailMs = PromotionMath.getPromotionRetailMListFromPartSales(partSales);
if(null == retailMs){
return null;
}
promotionDTO = this.queryTodayPromotion();
} catch (Exception e) {
LoggerUtil.logError(SalesCoreActivator.PLUGIN_ID, e);
throw new POSException("促销方案查询出错");
}
List<Promotion> promotions = promotionDTO.getPromotionList();
for(int i=0; i<promotions.size(); i++){
Promotion prmi = promotions.get(i);
int type = Integer.valueOf(prmi.getType());
switch(type){
case 0:
retailMs = PromotionMath.applySingle(retailMs, prmi);//单品促销
break;
case 1:
retailMs = PromotionMath.applyAllNM(retailMs, prmi);//整单满减
break;
case 2:
retailMs = PromotionMath.applySingleNM(retailMs, prmi);//单件金额满减(剪角折扣)
break;
case 3:
retailMs = PromotionMath.applyNumNm(retailMs, prmi);//数量满减
break;
case 4:
retailMs = PromotionMath.applySingleNG(retailMs, prmi);//单品满送赠品
break;
case 5:
retailMs = PromotionMath.applyAppoint(retailMs, prmi);//买指定商品可选购指定商品(折扣)
break;
case 6:
retailMs = PromotionMath.applyNM(retailMs, prmi);//第N件打M折
break;
case 7:
retailMs = PromotionMath.applyPackage(retailMs, prmi);//捆绑销售
break;
case 8:
retailMs = PromotionMath.applyFee(retailMs, prmi);//工费打折
break;
case 9:
retailMs = PromotionMath.applyFeeD(retailMs, prmi);//工费扣减
break;
}
}
return PromotionMath.convertRetailToItemList(partSales.getItemList(), retailMs);
}
/* (non-Javadoc)
* @see com.topsun.posclient.sales.core.service.IPartSaleService#queryTodayPromotion()
*/
public PromotionDTO queryTodayPromotion() throws POSException {
try {
PromotionDTO dto = partSaleDao.getValidTimePromotion();
sortByLevel(dto);
return dto;
} catch (Exception e) {
LoggerUtil.logError(SalesCoreActivator.PLUGIN_ID, e);
throw new POSException("促销信息查询出错");
}
}
/**
* 根据促销方案等级进行冒泡排序,等级数最大的优先级最高,排在最前
* @param dto 促销方案
*/
private void sortByLevel(PromotionDTO dto){
if(null == dto){
return;
}
List<Promotion> ps = dto.getPromotionList();
if(null == ps || ps.size() == 0){
return;
}
Promotion[] promotions = new Promotion[ps.size()];
for(int i=0; i<ps.size(); i++){
promotions[i] = ps.get(i);
}
List<Promotion> afterSortList = new ArrayList<Promotion>();
for(int i=0; i<promotions.length-1; i++){
for(int j=i+1; j<promotions.length; j++){
if (promotions[i].getLevel() > promotions[j].getLevel()){
Promotion temp = promotions[i];
promotions[i] = promotions[j];
promotions[j] = temp;
}
}
}
for(int i=0; i<promotions.length; i++){
afterSortList.add(promotions[i]);
}
dto.setPromotionList(afterSortList);
}
/**
* 零售时,将零售信息转换成Retail
* @param partSales 销售信息
* @return
* @throws POSException
*/
private Retail convertPartSalesToRetail(PartSales partSales) throws POSException{
if(null == partSales){
return null;
}
//设置基本信息
Retail retail = SalesDataUtil.getRetailFromPartSales(partSales);
//设置明细
retail.setRetailMList(SalesDataUtil.getRetailMsFromPartSales(partSales));
//零售结算 拆分到每个商品的结算金额
retail.setRetailMPList(SalesDataUtil.calculateRetailMPList(partSales));
//零售结算 整单结算金额分配
retail.setRetailPList(SalesDataUtil.calculateRetailPList(partSales));
//设置旧金
retail = SalesDataUtil.setOldGoldData(retail, partSales);
//设置断料
retail = SalesDataUtil.setUnSaleDetail(retail, partSales);
return retail;
}
/* (non-Javadoc)
* @see com.topsun.posclient.sales.core.service.IPartSaleService#checkMember(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
public com.topsun.posclient.datamodel.Member checkMember(String cardNo, String phone, String password, String newPassword) throws POSException {
com.topsun.posclient.datamodel.Member ret = null;
if(!POSServerCaller.isConnected()){
throw new POSException("当前处于离线状态,请确认服务器连接");
}
CheckMemberReqMemberData checkData = new CheckMemberReqMemberData();
if(null == cardNo || "".equals(cardNo)){
checkData.setCardNo(phone);
}else{
checkData.setCardNo(cardNo);
}
checkData.setPassWord(password);
checkData.setNewPassWord(newPassword);
CheckMemberReq req = new CheckMemberReq();
req.setMemberData(checkData);
req.setUserCredential(POSServerCaller.getDefaultUserCredential());
CheckMember checkMember54 = new CheckMember();
checkMember54.setCheckMemberReq(req);
CheckMemberResponse response = null;
try {
response = POSServerCaller.getWebService().checkMember(checkMember54);
} catch (Exception e) {
LoggerUtil.logError(SalesCoreActivator.PLUGIN_ID, e);
throw new POSException("联机失败");
}
String flag = response.getCheckMemberResult().getResult().getFlag();
if(flag.equals(POSServerCaller.SUCCESS)){
CheckMemberResult checkMemberResult = response.getCheckMemberResult();
Member member = checkMemberResult.getMember();
ret = new com.topsun.posclient.datamodel.Member();
ret.setAge(member.getAge());
ret.setBirthDay(ProjectUtil.getDate(member.getBirthDay()));
ret.setCardNo(member.getCardNo());
ret.setCardType(member.getCardType());
ret.setEffectiveDateE(ProjectUtil.getDate(member.getEffectiveDateE()));
ret.setEffectiveDateS(ProjectUtil.getDate(member.getEffectiveDateS()));
ret.setID(member.getId());
ret.setIDCard(member.getIdCard());
ret.setIssueDate(ProjectUtil.getDate(member.getIssueDate()));
ret.setMenberName(member.getMemberName());
ret.setPoint(member.getPoints());
ret.setSex(member.getSex());
ret.setStatus(member.getStatus());
ret.setMobile(member.getMobile());
ret.setCardTypeNM(member.getCardTypeNM());
return ret;
}else{
throw new POSException(response.getCheckMemberResult().getResult().getMsg());
}
}
/* (non-Javadoc)
* @see com.topsun.posclient.sales.core.service.IPartSaleService#applyVipSales(java.util.List, java.lang.String)
*/
public List<Item> applyVipSales(List<Item> items, String cardTypeNM) throws POSException {
for(Item item : items){
if(null == item.getZSFTJ() || item.getZSFTJ().equals("X")){
continue;
}
BigDecimal discount = getVipDiscountByCardType(cardTypeNM, item.getMatkl());//会员卡折扣系数
if(discount.compareTo(new BigDecimal(1)) == 1){
continue;
}
BigDecimal oldFactSalesAmount = item.getFactAmount();
if(item.getMatkl().startsWith("1")){//素金
BigDecimal zjf = item.getFactZJGF();
item.setFactZJGF(item.getFactZJGF().multiply(discount));//素金会员折扣在工费上
item.setFactAmount(item.getFactAmount().subtract(zjf.subtract(item.getFactZJGF())));
}else{
item.setFactAmount(item.getFactAmount().multiply(discount));//非素金折扣在实售上
}
item.setPretlAmount(oldFactSalesAmount.subtract(item.getFactAmount()));//设置优惠金额
}
return items;
}
/* (non-Javadoc)
* @see com.topsun.posclient.sales.core.service.IPartSaleService#writeCard(com.topsun.posclient.datamodel.Member)
*/
public void writeCard(com.topsun.posclient.datamodel.Member member) throws POSException {
try{
RFCardManager frCardMgmt = RFCardManager.getInstance();
frCardMgmt.writeCardData(member);
}catch(Exception e){
throw new POSException("更新卡失败");
}
}
}