promotion_helper.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. package helpers
  2. import (
  3. "fmt"
  4. "fohow.com/apps/models/balance_model"
  5. "fohow.com/apps/models/cent_model"
  6. "fohow.com/apps/models/order_model"
  7. "fohow.com/apps/models/product_model"
  8. "fohow.com/apps/models/promotion_model"
  9. "fohow.com/apps/models/user_model"
  10. "fohow.com/libs/tool"
  11. "github.com/astaxie/beego"
  12. "time"
  13. )
  14. //订单促销
  15. /*
  16. 如果促销条件中,设置的项目有一项不满足即视为不满足促销条件
  17. */
  18. func SetOrderPromotion(orderId string, wxUid int64) {
  19. beego.BeeLogger.Warn("******* SetOrderPromotion orderId:%s wxUid:%d", orderId, wxUid)
  20. firstOrder := false
  21. //获取订单明细
  22. order := order_model.GetOrderById(orderId, false)
  23. if order == nil {
  24. return
  25. }
  26. //检查会员是否首次下单
  27. list := order_model.GetWxUserOrders(wxUid)
  28. if len(list) == 1 {
  29. firstOrder = true
  30. }
  31. queryDate := time.Now()
  32. // 获取所有有效促销记录
  33. effectivePromotions := promotion_model.GetEffetivePromotions(queryDate, order.OrderType, order.Depart, false)
  34. for _, item := range effectivePromotions {
  35. //beego.Warn("item_name%d", item.Name)
  36. firstFlag := true
  37. totalFlag := true
  38. numsFlag := true
  39. if item.MaxTotal > 0 && order.TotalPrice > item.MaxTotal {
  40. totalFlag = false
  41. continue
  42. }
  43. if item.IsFirst && !firstOrder {
  44. firstFlag = false
  45. continue
  46. }
  47. if item.MinTotal > 0 && order.TotalPrice < item.MinTotal {
  48. totalFlag = false
  49. firstFlag = false
  50. continue
  51. }
  52. nums := int64(999)
  53. nums1 := int64(999)
  54. nums2 := int64(999)
  55. nums3 := int64(999)
  56. nums4 := int64(999)
  57. nums5 := int64(999)
  58. nums6 := int64(999)
  59. if item.Prod1 > 0 {
  60. prd := product_model.GetProductById(item.Prod1, true)
  61. nums1 = int64(0)
  62. if prd != nil {
  63. detail_nums1 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  64. if detail_nums1 > int64(0) && item.Prod1 > 0 && item.Nums1 > 0 {
  65. nums1 = int64(detail_nums1 / item.Nums1)
  66. }
  67. }
  68. }
  69. if item.Prod2 > 0 {
  70. prd := product_model.GetProductById(item.Prod2, true)
  71. nums2 = int64(0)
  72. if prd != nil {
  73. detail_nums2 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  74. if detail_nums2 > int64(0) && item.Prod2 > 0 && item.Nums2 > 0 {
  75. nums2 = int64(detail_nums2 / item.Nums2)
  76. }
  77. }
  78. }
  79. if item.Prod3 > 0 {
  80. prd := product_model.GetProductById(item.Prod3, true)
  81. if prd != nil {
  82. nums3 = int64(0)
  83. detail_nums3 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  84. if detail_nums3 > 0 && item.Prod3 > 0 && item.Nums3 > 0 {
  85. nums3 = int64(detail_nums3 / item.Nums3)
  86. }
  87. }
  88. }
  89. if item.MinTotal > 0 {
  90. nums4 = int64(order.TotalPrice / item.MinTotal)
  91. }
  92. if item.Prod4 > 0 {
  93. prd := product_model.GetProductById(item.Prod4, true)
  94. nums5 = int64(0)
  95. if prd != nil {
  96. detail_nums4 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  97. if detail_nums4 > 0 && item.Prod4 > 0 && item.Nums4 > 0 {
  98. nums5 = int64(detail_nums4 / item.Nums4)
  99. }
  100. }
  101. }
  102. if item.Prod5 > 0 {
  103. prd := product_model.GetProductById(item.Prod5, true)
  104. nums6 = int64(0)
  105. if prd != nil {
  106. detail_nums5 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  107. if detail_nums5 > 0 && item.Prod5 > 0 && item.Nums5 > 0 {
  108. nums6 = int64(detail_nums5 / item.Nums5)
  109. }
  110. }
  111. }
  112. val := []int64{nums1, nums2, nums3, nums4, nums5, nums6}
  113. nums = tool.Min(val...)
  114. if !item.IsMore {
  115. val := []int64{nums, 1}
  116. nums = tool.Min(val...)
  117. }
  118. if nums == int64(999) || nums == int64(0) {
  119. numsFlag = false
  120. }
  121. /* beego.Warn("item_name-1%d", item.Name)
  122. beego.Warn("firstFlag%v", firstFlag)
  123. beego.Warn("totalFlag%v", totalFlag)
  124. beego.Warn("numsFlag%v", numsFlag)
  125. beego.Warn("nums%d", nums)*/
  126. //满足促销条件
  127. if firstFlag && totalFlag && numsFlag {
  128. if item.SendProd1 > 0 && item.SendNums1 > 0 {
  129. sendNums1 := nums * item.SendNums1
  130. //赠送赠品1
  131. product := product_model.GetProductById(item.SendProd1, true)
  132. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums1, order.Depart)
  133. }
  134. if item.SendProd2 > 0 && item.SendNums2 > 0 {
  135. sendNums2 := nums * item.SendNums2
  136. //赠送赠品1
  137. product := product_model.GetProductById(item.SendProd2, true)
  138. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums2, order.Depart)
  139. }
  140. if item.SendProd3 > 0 && item.SendNums3 > 0 {
  141. sendNums3 := nums * item.SendNums3
  142. //赠送赠品1
  143. product := product_model.GetProductById(item.SendProd3, true)
  144. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums3, order.Depart)
  145. }
  146. if item.SendProd4 > 0 && item.SendNums4 > 0 {
  147. sendNums4 := nums * item.SendNums4
  148. //赠送赠品1
  149. product := product_model.GetProductById(item.SendProd4, true)
  150. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums4, order.Depart)
  151. }
  152. if item.SendProd5 > 0 && item.SendNums5 > 0 {
  153. sendNums5 := nums * item.SendNums5
  154. //赠送赠品1
  155. product := product_model.GetProductById(item.SendProd5, true)
  156. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums5, order.Depart)
  157. }
  158. //赠送积分 or 代办费
  159. if item.Cash > 0 {
  160. totalCash := nums * item.Cash
  161. source := balance_model.BALANCE_SOURCE_PROMOTION
  162. remark := fmt.Sprintf("促销赠送提货券")
  163. new(balance_model.Balance).Create(order.WxUserId, order.UserId, totalCash, source, order.OrderId, remark)
  164. }
  165. if item.Cent > 0 {
  166. totalCent := nums * item.Cent
  167. source := cent_model.PROMOTION_SEND
  168. remark := fmt.Sprintf("促销活动赠送")
  169. new(cent_model.CentBalance).Create(order.WxUserId, totalCent, source, order.OrderId, remark)
  170. }
  171. }
  172. }
  173. }
  174. //订单促销
  175. /*
  176. 如果促销条件中,设置的项目有一项不满足即视为不满足促销条件
  177. */
  178. func SetOrderPromotionPro(orderId string, wxUid int64) {
  179. beego.BeeLogger.Warn("******* SetOrderPromotionPro orderId:%s wxUid:%d", orderId, wxUid)
  180. firstOrder := false
  181. //获取订单明细
  182. order := order_model.GetOrderById(orderId, false)
  183. if order == nil {
  184. return
  185. }
  186. //检查会员是否首次下单
  187. list := order_model.GetWxUserOrders(wxUid)
  188. if len(list) == 1 {
  189. firstOrder = true
  190. }
  191. var noInovceArr []int64
  192. var promotionsArr []int64
  193. queryDate := time.Now()
  194. // 获取所有有效促销记录
  195. effectivePromotions := promotion_model.GetEffetivePromotions(queryDate, order.OrderType, order.Depart, false)
  196. for _, item := range effectivePromotions {
  197. //beego.Warn("item_name%d", item.Name)
  198. noInvovedAmount := order_model.GetNotInvovedAmount(item.Id, order.OrderId)
  199. firstFlag := true
  200. totalFlag := true
  201. numsFlag := true
  202. if item.MaxTotal > 0 && (order.TotalPrice-noInvovedAmount) > item.MaxTotal {
  203. totalFlag = false
  204. continue
  205. }
  206. if item.IsFirst && !firstOrder {
  207. firstFlag = false
  208. continue
  209. }
  210. if item.MinTotal > 0 && (order.TotalPrice-noInvovedAmount) < item.MinTotal {
  211. totalFlag = false
  212. firstFlag = false
  213. continue
  214. }
  215. nums := int64(999)
  216. nums1 := int64(999)
  217. nums2 := int64(999)
  218. nums3 := int64(999)
  219. nums4 := int64(999)
  220. nums5 := int64(999)
  221. nums6 := int64(999)
  222. if item.Prod1 > 0 {
  223. prd := product_model.GetProductById(item.Prod1, true)
  224. nums1 = int64(0)
  225. if prd != nil {
  226. detail_nums1 := int64(0)
  227. detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod1)
  228. if detailItem != nil {
  229. detail_nums1 = detailItem.Count
  230. }
  231. if detail_nums1 > int64(0) && item.Prod1 > 0 && item.Nums1 > 0 {
  232. nums1 = int64(detail_nums1 / item.Nums1)
  233. }
  234. }
  235. }
  236. if item.Prod2 > 0 {
  237. prd := product_model.GetProductById(item.Prod2, true)
  238. nums2 = int64(0)
  239. if prd != nil {
  240. detail_nums2 := int64(0)
  241. detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod2)
  242. if detailItem != nil {
  243. detail_nums2 = detailItem.Count
  244. }
  245. //detail_nums2 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  246. if detail_nums2 > int64(0) && item.Prod2 > 0 && item.Nums2 > 0 {
  247. nums2 = int64(detail_nums2 / item.Nums2)
  248. }
  249. }
  250. }
  251. if item.Prod3 > 0 {
  252. nums3 = int64(0)
  253. prd := product_model.GetProductById(item.Prod3, true)
  254. if prd != nil {
  255. detail_nums3 := int64(0)
  256. detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod3)
  257. if detailItem != nil {
  258. detail_nums3 = detailItem.Count
  259. }
  260. //detail_nums3 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  261. if detail_nums3 > 0 && item.Prod3 > 0 && item.Nums3 > 0 {
  262. nums3 = int64(detail_nums3 / item.Nums3)
  263. }
  264. }
  265. }
  266. if item.MinTotal > 0 {
  267. nums4 = int64((order.TotalPrice - noInvovedAmount) / item.MinTotal)
  268. }
  269. if item.Prod4 > 0 {
  270. prd := product_model.GetProductById(item.Prod4, true)
  271. nums5 = int64(0)
  272. if prd != nil {
  273. detail_nums4 := int64(0)
  274. detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod4)
  275. if detailItem != nil {
  276. detail_nums4 = detailItem.Count
  277. }
  278. //detail_nums4 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  279. if detail_nums4 > 0 && item.Prod4 > 0 && item.Nums4 > 0 {
  280. nums5 = int64(detail_nums4 / item.Nums4)
  281. }
  282. }
  283. }
  284. if item.Prod5 > 0 {
  285. prd := product_model.GetProductById(item.Prod5, true)
  286. nums6 = int64(0)
  287. if prd != nil {
  288. detail_nums5 := int64(0)
  289. detailItem := order_model.GetDetailsByOrderIdAndPid(orderId, item.Prod5)
  290. if detailItem != nil {
  291. detail_nums5 = detailItem.Count
  292. }
  293. //detail_nums5 := order_model.GetDetailsByOrderIdAndRelatePid(orderId, prd.RelateProductId)
  294. if detail_nums5 > 0 && item.Prod5 > 0 && item.Nums5 > 0 {
  295. nums6 = int64(detail_nums5 / item.Nums5)
  296. }
  297. }
  298. }
  299. val := []int64{nums1, nums2, nums3, nums4, nums5, nums6}
  300. nums = tool.Min(val...)
  301. if !item.IsMore {
  302. val := []int64{nums, 1}
  303. nums = tool.Min(val...)
  304. }
  305. if nums == int64(999) || nums == int64(0) {
  306. numsFlag = false
  307. }
  308. /* beego.Warn("item_name-1%d", item.Name)
  309. beego.Warn("firstFlag%v", firstFlag)
  310. beego.Warn("totalFlag%v", totalFlag)
  311. beego.Warn("numsFlag%v", numsFlag)
  312. beego.Warn("nums%d", nums)*/
  313. //满足促销条件
  314. if firstFlag && totalFlag && numsFlag {
  315. noInovceArr = append(noInovceArr, noInvovedAmount)
  316. promotionsArr = append(promotionsArr, item.Id)
  317. if item.SendProd1 > 0 && item.SendNums1 > 0 {
  318. sendNums1 := nums * item.SendNums1
  319. //赠送赠品1
  320. product := product_model.GetProductById(item.SendProd1, true)
  321. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums1, order.Depart)
  322. }
  323. if item.SendProd2 > 0 && item.SendNums2 > 0 {
  324. sendNums2 := nums * item.SendNums2
  325. //赠送赠品1
  326. product := product_model.GetProductById(item.SendProd2, true)
  327. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums2, order.Depart)
  328. }
  329. if item.SendProd3 > 0 && item.SendNums3 > 0 {
  330. sendNums3 := nums * item.SendNums3
  331. //赠送赠品1
  332. product := product_model.GetProductById(item.SendProd3, true)
  333. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums3, order.Depart)
  334. }
  335. if item.SendProd4 > 0 && item.SendNums4 > 0 {
  336. sendNums4 := nums * item.SendNums4
  337. //赠送赠品1
  338. product := product_model.GetProductById(item.SendProd4, true)
  339. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums4, order.Depart)
  340. }
  341. if item.SendProd5 > 0 && item.SendNums5 > 0 {
  342. sendNums5 := nums * item.SendNums5
  343. //赠送赠品1
  344. product := product_model.GetProductById(item.SendProd5, true)
  345. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, sendNums5, order.Depart)
  346. }
  347. //赠送积分 or 代办费
  348. if item.Cash > 0 {
  349. totalCash := nums * item.Cash
  350. source := balance_model.BALANCE_SOURCE_PROMOTION
  351. remark := fmt.Sprintf("促销赠送提货券")
  352. new(balance_model.Balance).Create(order.WxUserId, order.UserId, totalCash, source, order.OrderId, remark)
  353. }
  354. if item.Cent > 0 {
  355. totalCent := nums * item.Cent
  356. source := cent_model.PROMOTION_SEND
  357. remark := fmt.Sprintf("促销活动赠送")
  358. new(cent_model.CentBalance).Create(order.WxUserId, totalCent, source, order.OrderId, remark)
  359. }
  360. }
  361. }
  362. //更新促销减去金额
  363. if len(promotionsArr) > 0 {
  364. promotions := tool.CombineInt64ToString(promotionsArr)
  365. noinvoves := tool.CombineInt64ToString(noInovceArr)
  366. order_model.UpdateOrderPromotions(promotions, noinvoves, orderId)
  367. }
  368. }
  369. func max(vals ...int64) int64 {
  370. var max int64
  371. for _, val := range vals {
  372. if val > max {
  373. max = val
  374. }
  375. }
  376. return max
  377. }
  378. /*
  379. 找到所有当前时间>《开始时间》且 当前时间<《结束时间》 且《是否启用》=1的促销记录
  380. { flag1=1,flag2=1,flag3=1
  381. NUMs=99,NUMs1=99,NUMs2=99,NUMs3=99,NUMs4=99
  382. 如果《是否首次下单》=是,且当前订单不是首次订单,则flag1=0
  383. 如果《最大金额》>0则要求订单金额大于此项值,则flag2=0
  384. 如果:《购买产品ID1》>0且《购买数量1》>0则NUMs1=购买产品ID1的数量/《购买数量1》
  385. 如果:《购买产品ID2》>0且《购买数量2》>0则当前订单中NUMs2=购买产品ID2的数量/《购买数量2》
  386. 如果:《购买产品ID3》>0且《购买数量3》>0则当前订单中NUMs3=购买产品ID3的数量/《购买数量3》
  387. 如果:《最小金额》>0则要求订单金额大于或等于此项值,则NUMs4=订单金额/《最小金额》
  388. NUMs=min(NUMs1,NUMs2,NUMs3,NUMs4);
  389. if(NUMs=99或NUMs=0) 则flag3=0
  390. 如果《是否多买多送》=否,NUMs=MIN(1,NUMs)
  391. if(flag1=1并且flag2=1并且flag3=1)
  392. { 如果《赠送产品ID1》>0且《赠送数量1》>0,则按《赠送数量1》*NUMs赠送《赠送产品ID1》;
  393. 如果《赠送产品ID2》>0且《赠送数量2》>0,则按《赠送数量2》*NUMs赠送《赠送产品ID2》;
  394. 如果《赠送产品ID3》>0且《赠送数量3》>0,则按《赠送数量3》*NUMs赠送《赠送产品ID3》;
  395. }
  396. 找下一个促销记录
  397. }
  398. */
  399. //充值促销
  400. func BalanceOrderPromotion(orderId string, wxUid, depart int64) {
  401. beego.BeeLogger.Warn("******* BalanceOrderPromotion orderId:%s wxUid:%d", orderId, wxUid)
  402. //获取订单明细
  403. balanceOrder := balance_model.GetBalanceOrderByOId(orderId, false)
  404. if balanceOrder == nil {
  405. return
  406. }
  407. queryDate := time.Now()
  408. // 获取所有有效促销记录
  409. effectivePromotions := promotion_model.GetBalanceEffetivePromotions(queryDate, depart, false)
  410. for _, item := range effectivePromotions {
  411. //beego.Warn("item_name%d", item.Name)
  412. totalFlag := true
  413. numsFlag := true
  414. if item.MaxTotal > 0 && balanceOrder.PaiedPrice > item.MaxTotal {
  415. totalFlag = false
  416. continue
  417. }
  418. if item.MinTotal > 0 && balanceOrder.PaiedPrice < item.MinTotal {
  419. totalFlag = false
  420. continue
  421. }
  422. count := int64(0)
  423. if item.MinTotal > 0 {
  424. count = int64(balanceOrder.PaiedPrice / item.MinTotal)
  425. }
  426. if count == int64(0) {
  427. numsFlag = false
  428. }
  429. if !item.IsMore {
  430. val := []int64{count, 1}
  431. count = tool.Min(val...)
  432. }
  433. beego.Warn("item_name-1%d", item.Name)
  434. beego.Warn("totalFlag%v", totalFlag)
  435. beego.Warn("numsFlag%v", numsFlag)
  436. beego.Warn("nums%d", count)
  437. //满足促销条件
  438. if totalFlag && numsFlag {
  439. source := promotion_model.SOURCE_BALANCE
  440. remark := fmt.Sprintf("充值订单(%s)充值成功--促销(%s)", orderId, item.Name)
  441. if item.SendProd1 > 0 && item.SendNums1 > 0 {
  442. sendNums1 := count * item.SendNums1
  443. //生成赠品
  444. product := product_model.GetProductById(item.SendProd1, true)
  445. if product != nil {
  446. total := sendNums1 * product.Price
  447. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums1, source, remark)
  448. }
  449. }
  450. if item.SendProd2 > 0 && item.SendNums2 > 0 {
  451. sendNums2 := count * item.SendNums2
  452. //赠送赠品1
  453. product := product_model.GetProductById(item.SendProd2, true)
  454. if product != nil {
  455. total := sendNums2 * product.Price
  456. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums2, source, remark)
  457. }
  458. }
  459. if item.SendProd3 > 0 && item.SendNums3 > 0 {
  460. sendNums3 := count * item.SendNums3
  461. //赠送赠品3
  462. product := product_model.GetProductById(item.SendProd3, true)
  463. if product != nil {
  464. total := sendNums3 * product.Price
  465. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums3, source, remark)
  466. }
  467. }
  468. //赠送积分 or 代办费
  469. if item.Cash > 0 {
  470. totalCash := count * item.Cash
  471. source := balance_model.BALANCE_SOURCE_BALANCE_RECHARGE_PROMOTION
  472. remark := fmt.Sprintf("充值促销赠送提货券")
  473. new(balance_model.Balance).Create(balanceOrder.WxUserId, balanceOrder.UserId, totalCash, source, balanceOrder.OrderId, remark)
  474. }
  475. if item.Cent > 0 {
  476. totalCent := count * item.Cent
  477. source := cent_model.PROMOTION_SEND
  478. remark := fmt.Sprintf("充值促销活动赠送")
  479. new(cent_model.CentBalance).Create(balanceOrder.WxUserId, totalCent, source, balanceOrder.OrderId, remark)
  480. }
  481. }
  482. }
  483. }
  484. // 店长申请促销
  485. func ShopOrderPromotion(shopApplyId, wxUid, depart int64) {
  486. beego.BeeLogger.Warn("******* ShopOrderPromotion shopApplyId:%d wxUid:%d", shopApplyId, wxUid)
  487. //获取订单明细
  488. shopApply := user_model.GetShopApplicationById(shopApplyId)
  489. if shopApply == nil {
  490. return
  491. }
  492. queryDate := time.Now()
  493. // 获取所有有效促销记录
  494. effectivePromotions := promotion_model.GetShopEffetivePromotions(queryDate, depart, false)
  495. for _, item := range effectivePromotions {
  496. //beego.Warn("item_name%d", item.Name)
  497. totalFlag := true
  498. numsFlag := true
  499. if item.Total > 0 && shopApply.Total != item.Total {
  500. totalFlag = false
  501. continue
  502. }
  503. count := int64(0)
  504. if item.Total > 0 {
  505. count = int64(1)
  506. }
  507. if count == int64(0) {
  508. numsFlag = false
  509. }
  510. beego.Warn("item_name-1%d", item.Name)
  511. beego.Warn("totalFlag%v", totalFlag)
  512. beego.Warn("numsFlag%v", numsFlag)
  513. beego.Warn("nums%d", count)
  514. //满足促销条件
  515. if totalFlag && numsFlag {
  516. orderId := fmt.Sprintf("shopapply-(%d)", shopApply.Id)
  517. source := promotion_model.SOURCE_SHOP
  518. remark := fmt.Sprintf("店长申请成功(%d)--促销(%s)", shopApply.Id, item.Name)
  519. if item.SendProd1 > 0 && item.SendNums1 > 0 {
  520. sendNums1 := count * item.SendNums1
  521. //生成赠品
  522. product := product_model.GetProductById(item.SendProd1, true)
  523. if product != nil {
  524. total := sendNums1 * product.Price
  525. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums1, source, remark)
  526. }
  527. }
  528. if item.SendProd2 > 0 && item.SendNums2 > 0 {
  529. sendNums2 := count * item.SendNums2
  530. //赠送赠品1
  531. product := product_model.GetProductById(item.SendProd2, true)
  532. if product != nil {
  533. total := sendNums2 * product.Price
  534. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums2, source, remark)
  535. }
  536. }
  537. if item.SendProd3 > 0 && item.SendNums3 > 0 {
  538. sendNums3 := count * item.SendNums3
  539. //赠送赠品3
  540. product := product_model.GetProductById(item.SendProd3, true)
  541. if product != nil {
  542. total := sendNums3 * product.Price
  543. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums3, source, remark)
  544. }
  545. }
  546. if item.SendProd4 > 0 && item.SendNums4 > 0 {
  547. sendNums4 := count * item.SendNums4
  548. //赠送赠品4
  549. product := product_model.GetProductById(item.SendProd4, true)
  550. if product != nil {
  551. total := sendNums4 * product.Price
  552. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums4, source, remark)
  553. }
  554. }
  555. if item.SendProd5 > 0 && item.SendNums5 > 0 {
  556. sendNums5 := count * item.SendNums5
  557. //赠送赠品3
  558. product := product_model.GetProductById(item.SendProd5, true)
  559. if product != nil {
  560. total := sendNums5 * product.Price
  561. new(promotion_model.Present).CreatePresent(wxUid, product.Price, total, product.Id, sendNums5, source, remark)
  562. }
  563. }
  564. //赠送积分 or 代办费
  565. if item.Cash > 0 {
  566. totalCash := count * item.Cash
  567. source := balance_model.BALANCE_SOURCE_SHOP_PROMOTION
  568. remark := fmt.Sprintf("店长申请促销赠送提货券")
  569. new(balance_model.Balance).Create(shopApply.WxUId, shopApply.UserId, totalCash, source, orderId, remark)
  570. }
  571. if item.Cent > 0 {
  572. totalCent := count * item.Cent
  573. source := cent_model.PROMOTION_SEND
  574. remark := fmt.Sprintf("店长申请促销活动赠送")
  575. new(cent_model.CentBalance).Create(shopApply.WxUId, totalCent, source, orderId, remark)
  576. }
  577. }
  578. }
  579. }
  580. // 赠品明细写入订单
  581. func PresentTransferToOrder(orderId string, wxUid int64) {
  582. beego.BeeLogger.Warn("******* PresentTransferToOrder shopApplyId:%s wxUid:%d", orderId, wxUid)
  583. //获取订单明细
  584. order := order_model.GetOrderById(orderId, false)
  585. if order == nil {
  586. return
  587. }
  588. // 获取所有未写入赠品记录
  589. presents := promotion_model.GetAllNoPatchPresents(wxUid)
  590. for _, item := range presents {
  591. //赠送赠品
  592. product := product_model.GetProductById(item.SendProd, true)
  593. if product != nil {
  594. item.OrderId = orderId
  595. item.Status = true
  596. item.Save()
  597. order_model.SendCreate(order.OrderId, order.Id, product.Id, product.Price, product.Price, product.Name, item.SendNums, order.Depart)
  598. }
  599. }
  600. beego.BeeLogger.Warn("******* End PresentTransferToOrder shopApplyId:%s wxUid:%d", orderId, wxUid)
  601. }
  602. // 创建店铺申请订单
  603. func CreateShopApplyOrder(shopAppplyId int64) {
  604. beego.BeeLogger.Warn("begin do shopAppply id:(%d)", shopAppplyId)
  605. shopApply := user_model.GetShopApplicationById(shopAppplyId)
  606. if shopApply == nil {
  607. beego.BeeLogger.Warn("shopApply error id:(%d)", shopAppplyId)
  608. return
  609. }
  610. wxUser := user_model.GetWxUserById(shopApply.WxUId, false)
  611. if wxUser == nil {
  612. beego.BeeLogger.Warn("shopApply wxuser no existst id:(%d)", shopAppplyId)
  613. return
  614. }
  615. //创建赠品订单
  616. order := new(order_model.Order).CreateNew(wxUser.Id, wxUser.UserId,
  617. int64(0), int64(0), order_model.ORDER_TYPE_NORMAL, wxUser.Depart, order_model.SOURCE_XCX)
  618. if order == nil {
  619. return
  620. }
  621. total := int64(0)
  622. delOrder := true
  623. queryDate := time.Now()
  624. // 获取所有有效促销记录
  625. effectivePromotions := promotion_model.GetShopEffetivePromotions(queryDate, shopApply.Depart, false)
  626. for _, item := range effectivePromotions {
  627. //beego.Warn("item_name%d", item.Name)
  628. totalFlag := true
  629. numsFlag := true
  630. if item.Total > 0 && shopApply.Total != item.Total {
  631. totalFlag = false
  632. continue
  633. }
  634. count := int64(0)
  635. if item.Total > 0 {
  636. count = int64(1)
  637. }
  638. if count == int64(0) {
  639. numsFlag = false
  640. }
  641. beego.Warn("item_name-1%d", item.Name)
  642. beego.Warn("totalFlag%v", totalFlag)
  643. beego.Warn("numsFlag%v", numsFlag)
  644. beego.Warn("nums%d", count)
  645. //满足促销条件
  646. if totalFlag && numsFlag {
  647. orderId := fmt.Sprintf("shopapply-(%d)", shopApply.Id)
  648. order.Source = promotion_model.SOURCE_SHOP
  649. order.Remark = fmt.Sprintf("店长申请成功(%d)--促销(%s)", shopApply.Id, item.Name)
  650. if item.SendProd1 > 0 && item.SendNums1 > 0 {
  651. sendNums1 := count * item.SendNums1
  652. //生成赠品
  653. product := product_model.GetProductById(item.SendProd1, true)
  654. if product != nil {
  655. delOrder = false
  656. total += sendNums1 * product.Price
  657. new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums1, order.Depart)
  658. }
  659. }
  660. if item.SendProd2 > 0 && item.SendNums2 > 0 {
  661. sendNums2 := count * item.SendNums2
  662. //赠送赠品1
  663. product := product_model.GetProductById(item.SendProd2, true)
  664. if product != nil {
  665. delOrder = false
  666. total += sendNums2 * product.Price
  667. new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums2, order.Depart)
  668. }
  669. }
  670. if item.SendProd3 > 0 && item.SendNums3 > 0 {
  671. sendNums3 := count * item.SendNums3
  672. //赠送赠品3
  673. product := product_model.GetProductById(item.SendProd3, true)
  674. if product != nil {
  675. delOrder = false
  676. total += sendNums3 * product.Price
  677. new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums3, order.Depart)
  678. }
  679. }
  680. if item.SendProd4 > 0 && item.SendNums4 > 0 {
  681. sendNums4 := count * item.SendNums4
  682. //赠送赠品4
  683. product := product_model.GetProductById(item.SendProd4, true)
  684. if product != nil {
  685. delOrder = false
  686. total += sendNums4 * product.Price
  687. new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums4, order.Depart)
  688. }
  689. }
  690. if item.SendProd5 > 0 && item.SendNums5 > 0 {
  691. sendNums5 := count * item.SendNums5
  692. //赠送赠品5
  693. product := product_model.GetProductById(item.SendProd5, true)
  694. if product != nil {
  695. delOrder = false
  696. total += sendNums5 * product.Price
  697. new(order_model.OrderDetail).Create(order.OrderId, order.Id, product.Id, product.RelateProductId, product.Price, int64(0), int64(0), product.Name, "", "", sendNums5, order.Depart)
  698. }
  699. }
  700. //赠送积分 or 代办费
  701. if item.Cash > 0 {
  702. totalCash := count * item.Cash
  703. source := balance_model.BALANCE_SOURCE_SHOP_PROMOTION
  704. remark := fmt.Sprintf("店长申请促销赠送提货券")
  705. new(balance_model.Balance).Create(shopApply.WxUId, shopApply.UserId, totalCash, source, orderId, remark)
  706. }
  707. if item.Cent > 0 {
  708. totalCent := count * item.Cent
  709. source := cent_model.PROMOTION_SEND
  710. remark := fmt.Sprintf("店长申请促销活动赠送")
  711. new(cent_model.CentBalance).Create(shopApply.WxUId, totalCent, source, orderId, remark)
  712. }
  713. }
  714. }
  715. if !delOrder {
  716. order.Status = order_model.STATUS_PROCESSING
  717. order.TotalPrice = total
  718. order.Contact = shopApply.Name
  719. order.Address = shopApply.Address
  720. order.Tel = shopApply.Mobile
  721. order.Save()
  722. } else {
  723. order.Delete()
  724. }
  725. beego.BeeLogger.Warn("----------------end generate shopapply order ---%d ", shopApply.Id)
  726. return
  727. }