package com.zb.action;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zb.model.Phone;
import com.zb.model.Phonetype;
import com.zb.model.User;
import com.zb.service.OrderService;
import com.zb.service.PhoneService;
import com.zb.service.PhonetypeService;
import com.zb.service.UserService;
import com.zb.util.PageUtil;
public class UserAction extends ActionSupport {
private int userId;
private String userName;
private String userPassword;
private String userSex;
private int userAge;
private String userAddress;
private String userPostalCode;
private String phoneNum;
private String photoName;
private UserService userService;
private User user;
private String checkCodeUser;
private String YZMError;
private File photo;
private List<User> userList;
private List<Phone> phone1List;//第一行显示
private List<Phone> phone2List;//第二行显示
private PageUtil pageUtil;
private int Page;
private List<Phonetype> phonetypeList;
private PhonetypeService phonetypeService;
private OrderService orderService;
private PhoneService phoneService;
private int messageNum;
public String adminLogin() {
User user = userService.findUserByuserName(userName);
phonetypeList=phonetypeService.findPtList();
messageNum=orderService.countOrder();
if (user == null) {
this.addFieldError("error", "用戶不存在");
return "adminLoginError";
} else if (!user.getUserPassword().equals(userPassword)) {
this.addFieldError("error", "密码不正确");
return "adminLoginError";
} else {
ActionContext ac = ActionContext.getContext();
Map<String,Object> session = ac.getSession();
session.put("user", user);
session.put("phonetypeList", phonetypeList);
session.put("messageNum", messageNum);
System.out.println("messageNum:"+messageNum);
return "adminLogin";
}
}
public String userLogin() {
User user=userService.findUserByuserName(userName);
if(user==null){
this.addFieldError("userName", "用戶不存在");
return "userLoginError";
}else if(!user.getUserPassword().equals(userPassword)){
this.addFieldError("password", "密码不正确");
return "userLoginError";
}else{
ActionContext ac = ActionContext.getContext();
Map<String,Object> session = ac.getSession();
session.put("user", user);
phone1List=phoneService.findPhone1List();
phonetypeList=phonetypeService.findPtList();
session.put("phonetypeList", phonetypeList);
return "userLogin";
}
}
public String register() throws IOException {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
String checkCode= (String)session.get("checkCode");
System.out.println("验证码图片上的字:"+checkCode+"用户输入的验证码:"+checkCodeUser);
if(checkCode.equals(checkCodeUser)){
User user = new User();
user.setUserName(userName);
user.setUserPassword(userPassword);
userService.addUser(user);
return "register";
}else{
YZMError = "验证码错误";
return "registerError";
}
}
public String checkName() throws IOException {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String responseText="";
if(userService.findUserByuserName(userName)!=null){
responseText="用户名已经被使用";
}else{
responseText="用户名可以使用";
}
out.print(responseText);
out.flush();
out.close();
return "input";
}
public String findUserByUserId(){
System.out.println(this.getUserId());
user = userService.findUserByuserId(userId);
System.out.println(user.getUserName()+"111");
return "findUserByUserId";
}
public String findUserByUserIdHou(){
System.out.println(this.getUserId());
user = userService.findUserByuserId(userId);
return "findUserByUserIdHou";
}
public String improve() throws IOException{//完善个人信息
user = userService.findUserByuserName(userName);
user.setUserPassword(userPassword);
user.setUserSex(userSex);
user.setUserAge(userAge);
user.setUserAddress(userAddress);
user.setUserPostalCode(userPostalCode);
user.setPhoneNum(phoneNum);
SimpleDateFormat farmat =new SimpleDateFormat("yyyyMMddhhmmssSSS");
String photoName=farmat.format(new Date())+".jpg";
String path = ServletActionContext.getServletContext().getRealPath("/") + "\\upload\\";
File saveFile=new File(new File(path),photoName);
FileUtils.copyFile(photo, saveFile);
System.out.println(photoName);
user.setPhotoName(photoName);
userService.updateUser(user);
return "improve";
}
public String improveHou() throws IOException{//完善个人信息
user = userService.findUserByuserName(userName);
user.setUserPassword(userPassword);
user.setUserSex(userSex);
user.setUserAge(userAge);
user.setUserAddress(userAddress);
user.setUserPostalCode(userPostalCode);
user.setPhoneNum(phoneNum);
SimpleDateFormat farmat =new SimpleDateFormat("yyyyMMddhhmmssSSS");
String photoName=farmat.format(new Date())+".jpg";
String path = ServletActionContext.getServletContext().getRealPath("/") + "\\upload\\";
File saveFile=new File(new File(path),photoName);
FileUtils.copyFile(photo, saveFile);
System.out.println(photoName);
user.setPhotoName(photoName);
userService.updateUser(user);
return "improveHou";
}
public String exit(){
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
session.remove("user");
return "exit";
}
public String exitQian(){
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
session.remove("user");
return "exitQian";
}
public String findUserList(){
String hql="from User";
int pageSize=5;
pageUtil = new PageUtil();
int allRows = (int) userService.getCount();//获得总的数据个数
int totalPage = pageUtil.getTotalPages(pageSize,allRows);//获得总页数
if(Page>totalPage){
Page=totalPage;
}
int currentPage = pageUtil.getCurPage(Page);
int offest = pageUtil.getCurrentPageOffset(pageSize, currentPage);//得到当前开始的那个数据
//所有用户信息列表
userList =userService.findUserList(hql, offest, pageSize);//返回当前的那个和一共范围多少个
pageUtil.setAllRows(allRows);
pageUtil.setTotalPage(totalPage);
pageUtil.setCurrentPage(currentPage);
this.setPageUtil(pageUtil);
return "findUserList";
}
public String deleteUserById(){
user=userService.findUserByuserId(userId);
userService.deleteUser(user);
return "deleteUserById";
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
p