| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- package beans.itemprice;
- import beans.itemclass.ItemClass;
- import common.BaseClass;
- import common.Controller;
- import common.YosException;
- import common.data.Rows;
- import common.data.SQLFactory;
- import utility.tools.Encryption;
- import utility.tools.Math;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.HashMap;
- public class ItemPrice extends BaseClass {
- private Controller controller;
- private long sys_enterpriseid = -1;//企业id
- private long itemid;//商品id
- private BigDecimal marketprice = new BigDecimal(-1);//牌价,市场价
- private int pricegrade = 0;//价格等级
- private BigDecimal price = new BigDecimal(-1);//等级价格(商品原价)
- private BigDecimal gradediscountrate = new BigDecimal(0);//商户等级折扣率
- private BigDecimal graderateprice = new BigDecimal(-1);//商户等级折扣价格
- private BigDecimal contractprice = new BigDecimal(-1);//框架合同价格
- private BigDecimal projectcontractprice = new BigDecimal(-1);//框架合同价格
- private String cachekey = null;
- private ItemPrice(Controller controller, long itemid) {
- this.controller = controller;
- this.itemid = itemid;
- }
- private ItemPrice(Controller controller, long sys_enterpriseid, long itemid) {
- this.controller = controller;
- this.sys_enterpriseid = sys_enterpriseid;
- this.itemid = itemid;
- }
- /**
- * 获取商品牌价
- *
- * @return
- */
- public BigDecimal getMarketprice() throws YosException {
- if (marketprice.compareTo(BigDecimal.ZERO) < 0) {
- Rows rows = dbConnect.runSqlQuery("select marketprice from plm_item where siteid='" + controller.siteid + "'and itemid=" + itemid);
- if (rows.isNotEmpty()) {
- marketprice = rows.get(0).getBigDecimal("marketprice");
- } else {
- throw new YosException("找不到商品牌价信息itemid:" + itemid);
- }
- }
- return marketprice;
- }
- /**
- * 商品等级价格
- *
- * @return
- */
- public BigDecimal getPrice() throws YosException {
- if (price.floatValue() < 0) {
- SQLFactory sqlFactory = new SQLFactory(this, "商品等级价格查询");
- sqlFactory.addParameter("siteid", controller.siteid);
- sqlFactory.addParameter("itemid", itemid);
- sqlFactory.addParameter("sys_enterpriseid", this.sys_enterpriseid);
- Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
- if (rows.isNotEmpty()) {
- pricegrade = rows.get(0).getInteger("pricegrade");
- price = rows.get(0).getBigDecimal("price");
- gradediscountrate = rows.get(0).getBigDecimal("discountrate");
- //如果折扣率为0,则默认按照1计算
- gradediscountrate = (gradediscountrate.compareTo(BigDecimal.ZERO) == 0 ? new BigDecimal(1) : gradediscountrate);
- } else {
- throw new YosException("找不到商品等级价格信息itemid:" + itemid);
- }
- }
- return price;
- }
- /**
- * 价格等级
- *
- * @return
- * @throws YosException
- */
- public int getPricegrade() throws YosException {
- if (pricegrade < 0) {
- getPrice();
- }
- return pricegrade;
- }
- /**
- * 商户等级折后价
- *
- * @return
- */
- public BigDecimal getGraderateprice() throws YosException {
- if (graderateprice.compareTo(BigDecimal.ZERO) < 0) {
- graderateprice = getPrice().multiply(gradediscountrate);
- }
- return graderateprice;
- }
- private static HashMap<String, Rows> contractRowscacheMap = new HashMap<>();
- /**
- * 获取商户协议价,1:获取协议商品折扣;2:获取协议营销类别折扣(按最低价);3:获取协议折扣;4获取商户等级折扣价:5获取商品等级原价
- *
- * @return
- */
- public BigDecimal getContractprice() throws YosException {
- if (contractprice.compareTo(BigDecimal.ZERO) < 0) {
- /*
- * 第一步:查询框架合同并试图获取价格:优先级 商品价格、商品折扣==》类别折扣==》合同折扣
- */
- {
- SQLFactory contractQuery = new SQLFactory(this, "商户框架合同查询");
- contractQuery.addParameter("siteid", controller.siteid);
- contractQuery.addParameter("sys_enterpriseid", sys_enterpriseid);
- Rows contractRows = dbConnect.runSqlQuery(contractQuery.getSQL());
- if (contractRows.size() > 1) {
- throw new YosException("存在一个以上的价格协议信息:" + itemid);
- } else if (contractRows.size() == 1) {
- long sa_contractid = contractRows.get(0).getLong("sa_contractid");//合同id
- /*
- * 第二步:尝试从合同商品价格协议中获取价格
- */
- stepTwo(sa_contractid);
- /*
- *第三步:如果商品价格还是小于0,则尝试从合同类别折扣协议中去获取价格
- */
- stepThree(sa_contractid, getGraderateprice());
- /*
- * 第四步:如果商品价格还是小于0,则从合同折扣协议中去获取价格
- */
- BigDecimal discountrate = contractRows.get(0).getBigDecimal("discountrate");//折扣率
- if (contractprice.compareTo(BigDecimal.ZERO) <= 0 && discountrate.compareTo(BigDecimal.ZERO) > 0) {
- contractprice = getScalePrice(getGraderateprice().multiply(discountrate));
- }
- }
- }
- /*
- * 如果框架合同中没有获取到有效的价格,则返回商户等级价格信息
- */
- if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
- contractprice = getGraderateprice();
- }
- /*
- * 如果没有获取到有效的价格,则返回原价格信息
- */
- if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
- contractprice = getPrice();
- }
- }
- return contractprice;
- }
- /**
- * 获取项目合同价,项目合同中必须包含此商品
- *
- * @param sa_contractid 合同id
- * @return
- */
- public BigDecimal getContractprice(long sa_contractid) throws YosException {
- if (contractprice.compareTo(BigDecimal.ZERO) < 0) {
- /*
- * 第一步:查询框架合同并试图获取价格:优先级 商品价格、商品折扣==》类别折扣==》合同折扣
- */
- {
- /*
- * 第二步:尝试从合同商品价格协议中获取价格
- */
- stepTwo_Contract(sa_contractid);
- /*
- *第三步:如果商品价格还是小于0,则尝试从合同类别折扣协议中去获取价格
- */
- stepThree(sa_contractid, getMarketprice());
- /*
- * 第四步:如果商品价格还是小于0,则从合同折扣协议中去获取价格
- */
- Rows rows = dbConnect.runSqlQuery("SELECT discountrate from sa_contract WHERE sa_contractid=" + sa_contractid + " and siteid = '" + controller.siteid + "'");
- BigDecimal discountrate = BigDecimal.valueOf(1);//折扣率
- if (rows.isNotEmpty()) {
- discountrate = rows.get(0).getBigDecimal("discountrate");//折扣率
- }
- if (contractprice.compareTo(BigDecimal.ZERO) <= 0 && discountrate.compareTo(BigDecimal.ZERO) > 0) {
- contractprice = getScalePrice(getMarketprice().multiply(discountrate));
- }
- }
- /*
- * 如果框架合同中没有获取到有效的价格,则返回商户等级价格信息
- */
- if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
- // contractprice = getGraderateprice();
- contractprice = getMarketprice();
- }
- /*
- * 如果没有获取到有效的价格,则返回原价格信息
- */
- if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
- // contractprice = getPrice();
- contractprice = getMarketprice();
- }
- }
- return contractprice;
- }
- /**
- * 第二步:尝试从合同商品价格协议中获取价格
- *
- * @param sa_contractid
- * @throws YosException
- */
- public void stepTwo(Long sa_contractid) throws YosException {
- SQLFactory contract_itemQuery = new SQLFactory(this, "商户框架合同商品折扣查询");
- contract_itemQuery.addParameter("siteid", controller.siteid);
- contract_itemQuery.addParameter("sa_contractid", sa_contractid);
- contract_itemQuery.addParameter("itemid", itemid);
- Rows contract_itemRows = dbConnect.runSqlQuery(contract_itemQuery.getSQL());
- if (contract_itemRows.size() > 1) {
- throw new YosException("存在一个以上的价格协议信息:" + itemid);
- } else if (contract_itemRows.size() == 1) {
- //类型:折扣系数、指定单价
- String type = contract_itemRows.get(0).getString("type");
- //商品折扣率
- BigDecimal discountrate_item = contract_itemRows.get(0).getBigDecimal("discountrate");
- //商品价格
- BigDecimal price = contract_itemRows.get(0).getBigDecimal("price");
- if (type.equals("折扣系数") && discountrate_item.compareTo(BigDecimal.ZERO) > 0) {
- contractprice = getScalePrice(getGraderateprice().multiply(discountrate_item));
- } else if (type.equals("指定单价") && price.compareTo(BigDecimal.ZERO) > 0) {
- contractprice = price;
- }
- }
- }
- /**
- * 第二步:尝试从合同商品价格协议中获取价格
- *
- * @param sa_contractid
- * @throws YosException
- */
- public void stepTwo_Contract(Long sa_contractid) throws YosException {
- SQLFactory contract_itemQuery = new SQLFactory(this, "商户框架合同商品折扣查询");
- contract_itemQuery.addParameter("siteid", controller.siteid);
- contract_itemQuery.addParameter("sa_contractid", sa_contractid);
- contract_itemQuery.addParameter("itemid", itemid);
- Rows contract_itemRows = dbConnect.runSqlQuery(contract_itemQuery.getSQL());
- if (contract_itemRows.size() > 1) {
- throw new YosException("存在一个以上的价格协议信息:" + itemid);
- } else if (contract_itemRows.size() == 1) {
- //商品价格
- BigDecimal price = contract_itemRows.get(0).getBigDecimal("price");
- contractprice = price;
- }
- }
- /**
- * 第三步:如果商品价格还是小于0,则尝试从合同类别折扣协议中去获取价格
- *
- * @param sa_contractid
- */
- public void stepThree(Long sa_contractid, BigDecimal price) throws YosException {
- if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
- //获取商品的营销类别信息
- Rows itemclassidRows = dbConnect.runSqlQuery("select itemclassid from sa_itemsaleclass where siteid='" + controller.siteid + "' and itemid=" + itemid);
- ArrayList<Long> ids = itemclassidRows.toArrayList("itemclassid", new ArrayList<>());
- //获取所有的上级营销分类
- ArrayList<Long> itemclassids = ItemClass.getParentItemClassIds(controller, ids);
- //合并商品营销分类
- itemclassids.addAll(ids);
- //如果商品存在多个分类时,价格取值原则,折扣最小原则(默认原则),从后往前原则(依具体客户的需求实现)
- {
- /*
- * 最小原则取价
- */
- SQLFactory contract_itemclassQuery = new SQLFactory(this, "商户框架合同商品营销类别最低折扣查询");
- contract_itemclassQuery.addParameter("siteid", controller.siteid);
- contract_itemclassQuery.addParameter("sa_contractid", sa_contractid);
- contract_itemclassQuery.addParameter_in("itemclassid", itemclassids);
- Rows contract_itemsaleclassRows = dbConnect.runSqlQuery(contract_itemclassQuery.getSQL());
- if (contract_itemsaleclassRows.isNotEmpty()) {
- //商品类别折扣率
- BigDecimal discountrate_itemclass = contract_itemsaleclassRows.get(0).getBigDecimal("discountrate");
- if (discountrate_itemclass.compareTo(BigDecimal.ZERO) > 0) {
- contractprice = getScalePrice(price.multiply(discountrate_itemclass));
- }
- }
- }
- }
- }
- /**
- * 根据商户id及商品id获取商品价格,sys_enterpriseid为0时,只能获取到牌价信息
- *
- * @param controller
- * @param sys_enterpriseid
- * @param itemid
- * @return
- * @throws YosException
- */
- public static ItemPrice getItemPrice(Controller controller, long sys_enterpriseid, long itemid) throws YosException {
- return new ItemPrice(controller, sys_enterpriseid, itemid);
- }
- /**
- * 根据商户id及商品id获取商品价格,sys_enterpriseid为0时,只能获取到牌价信息
- *
- * @param controller
- * @param sys_enterpriseid
- * @param itemids
- * @return
- * @throws YosException
- */
- public static HashMap<Long, ItemPrice> getItemPrice(Controller controller, long sys_enterpriseid, ArrayList<Long> itemids) throws YosException {
- //批量查询时,为了减少数据库查询量,建立创建一个临时key,用于缓存部分通用信息
- String cachekey = Encryption.Encode_MD5(controller.usersiteid + Math.random(2) + Calendar.getInstance().getTimeInMillis());
- HashMap<Long, ItemPrice> map = new HashMap<>();
- for (long itemid : itemids) {
- ItemPrice itemPrice = new ItemPrice(controller, sys_enterpriseid, itemid);
- itemPrice.cachekey = cachekey;
- map.put(itemid, itemPrice);
- }
- contractRowscacheMap.remove(cachekey);
- return map;
- }
- /**
- * 根据商户id及商品id获取商品价格,只能获取到牌价信息
- *
- * @param controller
- * @param itemid
- * @return
- * @throws YosException
- */
- public static ItemPrice getItemPrice(Controller controller, long itemid) throws YosException {
- return getItemPrice(controller, 0, itemid);
- }
- /**
- * 根据商户id及商品id获取商品价格,只能获取到牌价信息
- *
- * @param controller
- * @param itemids
- * @return
- * @throws YosException
- */
- public static HashMap<Long, ItemPrice> getItemPrice(Controller controller, ArrayList<Long> itemids) throws YosException {
- return getItemPrice(controller, 0, itemids);
- }
- /**
- * 根据商户id及商品组id,返回商品组的每个商品价格信息
- *
- * @param controller
- * @param sys_enterpriseid
- * @param sa_itemgroupid
- * @return <itemid,ItemPrice>
- * @throws YosException
- */
- public static HashMap<Long, ItemPrice> getItemGroupPrice(Controller controller, long sys_enterpriseid, long sa_itemgroupid) throws YosException {
- ArrayList<ItemPrice> itemPriceList = new ArrayList<>();
- Rows rows = controller.dbConnect.runSqlQuery("select itemid from sa_itemgroupmx where siteid='" + controller.siteid + "' and sa_itemgroupid=" + sa_itemgroupid);
- return getItemPrice(controller, sys_enterpriseid, rows.toArrayList("itemid", new ArrayList<>()));
- }
- /**
- * 根据商户id及商品组id,返回商品组的每个商品价格信息
- *
- * @param controller
- * @param sys_enterpriseid
- * @param sa_itemgroupids
- * @return <sa_itemgroupid,<itemid,ItemPrice>>
- * @throws YosException
- */
- public static HashMap<Long, HashMap<Long, ItemPrice>> getItemGroupPrice(Controller controller, long sys_enterpriseid, long[] sa_itemgroupids) throws YosException {
- HashMap<Long, HashMap<Long, ItemPrice>> map = new HashMap<>();
- for (long sa_itemgroupid : sa_itemgroupids) {
- map.put(sa_itemgroupid, getItemGroupPrice(controller, sys_enterpriseid, sa_itemgroupid));
- }
- return map;
- }
- /**
- * 根据商品组id,返回商品组的每个商品价格信息
- *
- * @param controller
- * @param sa_itemgroupid
- * @return <itemid,ItemPrice>
- * @throws YosException
- */
- public static HashMap<Long, ItemPrice> getItemGroupPrice(Controller controller, long sa_itemgroupid) throws YosException {
- ArrayList<ItemPrice> itemPriceList = new ArrayList<>();
- Rows rows = controller.dbConnect.runSqlQuery("select itemid from sa_itemgroupmx where siteid='" + controller.siteid + "' and sa_itemgroupid=" + sa_itemgroupid);
- return getItemPrice(controller, rows.toArrayList("itemid", new ArrayList<>()));
- }
- /**
- * 根据商品组id,返回商品组的每个商品价格信息
- *
- * @param controller
- * @param sa_itemgroupids
- * @return <sa_itemgroupid,<itemid,ItemPrice>>
- * @throws YosException
- */
- public static HashMap<Long, HashMap<Long, ItemPrice>> getItemGroupPrice(Controller controller, long[] sa_itemgroupids) throws YosException {
- HashMap<Long, HashMap<Long, ItemPrice>> map = new HashMap<>();
- for (long sa_itemgroupid : sa_itemgroupids) {
- map.put(sa_itemgroupid, getItemGroupPrice(controller, sa_itemgroupid));
- }
- return map;
- }
- /**
- * @param price
- * @return
- */
- public BigDecimal getScalePrice(BigDecimal price) {
- return (price).setScale(2, BigDecimal.ROUND_HALF_UP);
- }
- }
|