* Date: 2019/3/27 21:44 */ namespace app\models\game; use app\admin\model\system\SystemConfig; use app\models\store\StoreOrder; use app\models\user\User; use app\models\user\UserBill; use crmeb\traits\ModelTrait; use crmeb\basic\BaseModel; /** * TODO 用户消费新增金额明细 model * Class UserBill * @package app\models\user */ class DayAward extends BaseModel { /** * 数据表主键 * @var string */ protected $pk = 'id'; /** * 模型名称 * @var string */ protected $name = 'day_award'; use ModelTrait; public static function getAward($uid, $id) { $info = self::get($id); if (!$info || $info['uid'] != $uid) return self::setErrorInfo('领取失败'); self::beginTrans(); try { $get = bcmul($info['award'], 0.9, 2); $left = bcsub($info['award'], $get, 2); $res = self::where('id', $id)->update(['status' => 1]) && User::bcInc($uid, 'brokerage_price', $get, 'uid') && UserBill::income('天天领红包', $uid, 'now_money', 'brokerage', $get, $id, User::where('uid', $uid)->value('brokerage_price'), '今日领红包' . $info['award'] . '扣除公益池实际到账' . $get) && StoreOrder::addPublicLake($left, $id, 'award'); if ($res) { self::commitTrans(); return true; } else { self::rollbackTrans(); return self::setErrorInfo('领取失败'); } } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo(); } } public static function dayAward() { if (self::whereTime('add_time', 'today')->find()) { return true; } if (!Lottery::whereTime('add_time', 'yesterday')->where('status', 1)->find()) { return true; } $all_point = Lottery::where('status', 1)->whereTime('open_time', 'yesterday')->sum('ticket'); $award_lake = sys_config('award_lake', 0, true); $single_award = bcdiv($award_lake, bcmul($all_point, 2), 2); if ($single_award < 0) { return true; } $list = Lottery::whereTime('add_time', 'yesterday')->where('status', 1) ->group('uid')->field('uid,SUM(ticket) as sum_ticket')->select(); self::beginTrans(); try { $res = true; foreach ($list as $v) { $award = bcmul($single_award, $v['sum_ticket'], 2); $res1 = self::create([ 'uid' => $v['uid'], 'point' => $v['sum_ticket'], 'add_time' => time(), 'award' => $award ]); $res = $res && $res1 && self::expendAwardLake($award, $res1->id); } if ($res) { self::commitTrans(); return true; } else { self::rollbackTrans(); return self::setErrorInfo('发放失败'); } } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo(); } } public static function dayAwardOut() { $all_award = self::whereTime('add_time', 'yesterday')->where('status', 0)->sum('award'); if ($all_award <= 0) { return true; } self::beginTrans(); try { $res = self::whereTime('add_time', 'yesterday')->where('status', 0)->update(['status' => 2]) && StoreOrder::addPublicLake($all_award, 0, 'award_out'); if ($res) { self::commitTrans(); return true; } else { self::rollbackTrans(); return self::setErrorInfo('过期失败'); } } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo(); } } public static function expendAwardLake($num, $link_id, $type = 'award') { $old = sys_config('award_lake', 0, true); $res = SystemConfig::where('menu_name', 'award_lake')->update(['value' => (float)$old - $num]); $str = ''; switch ($type) { case 'award': $str = '每日奖金'; break; } return $res && UserBill::expend('奖池支出', 0, 'award_lake', 'expend', $num, $link_id, (float)$old - $num, $str ?: '奖池支出'); } }