Bonus.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\basic;
  10. use mall\utils\BC;
  11. use think\facade\Db;
  12. class Bonus {
  13. /**
  14. * 优惠劵
  15. */
  16. public static function apply(&$data,$bonus_id=0){
  17. $bonus = Db::name("users_bonus")
  18. ->alias("u")
  19. ->field("b.*,u.id as users_bonus_id")
  20. ->join("promotion_bonus b","u.bonus_id=b.id","LEFT")
  21. ->where("u.user_id",Users::get("id"))
  22. ->where("u.status",0)
  23. ->where("u.id",$bonus_id)
  24. ->where("b.end_time > " . time())->find();
  25. if(empty($bonus)){
  26. return $data;
  27. }
  28. $price = $bonus["amount"];
  29. if($price <= 0){
  30. return $data;
  31. }
  32. if($bonus["order_amount"] > $data["real_amount"]){
  33. return $data;
  34. }
  35. $data["promotions"] = $data["promotions"] > 0 ? $data["promotions"] + $price : $price;
  36. $data["order_amount"] = $data["order_amount"] > $price ? BC::sub($data["order_amount"],$price) : 0.00;
  37. return $data;
  38. }
  39. public static function updateStatus($bonus_id,$order_id){
  40. $condition = [
  41. "id"=>$bonus_id,
  42. "user_id"=>Users::get("id")
  43. ];
  44. if(!Db::name("users_bonus")->where($condition)->count()){
  45. return false;
  46. }
  47. return Db::name("users_bonus")->where($condition)->update([
  48. "status"=>1,
  49. "order_id"=>$order_id
  50. ]);
  51. }
  52. }