| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace app\admin\model\system;
- use app\api\model\Voucher;
- use think\Model;
- class GrantVoucher extends Model
- {
- // 表名
- protected $name = 'system_grant_voucher';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'integer';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
- public static function grant_voucher($type, $user_id)
- {
- $config = [
- "give" => config("site.give_of_register"),
- "range" => explode(" - ", config("site.give_of_register_date_range")),
- "enum" => config("site.give_of_register_enum"),
- "give_switch" => config("site.give_of_register_switch"),
- "draw_switch" => config("site.give_of_register_draw_switch")
- ];
- if ($type == "buy") {
- $config = [
- "give" => config("site.give_of_buy"),
- "range" => explode(" - ", config("site.give_of_buy_date_range")),
- "enum" => config("site.give_of_buy_enum"),
- "give_switch" => config("site.give_of_buy_switch"),
- "draw_switch" => config("site.give_of_buy_draw_switch")
- ];
- }
- if ($type == "invite") {
- $config = [
- "give" => config("site.give_of_invite"),
- "range" => explode(" - ", config("site.give_of_invite_date_range")),
- "enum" => config("site.give_of_invite_enum"),
- "give_switch" => config("site.give_of_invite_switch"),
- "draw_switch" => config("site.give_of_invite_draw_switch")
- ];
- }
- if (1 == $config["give_switch"]) {
- if (strtotime($config["range"][0]) <= time() && strtotime($config["range"][1]) >= time()) {
- if (strlen($config["give"]) > 0) {
- if (1 == $config["draw_switch"]) { // 需要用户领取
- return self::create([
- "to_user_id" => $user_id,
- "voucher_ids" => $config["give"],
- "is_read" => 0,
- "take_effect" => $config['enum'],
- "draw" => 0,
- "createtime" => time(),
- "updatetime" => time(),
- ]);
- } else { // 直接发放到用户
- $vouchers = (new Voucher())->where("id", "in", explode(",", $config["give"]))->order("takeAmount", "ASC")->select();
- $take_effect_time = Voucher::getTakeEffectTime($config['enum']);
- $user_vouchers = [];
- foreach ($vouchers as $voucher) {
- array_push($user_vouchers, [
- "user_id" => $user_id,
- "voucher_id" => $voucher["id"],
- "takeAmount" => $voucher["takeAmount"],
- "fullAmount" => $voucher["fullAmount"],
- "status" => \E_BASE_STATUS::Normal,
- "take_effect_time" => $take_effect_time,
- "expiretime" => $take_effect_time + (24 * 60 * 60) * $voucher["indate_day"],
- "createtime" => time(),
- "updatetime" => time()
- ]);
- }
- return (new \app\api\model\user\Voucher())->insertAll($user_vouchers);
- }
- }
- }
- }
- return false;
- }
- }
|