// +---------------------------------------------------------------------- namespace App\Services; use App\Models\MemberModel; use App\Models\MotionModel; use App\Models\TradeModel; /** * 念佛记录-服务类 * @author wesmiler * @since 2020/11/11 * Class MotionService * @package App\Services */ class MotionService extends BaseService { /** * 构造函数 * @author wesmiler * @since 2020/11/11 * MotionService constructor. */ public function __construct() { $this->model = new MotionModel(); } /** * 保存记录 * @param $userId * @return array */ public function save($userId){ $params = request()->all(); $foId = isset($params['fo_id'])? intval($params['fo_id']) : 0; if(empty($foId)){ return message('佛像参数错误', false); } $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1, 'status' => 1]) ->select(['id', 'nickname', 'merits_num', 'coupon']) ->first(); if (!$memberInfo) { return message('您的账号不可操作或已冻结,请联系客服', false); } $days = 1; $checkInfo = $this->model::where(['user_id'=> $userId,'status'=> 1,'mark'=> 1]) ->select(['id','user_id','fo_id','create_time','days']) ->where('create_time','>=', strtotime(date('Y-m-d'))-86400) ->orderBy('create_time','desc') ->orderBy('days','desc') ->first(); if($checkInfo){ $createTime = strtotime(datetime($checkInfo->create_time, 'Y-m-d H:i:s')); if($createTime < strtotime(date('Y-m-d'))){ $days = $checkInfo->days+1; }else{ $days = $checkInfo->days; } } \DB::beginTransaction(); $data = [ 'user_id' => $userId, 'fo_id' => $foId, 'days' => $days, 'remark' => isset($params['remark']) ? $params['remark'] : '', 'create_time' => time(), 'update_time' => time(), 'status' => 1 ]; if (!$this->model::insertGetId($data)) { \DB::rollBack(); return message('念佛处理失败', false); } // 奖励 $giveGd = ConfigService::make()->getConfigByCode('nf_give_gd'); $giveGd = $giveGd ? $giveGd : 0; if ($giveGd > 0) { if (!MemberModel::where(['id' => $userId, 'mark' => 1])->increment('merits_num', $giveGd)) { \DB::rollBack(); return message("更新功德账户失败", false); } $data = [ 'user_id' => $userId, 'source_uid' => 0, 'type' => 3, 'coin_type' => 4, 'pay_type' => 4, 'money' => $giveGd, 'change_type' => 1, 'balance' => $memberInfo->merits_num, 'create_time' => time(), 'remark' => "祈福念佛", 'status' => 1, ]; if (!TradeModel::insertGetId($data)) { \DB::rollBack(); return message("处理功德奖励失败", false); } \DB::commit(); return message("功德值+{$giveGd}", true); } \DB::commit(); // 完成修行项目 PracticesService::make()->saveLog($userId, 10, '祈福念佛'); return message("祈福念佛", true); } /** * 统计 * @param $userId * @return array * */ public function counts($userId){ $today = $this->model::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1]) ->where('create_time','>=', strtotime(date('Y-m-d'))) ->count('id'); $total = $this->model::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1]) ->count('id'); $days = $this->model::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1]) ->where('create_time','>=', strtotime(date('Y-m-d'))) ->orderBy('create_time','desc') ->value('days'); $counts = [ 'today'=> intval($today), 'total'=> intval($total), 'days'=> intval($days), 'ranks'=> ['rank'=> 0], ]; if($total>0){ $model = $this->model::from(\DB::raw(env('DB_PREFIX').'motions as m,(select (@rank:=0)) as rank')) ->where(['mark'=> 1,'status'=> 1]) ->groupBy('user_id') ->orderBy('total','desc') ->select([\DB::raw('count(id) as total'),'days','user_id',\DB::raw('(@rank:=@rank+1) as rank')]); $binds = $model->getBindings(); $sql = str_replace('?', '%s', $model->toSql()); $sql = sprintf($sql, ...$binds); $ranks = $this->model::from(\DB::raw("({$sql}) as a")) ->where(['user_id'=> $userId]) ->select(['total','user_id','rank']) ->first(); if($ranks){ $ranks['rank'] = $ranks['rank']? $ranks['rank'] : 0; $counts['ranks'] = $ranks; } } return message(MESSAGE_OK, true, $counts); } }