CreditService.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sdf_sky
  5. * Date: 2017/4/24
  6. * Time: 下午5:20
  7. */
  8. namespace App\Services;
  9. use App\Models\Credit;
  10. use App\Models\UserData;
  11. use Carbon\Carbon;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. class CreditService
  15. {
  16. public static function create($user_id,$action,$coins = 0,$credits = 0,$source_id = 0 ,$source_subject = null,$through=false){
  17. DB::beginTransaction();
  18. try{
  19. /*用户登陆只添加一次积分*/
  20. if($action == 'login' && Credit::where('user_id','=',$user_id)->where('action','=',$action)->where('created_at','>',Carbon::today())->count()>0){
  21. return false;
  22. }
  23. /*记录详情数据*/
  24. Credit::create([
  25. 'user_id' => $user_id,
  26. 'action' => $action,
  27. 'source_id' => $source_id,
  28. 'source_subject' => $source_subject,
  29. 'coins' => $coins,
  30. 'credits' => $credits,
  31. 'created_at' => Carbon::now()
  32. ]);
  33. if(!$through){
  34. /*修改用户账户信息*/
  35. UserData::find($user_id)->increment('coins',$coins);
  36. UserData::find($user_id)->increment('credits',$credits);
  37. }
  38. DB::commit();
  39. return true;
  40. }catch (\Exception $e) {
  41. Log::error("credit_create_error:".$e->getMessage());
  42. DB::rollBack();
  43. return false;
  44. }
  45. }
  46. }