package com.lgx8.management.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.lgx8.common.PageArgument;
import com.lgx8.common.PageList;
import com.lgx8.common.servlet.BaseController;
import com.lgx8.common.util.Escape;
import com.lgx8.common.util.SendSms;
import com.lgx8.gateway.dao.ICardDao;
import com.lgx8.gateway.entities.Card;
import com.lgx8.management.dao.IMerchantDao;
import com.lgx8.management.dao.IOrganizationDao;
import com.lgx8.management.entities.Merchant;
import com.lgx8.management.entities.Organization;
import com.lgx8.management.service.IUserOrganizationMerchantService;
import com.lgx8.right.common.StringUtil;
import com.lgx8.right.dao.IUserDao;
import com.lgx8.right.entities.RunTimeUser;
import com.lgx8.right.entities.User;
import com.lgx8.right.service.IUserCardService;
/**
*
* @author chenbutao
* 说明:所有会员操作包含会员资料修改密码修改查询
* 切记不要多加菜单管理起来很乱一个列表包含一个实体的所有操作
*/
@Controller
public class UserController extends BaseController{
@Resource(name="userOrganizationMerchantService")
IUserOrganizationMerchantService userOrganizationMerchantService;
@Resource(name="userDao")
IUserDao userDao;
@Resource(name="cardDao")
ICardDao cardDao;
@Autowired
IUserCardService userCardService;
@Resource(name="merchantDao")
IMerchantDao merchantDao;
@Resource(name="organizationDao")
IOrganizationDao organizationDao;
/**
* 会员查询(所有会员查询入口)
* @param request
* @param response
* @return
*/
@RequestMapping("/member/memberPageQuery.do")
public ModelAndView memberPageQuery(HttpServletRequest request,HttpServletResponse response){
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
PageArgument pageArgment = getPageInfo(request);
String cardid = request.getParameter("cardid");
String mobile = request.getParameter("mobile");
String merchant = request.getParameter("merchant");
String truename = request.getParameter("truename");
String stime = request.getParameter("stime");
String etime = request.getParameter("etime");
long orid = 0;
long type = rtUser.getUsertype();
if(type==2){
Merchant or = (Merchant)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
if(or!=null){
orid = or.getID();
}
}
if(type==3){
Organization or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
if(or!=null){
orid = or.getID();
}
}
//if usertype=6l该用户为员工查询所有usertype=1的用户
UserQueryForm queryForm = new UserQueryForm();
queryForm.setCardid(cardid);
queryForm.setEtime(etime);
queryForm.setStime(stime);
queryForm.setTruename(truename);
queryForm.setMobile(mobile);
queryForm.setOrganizationid(orid);
queryForm.setParenttype(type);
queryForm.setMerchant(merchant);
PageList list = userDao.findUserByConditions(queryForm.toHql(), queryForm.getPara(), pageArgment);
printJSON(response, list.toJSON());
return null;
}
@RequestMapping("/member/modifyUserIni.do")
public ModelAndView modifyUserIni(HttpServletRequest request,HttpServletResponse response) {
String uid = request.getParameter("id");//商家id
User user = userDao.findUserById(Long.parseLong(uid));
return new ModelAndView("/management/member/update_member").addObject("user", user);
}
@RequestMapping("/member/addUser.do")
public ModelAndView addUserIni(HttpServletRequest request,HttpServletResponse response) {
/**
* 初始化添加页面数据暂未确定以后完善
*/
return new ModelAndView("/management/member/add_member");
}
@RequestMapping("/member/delUser.do")
public ModelAndView delUser(HttpServletRequest request,HttpServletResponse response) {
//准备删除的会员列表
String[] delUserIds = this.getKeyStoreArray(request);
if (delUserIds.length > 0) {
for (int i = 0 ; i < delUserIds.length ; i++) {
userCardService.delUserById(Long.parseLong(delUserIds[i]));
}
}
this.clearKeyStore(request);
return new ModelAndView(new RedirectView("../page/management/member/list_member.jsp"));
}
@RequestMapping("/member/resetUserPassword.do")
public ModelAndView resetUserPassword(HttpServletRequest request,HttpServletResponse response) {
//准备删除的会员列表
String[] delUserIds = this.getKeyStoreArray(request);
String password = request.getParameter("password");
if(password == null && "".equals(password))
{
password = "123456";
}
if (delUserIds.length > 0) {
for (int i = 0 ; i < delUserIds.length ; i++) {
userDao.resetUserPassword(Long.parseLong(delUserIds[i]), password);
}
}
this.clearKeyStore(request);
return new ModelAndView(new RedirectView("../page/management/member/list_member.jsp"));
}
@RequestMapping("/member/lockOrUnLockUser.do")
public ModelAndView lockOrUnLockUser(HttpServletRequest request,HttpServletResponse response) {
//准备删除的会员列表
String[] delUserIds = this.getKeyStoreArray(request);
String locked = request.getParameter("locked");
Boolean flag = true;
if("true".equals(locked))
{
flag = true;
}else
{
flag = false;
}
if (delUserIds.length > 0) {
for (int i = 0 ; i < delUserIds.length ; i++) {
userDao.lockUser(Long.parseLong(delUserIds[i]), flag);
}
}
this.clearKeyStore(request);
return new ModelAndView(new RedirectView("../page/management/member/list_member.jsp"));
}
/**
* 会员开卡操作
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping("/member/saveUser.do")
public ModelAndView saveUser(HttpServletRequest request,HttpServletResponse response) throws IOException {
String uid = request.getParameter("id");//获取当前用户id 用于修改用户基本信息
String email = request.getParameter("email");
String password = request.getParameter("password");
String truename = request.getParameter("truename");
String sex = request.getParameter("sex");
String mobile = request.getParameter("mobile");
String posturl = request.getParameter("posturl");
String cardid = request.getParameter("cardid");
if (posturl != null && !"".equals(posturl)) {
User user = userCardService.findUserById(Long.parseLong(uid));
user.setEmail(email);
user.setMobile(mobile);
user.setTruename(truename);
userCardService.updateUser(user);
return new ModelAndView(posturl).addObject("ErrMsg", "修改成功");
}
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
if (uid != null && !"".equals(uid)) {
User user = userCardService.findUserById(Long.parseLong(uid));
user.setEmail(email);
user.setSex(Integer.parseInt(sex));
user.setMobile(mobile);
user.setTruename(truename);
userCardService.updateUser(user);
return new ModelAndView(new RedirectView("../page/management/member/list_member.jsp"));
}
long orid = 0;
long type = 0l;
if(rtUser!=null){
type = rtUser.getUsertype();
if(type==2){
Merchant or = (Merchant)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
if(or!=null){
orid = or.getID();
}
}
if(type==3){
Organization or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
if(or!=null){
orid = or.getID();
}
}
}
if(orid!=0){
User user = new User();
user.setEmail(email);
user.setOpener(rtUser);
user.setRegistertime(new Date());
user.setLastlogin(new Date());
user.setUsername(mobile);
user.setPassword(password);
user.setTruename(truename);
user.setSex(Integer.parseInt(sex));
user.setMobile(mobile);
user.setUsertype(User.USER_TYPE_NORMAL);
user.setParenttype(type);
user.setOrganizationid(orid);
user.setEnabled(true);
user.setLocked(false);
user.setCardEnd("0");
user.setCardStart("0");
user.setCurMaxCard("0");
// userCardService.createUser(user);
// User ru = userDao.findUserById(rtUser.getId());
// String cardStart = ru.getCardStart();
// String cardCur = ru.getCurMaxCard();
// if("0".equals(cardCur))
// {
// cardCur = cardStart;
// }else
// {
// cardCur = Long.parseLong(cardCur)+1l+"";
// }
Card card = cardDao.findCard(cardid);
if(card == null )card = cardDao.createCard(cardid);
userCardService.createUser(user,card);
// ru.setCurMaxCard(cardCur);
// userCardService.updateUser(ru);
//当开卡成功后发短信到手机
//尊敬的会员您的乐共享登录名13036780222,卡/号818800010022密/码abc123,请登录www.lgx8.com【乐共享】
String content = "尊敬的会员您的乐共享登录名"+user.getUsername()+",卡/号"+user.getCard().getId()+"密/码"+password+",请登录www.lgx8.com";
SendSms.sendSms(user.getMobile(), content);
}
return new ModelAndView(new RedirectView("../page/management/member/list_member.jsp"));
}
@RequestMapping("/member/queryUsers.do")
public ModelAndView queryUsers(HttpServletRequest request,HttpServletResponse response){
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
PageArgument pageArgment = getPageInfo(request);
String type = request.getParameter("type");
String username = request.getParameter("username");
String merchantname = request.getParameter("merchantname");
String hql = " ";
if("2".equals(type))
{
if(rtUser.getUsertype() == 2)
{
hql = " select u from User u, Merchant m where u.enabled = true and u.locked = false and u.usertype = 2 and m.ID = u.organizationid and m.SJFRDB.id = u.id";
if(!StringUtil.isEmptyStr(merchantname))
{
hql += " and m.SJMC like '%"+merchantname+"%'";
}
hql += " order by u.id desc";
}else if(rtUser.getUsertype() == 3)
{
hql = " select u from User u, Organization o where u.enabled = true and u.locked = false and u.usertype = 3 and o.ID = u.organizationid and o.jgfzr.id = u.id";
if(!StringUtil.isEmptyStr(merchantname))
{
hql += " and o.JGMC like '%"+merchantname+"%'";
}
hql += " order by u.id desc";
}else
{
hql = " select u from User u, Merchant m where u.enabled = true and u.locked = false and u.usertype = 2 and m.ID = u.organizationid and m.SJFRDB.id = u.id";
if(!StringUtil.isEmptyStr(merchantname))
{
hql += " and m.SJMC like '%"+merchantname+"%'";
}
hql += " order by u.id desc";
hql += " union select u from User u, Organization o where u.enabled = true and u.locked = false and u.usertype = 3 and o.ID = u.organizationid and o.jgfzr.id = u.id";
if(!StringUtil.isEmptyStr(merchantname))
{
hql += " and o.JGMC like '%"+merchantname+"%'";
}
hql += " order by u.id desc";
}
}else
{
hql = " from User u where u.enabled = true and u.locked = false";
if(!StringUtil.isEmptyStr(username))
{
hql += " and (u.username like '%"+username+"%' or u.nickname like '%"+username+"%' or u.truename like '%"+username+"%' or u.card.id like '%"+username+"%')";
}
if(rtUser != null && (rtUser.isMerchant()||rtUser.isOrganization()) )
{
hql += " and u.organizationid = " + rtUser.getOrganizationid() + " and u.parent.id = " +rtUser.getId() ;
}
if(!StringUtil.isEmptyStr(merchantname))
{
hql += " and (exists (from Merchant m where m.ID= u.organizationid and m.SJMC like '%"+merchantname+"%' ) or";
hql += " exists (from Organization o where o.ID= u.organizationid and o.JGMC like '%"+merchantname+"%' ))";
}
hql += " order by u.id desc";
}
PageList list = userDao.findUserByConditions(hql, new Object[]{}, pageArgment);
@SuppressWarnings("unchecked")
List<User> users = list.getDataList();
for(User u : users)
{
if(u.getOrganizationid() != 0)
{
if(u.getUsertype() == 2)
{
Merchant m = merchantDao.getMerchantById(u.getOrganizationid());
u.setMerchantname(m.getSJMC());
}else if(u.getUsertype() == 3)
{
Organization o = organizationDao.getOrganizationById(u.getOrganizationid());
u.setMerchantname(o.getJGMC());
}
}
}
list.setDataList(users);
printJSON(response, list.toJSON());
return null;
}
@RequestMapping("/user/selectUserInfo.do")
public ModelAndView selectUserInfo(HttpServletRequest request,HttpServletResponse response) throws IOException{
//准备删除的推荐
BaseController bc = new BaseController();
String[] entityIds = bc.getKeyStoreArray(request);
String type = request.getParameter("type");
String uids = "";
String users = "";
String oids = "";
String orgs = "";
String uts = "";
if (entityIds.length > 0) {
for (int i = 0 ; i < entityIds.length ; i++) {
if(!StringUtil.isEmptyStr(uids)) uids += ",";
uids += entityIds[i];
}
List<User> us = userDao.listUser("from User u where id in ("+uids+")");
if("2".equals(type))
{
for(User u : us)
{
if(u.getOrganizationid() != 0)
{
if(u.getUsertype() == 2)
{
Merchant m = merchantDao.getMerchantById(u.getOrganizationid());
u.setMerchantname(m.getSJMC());
}else if(u.getUsertype() == 3)
{
Organization o = organizationDao.getOrganizationById(u.getOrganizationid());
u.setMerchantname(o.getJGMC());
}
}
}
}
for(User u : us)
{
if(!StringUtil.isEmptyStr(users)) users += ",";
users += u.getTruename();
if("2".equals(type) && (u.getUsertype() == 2 || u.getUsertype() == 3))
{
if(!StringUtil.isEmptyStr(oids)) oids += ",";
oids += u.getOrganizationid()+"";
if(!StringUtil.isEmptyStr(orgs)) orgs += ",";
orgs += u.getMerchantname()+"";
if(!StringUtil.isEmptyStr(orgs)) uts += ",";
uts += u.getUsertype()+"";
}
}
}
bc.clearKeyStore(request);
PrintWriter pw = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
pw.write(uids+":"+Escape.escape(users)+":"+oids+":"+Escape.escape(orgs)+":"+uts);
return null;
}
@RequestMapping("/member/queryMerOrgUsers.do")
public ModelAndView queryMerOrgUsers(HttpServletRequest request,HttpServletResponse response){
PageArgument pageArgment = getPageInfo(request);
String type = request.getParameter("type");
String username = request.getParameter("username");
String orgid = request.getParameter("orgid");
String hql = " select u from User u where u.enabled = true";
User u = null;
if("2".equals(type))
{
Merchant merchant = merchantDao.getMerchantById(Long.parseLong(orgid));
if(merchant != null)
{
u = merchant.getSJFRDB();
}
}else if("3".equals(type))
{
Organization org = organizationDao.getOrganizationById(Long.parseLong(orgid));
if(org != null)
{
u = org.getJgfzr();
}
}
if(u != null)
{
String cardStart = u.getCardStart();
String cardEnd = u.getCardEnd();
String[] css = cardStart.split(",");
String[] ces = cardEnd.split(",");
if(css.length >0)
{
String h1 = "";
for(int i=0; i < css.length ; i++)
{
if(!StringUtil.isEmptyStr(h1)) h1 += " or ";
h1 += " (u.card.id between "+css[i]+" and "+ces[i]+")";
}
hql += " and ("+h1+")";
}
}
if(!StringUtil.isEmptyStr(username))
{
hql += " and (u.username like '%"+username+"%' or u.nickname like '%"+username+"%' or u.truename like '%"+username+"%' or u.card.id like '%"+username+"%')";
}
hql += " order by u.id desc";
PageList list = userDao.findUserByConditions(hql, new Object[]{}, pageArgment);
printJSON(response, list.toJSON());
return null;
}
}