PaidRewardStatisticsService.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Database\QueryException;
  4. use Illuminate\Support\Facades\DB;
  5. class PaidRewardStatisticsService
  6. {
  7. /**
  8. * @param $DateID
  9. * @param $type
  10. * @param mixed $amount 单位:分
  11. * @return mixed
  12. */
  13. public function incrementRecordByDateIDAndType($DateID, $type, $amount)
  14. {
  15. $this->createIfNotExist($DateID, $type);
  16. return DB::table('QPRecordDB.dbo.RecordPaidRewardDailyStatistics')
  17. ->where(['DateID' => $DateID, 'StatType' => $type])
  18. ->increment('TotalAmount', $amount);
  19. }
  20. private function createIfNotExist($DateID, $type)
  21. {
  22. $exist = DB::table('QPRecordDB.dbo.RecordPaidRewardDailyStatistics')
  23. ->where(['DateID' => $DateID, 'StatType' => $type])->first();
  24. if (!$exist) {
  25. try {
  26. DB::table('QPRecordDB.dbo.RecordPaidRewardDailyStatistics')->insert([
  27. 'DateID' => $DateID,
  28. 'StatType' => $type,
  29. ]);
  30. } catch (QueryException $e) {
  31. if (stripos($e->getMessage(), 'duplicate key') === false) {
  32. throw $e;
  33. }
  34. }
  35. }
  36. return true;
  37. }
  38. }