DrugReportInfo.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_id 报告ID(来源于querydrugreport)
  14. * @property string $report_id 报告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 $sealed_report_url 盖章报告URL
  38. * @property string $report_sign_status 报告签收状态(0-待发送、2-待签收、3-已签收、4-已拒绝、7-对方已签收/更正待处理、13-对方已拒绝/更正待签收)
  39. * @property array|null $raw_data 报告待签收原始数据(来源querydrugreport)
  40. * @property array|null $seal_raw_data 报告已签收原始数据(来源querysealdrugreport)
  41. * @property array|null $opt_raw_data 报告操作原始数据(来源drugreport.opt.history)
  42. * @property int $is_sign 是否签收(1-未签收、2-已签收)
  43. * @property int $is_seal 是否盖章(1-未盖章、2-已盖章)
  44. * @property Carbon $create_time 创建时间
  45. * @property Carbon $update_time 更新时间
  46. * @property-read string $is_seal_text
  47. * @property-read string $is_sign_text
  48. * @method static Builder|DrugReportInfo applyFilters(array $filters = [], array $options = [])
  49. * @method static Builder|DrugReportInfo newModelQuery()
  50. * @method static Builder|DrugReportInfo newQuery()
  51. * @method static Builder|DrugReportInfo query()
  52. * @method static Builder|DrugReportInfo whereAssRefEntId($value)
  53. * @method static Builder|DrugReportInfo whereBatchNo($value)
  54. * @method static Builder|DrugReportInfo whereBillCode($value)
  55. * @method static Builder|DrugReportInfo whereBillDetailId($value)
  56. * @method static Builder|DrugReportInfo whereBillId($value)
  57. * @method static Builder|DrugReportInfo whereBillTime($value)
  58. * @method static Builder|DrugReportInfo whereBillType($value)
  59. * @method static Builder|DrugReportInfo whereCreateTime($value)
  60. * @method static Builder|DrugReportInfo whereDrugId($value)
  61. * @method static Builder|DrugReportInfo whereDrugName($value)
  62. * @method static Builder|DrugReportInfo whereDrugReportId($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 whereSealRawData($value)
  84. * @method static Builder|DrugReportInfo whereSealedReportUrl($value)
  85. * @method static Builder|DrugReportInfo whereUpdateTime($value)
  86. * @mixin \Eloquent
  87. */
  88. class DrugReportInfo extends Model
  89. {
  90. use HasFactory, Filterable;
  91. protected $table = 'drug_report_info';
  92. protected $guarded = [];
  93. const CREATED_AT = 'create_time';
  94. const UPDATED_AT = 'update_time';
  95. protected $casts = [
  96. 'pkg_ratio_list' => 'array',
  97. 'raw_data' => 'array',
  98. 'seal_raw_data' => 'array',
  99. 'opt_raw_data' => 'array',
  100. ];
  101. protected $hidden = [
  102. 'create_time',
  103. 'update_time',
  104. 'drug_report_id',
  105. 'pkg_ratio_list',
  106. 'raw_data',
  107. 'seal_raw_data',
  108. 'opt_raw_data',
  109. ];
  110. /**
  111. * @var array|string[] 定义可搜索字段(可选,可在调用时覆盖)
  112. */
  113. public static $searchable = ['drug_id', 'drug_name', 'produce_ent_id', 'produce_ent_name'];
  114. /**
  115. * 获取「是否签收」文本
  116. * @return string
  117. */
  118. public function getIsSignTextAttribute(): string
  119. {
  120. $options = [
  121. 0 => '',
  122. 1 => '未签收',
  123. 2 => '已签收',
  124. ];
  125. return $options[$this->is_sign] ?? "未知「{$this->is_sign}」";
  126. }
  127. /**
  128. * 获取「是否盖章」文本
  129. * @return string
  130. */
  131. public function getIsSealTextAttribute(): string
  132. {
  133. $options = [
  134. 0 => '',
  135. 1 => '未盖章',
  136. 2 => '已盖章',
  137. ];
  138. return $options[$this->is_sign] ?? "未知「{$this->is_sign}」";
  139. }
  140. /**
  141. * 批量更新或插入
  142. * @param array $data
  143. * @param array $uniqueKeys
  144. * @return int
  145. * @throws \Throwable
  146. */
  147. public static function bulkUpsert(array $data, array $uniqueKeys = ['batch_no', 'drug_id']): int
  148. {
  149. if (empty($data)) {
  150. return 0;
  151. }
  152. return DB::transaction(function () use ($data, $uniqueKeys) {
  153. $count = 0;
  154. foreach (array_chunk($data, 100) as $chunk) {
  155. self::upsert(
  156. $chunk,
  157. $uniqueKeys, // 组合唯一键
  158. array_keys($data[0]) // 要更新的字段
  159. );
  160. $count += count($chunk);
  161. }
  162. return $count;
  163. });
  164. }
  165. }