import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.easyjf.example.business.IUser;
import com.easyjf.example.business.IUserService;
import com.easyjf.util.CommUtil;
import com.easyjf.web.tools.IPageList;
import com.easyjf.web.tools.ListQuery;
import com.easyjf.web.tools.PageList;
import com.opensymphony.webwork.interceptor.ParameterAware;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Preparable;
public class UserManageAction extends ActionSupport implements ParameterAware,Preparable {
private List allUser;
private IUserService userService;
private IUser user;
private Map parameters;
//用户查询
public String query() throws Exception {
String[] temp=(String[])parameters.get("page");
int currentPage=CommUtil.null2Int(temp!=null?temp[0]:"0");
temp=(String[])parameters.get("pageSize");
int pageSize=CommUtil.null2Int(temp!=null?temp[0]:"0");
if(currentPage<1)currentPage=1;
if(pageSize<1)pageSize=10;
String scope="1=1";
Collection paras=new ArrayList();
temp=(String[])parameters.get("orderType");
String orderType=CommUtil.null2String(temp!=null?temp[0]:"");
temp=(String[])parameters.get("orderField");
String orderField=CommUtil.null2String(temp!=null?temp[0]:"");
temp=(String[])parameters.get("queryUserName");
String userName=CommUtil.null2String(temp!=null?temp[0]:"");
temp=(String[])parameters.get("queryTel");
String tel=CommUtil.null2String(temp!=null?temp[0]:"");
if(!userName.equals(""))
{
scope+=" and userName like ?";
paras.add("%"+userName+"%");
}
if(!tel.equals(""))
{
scope+=" and tel like ?";
paras.add("%"+tel+"%");
}
if(orderField.equals(""))//默认按用户名排序
{
orderField="userName";
orderType="desc";
}
if(!orderField.equals(""))
{
scope +=" order by "+orderField;
if(!orderType.equals(""))scope+=" "+orderType;
}
IPageList pList=new PageList(new ListQuery(userService.query(scope,paras)));
pList.doList(pageSize,currentPage,"","");
if(pList!=null){
this.parameters.put("list",pList.getResult());
this.parameters.put("pages",new Integer(pList.getPages()));
this.parameters.put("rows",new Integer(pList.getRowCount()));
this.parameters.put("page",new Integer(pList.getCurrentPage()));
this.parameters.put("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages()));
}
this.parameters.put("orderType",orderType);
this.parameters.put("orderField",orderField);
this.allUser = userService.query(scope,paras);
return execute();
}
//添加或修改用户
public String edit() throws Exception
{
return execute();
}
//删除用户
public String del() throws Exception
{
if(user!=null && userService.del(this.user))
{
this.parameters.put("msg","数据删除成功!");
}
else
{
this.parameters.put("msg","数据修改失败");
}
return query();
}
//保存用户信息
public String save() throws Exception
{
IUser obj=this.getUser();
boolean ret=false;
if(obj!=null){
if((obj.getCid()!=null)&&(!obj.getCid().equals("")))ret=userService.update(user);
else ret=userService.save(user);
}
if(ret)
{
this.parameters.put("msg","数据操作成功!");
}
else
{
this.parameters.put("msg","数据操作失败");
}
return query();
}
public IUserService getUserService() {
return userService;
}
public List getAllUser() {
return allUser;
}
public IUser getUser() {
return user;
}
public void setUser(IUser user) {
this.user = user;
}
public void setUserService(IUserService userService)
{
this.userService=userService;
}
public void setParameters(Map parameters) {
this.parameters=parameters;
}
public Map getParameters() {
return parameters;
}
public void prepare() throws Exception {
this.form2Obj();
}
//根据页面参数创建IUser对象
private void form2Obj() {
String[] temp=(String[])parameters.get("cid");
String cid=temp!=null?temp[0]:"";
IUser user=null;
if(cid!=null && (!cid.equals("")))user=userService.read(cid);
if(user==null)user=userService.newUser();
setUser(user);
}
}