Bonus.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 app\api\service;
  10. use think\facade\Config;
  11. use mall\basic\Users;
  12. use app\common\exception\BaseException;
  13. use app\common\models\promotion\PromotionBonus as BonusModel;
  14. use app\common\models\users\UsersBonus as UsersBonusModel;
  15. class Bonus extends Service {
  16. /**
  17. * 获取列表数据
  18. * @param $data
  19. * @return array
  20. * @throws BaseException
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\DbException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public static function getList($data){
  26. $condition = [
  27. ["type","=",0],
  28. ["status","=",0],
  29. ["end_time",">",time()]
  30. ];
  31. $size = Config::get("website.pageSize");
  32. $page = $data["page"]??1;
  33. $count = BonusModel::where($condition)->count();
  34. $result = BonusModel::where($condition)->order("id","desc")->page($page,$size)->select()->toArray();
  35. $data = [];
  36. foreach($result as $key=>$value){
  37. $data[$key] = [
  38. "id"=>$value["id"],
  39. "name"=>$value["name"],
  40. "amount"=>number_format($value["amount"]),
  41. "price"=>$value["order_amount"],
  42. "end_time"=>date('Y-m-d',strtotime($value["end_time"]))
  43. ];
  44. if($value["giveout"] != 0 && ($value["used"] >= $value["giveout"])){
  45. $data[$key]['is_receive'] = 2;
  46. }else{
  47. $data[$key]['is_receive'] = UsersBonusModel::where("bonus_id",$value["id"])->where("user_id",Users::get("id"))->count();
  48. }
  49. }
  50. $array = [ "list"=>$data??[], "page"=>$page, "total"=>0, "size"=>$size ];
  51. $total = ceil($count / $size);
  52. $array["total"] = $total;
  53. if($total == $page -1){
  54. throw new BaseException("没有数据了哦!",-1,$array);
  55. }
  56. return $array;
  57. }
  58. /**
  59. * 领劵
  60. * @param $id
  61. * @return int
  62. * @throws BaseException
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public static function receive($id){
  68. if(!$row=BonusModel::where([
  69. ["id","=",$id],
  70. ["type","=",0],
  71. ["status","=",0],
  72. ["end_time",">",time()],
  73. ])->find()){
  74. throw new BaseException("优惠劵已过期",0,2);
  75. }
  76. if($row["giveout"] != 0 && ($row["used"] >= $row["giveout"])){
  77. throw new BaseException("优惠劵已领完",0,1);
  78. }
  79. if(UsersBonusModel::where(["user_id"=>Users::get("id"),"bonus_id"=>$id])->count()){
  80. throw new BaseException("该优惠劵您已领取过了",0,1);
  81. }
  82. UsersBonusModel::create([ "user_id"=>Users::get("id"), "type"=>$row["type"], "bonus_id"=>$id, "create_time"=>time() ]);
  83. BonusModel::where("id",$id)->inc("used")->update();
  84. return 1;
  85. }
  86. }