订单计算相关
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.

235 lines
6.4 KiB

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