| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Carbon;
- use App\Models\DrugReportInfo;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- class DrugReportAss extends Model
- {
- use HasFactory;
- protected $table = 'drug_report_ass';
- // 是否主动维护时间戳
- public $timestamps = false;
- protected $guarded = [];
- public function drug_report_info(): HasMany
- {
- return $this->hasMany(DrugReportInfo::class, 'drug_id', 'drug_id')
- ->where('drug_report_info.batch_no', 'drug_report_ass.batch_no');
- }
- /**
- * 批量更新或插入
- * @param array $data
- * @param array $uniqueKeys
- * @return int
- * @throws \Throwable
- */
- public static function bulkUpsert(array $data, array $uniqueKeys = ['batch_no', 'drug_id','bill_id','bill_detail_id']): int
- {
- if (empty($data)) {
- return 0;
- }
- return DB::transaction(function () use ($data, $uniqueKeys) {
- $count = 0;
- foreach (array_chunk($data, 100) as $chunk) {
- try{
- $res = DrugReportAss::query()->upsert(
- $chunk,
- $uniqueKeys, // 组合唯一键
- ['update_time'] // 要更新的字段
- );
- if (!$res){
- log::info('increment_drug_upsert:更新失败:'.$res);
- }
- } catch (\Exception $e) {
- log::info('increment_drug_upsert:'.$e->getMessage());
- return $count;
- }
- $count += count($chunk);
- }
- return $count;
- });
- }
- }
|