123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace app\modules\admin\models;
- use Yii;
- /**
- * This is the model class for table "{{%attachment}}".
- *
- * @property int $a_id ID
- * @property string $file_name 文件名
- * @property string $file_path 路径
- * @property int $file_size 文件大小字节数
- * @property string $file_ext 文件扩展名
- * @property int $is_image 是否图片
- * @property int $is_thumb 是否缩略图
- * @property int $downloads 下载次数
- * @property string $hash 文件路径的md5
- * @property string $md5 文件内容的md5
- * @property int $user_id 前台用户
- * @property int $admin_id 后台用户
- * @property int $upload_time 创建时间
- * @property int $upload_ip 创建IP
- * @property string $folder 文件类型
- * @property int $list_order
- * @property int|null $is_used 使用状态 1:已使用 0:未使用 -1:删除
- * @property int $status 审核状态,-1:审核未通过 0:待审核 1:审核通过
- */
- class Attachment extends \app\common\models\EActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%attachment}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['file_path', 'hash', 'upload_time'], 'required'],
- [['file_size', 'is_image', 'is_thumb', 'downloads', 'user_id', 'admin_id', 'upload_time', 'upload_ip', 'list_order', 'is_used', 'status'], 'integer'],
- [['file_name'], 'string', 'max' => 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();
- }
- }
- }
- }
|