2016-07-25 15:20:01 +08:00
#coding=utf-8
class ProjectsService
include ApplicationHelper
2016-08-03 14:55:54 +08:00
include ApiHelper
include ActionView :: Helpers :: DateHelper
include ProjectsHelper
2016-07-25 15:20:01 +08:00
#获取指定用户的项目列表
def user_projects ( user )
projects = user . projects . not_deleted . select ( " projects.*,(SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS updated_at " ) . order ( " updated_at desc " )
projects
end
#显示项目
def show_project ( params , current_user )
project = Project . find ( params [ :id ] )
2016-08-03 10:56:55 +08:00
project . generate_invite_code
2016-08-04 09:08:18 +08:00
project . generate_qrcode
2016-07-25 15:20:01 +08:00
project
end
2016-07-28 14:49:45 +08:00
def send_wechat_create_project_notice user , project
count = ShieldWechatMessage . where ( " container_type='User' and container_id= #{ user . id } and shield_type='Project' and shield_id= #{ project . id } " ) . count
2016-07-28 17:26:57 +08:00
Rails . logger . info " !!!!!!!!!!!!!!!!!!!!!! #{ project } "
2016-07-25 15:20:01 +08:00
if count == 0
ws = WechatService . new
2016-07-28 14:49:45 +08:00
title = " 恭喜您创建项目成功。 "
ws . create_project_notice user . id , " create_project_notice " , project . id , title , project . name , format_time ( project . created_on ) , " 点击查看项目详情。 "
2016-07-25 15:20:01 +08:00
end
end
2016-07-28 14:49:45 +08:00
def createNewProject params , user
status = - 1
issue_custom_fields = IssueCustomField . sorted . all
trackers = Tracker . sorted . all
project = Project . new
project [ :name ] = params [ :name ]
project [ :description ] = ''
2016-07-28 18:21:42 +08:00
project [ :is_public ] = '0' #公开
2016-07-28 14:49:45 +08:00
project [ :project_type ] = 0
project [ :project_new_type ] = 1
project [ :identifier ] = Project . next_identifier if Setting . sequential_project_identifiers?
project . organization_id = params [ :organization_id ]
project . user_id = user . id
# if validate_parent_id && @project.save
if project . save
2016-07-28 17:26:57 +08:00
p = Project . find ( " #{ project . id } " )
send_wechat_create_project_notice user , p
2016-07-28 14:49:45 +08:00
r = Role . givable . find_by_id ( Setting . new_project_user_role_id . to_i ) || Role . givable . first
m = Member . new ( :user = > user , :roles = > [ r ] )
# project's score
if ProjectScore . where ( " project_id=? " , project . id ) . first . nil?
ProjectScore . create ( :project_id = > project . id , :score = > false )
2016-07-25 15:20:01 +08:00
end
2016-07-28 14:49:45 +08:00
# end
project_info = ProjectInfo . new ( :user_id = > user . id , :project_id = > project . id )
user_grades = UserGrade . create ( :user_id = > user . id , :project_id = > project . id )
project_status = ProjectStatus . create ( :project_id = > project . id , :watchers_count = > 0 , :changesets_count = > 0 , :project_type = > project . project_type , :grade = > 0 )
project . members << m
project . project_infos << project_info
status = 0
2016-07-25 15:20:01 +08:00
end
2016-07-28 14:49:45 +08:00
status
2016-07-25 15:20:01 +08:00
end
2016-07-28 14:49:45 +08:00
#修改项目成员角色
def modify_user_project_role params
2016-07-25 15:20:01 +08:00
status = - 1
2016-07-28 14:49:45 +08:00
project = Project . find ( " #{ params [ :id ] } " )
member = project . member_principals . includes ( :roles , :principal ) . where ( " user_id=? " , params [ :user_id ] ) . first
2016-07-25 15:20:01 +08:00
2016-07-28 14:49:45 +08:00
if member
member . member_roles [ 0 ] . role_id = params [ :role_id ]
2016-07-25 15:20:01 +08:00
2016-07-28 14:49:45 +08:00
role = Role . find ( params [ :role_id ] )
if role . allowed_to? ( :is_manager )
projectInfo = ProjectInfo . new ( :user_id = > member . user_id , :project_id = > project . id )
projectInfo . save
else
user_admin = ProjectInfo . where ( " user_id = ? and project_id = ? " , member . user_id , project . id )
if user_admin . size > 0
user_admin . each do | user |
user . destroy
end
2016-07-25 15:20:01 +08:00
end
end
2016-07-28 14:49:45 +08:00
if member . member_roles [ 0 ] . save
status = 0
2016-07-25 15:20:01 +08:00
end
2016-07-28 14:49:45 +08:00
end
status
end
class JoinProjectError < Errors
define_error [
2016-08-03 16:31:33 +08:00
0 , '您已成功加入项目' ,
2016-07-28 14:49:45 +08:00
1 , '您的邀请码不正确' ,
2 , '您还未登录' ,
3 , '您已经是该项目的管理人员' ,
4 , '您已经是该项目的开发人员' ,
5 , '您已经是该项目的报告人员' ,
6 , '该项目不存在或已被删除啦' ,
2016-08-03 09:37:24 +08:00
7 , '您的申请已提交,请等待管理员审批' ,
8 , '您已经发送过申请了,请耐心等待' ,
2016-07-28 14:49:45 +08:00
'未知错误,请稍后再试'
]
end
def join_project params , current_user
status = - 1
2016-08-01 11:14:03 +08:00
project = Project . find_by_invite_code ( params [ :invite_code ] ) if params [ :invite_code ]
2016-07-28 14:49:45 +08:00
if project
if project [ :is_delete ] == 1
status = 6
else
if current_user . member_of? ( project ) #如果已经是成员
member = project . member_principals . includes ( :roles , :principal ) . where ( " user_id=? " , current_user . id ) . first
status = member . member_roles [ 0 ] . role_id
else
if params [ :invite_code ] . present?
2016-08-01 11:14:03 +08:00
2016-08-03 16:31:33 +08:00
if params [ :role ] == 5
2016-08-02 15:50:15 +08:00
members = [ ]
user_grades = [ ]
project_info = [ ]
2016-08-01 11:14:03 +08:00
2016-08-02 15:50:15 +08:00
user_grades << UserGrade . new ( :user_id = > current_user . id , :project_id = > project . id )
project . user_grades << user_grades unless user_grades . first . user_id . nil?
2016-08-01 11:14:03 +08:00
2016-08-02 15:50:15 +08:00
members << Member . new ( :role_ids = > [ " 5 " ] , :user_id = > current_user . id )
project . members << members
2016-08-01 11:14:03 +08:00
2016-08-02 15:50:15 +08:00
project_info << ProjectInfo . new ( :user_id = > current_user . id , :project_id = > project . id )
project . project_infos << project_info
status = 0
else
2016-08-03 09:37:24 +08:00
if ! AppliedProject . where ( :project_id = > project . id , :user_id = > current_user . id ) . first . nil?
status = 8
else
2016-08-04 14:07:35 +08:00
AppliedProject . create ( :user_id = > current_user . id , :project_id = > project . id , :role = > params [ :role ] )
2016-08-02 15:50:15 +08:00
2016-08-03 09:37:24 +08:00
status = 7
end
2016-08-02 15:50:15 +08:00
end
2016-07-28 14:49:45 +08:00
else
status = 4
end
end
end
else
2016-08-04 14:47:39 +08:00
status = 1
2016-07-25 15:20:01 +08:00
end
status
end
2016-08-03 09:37:24 +08:00
def get_project_review_members ( project , role_id , current_user )
review_members = [ ]
if role_id == 3 || role_id == 4
#AppliedMessage.where(:applied_id => @applied_message.applied_id, :project_id => @applied_message.project_id, :status => 1,
#:applied_type => "AppliedProject")
apply_messages = AppliedMessage . where ( :user_id = > current_user . id , :project_id = > project . id , :status = > 1 , :role = > role_id )
apply_messages . each do | m |
user = User . find ( m . applied_user_id )
if user
2016-08-09 14:09:13 +08:00
img_url = " /images/ " + url_to_avatar ( user )
2016-08-03 09:37:24 +08:00
gender = user . user_extensions . gender . nil? ? 0 : user . user_extensions . gender
work_unit = get_user_work_unit user
location = get_user_location user
review_members << { :id = > user . id , :img_url = > img_url , :nickname = > user . login , :gender = > gender ,
:work_unit = > work_unit , :mail = > user . mail , :location = > location ,
role_name : " " ,
name : user . show_name ,
roles_id : role_id ,
2016-08-03 14:55:54 +08:00
:brief_introduction = > user . user_extensions . brief_introduction , :realname = > user . realname , :real_name = > user . realname }
2016-08-03 09:37:24 +08:00
end
end
end
review_members
end
2016-08-04 18:09:54 +08:00
def get_reviewer_info params , current_user
status = - 1
2016-08-03 09:37:24 +08:00
info = nil
project = Project . find ( params [ :project_id ] )
if project
2016-08-04 18:09:54 +08:00
# messages = AppliedProject.where(:project_id=> project.id, :user_id=>params[:user_id]).first
#status 1未处理 5拒绝 7同意
applied_message = AppliedMessage . where ( :user_id = > current_user . id , :project_id = > project . id , :applied_user_id = > params [ :user_id ] , :applied_type = > " AppliedProject " ) . last
if applied_message
status = applied_message . status
2016-08-03 09:37:24 +08:00
user = User . find ( params [ :user_id ] )
if user
2016-08-09 14:09:13 +08:00
img_url = " /images/ " + url_to_avatar ( user )
2016-08-03 09:37:24 +08:00
gender = user . user_extensions . gender . nil? ? 0 : user . user_extensions . gender
work_unit = get_user_work_unit user
location = get_user_location user
info = { :id = > user . id , :img_url = > img_url , :nickname = > user . login , :gender = > gender ,
:work_unit = > work_unit , :mail = > user . mail , :location = > location ,
role_name : " " ,
name : user . show_name ,
2016-08-04 18:09:54 +08:00
roles_id : applied_message . role ,
2016-08-03 09:37:24 +08:00
:brief_introduction = > user . user_extensions . brief_introduction , :realname = > user . realname }
end
end
end
2016-08-04 18:09:54 +08:00
{ status : status , reviewer : info }
2016-08-03 09:37:24 +08:00
end
def deal_join_apply params , current_user
status = - 1
message = " "
project = Project . find ( params [ :project_id ] )
2016-08-03 10:56:55 +08:00
user = User . find ( params [ :user_id ] )
2016-08-03 09:37:24 +08:00
if project
2016-08-03 10:56:55 +08:00
applied_messages = AppliedMessage . where ( :user_id = > current_user . id , :project_id = > project . id , :status = > 1 , :applied_user_id = > user . id , :applied_type = > " AppliedProject " )
apply_message = applied_messages . first
if apply_message
if user . member_of? ( project )
message = " 已经是该项目的成员了 "
2016-08-09 09:23:06 +08:00
u_member = project . member_principals . includes ( :roles , :principal ) . where ( " user_id=? " , params [ :user_id ] ) . first
if u_member . member_roles [ 0 ] . role_id == 5
applied_messages . update_all ( :status = > 5 )
else
applied_messages . update_all ( :status = > 7 )
end
2016-08-03 10:56:55 +08:00
else
applied_project = apply_message . applied
ap_role = applied_project . try ( :role )
if ap_role
2016-08-03 14:55:54 +08:00
rolename = ap_role == 3 ? " 管理人员 " : " 开发人员 "
2016-08-03 10:56:55 +08:00
if params [ :type ] == 0
#同意
members = [ ]
user_grades = [ ]
project_info = [ ]
members << Member . new ( :role_ids = > [ " #{ ap_role } " ] , :user_id = > user . id )
user_grades << UserGrade . new ( :user_id = > user . id , :project_id = > project . id )
role = Role . find ( ap_role )
project_info << ProjectInfo . new ( :project_id = > project . id , :user_id = > user . id ) if role . allowed_to? ( :is_manager )
project . members << members
project . project_infos << project_info
project . user_grades << user_grades unless user_grades . first . user_id . nil?
# 添加成功后所有管理员收到的消息状态都要更新
applied_messages . update_all ( :status = > 7 , :viewed = > true )
# 添加成功后,申请人收到消息
AppliedMessage . create ( :user_id = > user . id , :applied_type = > " AppliedProject " , :applied_id = > applied_project . id ,
:status = > 6 , :viewed = > false , :applied_user_id = > current_user . id , :role = > applied_project . role , :project_id = > applied_project . project_id )
message = " 您已同意 " + rolename + user . show_name + " ,加入项目 "
2016-08-03 09:37:24 +08:00
2016-08-03 10:56:55 +08:00
else
#拒绝
AppliedMessage . create ( :user_id = > user . id , :applied_type = > " AppliedProject " , :applied_id = > applied_project . id , :status = > 4 ,
:viewed = > false , :applied_user_id = > current_user . id , :role = > applied_project . role , :project_id = > applied_project . project_id )
2016-08-03 09:37:24 +08:00
2016-08-03 10:56:55 +08:00
applied_messages . update_all ( :status = > 5 , :viewed = > true )
applied_project . delete
2016-08-03 09:37:24 +08:00
2016-08-03 10:56:55 +08:00
message = " 您已拒绝 " + rolename + user . show_name + " ,加入项目 "
end
status = 0
else
message = " 该申请不存在或已被处理 "
end
end
else
message = " 该申请不存在或已被处理 "
end
2016-08-03 09:37:24 +08:00
else
2016-08-03 10:56:55 +08:00
message = " 该项目不存在或已被删除 "
2016-08-03 09:37:24 +08:00
end
{ :status = > status , :message = > message }
end
2016-07-25 15:20:01 +08:00
end