/** * 计算佣金 * 2020/05/22 */ package ordercalc import ( "errors" "strings" ) /** * 计算某一商品对分销商的分销价 * @param business 分销商信息 * @param productSpecialPrice 分销特殊价格 * @param distributionPrice 基础分销价 * @param percentage 分销比例 * @param productPrice 商品销售价 * 2020/05/22 * gz */ func DistributionPrice(business map[string]string, productSpecialPrice []map[string]string, distributionPrice interface{}, percentAge interface{}, productPrice interface{}) (float64, error) { product_price := ToString(productPrice) if len(business) < 1 { return 0, errors.New("参数错误") } _, idExist := business["Id"] _, levelIdExist := business["LevelId"] if !idExist || !levelIdExist { return 0, errors.New("分销商参数错误") } distribution_price := ToString(distributionPrice) distribution_price_float, err := ToFloat64(distributionPrice) if err != nil { return 0, err } percent_age := ToString(percentAge) if len(productSpecialPrice) < 1 && percent_age == "" { return distribution_price_float, nil //没有特殊价格,没有分销比例,返回原分销价 } var formula string //分销价计算公式 var is_special_price bool = false //是否使用特殊价格方式,因特殊价格中可能只能会员价,记录以供再次判断 var ok bool if len(productSpecialPrice) > 0 { //特殊价格方式 is_special_price = true //有一条特殊价格,则是使用特殊价 for _, item := range productSpecialPrice { if _, ok = item["BusinessId"]; ok { if item["BusinessId"] != "0" && item["BusinessId"] == business["Id"] { formula = item["DistributionPrice"] break } } if _, ok = item["BusinessLevelId"]; ok { if item["BusinessLevelId"] != "0" && item["BusinessLevelId"] == business["LevelId"] { formula = item["DistributionPrice"] } } } } var price []string var result float64 = 0 //最终分销价 if is_special_price { if formula != "" { formula = strings.Replace(formula, "#distribution_price#", distribution_price, 1) } else { formula = distribution_price } //计算表达式 switch { case strings.Contains(formula, "+"): price = strings.Split(formula, "+") result = FloatAdd(price[0], price[1]) case strings.Contains(formula, "-"): price = strings.Split(formula, "-") result = FloatSub(price[0], price[1]) case strings.Contains(formula, "*"): price = strings.Split(formula, "*") result = FloatMul(price[0], price[1], 2) case strings.Contains(formula, "/"): price = strings.Split(formula, "/") result = FloatQuo(price[0], price[1], 2) default: //使用固定价格或者使用基础分销价 if formula != "" { result, _ = ToFloat64(formula) } else { result = distribution_price_float } } } else { //使用分销比例 if product_price == "" { return 0, errors.New("商品价格错误") } price = strings.Split(percent_age, ":") totalCommission := FloatSub(productPrice, distributionPrice) //总佣金 if totalCommission <= 0 { return ToFloat64(productPrice) //总佣金为负,返回商品原价 } if _, ok = business["Grade"]; ok { level_count := len(price) //分销级数 var totalPercent, cent, commission float64 //总分配份数,分销比例,获得的佣金 switch level_count { case 3: totalPercent = FloatAdd(price[0], price[1], price[2]) if totalPercent <= 0 { result = distribution_price_float //佣金分配比例为0,返回基础分销价 } else { switch business["Grade"] { case "1": //一级分销商得到所有佣金 cent = 1 //分销比例 case "2": //二级分销商得到所有佣金 cent = FloatQuo(FloatAdd(price[0], price[1]), totalPercent) //分销比例 case "0", "3": //三级或无级别分销商得到所有佣金 cent = FloatQuo(price[0], totalPercent) //分销比例 } commission = FloatMul(totalCommission, cent, 2) result = FloatSub(productPrice, commission) //分销价=商品价-佣金 } case 2: totalPercent = FloatAdd(price[0], price[1]) if totalPercent <= 0 { result = distribution_price_float //佣金分配比例为0,返回基础分销价 } else { switch business["Grade"] { case "1": //一级分销商得到所有佣金 cent = 1 //分销比例 case "0", "2": //二级或无级别分销商得到所有佣金 cent = FloatQuo(price[0], totalPercent) //分销比例 } commission = FloatMul(totalCommission, cent, 2) result = FloatSub(productPrice, commission) //分销价=商品价-佣金 } case 1: //只有一级 result = distribution_price_float } } else { return 0, errors.New("分销商参数错误,没有分销级别") } } if result < 0 { result, _ = ToFloat64(productPrice) } return result, nil } /** * 计算某一商品的VIP价 * @param productSpecialPrice 特殊价格 * @param productPrice 商品销售价 * 2020/06/05 * gz */ func VipPrice(productSpecialPrice []map[string]string, productPrice interface{}) (float64, error) { product_price := ToString(productPrice) product_price_float, _ := ToFloat64(productPrice) if len(productSpecialPrice) < 1 { return product_price_float, nil //没有特殊价格,返回原价 } var formula string //VIP价计算公式 var ok bool for _, item := range productSpecialPrice { if _, ok = item["RetailPrice"]; ok { if item["RetailPrice"] != "" { formula = item["RetailPrice"] break } } } if formula != "" { formula = strings.Replace(formula, "#retail_price#", product_price, 1) } else { formula = product_price } //计算表达式 var price []string var result float64 = 0 //最终VIP价 switch { case strings.Contains(formula, "+"): price = strings.Split(formula, "+") result = FloatAdd(price[0], price[1]) case strings.Contains(formula, "-"): price = strings.Split(formula, "-") result = FloatSub(price[0], price[1]) case strings.Contains(formula, "*"): price = strings.Split(formula, "*") result = FloatMul(price[0], price[1], 2) case strings.Contains(formula, "/"): price = strings.Split(formula, "/") result = FloatQuo(price[0], price[1], 2) default: result = product_price_float } if result < 0 || result > product_price_float { result = product_price_float } return result, nil }