[ADD]sync phenglei user job

This commit is contained in:
vilet.yy 2021-03-05 16:44:37 +08:00
parent 8aefcad510
commit 0aa75e077e
5 changed files with 64 additions and 30 deletions

View File

@ -1,4 +1,5 @@
class Admins::ProjectsController < Admins::BaseController class Admins::ProjectsController < Admins::BaseController
before_action :load_project, only: [:sync_phenglei_user]
def index def index
sort_by = params[:sort_by] ||= 'created_on' sort_by = params[:sort_by] ||= 'created_on'
@ -6,7 +7,7 @@ class Admins::ProjectsController < Admins::BaseController
search = params[:search].to_s.strip search = params[:search].to_s.strip
projects = Project.where("name like ?", "%#{search}%").order("#{sort_by} #{sort_direction}") projects = Project.where("name like ?", "%#{search}%").order("#{sort_by} #{sort_direction}")
@projects = paginate projects.includes(:owner, :members, :issues, :versions, :attachments, :project_score) @projects = paginate projects.includes(:owner, :members, :issues, :versions, :attachments, :project_score, :license)
end end
def destroy def destroy
@ -22,4 +23,20 @@ class Admins::ProjectsController < Admins::BaseController
redirect_to admins_projects_path redirect_to admins_projects_path
flash[:danger] = "删除失败" flash[:danger] = "删除失败"
end end
def sync_phenglei_user
if @project.is_secret
SyncPhengleiUserJob.perform_later(@project.id)
redirect_to admins_projects_path
flash[:success] = "已开启后台同步任务"
else
redirect_to admins_projects_path
flash[:danger] = "非风雷协议项目"
end
end
private
def load_project
@project = Project.find_by!(id: params[:id])
end
end end

View File

@ -0,0 +1,38 @@
class SyncPhengleiUserJob < ApplicationJob
queue_as :default
def perform(project_id)
project = Project.find_by_id(project_id)
return if project.nil?
member_count, success_count, error_count, not_exsit_count = 0, 0, 0, 0
Rails.logger.info("======begin to sync phenglei user to project#{project.owner.login + "/" + project.identifier}")
doc = SimpleXlsxReader.open("#{Rails.root}/public/phenglei_user.xlsx")
data = doc.sheets.first.rows
data.each_with_index do |i, index|
next if index == 0 || i[1].nil?
puts "======开始处理#{i[1]}"
user = User.find_by(phone: i[1])
if user.present?
if project.member?(user.id)
puts "======#{i[1]}用户已经是外围贡献者了"
member_count += 1
else
interactor = Projects::AddMemberInteractor.call(project.owner, project, user, "read", true)
if interactor.error.nil?
puts "========用户#{i[1]}成功添加为项目的外围贡献者======="
success_count += 1
else
puts "========用户#{i[1]}添加失败"
error_count += 1
end
end
else
puts "=====#{i[1]}用户不存在"
not_exsit_count += 1
next
end
end
Rails.logger.info("======已存在外围贡献者数量#{member_count}, 成功添加用户数量#{success_count}, 添加失败用户数量#{error_count}, 找不到用户数量#{not_exsit_count}")
Rails.logger.info("======end to sync phenglei user to project#{project.owner.login + "/" + project.identifier}")
end
end

View File

@ -39,6 +39,9 @@
</td> </td>
<td><%= project.created_on&.strftime('%Y-%m-%d %H:%M') %></td> <td><%= project.created_on&.strftime('%Y-%m-%d %H:%M') %></td>
<td class="action-container"> <td class="action-container">
<% if project.is_secret %>
<%= link_to "同步", sync_phenglei_user_admins_project_path(project.id), method: :get, data:{confirm: "确认同步吗?"}, class: "sync-phenglei-user-project-action" %>
<% end %>
<%= link_to "删除", admins_project_path(project.id), method: :delete, data:{confirm: "确认删除的吗?"}, class: "delete-project-action" %> <%= link_to "删除", admins_project_path(project.id), method: :delete, data:{confirm: "确认删除的吗?"}, class: "delete-project-action" %>
</td> </td>
</tr> </tr>

View File

@ -824,7 +824,11 @@ Rails.application.routes.draw do
resources :courses, only: [:index, :destroy, :update] resources :courses, only: [:index, :destroy, :update]
resources :projects, only: [:index, :destroy] resources :projects, only: [:index, :destroy] do
member do
get :sync_phenglei_user
end
end
resources :disciplines, only: [:index, :create, :edit, :update, :destroy] do resources :disciplines, only: [:index, :create, :edit, :update, :destroy] do
post :adjust_position, on: :member post :adjust_position, on: :member

View File

@ -1,28 +0,0 @@
desc "Create Phenglei project members from excel"
namespace :create_phenglei_project_member do
task init: :environment do
project = Project.find_by_id(477)
doc = SimpleXlsxReader.open("#{Rails.root}/public/phenglei_user.xlsx")
data = doc.sheets.first.rows
err_rows = []
success_count = 0
data.each_with_index do |i, index|
next if index == 0 || i[1].nil?
begin
user = User.find_by(phone: i[1])
next unless user.present?
unless project.member?(user.id)
Projects::AddMemberInteractor.call(project.owner, project, user, "read", true)
success_count += 1
puts "========成功添加手机号为#{i[1]}的用户为项目#{project.id}的协作者======="
end
puts "========#{i[0]}数据处理完毕======="
rescue => e
puts e
err_rows += i
end
end
puts success_count
puts err_rows
end
end