DrugReportInfo.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Models;
  3. use App\Traits\Filterable;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * App\Models\DrugReportInfo
  11. *
  12. * @property int $id
  13. * @property string $drug_report_v2_id 药检报告ID(来源于querysealdrugreport)
  14. * @property string $drug_report_id 报告ID(来源于querydrugreport)
  15. * @property string $report_id 报告ID(来源于querysealdrugreport)
  16. * @property string $report_name 报告名称
  17. * @property string $report_no 报告编号
  18. * @property string $report_date 报告日期
  19. * @property string $batch_no 批次号
  20. * @property string $drug_id 药品ID
  21. * @property string $drug_name 药品名称
  22. * @property string $prod_code 药品子类编码
  23. * @property string $pkg_spec 包装规格
  24. * @property string $prepn_spec 制剂规格
  25. * @property string $prepn_type_desc 剂型
  26. * @property array|null $pkg_ratio_list 包装比例
  27. * @property int $bill_type 单据类型
  28. * @property string $bill_id 单据ID
  29. * @property string $bill_detail_id 单据明细ID
  30. * @property string $bill_time 单据时间
  31. * @property string $bill_code 单据编码
  32. * @property string $produce_date 生产日期
  33. * @property string $produce_ent_id 生产企业ID
  34. * @property string $produce_ent_name 生产企业
  35. * @property string $ass_ref_ent_id 委托企业ID
  36. * @property string $from_ref_ent_id 发货企业ID
  37. * @property string $from_ent_name 发货企业
  38. * @property string $sealed_report_url 盖章报告URL
  39. * @property array|null $raw_data 待签收原始数据(来源于querydrugreport)
  40. * @property array|null $seal_raw_data 已签收原始数据(来源于querysealdrugreport)
  41. * @property int $is_sign 是否签收(1-未签收、2-已签收)
  42. * @property int $is_seal 是否盖章(1-未盖章、2-已盖章)
  43. * @property Carbon $create_time 创建时间
  44. * @property Carbon $update_time 更新时间
  45. * @property-read string $is_seal_text
  46. * @property-read string $is_sign_text
  47. * @method static Builder|DrugReportInfo applyFilters(array $filters = [], array $options = [])
  48. * @method static Builder|DrugReportInfo newModelQuery()
  49. * @method static Builder|DrugReportInfo newQuery()
  50. * @method static Builder|DrugReportInfo query()
  51. * @method static Builder|DrugReportInfo whereAssRefEntId($value)
  52. * @method static Builder|DrugReportInfo whereBatchNo($value)
  53. * @method static Builder|DrugReportInfo whereBillCode($value)
  54. * @method static Builder|DrugReportInfo whereBillDetailId($value)
  55. * @method static Builder|DrugReportInfo whereBillId($value)
  56. * @method static Builder|DrugReportInfo whereBillTime($value)
  57. * @method static Builder|DrugReportInfo whereBillType($value)
  58. * @method static Builder|DrugReportInfo whereCreateTime($value)
  59. * @method static Builder|DrugReportInfo whereDrugId($value)
  60. * @method static Builder|DrugReportInfo whereDrugName($value)
  61. * @method static Builder|DrugReportInfo whereDrugReportId($value)
  62. * @method static Builder|DrugReportInfo whereDrugReportV2Id($value)
  63. * @method static Builder|DrugReportInfo whereFromEntName($value)
  64. * @method static Builder|DrugReportInfo whereFromRefEntId($value)
  65. * @method static Builder|DrugReportInfo whereId($value)
  66. * @method static Builder|DrugReportInfo whereIsSeal($value)
  67. * @method static Builder|DrugReportInfo whereIsSign($value)
  68. * @method static Builder|DrugReportInfo wherePkgRatioList($value)
  69. * @method static Builder|DrugReportInfo wherePkgSpec($value)
  70. * @method static Builder|DrugReportInfo wherePrepnSpec($value)
  71. * @method static Builder|DrugReportInfo wherePrepnTypeDesc($value)
  72. * @method static Builder|DrugReportInfo whereProdCode($value)
  73. * @method static Builder|DrugReportInfo whereProduceDate($value)
  74. * @method static Builder|DrugReportInfo whereProduceEntId($value)
  75. * @method static Builder|DrugReportInfo whereProduceEntName($value)
  76. * @method static Builder|DrugReportInfo whereRawData($value)
  77. * @method static Builder|DrugReportInfo whereReportDate($value)
  78. * @method static Builder|DrugReportInfo whereReportId($value)
  79. * @method static Builder|DrugReportInfo whereReportName($value)
  80. * @method static Builder|DrugReportInfo whereReportNo($value)
  81. * @method static Builder|DrugReportInfo whereSealRawData($value)
  82. * @method static Builder|DrugReportInfo whereSealedReportUrl($value)
  83. * @method static Builder|DrugReportInfo whereUpdateTime($value)
  84. * @mixin \Eloquent
  85. */
  86. class DrugReportInfo extends Model
  87. {
  88. use HasFactory, Filterable;
  89. protected $table = 'drug_report_info';
  90. protected $guarded = [];
  91. const CREATED_AT = 'create_time';
  92. const UPDATED_AT = 'update_time';
  93. protected $casts = [
  94. 'pkg_ratio_list' => 'array',
  95. 'raw_data' => 'array',
  96. 'seal_raw_data' => 'array',
  97. ];
  98. protected $hidden = [
  99. 'create_time',
  100. 'update_time',
  101. 'drug_report_v2_id',
  102. 'drug_report_id',
  103. 'pkg_ratio_list',
  104. 'raw_data',
  105. 'seal_raw_data',
  106. ];
  107. /**
  108. * @var array|string[] 定义可搜索字段(可选,可在调用时覆盖)
  109. */
  110. public static $searchable = ['drug_id', 'drug_name', 'produce_ent_id', 'produce_ent_name'];
  111. /**
  112. * 获取「是否签收」文本
  113. * @return string
  114. */
  115. public function getIsSignTextAttribute(): string
  116. {
  117. $options = [
  118. 0 => '',
  119. 1 => '未签收',
  120. 2 => '已签收',
  121. ];
  122. return $options[$this->is_sign] ?? "未知「{$this->is_sign}」";
  123. }
  124. /**
  125. * 获取「是否盖章」文本
  126. * @return string
  127. */
  128. public function getIsSealTextAttribute(): string
  129. {
  130. $options = [
  131. 0 => '',
  132. 1 => '未盖章',
  133. 2 => '已盖章',
  134. ];
  135. return $options[$this->is_sign] ?? "未知「{$this->is_sign}」";
  136. }
  137. /**
  138. * 批量更新或插入
  139. * @param array $data
  140. * @param array $uniqueKeys
  141. * @return int
  142. * @throws \Throwable
  143. */
  144. public static function bulkUpsert(array $data, array $uniqueKeys = ['batch_no', 'drug_id']): int
  145. {
  146. if (empty($data)) {
  147. return 0;
  148. }
  149. return DB::transaction(function () use ($data, $uniqueKeys) {
  150. $count = 0;
  151. foreach (array_chunk($data, 100) as $chunk) {
  152. self::upsert(
  153. $chunk,
  154. $uniqueKeys, // 组合唯一键
  155. array_keys($data[0]) // 要更新的字段
  156. );
  157. $count += count($chunk);
  158. }
  159. return $count;
  160. });
  161. }
  162. }