1.任务安排
需要完成已选座位查询,以可视化的方式展示座位已选的状态,同时显示已选座位的用户列表。
2.任务分析
- 先根据活动id获取教室名称,然后根据教室名称找到这个教室所有的座位id,然后根据座位id获取每个座位的状态,形成一个包含01234这5种状态的字符串,记为字符串1。其中0表示过道,1表示可选座位,2表示不可选座位,3表示可能损坏座位,4表示已损坏座位。
- 然后再根据座位id去获取每个座位在当前活动中的状态,形成一个包含01这2种状态的字符串,记为字符串2。其中0表示该座位在活动中不可使用,1表示该座位在活动中可使用。
- 然后我们对字符串1和字符串2进行按位操作得到新的字符串,记为字符串3。具体运算规则如下,如果字符串2中某个位置为1,则字符串3中该位置为2,如果字符串2中某个位置为0,则字符串3中该位置和字符串1中该位置值相同。
- 然后我们再根据用户选座情况得到一个包含01这2种状态字符串4,0表示该座位没有被选,1表示该座位被选。
- 我们对字符串3和字符串4进行按位操作得到包含012345这6种状态的字符串,记为字符串5。新增加的5表示该座位被某个用户选择。具体运算规则如下,如果字符串4中某个位置为1,且字符串3中该位置为1或3,那么字符串5中该位置为5,其他情况下,字符串5中的值等于字符串3中对应位置的值。
- 前端可以根据字符串5来对选座情况进行还原,用不同颜色表示不同的状态来进行可视化展示。
1 | 2 | 3 | |
4 | 5 | 6 | |
7 | 8 | 9 | |
10 | 11 | 12 | |
上图表示没有人选座时候的布局情况
- 白色表示过道
- 红色表示尚未被选择的座位
- 蓝色表示不可使用的座位
- 黄色表示可能损坏的座位
- 黑色表示已经损坏的座位
- 绿色表示被选择的座位
1 | 2 | 3 | |
4 | 5 | 6 | |
7 | 8 | 9 | |
10 | 11 | 12 | |
上图表示的是有一个用户选座,选的座位是12号座位
每有一个用户选座,就会形成一个pick_seat对象。然后将pick_seat对象数组发送给前端。但是由于教室的信息是依赖于用用户的选座记录发送给前端的,所以当没有用户选座时,无法传递给前端这些信息。所以设置了0号位置的虚拟用户,实际上不存在0号位置,只是为了能保证传递给前端教室的信息。
pick_seat的属性如下:括号中为示例数据
-
行数(4)
列数(4)
座位编号串(1,0,2,3,4,0,5,6,7,0,8,9,10,0,11,12)
选座信息串(1022101110314015)
其中0表示过道,1表示尚未被选择的座位,2表示不可使用的座位,3表示可能损坏的座位,4表示已经损坏的座位,5表示被选择的座位。
用户id(test)
用户名(test)
座位的相对位置(0) -
行数(4)
列数(4)
座位编号串(1,0,2,3,4,0,5,6,7,0,8,9,10,0,11,12)
选座信息串(1022101110314015)
其中0表示过道,1表示尚未被选择的座位,2表示不可使用的座位,3表示可能损坏的座位,4表示已经损坏的座位,5表示被选择的座位。
用户id(test)
用户名(test)
座位的相对位置(12)
3.具体代码
public Pick_Seat_Info[] show_pick_seat(String act_id)
{
String room_name=activityMapper.find_room_name_by_act_id(Integer.valueOf(act_id));
String room_seats=activityMapper.find_room_seats_by_room_name(room_name);
String[] room_seat=room_seats.split(",");
String room_layout1="";//通过seat表获取的原始布局信息
for(int i=0;i<room_seat.length;i++)
{
room_layout1+=activityMapper.find_seat_status_by_seat_id(Integer.valueOf(room_seat[i]));
}
String room_layout2="";//通过act_room_seat表获取的针对某个活动设置的布局信息
for(int i=0;i<room_seat.length;i++)
{
room_layout2+=activityMapper.find_act_seat_status_by_act_id_and_seat_id(Integer.valueOf(act_id),
Integer.valueOf(room_seat[i]));
}
String[] layout1=room_layout1.split("");
String[] layout2=room_layout2.split("");
String room_layout3="";//room_layout1和room_layout2按位运算后得到的新串
for(int i=0;i<layout1.length;i++)
{
String result="";
if(layout2[i].equals("1"))
{
result=layout1[i];
}
else
{
result="2";
}
room_layout3+=result;
}
String room_layout4="";//通过user_pick_seat表获取的对于某一个活动用户的选座情况。
String user_pick_info="";//用于存储用户选座信息的字符串
int count=activityMapper.find_pick_count_by_act_id(Integer.valueOf(act_id));
Pick_Seat_Info[] pick_seat_infos=new Pick_Seat_Info[count+1];
String[] user_pick_infos=new String[count];
int pick_number=0;
String seat_nos="";
for(int i=0;i<room_seat.length;i++)
{
int act_room_seat_id=activityMapper.find_act_room_seat_id_by_act_id_and_seat_id(Integer.valueOf(act_id),
Integer.valueOf(room_seat[i]));
room_layout4+=String.valueOf(activityMapper.find_act_seat_is_picked(act_room_seat_id));
System.out.println(act_room_seat_id);
String user_openid=activityMapper.find_user_openid_by_act_room_seat_id(act_room_seat_id);
String username=activityMapper.find_username_by_user_openid(user_openid);
int seat_no=activityMapper.find_seat_no_by_seat_id(Integer.valueOf(room_seat[i]));
if(i<room_seat.length-1)
{
seat_nos+=seat_no+",";
}
else
{
seat_nos+=seat_no;
}
if(activityMapper.find_act_seat_is_picked(act_room_seat_id)==1)
{
user_pick_infos[pick_number]=user_openid+","+username+","+seat_no;//返回用户名,用户id,用户选座
pick_number++;
}
else
{
//user_pick_info+="无|";
}
}
String[] layout3=room_layout3.split("");
String[] layout4=room_layout4.split("");
String room_layout5="";//room_layout3和room_layout4按位运算后得到的新串
for(int i=0;i<layout3.length;i++)
{
String result="";
if(layout4[i].equals("1"))
{
if(layout3[i].equals("1")||layout3[i].equals("3"))
{
result="5";
}
else
{
result=layout3[i];
}
}
else
{
result=layout3[i];
}
room_layout5+=result;
}
Model model=find_model_by_room_name(room_name);
int row=model.getModel_row();//行数
int col=model.getModel_col();//列数
for(int i=0;i<count+1;i++)
{
if(i==0)
{
pick_seat_infos[i]=new Pick_Seat_Info(row,col,seat_nos,room_layout5,"test","test",0);
}
else
{
String user_openid=user_pick_infos[i-1].split(",")[0];//用户id
String username=user_pick_infos[i-1].split(",")[1];//用户名
int seat_no=Integer.valueOf(user_pick_infos[i-1].split(",")[2]);//座位在教室的相对位置
pick_seat_infos[i]=new Pick_Seat_Info(row,col,seat_nos,room_layout5,user_openid,username,seat_no);
}
}
return pick_seat_infos;
}