DrugReportInfo.php 6.9 KB

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