create or replace procedure parent_bind_stu(
IN_WX_OPENID varchar2,--输入进来的微信ID
IN_STU_USER_ID number,--输入进来的学生ID
IN_PARENT_TYPE number,--输入进来的家长类型
IN_PHONE varchar2,--输入的手机号码
OUT_BIND_TYPE out number--输出状态
)
as
TMP_IS_WX number;--微信是否存在:1=存在、0=不存在
TMP_PARENT_USER_ID number;--家长用户ID
TMP_IS_ROLE number;--家长角色是否存在:1=存在、0=不存在
TMP_IS_PHONE number;--电话号码是否存在:1=存在、0=不存在
TMP_IS_PARENT number;--家长信息表是否存在:1=存在、0=不存在
begin
select case when exists(select 1 from base_login_user where data_flag=1 and wx_openid=IN_WX_OPENID) then 1 else 0 end into TMP_IS_WX from dual;--微信是否存在
if TMP_IS_WX=1 then--微信有账号时
select id into TMP_PARENT_USER_ID from base_login_user where data_flag=1 and wx_openid=IN_WX_OPENID;--获取家长用户ID
update base_login_user set USER_MOBILE=IN_PHONE where ID=TMP_PARENT_USER_ID;--更新账号信息表
insert into sch_stu_parent(user_id,stu_user_id,parent_type) values(TMP_PARENT_USER_ID,IN_STU_USER_ID,IN_PARENT_TYPE);--新增家长信息表
select case when exists(select 1 from base_login_role_map where data_flag=1 and role_id=90 and user_id=TMP_PARENT_USER_ID) then 1 else 0 end into TMP_IS_ROLE from dual;--是否拥有家长角色
if TMP_IS_ROLE=0 then--该家长用户没有家长角色
insert into base_login_role_map(user_id,role_id) values(TMP_PARENT_USER_ID,90);--新增角色表
end if;
OUT_BIND_TYPE:=1;--成功
else --微信没有账号时
select case when exists(select 1 from base_login_user where data_flag=1 and USER_MOBILE=IN_PHONE) then 1 else 0 end into TMP_IS_PHONE from dual;--手机号码是否存在
if TMP_IS_PHONE=1 then--手机号码有账号
select id into TMP_PARENT_USER_ID from base_login_user where data_flag=1 and USER_MOBILE=IN_PHONE;--获取家长ID
update base_login_user set wx_openid=IN_WX_OPENID where ID=TMP_PARENT_USER_ID;--更新账号信息表
select case when exists(select 1 from sch_stu_parent where data_flag=1 and user_id=TMP_PARENT_USER_ID and stu_user_id=IN_STU_USER_ID) then 1 else 0 end into TMP_IS_PARENT from dual;--家长信息表是否存在
if TMP_IS_PARENT=0 then--家长信息表不存在
insert into sch_stu_parent(user_id,stu_user_id,parent_type) values(TMP_PARENT_USER_ID,IN_STU_USER_ID,IN_PARENT_TYPE);--新增家长信息表
end if;
select case when exists(select 1 from base_login_role_map where data_flag=1 and role_id=90 and user_id=TMP_PARENT_USER_ID) then 1 else 0 end into TMP_IS_ROLE from dual;--是否拥有家长角色
if TMP_IS_ROLE=0 then--该家长用户没有家长角色
insert into base_login_role_map(user_id,role_id) values(TMP_PARENT_USER_ID,90);--新增角色表
end if;
else--手机号码没账号时
insert into base_login_user(wx_openid,user_pwd,user_mobile) values(IN_WX_OPENID,'e10adc3949ba59abbe56e057f20f883e',IN_PHONE) RETURNING ID INTO TMP_PARENT_USER_ID;--新增账号信息表
insert into base_login_user_info(user_id,user_name) values(TMP_PARENT_USER_ID,'家长');--新增用户信息表
insert into base_login_role_map(user_id,role_id) values(TMP_PARENT_USER_ID,90);--新增账号角色对应表
insert into sch_stu_parent(user_id,stu_user_id,parent_type) values(TMP_PARENT_USER_ID,IN_STU_USER_ID,IN_PARENT_TYPE);--新增家长表
end if;
end if;
commit;
end;
这里是具体语句,需要自行理解。要看Mybatis的存储过程使用可以去我的文章看看。