订单计算相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.3 KiB

3 years ago
  1. /**
  2. * 计算佣金
  3. * 2020/05/22
  4. */
  5. package ordercalc
  6. import (
  7. "errors"
  8. "strings"
  9. )
  10. /**
  11. * 计算某一商品对分销商的分销价
  12. * @param business 分销商信息
  13. * @param productSpecialPrice 分销特殊价格
  14. * @param distributionPrice 基础分销价
  15. * @param percentage 分销比例
  16. * @param productPrice 商品销售价
  17. * 2020/05/22
  18. * gz
  19. */
  20. func DistributionPrice(business map[string]string, productSpecialPrice []map[string]string, distributionPrice interface{}, percentAge interface{}, productPrice interface{}) (float64, error) {
  21. product_price := ToString(productPrice)
  22. if len(business) < 1 {
  23. return 0, errors.New("参数错误")
  24. }
  25. _, idExist := business["Id"]
  26. _, levelIdExist := business["LevelId"]
  27. if !idExist || !levelIdExist {
  28. return 0, errors.New("分销商参数错误")
  29. }
  30. distribution_price := ToString(distributionPrice)
  31. distribution_price_float, err := ToFloat64(distributionPrice)
  32. if err != nil {
  33. return 0, err
  34. }
  35. percent_age := ToString(percentAge)
  36. if len(productSpecialPrice) < 1 && percent_age == "" {
  37. return distribution_price_float, nil //没有特殊价格,没有分销比例,返回原分销价
  38. }
  39. var formula string //分销价计算公式
  40. var is_special_price bool = false //是否使用特殊价格方式,因特殊价格中可能只能会员价,记录以供再次判断
  41. var ok bool
  42. if len(productSpecialPrice) > 0 { //特殊价格方式
  43. is_special_price = true //有一条特殊价格,则是使用特殊价
  44. for _, item := range productSpecialPrice {
  45. if _, ok = item["BusinessId"]; ok {
  46. if item["BusinessId"] != "0" && item["BusinessId"] == business["Id"] {
  47. formula = item["DistributionPrice"]
  48. break
  49. }
  50. }
  51. if _, ok = item["BusinessLevelId"]; ok {
  52. if item["BusinessLevelId"] != "0" && item["BusinessLevelId"] == business["LevelId"] {
  53. formula = item["DistributionPrice"]
  54. }
  55. }
  56. }
  57. }
  58. var price []string
  59. var result float64 = 0 //最终分销价
  60. if is_special_price {
  61. if formula != "" {
  62. formula = strings.Replace(formula, "#distribution_price#", distribution_price, 1)
  63. } else {
  64. formula = distribution_price
  65. }
  66. //计算表达式
  67. switch {
  68. case strings.Contains(formula, "+"):
  69. price = strings.Split(formula, "+")
  70. result = FloatAdd(price[0], price[1])
  71. case strings.Contains(formula, "-"):
  72. price = strings.Split(formula, "-")
  73. result = FloatSub(price[0], price[1])
  74. case strings.Contains(formula, "*"):
  75. price = strings.Split(formula, "*")
  76. result = FloatMul(price[0], price[1], 2)
  77. case strings.Contains(formula, "/"):
  78. price = strings.Split(formula, "/")
  79. result = FloatQuo(price[0], price[1], 2)
  80. default:
  81. //使用固定价格或者使用基础分销价
  82. if formula != "" {
  83. result, _ = ToFloat64(formula)
  84. } else {
  85. result = distribution_price_float
  86. }
  87. }
  88. } else {
  89. //使用分销比例
  90. if product_price == "" {
  91. return 0, errors.New("商品价格错误")
  92. }
  93. price = strings.Split(percent_age, ":")
  94. totalCommission := FloatSub(productPrice, distributionPrice) //总佣金
  95. if totalCommission <= 0 {
  96. return ToFloat64(productPrice) //总佣金为负,返回商品原价
  97. }
  98. if _, ok = business["Grade"]; ok {
  99. level_count := len(price) //分销级数
  100. var totalPercent, cent, commission float64 //总分配份数,分销比例,获得的佣金
  101. switch level_count {
  102. case 3:
  103. totalPercent = FloatAdd(price[0], price[1], price[2])
  104. if totalPercent <= 0 {
  105. result = distribution_price_float //佣金分配比例为0,返回基础分销价
  106. } else {
  107. switch business["Grade"] {
  108. case "1":
  109. //一级分销商得到所有佣金
  110. cent = 1 //分销比例
  111. case "2":
  112. //二级分销商得到所有佣金
  113. cent = FloatQuo(FloatAdd(price[0], price[1]), totalPercent) //分销比例
  114. case "0", "3":
  115. //三级或无级别分销商得到所有佣金
  116. cent = FloatQuo(price[0], totalPercent) //分销比例
  117. }
  118. commission = FloatMul(totalCommission, cent, 2)
  119. result = FloatSub(productPrice, commission) //分销价=商品价-佣金
  120. }
  121. case 2:
  122. totalPercent = FloatAdd(price[0], price[1])
  123. if totalPercent <= 0 {
  124. result = distribution_price_float //佣金分配比例为0,返回基础分销价
  125. } else {
  126. switch business["Grade"] {
  127. case "1":
  128. //一级分销商得到所有佣金
  129. cent = 1 //分销比例
  130. case "0", "2":
  131. //二级或无级别分销商得到所有佣金
  132. cent = FloatQuo(price[0], totalPercent) //分销比例
  133. }
  134. commission = FloatMul(totalCommission, cent, 2)
  135. result = FloatSub(productPrice, commission) //分销价=商品价-佣金
  136. }
  137. case 1:
  138. //只有一级
  139. result = distribution_price_float
  140. }
  141. } else {
  142. return 0, errors.New("分销商参数错误,没有分销级别")
  143. }
  144. }
  145. if result < 0 {
  146. result, _ = ToFloat64(productPrice)
  147. }
  148. return result, nil
  149. }
  150. /**
  151. * 计算某一商品的VIP价
  152. * @param productSpecialPrice 特殊价格
  153. * @param productPrice 商品销售价
  154. * 2020/06/05
  155. * gz
  156. */
  157. func VipPrice(productSpecialPrice []map[string]string, productPrice interface{}) (float64, error) {
  158. product_price := ToString(productPrice)
  159. product_price_float, _ := ToFloat64(productPrice)
  160. if len(productSpecialPrice) < 1 {
  161. return product_price_float, nil //没有特殊价格,返回原价
  162. }
  163. var formula string //VIP价计算公式
  164. var ok bool
  165. for _, item := range productSpecialPrice {
  166. if _, ok = item["RetailPrice"]; ok {
  167. if item["RetailPrice"] != "" {
  168. formula = item["RetailPrice"]
  169. break
  170. }
  171. }
  172. }
  173. if formula != "" {
  174. formula = strings.Replace(formula, "#retail_price#", product_price, 1)
  175. } else {
  176. formula = product_price
  177. }
  178. //计算表达式
  179. var price []string
  180. var result float64 = 0 //最终VIP价
  181. switch {
  182. case strings.Contains(formula, "+"):
  183. price = strings.Split(formula, "+")
  184. result = FloatAdd(price[0], price[1])
  185. case strings.Contains(formula, "-"):
  186. price = strings.Split(formula, "-")
  187. result = FloatSub(price[0], price[1])
  188. case strings.Contains(formula, "*"):
  189. price = strings.Split(formula, "*")
  190. result = FloatMul(price[0], price[1], 2)
  191. case strings.Contains(formula, "/"):
  192. price = strings.Split(formula, "/")
  193. result = FloatQuo(price[0], price[1], 2)
  194. default:
  195. result = product_price_float
  196. }
  197. if result < 0 || result > product_price_float {
  198. result = product_price_float
  199. }
  200. return result, nil
  201. }