Attachment.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\modules\admin\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "{{%attachment}}".
  6. *
  7. * @property int $a_id ID
  8. * @property string $file_name 文件名
  9. * @property string $file_path 路径
  10. * @property int $file_size 文件大小字节数
  11. * @property string $file_ext 文件扩展名
  12. * @property int $is_image 是否图片
  13. * @property int $is_thumb 是否缩略图
  14. * @property int $downloads 下载次数
  15. * @property string $hash 文件路径的md5
  16. * @property string $md5 文件内容的md5
  17. * @property int $user_id 前台用户
  18. * @property int $admin_id 后台用户
  19. * @property int $upload_time 创建时间
  20. * @property int $upload_ip 创建IP
  21. * @property string $folder 文件类型
  22. * @property int $list_order
  23. * @property int|null $is_used 使用状态 1:已使用 0:未使用 -1:删除
  24. * @property int $status 审核状态,-1:审核未通过 0:待审核 1:审核通过
  25. */
  26. class Attachment extends \app\common\models\EActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%attachment}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['file_path', 'hash', 'upload_time'], 'required'],
  42. [['file_size', 'is_image', 'is_thumb', 'downloads', 'user_id', 'admin_id', 'upload_time', 'upload_ip', 'list_order', 'is_used', 'status'], 'integer'],
  43. [['file_name'], 'string', 'max' => 200],
  44. [['file_path'], 'string', 'max' => 500],
  45. [['file_ext'], 'string', 'max' => 10],
  46. [['hash', 'md5', 'folder'], 'string', 'max' => 32],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'a_id' => 'A ID',
  56. 'file_name' => 'File Name',
  57. 'file_path' => 'File Path',
  58. 'file_size' => 'File Size',
  59. 'file_ext' => 'File Ext',
  60. 'is_image' => 'Is Image',
  61. 'is_thumb' => 'Is Thumb',
  62. 'downloads' => 'Downloads',
  63. 'hash' => 'Hash',
  64. 'md5' => 'Md5',
  65. 'user_id' => 'User ID',
  66. 'admin_id' => 'Admin ID',
  67. 'upload_time' => 'Upload Time',
  68. 'upload_ip' => 'Upload Ip',
  69. 'folder' => 'Folder',
  70. 'list_order' => 'List Order',
  71. 'is_used' => 'Is Used',
  72. 'status' => 'Status',
  73. ];
  74. }
  75. /**
  76. * 处理附件和信息关系
  77. * @param $files string 附件路径
  78. * @param $table_name 表名
  79. * @param $field 字段
  80. * @param $id 信息ID
  81. */
  82. public static function relateAttachmentByTable($files,$table_name,$field,$id)
  83. {
  84. if(empty($files))return;
  85. $fileList = explode(",",$files);
  86. $used = [];
  87. $newa = [];
  88. $attachmentIndexList = \app\modules\admin\models\AttachmentIndex::find()->where("table_name='".$table_name."' and field='".$field."' and data_id=$id")->all();
  89. if(is_array($attachmentIndexList))foreach($attachmentIndexList as $index)
  90. {
  91. $used[] = $index->a_id;
  92. }
  93. if(is_array($fileList))foreach($fileList as $file_path)
  94. {
  95. $attachment = \app\modules\admin\models\Attachment::find()->where("hash='".md5($file_path)."'")->one();
  96. if($attachment)
  97. {
  98. $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();
  99. if(empty($attachmentIndex))
  100. {
  101. $indexModel = new \app\modules\admin\models\AttachmentIndex();
  102. $indexModel->a_id = $attachment->a_id;
  103. $indexModel->table_name = $table_name;
  104. $indexModel->field = $field;
  105. $indexModel->data_id = $id;
  106. $indexModel->save();
  107. }
  108. $newa[] = $attachment->a_id;
  109. }
  110. }
  111. if(!empty($newa))
  112. {
  113. \app\modules\admin\models\Attachment::updateAll(['is_used' => 1], ['in', 'a_id', $newa]);
  114. }
  115. //处理未被使用的附件状态
  116. if(!empty($used))
  117. {
  118. foreach($used as $u)
  119. {
  120. if(!in_array($u,$newa))
  121. {
  122. $diff[] = $u;
  123. }
  124. }
  125. }
  126. if(!empty($diff))
  127. {
  128. \app\modules\admin\models\Attachment::updateAll(['is_used' => -1], ['in', 'a_id', $diff]);
  129. \app\modules\admin\models\AttachmentIndex::deleteAll("a_id in(".join(",",$diff).") and table_name='".$table_name."' and field='".$field."' and data_id=$id");
  130. //写附件垃圾表
  131. $attachmentList = Attachment::findAll($diff);
  132. if(is_array($attachmentList))foreach($attachmentList as $attachment)
  133. {
  134. $trash = new AttachmentTrash();
  135. $trash->a_id = $attachment->a_id;
  136. $trash->file_path = $attachment->file_path;
  137. $trash->is_oss = Yii::$app->params['oss']['OPEN_OSS'];
  138. $trash->create_time = TIMESTAMP;
  139. $trash->save();
  140. }
  141. }
  142. }
  143. /**
  144. * 释放附件和信息关系
  145. * @param $table_name string 表
  146. * @param $field string 字段
  147. * @param $id 信息ID
  148. */
  149. public static function releaseAttachmentByTable($id,$table_name,$field)
  150. {
  151. $used = [];
  152. $attachmentIndexList = \app\modules\admin\models\AttachmentIndex::find()->where("table_name='".$table_name."' and field='".$field."' and data_id=$id")->all();
  153. if(is_array($attachmentIndexList))foreach($attachmentIndexList as $index)
  154. {
  155. $used[] = $index->a_id;
  156. $index->delete();
  157. }
  158. if(!empty($used))
  159. {
  160. \app\modules\admin\models\Attachment::updateAll(['is_used' => -1], ['in', 'a_id', $used]);
  161. //写附件垃圾表
  162. $attachmentList = Attachment::findAll($used);
  163. if(is_array($attachmentList))foreach($attachmentList as $attachment)
  164. {
  165. $trash = new AttachmentTrash();
  166. $trash->a_id = $attachment->a_id;
  167. $trash->file_path = $attachment->file_path;
  168. $trash->is_oss = Yii::$app->params['oss']['OPEN_OSS'];
  169. $trash->create_time = TIMESTAMP;
  170. $trash->save();
  171. }
  172. }
  173. }
  174. }