200], [['file_path'], 'string', 'max' => 500], [['file_ext'], 'string', 'max' => 10], [['hash', 'md5', 'folder'], 'string', 'max' => 32], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'a_id' => 'A ID', 'file_name' => 'File Name', 'file_path' => 'File Path', 'file_size' => 'File Size', 'file_ext' => 'File Ext', 'is_image' => 'Is Image', 'is_thumb' => 'Is Thumb', 'downloads' => 'Downloads', 'hash' => 'Hash', 'md5' => 'Md5', 'user_id' => 'User ID', 'admin_id' => 'Admin ID', 'upload_time' => 'Upload Time', 'upload_ip' => 'Upload Ip', 'folder' => 'Folder', 'list_order' => 'List Order', 'is_used' => 'Is Used', 'status' => 'Status', ]; } /** * 处理附件和信息关系 * @param $files string 附件路径 * @param $table_name 表名 * @param $field 字段 * @param $id 信息ID */ public static function relateAttachmentByTable($files,$table_name,$field,$id) { if(empty($files))return; $fileList = explode(",",$files); $used = []; $newa = []; $attachmentIndexList = \app\modules\admin\models\AttachmentIndex::find()->where("table_name='".$table_name."' and field='".$field."' and data_id=$id")->all(); if(is_array($attachmentIndexList))foreach($attachmentIndexList as $index) { $used[] = $index->a_id; } if(is_array($fileList))foreach($fileList as $file_path) { $attachment = \app\modules\admin\models\Attachment::find()->where("hash='".md5($file_path)."'")->one(); if($attachment) { $attachmentIndex = \app\modules\admin\models\AttachmentIndex::find()->where("a_id=".$attachment->a_id." and table_name='".$table_name."' and field='".$field."' and data_id=$id")->one(); if(empty($attachmentIndex)) { $indexModel = new \app\modules\admin\models\AttachmentIndex(); $indexModel->a_id = $attachment->a_id; $indexModel->table_name = $table_name; $indexModel->field = $field; $indexModel->data_id = $id; $indexModel->save(); } $newa[] = $attachment->a_id; } } if(!empty($newa)) { \app\modules\admin\models\Attachment::updateAll(['is_used' => 1], ['in', 'a_id', $newa]); } //处理未被使用的附件状态 if(!empty($used)) { foreach($used as $u) { if(!in_array($u,$newa)) { $diff[] = $u; } } } if(!empty($diff)) { \app\modules\admin\models\Attachment::updateAll(['is_used' => -1], ['in', 'a_id', $diff]); \app\modules\admin\models\AttachmentIndex::deleteAll("a_id in(".join(",",$diff).") and table_name='".$table_name."' and field='".$field."' and data_id=$id"); //写附件垃圾表 $attachmentList = Attachment::findAll($diff); if(is_array($attachmentList))foreach($attachmentList as $attachment) { $trash = new AttachmentTrash(); $trash->a_id = $attachment->a_id; $trash->file_path = $attachment->file_path; $trash->is_oss = Yii::$app->params['oss']['OPEN_OSS']; $trash->create_time = TIMESTAMP; $trash->save(); } } } /** * 释放附件和信息关系 * @param $table_name string 表 * @param $field string 字段 * @param $id 信息ID */ public static function releaseAttachmentByTable($id,$table_name,$field) { $used = []; $attachmentIndexList = \app\modules\admin\models\AttachmentIndex::find()->where("table_name='".$table_name."' and field='".$field."' and data_id=$id")->all(); if(is_array($attachmentIndexList))foreach($attachmentIndexList as $index) { $used[] = $index->a_id; $index->delete(); } if(!empty($used)) { \app\modules\admin\models\Attachment::updateAll(['is_used' => -1], ['in', 'a_id', $used]); //写附件垃圾表 $attachmentList = Attachment::findAll($used); if(is_array($attachmentList))foreach($attachmentList as $attachment) { $trash = new AttachmentTrash(); $trash->a_id = $attachment->a_id; $trash->file_path = $attachment->file_path; $trash->is_oss = Yii::$app->params['oss']['OPEN_OSS']; $trash->create_time = TIMESTAMP; $trash->save(); } } } }