Sfoglia il codice sorgente

wesmiler 报恩寺项目提交

wesmiler 4 anni fa
parent
commit
d8f72f1f7f
2 ha cambiato i file con 174 aggiunte e 41 eliminazioni
  1. 0 41
      app/Models/MontionModel.php
  2. 174 0
      app/Services/MotionService.php

+ 0 - 41
app/Models/MontionModel.php

@@ -1,41 +0,0 @@
-<?php
-// +----------------------------------------------------------------------
-// | Laravel框架 [ Laravel ]
-// +----------------------------------------------------------------------
-// | 版权所有 2017~2021 Laravel研发中心
-// +----------------------------------------------------------------------
-// | 官方网站: http://www.laravel.cn
-// +----------------------------------------------------------------------
-// | Author: wesmiler <12345678@qq.com>
-// +----------------------------------------------------------------------
-
-namespace App\Models;
-
-/**
- * 念佛记录管理-模型
- * @author wesmiler
- * @since 2020/11/11
- * Class MontionModel
- * @package App\Models
- */
-class MontionModel extends BaseModel
-{
-    // 设置数据表
-    protected $table = 'montions';
-
-    /**
-     * 获取记录信息
-     * @param int $id 记录ID
-     * @return array|string
-     * @author wesmiler
-     * @since 2020/11/11
-     */
-    public function getInfo($id)
-    {
-        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
-        if ($info) {
-
-        }
-        return $info;
-    }
-}

+ 174 - 0
app/Services/MotionService.php

@@ -0,0 +1,174 @@
+<?php
+// +----------------------------------------------------------------------
+// | Laravel框架 [ Laravel ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 Laravel研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: wesmiler <12345678@qq.com>
+// +----------------------------------------------------------------------
+
+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','days'])
+            ->where('create_time','>=', strtotime(date('Y-m-d'))-86400)
+            ->orderBy('create_time','desc')
+            ->orderBy('days','desc')
+            ->first();
+        if($checkInfo){
+            $dateTime = strtotime(date('Y-m-d', $checkInfo->create_time));
+            if($dateTime < 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);
+            }
+
+            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(\DB::raw('count(id) as total'))
+                ->orderBy('create_time', 'asc')
+                ->select([\DB::raw('count(id) as total'),'days','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(['id'=> $userId])
+                ->select([\DB::raw('count(id) as total'),'id','rank'])
+                ->first();
+            if($ranks){
+                $counts['ranks'] = $ranks;
+            }
+        }
+
+
+        return message(MESSAGE_OK, true, $counts);
+    }
+}