新版订单消耗系统

Oplog.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Oplog extends Model
  5. {
  6. protected $table = 'oplog';
  7. public $timestamps = false;
  8. public static function add($table, $data_id, $type, $before = [], $after = [], $op_reason = null, $columnsConfig = 'PROJECT')
  9. {
  10. $log = new Oplog();
  11. $log->table_name = $table;
  12. $log->data_id = $data_id;
  13. $log->type = $type;
  14. $log->op_reason = $op_reason;
  15. $log->before_data = json_encode($before, JSON_UNESCAPED_UNICODE);
  16. $log->after_data = json_encode($after, JSON_UNESCAPED_UNICODE);
  17. $log->op_user_id = \Auth::id();
  18. $user = Users::select('name', 'role_id', 'real_name')->where('id', $log->op_user_id)->first();
  19. $role_name = Roles::where('id', $user->role_id)->value('name');
  20. $log->op_user_name = $user->real_name;
  21. $log->op_user_role = $role_name;
  22. #名词
  23. $columns = config('columns_note.'.$columnsConfig);
  24. if($type == 2){
  25. $log->content = '';
  26. if( !empty($after) && !empty($before) ){
  27. foreach( $after as $k=>$v ){
  28. if( $v != $before[$k] ){
  29. $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
  30. $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
  31. }
  32. }
  33. }
  34. } elseif($type==1){
  35. $log->content = '创建';
  36. } elseif($type==3){
  37. $log->content = '删除';
  38. } elseif($type==4){
  39. $log->content = '锁定';
  40. } elseif($type==2){
  41. $log->content = '编辑';
  42. }
  43. $log->save();
  44. return true;
  45. }
  46. }