package com.kq.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.kq.entity.Customer;
import com.kq.entity.UserInf;
import com.kq.service.UserService;
import com.kq.utils.AjaxJson;
import com.kq.utils.DataResult;
import com.kq.utils.MyDataResult;
@Controller
@RequestMapping(value = "/UC")
public class UserController {
@Autowired
@Qualifier("UserService")
private UserService UserService;
/**
* 插入一个新管理员用户
*
* @param UserInf
* @return
*/
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
@ResponseBody
public AjaxJson addUser(UserInf UserInf) {
try {
AjaxJson json = new AjaxJson();
// UserInf.setCreatedate(UtilsHelper.getDateFormatTime());// 设置管理员创建时间
UserService.addUser(UserInf);
json.setSuccess(true);
return json;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
AjaxJson json = new AjaxJson();
json.setSuccess(false);
return json;
}
}
/**
* 插入一个客户用户
*
* @param UserInf
* @return
*/
@RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
@ResponseBody
public AjaxJson addCustomer(Customer customer) {
try {
AjaxJson json = new AjaxJson();
UserService.addCustomer(customer);
json.setSuccess(true);
return json;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
AjaxJson json = new AjaxJson();
json.setSuccess(false);
return json;
}
}
/**
* 根据管理员loginname查询管理员全部信息
*/
@RequestMapping(value = "/selectById", method = RequestMethod.POST)
@ResponseBody
public AjaxJson selectUserById(int id) {
try {
UserInf UserInf = new UserInf();
UserInf = UserService.selectUserById(id);
if (UserInf != null) {
AjaxJson json=new AjaxJson();
json.setObj(UserInf);
json.setSuccess(true);
json.setMsg("查询成功");
return json;
} else {
AjaxJson json=new AjaxJson();
json.setMsg("查无此人");
json.setSuccess(false);
return json;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
AjaxJson json=new AjaxJson();
json.setSuccess(false);
json.setMsg("系统异常");
return json;
}
}
/**
* 管理员信息修改
*/
@RequestMapping(value = "/updataById", method = RequestMethod.POST)
public @ResponseBody AjaxJson updateUserById(UserInf UserInf) {
AjaxJson json = new AjaxJson();
try {
UserService.updateUserById(UserInf);
json.setSuccess(true);
return json;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
json.setSuccess(false);
return json;
}
}
/**
* 客户信息修改
*/
@RequestMapping(value = "/updataCustomerById", method = RequestMethod.POST)
public @ResponseBody AjaxJson updateCustomerById(Customer Customer) {
AjaxJson json = new AjaxJson();
try {
UserService.updateCustomerById(Customer);
json.setSuccess(true);
return json;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
json.setSuccess(false);
return json;
}
}
/**
* 普通员工修改客户信息
*/
@RequestMapping(value = "/updateCustomerByIdGeneral", method = RequestMethod.POST)
public @ResponseBody AjaxJson updateCustomerByIdgeneral(Customer Customer,HttpSession session) {
AjaxJson json = new AjaxJson();
try {
if(Customer.getCreator().equals(session.getAttribute("user").toString())) {
UserService.updateCustomerById(Customer);
json.setSuccess(true);
}else {
json.setSuccess(false);
json.setMsg("无法修改其他归属人员的数据!");
}
return json;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
json.setSuccess(false);
return json;
}
}
/**
* 展示全部用户信息
* @return ArrayList<StudentInfo>
*/
@ResponseBody
@RequestMapping(value = "/showAll", method = RequestMethod.POST)
public AjaxJson show_all_student_info(String page,String rows,String name,String value,HttpSession session){
AjaxJson json = new AjaxJson();
MyDataResult result1;
if(page==null||rows==null){
result1=new MyDataResult(1,10,name,value,session.getAttribute("user").toString());
}else{
result1=new MyDataResult(Integer.parseInt(page), Integer.parseInt(rows),name,value,session.getAttribute("user").toString());
}
List<Map<String, Object>> Users =UserService.selectAllUser(result1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("rows", Users);
DataResult result=new DataResult();
result.setTotalSize(UserService.findUserCount(result1));
json.setAttributes(map);
json.setSuccess(true);
json.setObj(result);
return json;
}
/**
* 展示全部客户信息
* @return ArrayList<StudentInfo>
*/
@ResponseBody
@RequestMapping(value = "/showAllCustomer", method = RequestMethod.POST)
public AjaxJson show_all_student_info_Customer(String page,String rows,String name,String value,HttpSession session){
AjaxJson json = new AjaxJson();
MyDataResult result1;
if(page==null||rows==null){
result1=new MyDataResult(1,10,name,value,session.getAttribute("user").toString());
}else{
result1=new MyDataResult(Integer.parseInt(page), Integer.parseInt(rows),name,value,session.getAttribute("user").toString());
}
List<Map<String, Object>> Customer =UserService.selectAllCustomer(result1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("rows", Customer);
DataResult result=new DataResult();
result.setTotalSize(UserService.findCustomerCount(result1));
json.setAttributes(map);
json.setSuccess(true);
json.setObj(result);
return json;
}
/**
* 根据登录人展示部分客户信息
* @return ArrayList<StudentInfo>
*/
@ResponseBody
@RequestMapping(value = "/showCustomerByCreator", method = RequestMethod.POST)
public AjaxJson showCustomerByCreator(String page,String rows,String name,String value,HttpSession session)