DrugReportInfo.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Facades\Servers\Encrypts\AccessToken;
  7. class DrugReportInfo extends Model
  8. {
  9. protected $table = 'drug_report_info';
  10. // 是否主动维护时间戳
  11. public $timestamps = false;
  12. protected $guarded = [];
  13. /**
  14. * 获取「是否签收」文本
  15. * @return string
  16. */
  17. public function getIsSignTextAttribute(): string
  18. {
  19. $options = [
  20. 0 => '',
  21. 1 => '未签收',
  22. 2 => '已签收',
  23. ];
  24. return $options[$this->is_sign] ?? "未知「{$this->is_sign}」";
  25. }
  26. /**
  27. * 获取「是否盖章」文本
  28. * @return string
  29. */
  30. public function getIsSealTextAttribute(): string
  31. {
  32. $options = [
  33. 0 => '',
  34. 1 => '未盖章',
  35. 2 => '已盖章',
  36. ];
  37. return $options[$this->is_sign] ?? "未知「{$this->is_sign}」";
  38. }
  39. /**
  40. * 批量更新或插入
  41. * @param array $data
  42. * @param array $uniqueKeys
  43. * @return int
  44. * @throws \Throwable
  45. */
  46. public static function bulkUpsert(array $data, array $uniqueKeys = ['report_id']): int
  47. {
  48. if (empty($data)) {
  49. return 0;
  50. }
  51. return DB::transaction(function () use ($data, $uniqueKeys) {
  52. $count = 0;
  53. foreach (array_chunk($data, 100) as $chunk) {
  54. self::upsert(
  55. $chunk,
  56. $uniqueKeys, // 组合唯一键
  57. ['update_time','seal_report_url','report_name','is_seal'] // 要更新的字段
  58. );
  59. $count += count($chunk);
  60. }
  61. return $count;
  62. });
  63. }
  64. }