1:使用spring boot微服務時,想創建一個靜態util類供其他地方調用
2:使用@PostConstruct註解初始化注入對象,此註解在:當前類中只能使用一次,而且不能是static,只能返回void,不能允許有參數。
3:此方法執行是在依賴注入完成後執行。可使用靜態變量接收初始化的接口。
4:完整代碼如下:
package com.x3.bills.factory.utils;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.x3.b2b.sdk.entity.BillPrice;
import com.x3.b2b.sdk.entity.ParamBillPrice;
import com.x3.b2b.sdk.service.IB2BService;
import com.x3.base.core.exception.BusinessException;
import com.x3.base.core.util.DoubleUtil;
import com.x3.base.core.util.StringUtil;
import com.x3.bills.base.archives.biz.IclothingBiz;
import com.x3.bills.base.archives.biz.IparamBiz;
import com.x3.bills.base.archives.entity.ClothingInfoEntity;
@Component
public class SuppPriceUtil {
private static IparamBiz curParamBiz;
private static IB2BService curB2bService;
private static IclothingBiz curClothingBiz;
@Autowired
private IparamBiz paramBiz;
@Autowired
private IB2BService b2bService;
@Autowired
private IclothingBiz clothingBiz;
@PostConstruct
private void Init(){
curParamBiz=this.paramBiz;
curB2bService=this.b2bService;
curClothingBiz=this.clothingBiz;
}
/**
* 根據貨品、及收發地等信息獲取供應商結算價格 和系統參數1051有關
* @param clos 貨品集合
* @param set_depot_id 發貨地ID
* @param get_depot_id 收貨地ID
* @param sell_class_id 發貨類型,可爲空
* @return
*/
public static List<BillPrice> getSuppPrice(List<String> clos,String set_depot_id,String get_depot_id,String sell_class_id){
//驗證參數
if(clos==null || clos.size()==0){
throw new BusinessException("SuppPriceUtil-getPrice-01", "獲取供應商價格失敗,參數clos至少包含一個元素!");
}
if(StringUtil.isBlankOrNull(set_depot_id)){
throw new BusinessException("SuppPriceUtil-getPrice-02", "獲取供應商價格失敗,參數set_depot_id不允許爲空!");
}
if(StringUtil.isBlankOrNull(get_depot_id)){
throw new BusinessException("SuppPriceUtil-getPrice-03", "獲取供應商價格失敗,參數get_depot_id不允許爲空!");
}
String p_1051=curParamBiz.getParameterValue(1051, "");
if(StringUtil.isBlankOrNull(p_1051)){
throw new BusinessException("SuppPriceUtil-getPrice-04", "獲取供應商價格失敗,請聯繫系統管理員檢查系統參數1051是否設置正確!");
}
switch(p_1051){
case "0"://實際成本
case "1"://採購成本
case "2"://吊牌價
//折扣保留6位小數
return getOtherPrice(clos,p_1051);
case "3":
return getPolicyPrice(clos,set_depot_id,get_depot_id,sell_class_id);
default:
throw new BusinessException("SuppPriceUtil-getPrice-05", "獲取供應商價格失敗,非法類型!");
}
}
/**
* 獲取實際成本,採購成本,吊牌價
* @param clos clothing_id 集合
* @param p_1051 參數0,1,2
* @return
*/
private static List<BillPrice> getOtherPrice(List<String> clos,String p_1051){
List<ClothingInfoEntity> closlist=curClothingBiz.getClothingInfo(clos);
List<BillPrice> list=new ArrayList<>();
if(list!=null && list.size()>0){
BillPrice priceEntity=null;
for(ClothingInfoEntity closEntity:closlist){
priceEntity=new BillPrice();
priceEntity.setClothing_id(closEntity.getClothing_id());
switch(p_1051){
case "0"://實際成本
priceEntity.setS_price(closEntity.getCost());
//折扣=實際成本價/吊牌價
priceEntity.setSale_rate(DoubleUtil.round(DoubleUtil.div(closEntity.getCost(), closEntity.getClothing_jprice()),6));
break;
case "1"://採購成本
priceEntity.setS_price(closEntity.getCost1());
//折扣=採購成本價/吊牌價
priceEntity.setSale_rate(DoubleUtil.round(DoubleUtil.div(closEntity.getCost1(), closEntity.getClothing_jprice()),6));
break;
case "2"://吊牌價 1折
priceEntity.setS_price(closEntity.getClothing_jprice());
priceEntity.setSale_rate(1);
break;
default:
throw new BusinessException("SuppPriceUtil-getOtherPrice-01", "獲取供應商價格失敗,非法類型!");
}
priceEntity.setJ_price(closEntity.getClothing_jprice());
list.add(priceEntity);
}
}
return list;
}
/**
* B2B 獲取供應商政策價格
* @param clos clothing_id 集合
* @param set_depot_id 發貨地ID
* @param get_depot_id 收貨地ID
* @param sell_class_id 發貨類型
* @return
*/
private static List<BillPrice> getPolicyPrice(List<String> clos,String set_depot_id,String get_depot_id,String sell_class_id){
ParamBillPrice price_param = new ParamBillPrice();
price_param.setClothing_id(clos);
price_param.setFlow_type(5);
price_param.setSet_depot_id(set_depot_id);
price_param.setGet_depot_id(get_depot_id);
price_param.setSet_selltype_id(sell_class_id);
if (curB2bService == null) {
throw new BusinessException("SuppPriceUtil-getPrice-04", "獲取供應商價格失敗,B2B SDK實例爲空!");
}
return curB2bService.getBillPrice(price_param);
}
}