GrantVoucher.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\admin\model\system;
  3. use app\api\model\Voucher;
  4. use think\Model;
  5. class GrantVoucher extends Model
  6. {
  7. // 表名
  8. protected $name = 'system_grant_voucher';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'integer';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. public static function grant_voucher($type, $user_id)
  16. {
  17. $config = [
  18. "give" => config("site.give_of_register"),
  19. "range" => explode(" - ", config("site.give_of_register_date_range")),
  20. "enum" => config("site.give_of_register_enum"),
  21. "give_switch" => config("site.give_of_register_switch"),
  22. "draw_switch" => config("site.give_of_register_draw_switch")
  23. ];
  24. if ($type == "buy") {
  25. $config = [
  26. "give" => config("site.give_of_buy"),
  27. "range" => explode(" - ", config("site.give_of_buy_date_range")),
  28. "enum" => config("site.give_of_buy_enum"),
  29. "give_switch" => config("site.give_of_buy_switch"),
  30. "draw_switch" => config("site.give_of_buy_draw_switch")
  31. ];
  32. }
  33. if ($type == "invite") {
  34. $config = [
  35. "give" => config("site.give_of_invite"),
  36. "range" => explode(" - ", config("site.give_of_invite_date_range")),
  37. "enum" => config("site.give_of_invite_enum"),
  38. "give_switch" => config("site.give_of_invite_switch"),
  39. "draw_switch" => config("site.give_of_invite_draw_switch")
  40. ];
  41. }
  42. if (1 == $config["give_switch"]) {
  43. if (strtotime($config["range"][0]) <= time() && strtotime($config["range"][1]) >= time()) {
  44. if (strlen($config["give"]) > 0) {
  45. if (1 == $config["draw_switch"]) { // 需要用户领取
  46. return self::create([
  47. "to_user_id" => $user_id,
  48. "voucher_ids" => $config["give"],
  49. "is_read" => 0,
  50. "take_effect" => $config['enum'],
  51. "draw" => 0,
  52. "createtime" => time(),
  53. "updatetime" => time(),
  54. ]);
  55. } else { // 直接发放到用户
  56. $vouchers = (new Voucher())->where("id", "in", explode(",", $config["give"]))->order("takeAmount", "ASC")->select();
  57. $take_effect_time = Voucher::getTakeEffectTime($config['enum']);
  58. $user_vouchers = [];
  59. foreach ($vouchers as $voucher) {
  60. array_push($user_vouchers, [
  61. "user_id" => $user_id,
  62. "voucher_id" => $voucher["id"],
  63. "takeAmount" => $voucher["takeAmount"],
  64. "fullAmount" => $voucher["fullAmount"],
  65. "status" => \E_BASE_STATUS::Normal,
  66. "take_effect_time" => $take_effect_time,
  67. "expiretime" => $take_effect_time + (24 * 60 * 60) * $voucher["indate_day"],
  68. "createtime" => time(),
  69. "updatetime" => time()
  70. ]);
  71. }
  72. return (new \app\api\model\user\Voucher())->insertAll($user_vouchers);
  73. }
  74. }
  75. }
  76. }
  77. return false;
  78. }
  79. }