MotionService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\MemberModel;
  13. use App\Models\MotionModel;
  14. use App\Models\TradeModel;
  15. /**
  16. * 念佛记录-服务类
  17. * @author wesmiler
  18. * @since 2020/11/11
  19. * Class MotionService
  20. * @package App\Services
  21. */
  22. class MotionService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author wesmiler
  27. * @since 2020/11/11
  28. * MotionService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new MotionModel();
  33. }
  34. /**
  35. * 保存记录
  36. * @param $userId
  37. * @return array
  38. */
  39. public function save($userId){
  40. $params = request()->all();
  41. $foId = isset($params['fo_id'])? intval($params['fo_id']) : 0;
  42. if(empty($foId)){
  43. return message('佛像参数错误', false);
  44. }
  45. $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1, 'status' => 1])
  46. ->select(['id', 'nickname', 'merits_num', 'coupon'])
  47. ->first();
  48. if (!$memberInfo) {
  49. return message('您的账号不可操作或已冻结,请联系客服', false);
  50. }
  51. $days = 1;
  52. $checkInfo = $this->model::where(['user_id'=> $userId,'status'=> 1,'mark'=> 1])
  53. ->select(['id','user_id','fo_id','create_time','days'])
  54. ->where('create_time','>=', strtotime(date('Y-m-d'))-86400)
  55. ->orderBy('create_time','desc')
  56. ->orderBy('days','desc')
  57. ->first();
  58. if($checkInfo){
  59. $createTime = strtotime(datetime($checkInfo->create_time, 'Y-m-d H:i:s'));
  60. if($createTime < strtotime(date('Y-m-d'))){
  61. $days = $checkInfo->days+1;
  62. }else{
  63. $days = $checkInfo->days;
  64. }
  65. }
  66. \DB::beginTransaction();
  67. $data = [
  68. 'user_id' => $userId,
  69. 'fo_id' => $foId,
  70. 'days' => $days,
  71. 'remark' => isset($params['remark']) ? $params['remark'] : '',
  72. 'create_time' => time(),
  73. 'update_time' => time(),
  74. 'status' => 1
  75. ];
  76. if (!$this->model::insertGetId($data)) {
  77. \DB::rollBack();
  78. return message('念佛处理失败', false);
  79. }
  80. // 奖励
  81. $giveGd = ConfigService::make()->getConfigByCode('nf_give_gd');
  82. $giveGd = $giveGd ? $giveGd : 0;
  83. if ($giveGd > 0) {
  84. if (!MemberModel::where(['id' => $userId, 'mark' => 1])->increment('merits_num', $giveGd)) {
  85. \DB::rollBack();
  86. return message("更新功德账户失败", false);
  87. }
  88. $data = [
  89. 'user_id' => $userId,
  90. 'source_uid' => 0,
  91. 'type' => 3,
  92. 'coin_type' => 4,
  93. 'pay_type' => 4,
  94. 'money' => $giveGd,
  95. 'change_type' => 1,
  96. 'balance' => $memberInfo->merits_num,
  97. 'create_time' => time(),
  98. 'remark' => "祈福念佛",
  99. 'status' => 1,
  100. ];
  101. if (!TradeModel::insertGetId($data)) {
  102. \DB::rollBack();
  103. return message("处理功德奖励失败", false);
  104. }
  105. \DB::commit();
  106. return message("功德值+{$giveGd}", true);
  107. }
  108. \DB::commit();
  109. // 完成修行项目
  110. PracticesService::make()->saveLog($userId, 10, '祈福念佛');
  111. return message("祈福念佛", true);
  112. }
  113. /**
  114. * 统计
  115. * @param $userId
  116. * @return array
  117. *
  118. */
  119. public function counts($userId){
  120. $today = $this->model::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1])
  121. ->where('create_time','>=', strtotime(date('Y-m-d')))
  122. ->count('id');
  123. $total = $this->model::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1])
  124. ->count('id');
  125. $days = $this->model::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1])
  126. ->where('create_time','>=', strtotime(date('Y-m-d')))
  127. ->orderBy('create_time','desc')
  128. ->value('days');
  129. $counts = [
  130. 'today'=> intval($today),
  131. 'total'=> intval($total),
  132. 'days'=> intval($days),
  133. 'ranks'=> ['rank'=> 0],
  134. ];
  135. if($total>0){
  136. $model = $this->model::from(\DB::raw(env('DB_PREFIX').'motions as m,(select (@rank:=0)) as rank'))
  137. ->where(['mark'=> 1,'status'=> 1])
  138. ->groupBy('user_id')
  139. ->orderBy(\DB::raw('count(id) desc'))
  140. ->select([\DB::raw('count(id) as total'),'days','user_id',\DB::raw('(@rank:=@rank+1) as rank')]);
  141. $binds = $model->getBindings();
  142. $sql = str_replace('?', '%s', $model->toSql());
  143. $sql = sprintf($sql, ...$binds);
  144. $ranks = $this->model::from(\DB::raw("({$sql}) as a"))
  145. ->where(['user_id'=> $userId])
  146. ->select(['total','user_id','rank'])
  147. ->first();
  148. if($ranks){
  149. $ranks['rank'] = $ranks['rank']? $ranks['rank'] : 0;
  150. $counts['ranks'] = $ranks;
  151. }
  152. }
  153. return message(MESSAGE_OK, true, $counts);
  154. }
  155. }