package com.lgx8.management.action;
import java.util.ArrayList;
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.StrUtil;
import com.lgx8.management.dao.IOrganizationDao;
import com.lgx8.management.entities.Organization;
import com.lgx8.management.service.IUserOrganizationMerchantService;
import com.lgx8.right.common.EncryptUtil;
import com.lgx8.right.dao.IUserDao;
import com.lgx8.right.entities.RunTimeUser;
import com.lgx8.right.entities.User;
import com.lgx8.right.service.IUserCardService;
@Controller
public class OrganizationAction extends BaseController {
@Autowired
IOrganizationDao organizationDao;
@Autowired
IUserCardService userCardService;
@Resource(name="userDao")
IUserDao userDao;
@Autowired
IUserOrganizationMerchantService userOrganizationMerchantService;
@RequestMapping("/initmodifyor.do")
public String initOriganization(HttpServletRequest request,HttpServletResponse response){
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
if(rtUser!=null){
long _oid = rtUser.getOrganizationid();
Organization or = organizationDao.getOrganizationById(_oid);
request.setAttribute("organization", or);
request.setAttribute("user", rtUser);
}
return "/management/organization/update_organization";
}
@RequestMapping("/savemodifyor.do")
public String saveOriganization(HttpServletRequest request,HttpServletResponse response){
String u_id = request.getParameter("u_id");
String password_new = request.getParameter("new_password");
String userName = request.getParameter("userName");
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
if(u_id!=null&&u_id.length()!=0&&rtUser!=null){
User u = userCardService.findUserById(Integer.parseInt(u_id));
u.setUsername(userName);
u.setPassword(EncryptUtil.toMD5(password_new));
userCardService.updateUser(u);
long _oid = u.getOrganizationid();
Organization or = organizationDao.getOrganizationById(_oid);
String sid = request.getSession().getId();
request.getSession().removeAttribute(sid);
request.setAttribute("user", u);
request.setAttribute("organization", or);
request.getSession().setAttribute(sid, new RunTimeUser(u));
}
return "/management/organization/result_organization";
}
@RequestMapping("/listmerchant.do")
public ModelAndView listMerchant(HttpServletRequest request,HttpServletResponse response){
String st_time = request.getParameter("tj_st");
String ed_time = request.getParameter("tj_ed");
PageArgument pageArgment = getPageInfo(request);
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
long _orid = 0;
if(rtUser!=null){
long _type = rtUser.getUsertype();
if(_type==3){
Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
if(_or!=null){
_orid = _or.getID();
}
}
}
if(_orid!=0){
String hql = "";
Object[] o = null;
if(st_time!=null&&st_time.length()!=0){
if(ed_time!=null&&ed_time.length()!=0){
hql = " from Merchant where DELETEFLAG = 0 and ssjgbh=? sjtjsj>=str_to_date(?,'%Y-%m-%d %H:%i:%s') and sjtjsj<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by sjtjsj desc";
o = new Object[]{rtUser.getId(),st_time,ed_time};
}else{
hql = " from Merchant where DELETEFLAG = 0 and ssjgbh=? sjtjsj>=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by sjtjsj desc";
o = new Object[]{_orid,st_time};
}
}else{
if(ed_time!=null&&ed_time.length()!=0){
hql = " from Merchant where DELETEFLAG = 0 and ssjgbh=? sjtjsj<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by sjtjsj desc";
o = new Object[]{_orid,ed_time};
}else{
hql = " from Merchant where DELETEFLAG = 0 and ssjgbh=? order by sjtjsj desc";
o = new Object[]{_orid};
}
}
PageList list = userOrganizationMerchantService.listMerchantByConditions(hql, o, pageArgment);
printJSON(response, list.toJSON());
}
return null;
}
@RequestMapping("/listorganization.do")
public ModelAndView listOrgnization(HttpServletRequest request,HttpServletResponse response){
String st_time = request.getParameter("tj_st");
String ed_time = request.getParameter("tj_ed");
PageArgument pageArgment = getPageInfo(request);
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
long _orid = 0;
if(rtUser!=null){
long _type = rtUser.getUsertype();
if(_type==3){
Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
if(_or!=null){
_orid = _or.getID();
}
}
}
if(_orid!=0){
String hql = "";
Object[] o = null;
if(st_time!=null&&st_time.length()!=0){
if(ed_time!=null&&ed_time.length()!=0){
hql = " from Organization where DELETEFLAG = 0 and jgssjg=? jgtjsj>=str_to_date(?,'%Y-%m-%d %H:%i:%s') and jgtjsj<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by jgtjsj desc";
o = new Object[]{rtUser.getId(),st_time,ed_time};
}else{
hql = " from Organization where DELETEFLAG = 0 and jgssjg=? jgtjsj>=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by jgtjsj desc";
o = new Object[]{_orid,st_time};
}
}else{
if(ed_time!=null&&ed_time.length()!=0){
hql = " from Organization where DELETEFLAG = 0 and jgssjg=? jgtjsj<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by jgtjsj desc";
o = new Object[]{_orid,ed_time};
}else{
hql = " from Organization where DELETEFLAG = 0 and jgssjg=? order by jgtjsj desc";
o = new Object[]{_orid};
}
}
PageList list = userOrganizationMerchantService.listOrganizationByConditions(hql, o, pageArgment);
printJSON(response, list.toJSON());
}
return null;
}
@RequestMapping("/organization/listorganization.do")
public ModelAndView listAllOrgnization(HttpServletRequest request,HttpServletResponse response){
String st_time = request.getParameter("tj_st");
String ed_time = request.getParameter("tj_ed");
PageArgument pageArgment = getPageInfo(request);
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
StringBuffer sb = new StringBuffer();
List<Object> o = new ArrayList<Object> ();
if(st_time!=null&&st_time.length()>0){
sb.append(" and jgtjsj>=str_to_date(?,'%Y-%m-%d %H:%i:%s')");
o.add(st_time);
}
if(ed_time!=null&&ed_time.length()>0){
sb.append(" and jgtjsj<=str_to_date(?,'%Y-%m-%d %H:%i:%s')");
o.add(ed_time);
}
if(rtUser!=null&&rtUser.getUsertype()!=User.USER_TYPE_NORMAL&&
rtUser.getUsertype()!=User.USER_TYPE_MERCHANT&&rtUser.getUsertype()!=User.USER_TYPE_TGSTATION){
String hql = "";
long _type = rtUser.getUsertype();
if(_type==User.USER_TYPE_TGCENTER||_type==User.USER_TYPE_YYCENTER){
Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
if(_or!=null){
sb.append(" and jgssjg="+_or.getID());
}
}
if(!sb.toString().equals("")){
hql = " from Organization where DELETEFLAG = '0' and "+sb.toString().substring(4)+" order by jgtjsj desc";
}else{
hql = " from Organization where DELETEFLAG = '0' order by jgtjsj desc";
}
PageList list = userOrganizationMerchantService.listOrganizationByConditions(hql, o.toArray(), pageArgment);
printJSON(response, list.toJSON());
}
return null;
}
@RequestMapping("/organization/addOrganizationInit.do")
public ModelAndView createOrganizationInit(HttpServletRequest request,HttpServletResponse response)
{
List<Organization> orgs = organizationDao.listAllOrganizations(null);
request.setAttribute("orgs", orgs);
return new ModelAndView("/management/organization/modify_organization");
}
@RequestMapping("/organization/addOrganization.do")
public ModelAndView createOrganization(HttpServletRequest request,HttpServletResponse response)
{
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
String jgmc = request.getParameter("JGMC");
String jgid = request.getParameter("JGID");
String jgszqy = request.getParameter("JGSZQY");
String jgfzr = request.getParameter("Jgfzr");
String jgdh = request.getParameter("JGDH");
String jgszdz = request.getParameter("JGSZDZ");
String jglx = request.getParameter("JGLX");
String ssjg = request.getParameter("SSJG");
String posturl = request.getParameter("posturl");
if (!StrUtil.isNullStr(jgid) && !"0".equals(jgid)) {
Organization uo = organizationDao.getOrganizationById(Long.parseLong(jgid));
uo.setJGMC(jgmc);
uo.setJGSZDZ(jgszdz);
uo.setJGDH(jgdh);
uo.setJGSZQY(jgszqy);
uo.setJGLX(jglx);
//所属机构
if(!StrUtil.isNullStr(ssjg) && !"0".equals(ssjg))
{
Organization so = organizationDao.getOrganizationById(Long.parseLong(ssjg));
uo.setSsjg(so);
}
//负责人
User u = null;
if(!StrUtil.isNullStr(jgfzr))
{
u = userDao.findUserByUserName(jgfzr);
if(uo.getJgfzr() != null)
{
if(u.getId() != uo.getJgfzr().getId())
{
u.setUsertype(Long.parseLong(jglx));
}
uo.setJgfzr(u);
}
}
organizationDao.updateOrganization(uo);
if (!StrUtil.isNullStr(posturl)){
return new ModelAndView(posturl).addObject("ErrMsg", "修改成功");
}
return new ModelAndView(new RedirectView("../page/management/organization/list_organization.jsp"));
}
Organization org = new Organization();
org.setJGMC(jgmc);
org.setJGDH(jgdh);
org.setJGSZDZ(jgszdz);
org.setJGSZQY(jgszqy);
org.setJGLX(jglx);
org.setJGTJR(rtUser);
org.setJGTJSJ(new Date());
org.setDELETEFLAG("0");
User u = null;
if(!StrUtil.isNullStr(jgfzr))
{
u = userDao.findUserByUserName(jgfzr);
u.setUsertype(Long.parseLong(jglx));
org.setJgfzr(u);
}
if(!StrUtil.isNullStr(ssjg) && !"0".equals(ssjg))
{
Organization sj = organizationDao.getOrganizationById(Long.parseLong(ssjg));
org.setSsjg(sj);
}
organizationDao.createOrganization(org);
if(u != null)
{
u.setOrganizationid(org.getID());
userDao.updateUser(u);
}
return new ModelAndView(new RedirectView("../page/management/organization/list_organization.jsp"));
}
@RequestMapping("/organization/delOrganization.do")
public ModelAndView delOrganization(HttpServletRequest request,HttpServletResponse response) {
String keyid= request.getParameter("keyid");
if (!"".equals(keyid)) {
String[] keyArray = keyid.split(",");
for (int i = 0 ; i < keyArray.length ; i++) {
Organization m = organizationDao.getOrganizationById(Long.parseLong(keyArray[i]));
if (m != null )
{
m.setDELETEFLAG("1");
organizationDao.updateOrganization(m);
}
}
}
return new ModelAndView(new RedirectView("../page/management/organization/list_organization.jsp"));
}
@RequestMapping("/organization/modifyOrganizationInit.do")
public ModelAndView modifyOrganizationInit(HttpServletRequest request,HttpServletResponse response) {
String SJID = request.getParameter("id");//商家id
Organization or = organizationDao.getOrganizationById(Long.parseLong(SJID));
List<Organization> orgs = organizationDao.listAllOrganizations(SJID);
request.setAttribute("orgs", orgs);
return new ModelAndView("/management/organization/modify_organization").addObject("organization", or);
}
@RequestMapping("/organization/setOrganizationCard.do")
public ModelAndView setOrganizationCard(HttpServletRequest request,HttpServletResponse response) {
String SJID = request.getParameter("id");//商家id
Organization or = organizationDao.getOrganizationById(Long.parseLong(SJID));
List<Organization> orgs = organizationDao.listAllOrganizations(SJID);
request.setAttribute("orgs", orgs);
return new ModelAndView("/management/organization/setcard_organization").addObject("organization", or);
}
}