package com.gec.attendanceclient.view;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
import com.gec.attendance.common.constaint.CommandType;
import com.gec.attendance.common.entity.Employee;
import com.gec.attendanceclient.listener.ReceivedataCallBackListener;
import com.gec.attendanceclient.utils.DateUtils;
import com.gec.attendanceclient.utils.SocketUtils;
public class AttendanceClientView implements ReceivedataCallBackListener {
private SocketUtils socketUtils;
//定义一个成员变量员工对象,此对象记录登录后的员工对象值
private Employee employee;
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
AttendanceClientView view = new AttendanceClientView();
view.initSocketConnection();
}
/*
* 初始化网络连接
*/
public void initSocketConnection() {
try {
Socket socket = new Socket("192.168.59.33", 30001);
if (socket != null && !socket.isClosed()) {
// 针对SocktUtils工具类实现初始化
socketUtils = new SocketUtils(socket, this);
// 运行此客户端系统
doAction();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doAction() {
System.out.println("欢迎进入粤嵌智能考勤系统,请登录");
inputLoginInfo();
}
@Override
public void receviceCallBackListener(Object result) {
HashMap<String, Object> resultMap = (HashMap<String, Object>) result;
resultMap.forEach((k, v) -> {
// 读取客户端发送过来的请求指令
if (k.equals(CommandType.EMP_LOGIN_RESPONSE)) {
Object responseResult=resultMap.get(CommandType.EMP_LOGIN_RESPONSE);
if(responseResult==null)
{
System.out.println("登录失败,请重新登录!");
//返回用户登录录入功能
inputLoginInfo();
}else
{
Employee getEmployee=(Employee) responseResult;
//此对象记录登录后的员工对象值
this.employee=getEmployee;
toMainMenu();
}
}else if(k.equals(CommandType.REGISTER_EMP_RESPONSE)) {
Object responseResult=resultMap.get(CommandType.REGISTER_EMP_RESPONSE);
Boolean registerSuccess=(Boolean) responseResult;
if(registerSuccess)
{
System.out.println("用户注册成功");
}else
System.out.println("用户注册失败成功");
//返回主菜单
toMainMenu();
}else if(k.equals(CommandType.CLOCK_IN_TIME_RESPONSE)) {
Object responseResult=resultMap.get(CommandType.CLOCK_IN_TIME_RESPONSE);
Boolean isFlag=(Boolean) responseResult;
if(isFlag)
{
System.out.println("员工上班打卡成功");
}else
System.out.println("员工上班打卡失败");
//返回主菜单
toMainMenu();
}else if(k.equals(CommandType.CLOCK_OFF_TIME_RESPONSE)) {
Object responseResult=resultMap.get(CommandType.CLOCK_OFF_TIME_RESPONSE);
Boolean isFlag=(Boolean) responseResult;
if(isFlag)
{
System.out.println("员工下班打卡成功");
}else
System.out.println("员工下班打卡失败");
//返回主菜单
toMainMenu();
}
});
}
/*
* 从键盘录入登录内容
*/
public void inputLoginInfo() {
System.out.println("请输入登录名:");
String loginName = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
Employee employee = new Employee();
employee.setLoginName(loginName);
employee.setPassword(password);
HashMap<String, Employee> sendMapData = new HashMap<>();
sendMapData.put(CommandType.EMP_LOGIN, employee);
socketUtils.writeObject(sendMapData);
}
/*
* 进入主菜单
* */
public void toMainMenu()
{
if(employee!=null && employee.getRole()==1)
{
toMainMenuByAdmin();
}else {
toMainMenuByEmp();
}
}
public void toMainMenuByAdmin()
{
System.out.println("登录成功,进入系统主菜单");
System.out.println("按1:注册员工信息");
System.out.println("按2:查询员工考勤信息");
System.out.println("按3:重新登录");
Integer choose=Integer.parseInt(sc.nextLine());
switch (choose) {
case 1:
inputRegisterEmp();
break;
case 2:
break;
case 3:
inputLoginInfo();
break;
default:
break;
}
}
public void toMainMenuByEmp()
{
System.out.println("登录成功,进入系统主菜单");
System.out.println("按1:上班打卡");
System.out.println("按2:下班打卡");
System.out.println("按3:查看员工考勤信息");
System.out.println("按4:重新登录");
System.out.println("按5:退出系统");
Integer choose=Integer.parseInt(sc.nextLine());
switch (choose) {
case 1:
clockInAction();
break;
case 2:
clockOffAction();
break;
case 4:
inputLoginInfo();
break;
default:
break;
}
}
public void inputRegisterEmp()
{
System.out.println("请输入员工名:");
String employeeName=sc.nextLine();
System.out.println("请输入员工编号:");
String employeeNo=sc.nextLine();
System.out.println("请输入登录名:");
String loginName=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.nextLine();
System.out.println("请输入工作职位:");
String job=sc.nextLine();
System.out.println("请输入职日期:");
Employee employee=new Employee();
String strHireDate=sc.nextLine();
while(!DateUtils.isDateFormatter(strHireDate))
{
System.out.println("输入日期格式 有误,请重新输入");
strHireDate=sc.nextLine();
}
try {
Date hiredate=new SimpleDateFormat("yyyy-MM-dd").parse(strHireDate);
employee.setHiredate(hiredate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("请输入月薪:");
Float sal=Float.parseFloat(sc.nextLine());
System.out.println("请输入角色:");
Integer role=Integer.parseInt(sc.nextLine());
employee.setEmployeeName(employeeName);
employee.setEmployeeNo(employeeNo);
employee.setSal(sal);
employee.setJob(job);
employee.setLoginName(loginName);
employee.setPassword(password);
employee.setRole(role);
HashMap<String, Employee> sendMapData=new HashMap<>();
sendMapData.put(CommandType.REGISTER_EMP_REQUEST, employee);
socketUtils.writeObject(sendMapData);
}
/*
* 实现上班打卡
* */
public void clockInAction()
{
HashMap<String, Employee> sendMapData=new HashMap<>();
sendMapData.put(CommandType.CLOCK_IN_TIME, employee);
socketUtils.writeObject(sendMapData);
}
/*
* 实现下班打卡
* */
public void clockOffAction()
{
HashMap<String, Employee> sendMapData=new HashMap<>();
sendMapData.put(CommandType.CLOCK_OFF_TIME, employee);
socketUtils.writeObject(sendMapData);
}
}