package com.yc.sdk.shopping.service; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.Map.Entry; import org.springframework.context.annotation.Scope; import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.stereotype.Service; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.yc.action.grid.GridUtils; import com.yc.exception.ApplicationException; import com.yc.sdk.shopping.entity.MatOptionPriceEntity; import com.yc.sdk.shopping.entity.MatPriceEntity; import com.yc.service.BaseService; @Service("MatPriceImpl") @Scope("prototype") public class MatPriceImpl extends BaseService implements MatPriceIfc { static Gson gson=new Gson(); @Override public MatPriceEntity getMatPrice(String matCode,String cltCode,Date docDate) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sql = " set nocount on ; \n" + " declare @MatCode varchar(50)="+GridUtils.prossSqlParm(matCode)+",@CltCode varchar(50)="+GridUtils.prossSqlParm(cltCode)+",@DocDate datetime="+GridUtils.prossSqlParm(docDate==null?null:sdf.format(docDate)) +" \n" + " declare @CltType varchar(50),@DocCode varchar(20) ; \n" + " declare @price2 money ,@price3 money,@lastPrice money ; \n" + " declare @CustPoints money,@Points money , @Weight money ; \n " + " select @CltType = CltType from t110203 a where CltCode = @CltCode \n" + " if isnull(@CltType,'') = '' select top 1 @CltType = CltType from t714001 a \n" + " if (@DocDate is null) select @DocDate = convert(datetime,convert(varchar(10),getdate(),120)) ; \n " + " select @DocCode = a.DocCode,@price3 = a.SalesPrice,@Points = a.Points,@Weight = a.BaseUomWeight \n" + " from t110503 a \n" + " where MatCode = @MatCode \n" + " select top 1 @price2 = a.Price \n" + " from t710109 a \n" + " where a.DocCode = @DocCode and a.CltType = @CltType \n" + " and convert(datetime,convert(varchar(10),@DocDate,120)) between a.DateStart and a.DateEnd \n" + " order by a.Priority asc ; \n" + " if isnull(@price2,0) <> 0 and ISNULL(@price2,0) < ISNULL(@price3,0) \n" + " select @lastPrice = @price2 ; \n" + " else \n" + " select @lastPrice = @price3 ; \n" + " select @lastPrice as Price, isnull(@Points,0) + isnull(@CustPoints,0) as Points,@Weight as Weight ; \n" ; MatPriceEntity matPriceEntity = new MatPriceEntity() ; Map map = null ; try { map = this.jdbcTemplate.queryForMap(sql) ; matPriceEntity.setMatCode(matCode); matPriceEntity.setPrice(map.get("Price")== null ? null : Double.parseDouble(map.get("Price").toString())); matPriceEntity.setPoints(map.get("Points")== null ? 0 : Double.parseDouble(map.get("Points").toString())); matPriceEntity.setWeight(map.get("Weight")== null ? 0 : Double.parseDouble(map.get("Weight").toString())); }catch(DataAccessException e ) { if (e instanceof EmptyResultDataAccessException){ //return 0 ; }else { e.printStackTrace(); throw new ApplicationException(e.getMessage()); } }catch(Exception e){ e.printStackTrace(); throw e; } return matPriceEntity; } public MatPriceEntity getOrderPrice(String matCode,String cltCode,Date docDate,Double digit) { //if (cltType == null || cltType.equals("")) cltType = "临时客户" ; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sql = " set nocount on ; \n" + " declare @price money , @price2 money ,@price3 money,@lastPrice money \n" + " declare @MatCode varchar(50),@CltCode varchar(50),@CltType varchar(50),@DocDate datetime,@Digit money \n" + " declare @CustPoints money,@Points money , @Weight money ; \n " + " select @MatCode = "+GridUtils.prossSqlParm(matCode)+",@CltCode = "+GridUtils.prossSqlParm(cltCode)+",@DocDate = "+GridUtils.prossSqlParm(docDate==null?null:sdf.format(docDate))+",@Digit = "+digit+" \n" + " select @CltType = CltType from t110203 a where CltCode = @CltCode \n" + " if isnull(@CltType,'') = '' select top 1 @CltType = CltType from t714001 a \n" + " if (@DocDate is null) select @DocDate = convert(datetime,convert(varchar(10),getdate(),120)) ; \n " + " select top 1 @price = a.Price \n" + " from t710108 a join t110503 b on a.doccode = b.DocCode \n" + " where b.MatCode = @MatCode and a.CltType = @CltType \n" + " and convert(datetime,convert(varchar(10),@DocDate,120)) between a.DateStart \n " + " and a.DateEnd and isnull(a.digit,0) <= isnull(@Digit,0) \n" + " order by a.digit desc,a.Priority asc ; \n" + " select top 1 @price2 = a.Price \n" + " from t710109 a join t110503 b on a.doccode = b.DocCode \n" + " where b.MatCode = @MatCode and a.CltType = @CltType \n" + " and convert(datetime,convert(varchar(10),@DocDate,120)) between a.DateStart and a.DateEnd \n" + " order by a.Priority asc ; \n " + " select @price3 = SalesPrice ,@Points = a.Points,@Weight = BaseUomWeight \n" + " from t110503 a \n " + " where MatCode = @MatCode \n" + " if ISNULL(@price,0) <> 0 and ISNULL(@price,0) < ISNULL(@price2,0) \n" + " select @lastPrice = @price ; else select @lastPrice = @price2 \n " + " if ISNULL(@lastPrice,0) = 0 select @lastPrice = @price3 \n " + " else if ISNULL(@lastPrice,0) > ISNULL(@price3,0) select @lastPrice = @price3 ; \n" + " select @CustPoints = a.Points \n" + " from t710111 a join t110503 b on a.DocCode = b.DocCode \n" + " where b.MatCode = @MatCode and a.CltType = @CltType ; \n " + " select @lastPrice as Price, isnull(@Points,0) + isnull(@CustPoints,0) as Points,@Weight as Weight ; \n" ; MatPriceEntity matPriceEntity = new MatPriceEntity() ; Map map = null ; try { map = this.jdbcTemplate.queryForMap(sql) ; matPriceEntity.setMatCode(matCode); matPriceEntity.setPrice(map.get("Price")== null ? null : Double.parseDouble(map.get("Price").toString())); matPriceEntity.setPoints(map.get("Points")== null ? 0 : Double.parseDouble(map.get("Points").toString())); matPriceEntity.setWeight(map.get("Weight")== null ? 0 : Double.parseDouble(map.get("Weight").toString())); }catch(DataAccessException e ) { if (e instanceof EmptyResultDataAccessException){ //price = 0 ; }else { e.printStackTrace(); throw new ApplicationException(e.getMessage()); } }catch(Exception e){ e.printStackTrace(); throw e; } return matPriceEntity; } @Override public MatOptionPriceEntity getOptionPriceAndWeight(Integer cartId) { String sql = " set nocount on ; \n" + " select OptionJson,MatCode from t710205 a where CartId = "+cartId+" \n"; MatOptionPriceEntity matOptionPrices = new MatOptionPriceEntity() ; Map map = null ; String optionJson = null ; String matCode = null ; try { map = this.jdbcTemplate.queryForMap(sql); }catch(DataAccessException e ) { if (e instanceof EmptyResultDataAccessException){ return matOptionPrices ; }else { e.printStackTrace(); throw new ApplicationException(e.getMessage()); } }catch(Exception e){ e.printStackTrace(); throw e; } optionJson = (map.get("OptionJson") == null ? null :(String) map.get("OptionJson") ) ; matCode = (map.get("matCode") == null ? null :(String) map.get("matCode") ) ; if (optionJson == null || "".equals(optionJson)) return matOptionPrices ; Map lists=null ; lists = gson.fromJson(optionJson,new TypeToken>(){}.getType()); for (Entry entry : lists.entrySet()) { if (entry.getValue() == null) continue ; String[] values = ((String)entry.getValue()).split(",") ; for (int i = 0; i < values.length; i++) { MatOptionPriceEntity matOptionPrice = this.getMatOptionPriceEntity(Integer.parseInt(entry.getKey()), values[i], matCode); matOptionPrices.addPrice(matOptionPrice.getPrice()); matOptionPrices.addPoints(matOptionPrice.getPoints()); matOptionPrices.addWeight(matOptionPrice.getWeight()); } } return matOptionPrices; } @Override public MatOptionPriceEntity getMatOptionPriceEntity(Integer optionId, String value , String matCode) { String sql = "set nocount on ; \n" + " select d.MatCode,c.Price,c.PricePrefix,c.Points,c.PointsPrefix,c.Weight,c.WeightPrefix \n" + " from t710116 a \n" + " join _sysdict b on a.Type = b.interValue \n " + " join t710107 c on a.OptionId = c.OptionId \n " + " join t110503 d on c.DocCode = d.DocCode \n" + " where b.dictid = -71011610 \n" + " and a.OptionId = "+optionId+" \n" + " and c.Value = "+GridUtils.prossSqlParm(value)+" \n " + " and b.interValue in ('select','radio','checkbox') \n" + " and d.MatCode = "+GridUtils.prossSqlParm(matCode)+" \n "; MatOptionPriceEntity matOptionPrice = new MatOptionPriceEntity() ; Map map = null ; try { map = this.jdbcTemplate.queryForMap(sql) ; }catch(DataAccessException e ) { if (e instanceof EmptyResultDataAccessException){ return matOptionPrice ; }else { e.printStackTrace(); throw new ApplicationException(e.getMessage()); } }catch(Exception e){ e.printStackTrace(); throw e; } matOptionPrice.setMatCode(matCode); matOptionPrice.setPrice(map.get("Price")== null ? null : ( map.get("PricePrefix") == null || map.get("PricePrefix").equals("1") ? Double.parseDouble(map.get("Price").toString()) : 0L - Double.parseDouble(map.get("Price").toString()) ) ); matOptionPrice.setPoints(map.get("Points")== null ? 0 : ( map.get("PointsPrefix") == null || map.get("PointsPrefix").equals("1") ? Double.parseDouble(map.get("Points").toString()) : 0L - Double.parseDouble(map.get("Points").toString()) ) ); matOptionPrice.setWeight(map.get("Weight")== null ? 0 : ( map.get("WeightPrefix") == null || map.get("WeightPrefix").equals("1") ? Double.parseDouble(map.get("Weight").toString()) : 0L - Double.parseDouble(map.get("Weight").toString()) ) ); return matOptionPrice; } }