Package com.lgx8.management.action

Source Code of com.lgx8.management.action.ManagementAction

package com.lgx8.management.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

import com.lgx8.common.PageArgument;
import com.lgx8.common.PageList;
import com.lgx8.common.servlet.BaseController;
import com.lgx8.common.util.Constants;
import com.lgx8.gateway.dao.ICardDao;
import com.lgx8.gateway.dao.IProductDao;
import com.lgx8.gateway.entities.Card;
import com.lgx8.management.dao.IAdvertisementDao;
import com.lgx8.management.dao.ICustmerXFDao;
import com.lgx8.management.dao.IMerchantDao;
import com.lgx8.management.dao.IRechargehistoryDao;
import com.lgx8.management.entities.Advertisement;
import com.lgx8.management.entities.CustmerConsumeCount;
import com.lgx8.management.entities.Custmerxf;
import com.lgx8.management.entities.Merchant;
import com.lgx8.management.entities.Organization;
import com.lgx8.management.service.CustmerConsumeCountService;
import com.lgx8.management.service.ICustmerXFService;
import com.lgx8.management.service.IUserOrganizationMerchantService;
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 ManagementAction extends BaseController {

  @Autowired
  IProductDao productDao;
  @Autowired
  IUserDao userDao;
  @Autowired
  ICardDao cardDao;
  @Autowired
  IUserCardService userCardService;
  @Autowired
  ICustmerXFDao custmerXFDao;
  @Autowired
  ICustmerXFService custmerService;
  @Autowired
  IRechargehistoryDao rechargehistoryDao;
  @Autowired
  IMerchantDao merchantDao;
  @Autowired
  IAdvertisementDao advertisementDao;
  @Autowired
  IUserOrganizationMerchantService userOrganizationMerchantService;
  @Autowired
  CustmerConsumeCountService custmerConsumeCountService;
  /**
   * 会员查询(适用于各级查询)
   * @param request
   * @param response
   * @return
   */
  @RequestMapping("/listMyMem.do")
  public ModelAndView queryMembersByMerchant(HttpServletRequest request,HttpServletResponse response){
    String st_time = request.getParameter("open_st");
    String ed_time = request.getParameter("open_ed");
    PageArgument pageArgment = getPageInfo(request);
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    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){
      String hql = "";
      Object[] o = null;
      if(st_time!=null&&st_time.length()!=0){
        if(ed_time!=null&&ed_time.length()!=0){
          hql = " from User where organizationid=? and parenttype=? and usertype=1 and registertime>=str_to_date(?,'%Y-%m-%d %H:%i:%s') and registertime<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by registertime desc";
          o = new Object[]{_orid,_type,st_time,ed_time};
        }else{
          hql = " from User where organizationid=? and parenttype=? and usertype=1 and registertime>=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by registertime desc";
          o = new Object[]{_orid,_type,st_time};
        }
      }else{
        if(ed_time!=null&&ed_time.length()!=0){
          hql = " from User where organizationid=? and parenttype=? and usertype=1 and registertime<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by registertime desc";
          o = new Object[]{_orid,_type,ed_time};
        }else{
          hql = " from User where organizationid=? and parenttype=? and usertype=1 order by registertime desc";
          o = new Object[]{_orid,_type};
        }
      }
      PageList list = userDao.findUserByConditions(hql, o, pageArgment);
      printJSON(response, list.toJSON());
    }
    return null;
  }
 
 
  /**
   * 会员开卡操作
   * @param request
   * @param response
   * @return
   */
  @RequestMapping("/savemembercard.do")
  public String saveMemberOpenCard(HttpServletRequest request,HttpServletResponse response){
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String realName = request.getParameter("realName");
    String sex = request.getParameter("sex");
    String phoneNum = request.getParameter("phoneNum");
    String cardNum = request.getParameter("cardNum");
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    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(phoneNum);
      user.setPassword(password);
      user.setTruename(realName);
      user.setSex(Integer.parseInt(sex));
      user.setMobile(phoneNum);
      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");
      if(cardNum!=null&&cardNum.length()!=0){
        Card card = cardDao.findCard(cardNum);
        userCardService.createUser(user,card);
      }else{
//        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.createCard();
        userCardService.createUser(user,card);
       
//        ru.setCurMaxCard(cardCur);
//        userCardService.updateUser(ru);
      }
     
      request.setAttribute("userXX", user);
    }
    return "/management/merchant/result_opencard";
  }
 
  /**
   * 罗列所有消费记录 by merchant
   * @param request
   * @param response
   * @return
   */
  @RequestMapping("/listcustmerxf.do")
  public ModelAndView queryCustmerXFByMerchant(HttpServletRequest request,HttpServletResponse response){
    String st_time = request.getParameter("xf_st");
    String ed_time = request.getParameter("xf_ed");
    String xflx = request.getParameter("xf_hidden_lx");
    String hy_phone = request.getParameter("tel_phone");
    String hy_cardNum = request.getParameter("car_num");
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    PageArgument pageArgment = getPageInfo(request);
    StringBuffer sb = new StringBuffer();
    if(st_time!=null&&!st_time.equals("")){
      sb.append(" and t1.XFSJ>=str_to_date('"+st_time+"','%Y-%m-%d %H:%i:%s')");
    }
    if(ed_time!=null&&!ed_time.equals("")){
      sb.append(" and t1.XFSJ<=str_to_date('"+st_time+"','%Y-%m-%d %H:%i:%s')");
    }
    if(xflx!=null&&!xflx.equals("")){
      sb.append(" and t1.XFLX='"+xflx+"'");
    }
    if(hy_phone!=null&&hy_phone.length()>0){
      sb.append(" and t1.xfhy.mobile='"+hy_phone+"'");
    }
    if(hy_cardNum!=null&&hy_cardNum.length()>0){
      sb.append(" and t1.xfhy.card.id='"+hy_cardNum+"'");
    }
    long _type = 0l;
    if(rtUser!=null){
      _type = rtUser.getUsertype();
      if(_type==User.USER_TYPE_MERCHANT){
        Merchant _or = (Merchant)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
        if(_or!=null){
          sb.append(" and t1.merchant.id="+_or.getID());
        }
      }
     
      if(_type==User.USER_TYPE_TGSTATION){
        Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
        if(_or!=null){
          sb.append(" and t1.xfhy.organizationid="+_or.getID());
        }
      }
      /**
       * 当推广中心和运营中心查询下属联盟店家的消费信息
       */
      if(_type==User.USER_TYPE_TGCENTER||_type==User.USER_TYPE_YYCENTER){
        Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
        if(_or!=null){
          sb.append(" and t1.merchant.organization.id="+_or.getID());
        }
      }
     
      String hql = "";
      Object[] o = new Object[]{};
      if(sb.toString()!=null&&!sb.toString().equals("")){
        hql = " from Custmerxf t1 where "+sb.toString().substring(4)+" order by t1.XFSJ desc";
      }else{
        hql = " from Custmerxf t1 order by t1.XFSJ desc";
      }
      PageList list = custmerXFDao.listCustmerXFByConditions(hql, o, pageArgment);
      printJSON(response, list.toJSON());
    }
    return null;
  }
 
  @RequestMapping("/merchant/listCustmerConsume.do")
  public String queryCustmerConsumeByCondition(HttpServletRequest request,HttpServletResponse response){
    String name = request.getParameter("name");
    User u = RunTimeUser.getRunTimeUser(request);
    if(u.getUsertype()==User.USER_TYPE_MERCHANT){
      long m_id = u.getOrganizationid();
      if(m_id!=0){
        Merchant m = merchantDao.getMerchantById(m_id);
        name = m.getSJMC();
      }
    }
    String st_time = request.getParameter("st_time");
    String ed_time = request.getParameter("ed_time");
    String str_curPage = request.getParameter("curPage");
    int curPage = Integer.parseInt(str_curPage==null?"1":str_curPage);
    List<CustmerConsumeCount> list = custmerConsumeCountService.getCustmerConsumeCountByCondition(name, st_time, ed_time, curPage-1);
    int count = custmerConsumeCountService.getCustmerConsumeCountByCondition(name, st_time, ed_time);
    request.setAttribute("st_time", st_time);
    request.setAttribute("ed_time", ed_time);
    request.setAttribute("name", name);
    request.setAttribute("curPage", str_curPage);
    request.setAttribute("list", list);
    request.setAttribute("count", count+"");
    return "/management/custmerConsume/listCountCustmerConsume";
  }
 
  /**
   *
   */
  @RequestMapping("/listcustmerxf_tgz.do")
  public ModelAndView queryCustmerXFByOrganization(HttpServletRequest request,HttpServletResponse response){
    String st_time = request.getParameter("xf_st");
    String ed_time = request.getParameter("xf_ed");
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    PageArgument pageArgment = getPageInfo(request);
    long _orid = 0;
    long _type = 0l;
    if(rtUser!=null){
      _type = rtUser.getUsertype();
      if(_type==3){
        Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
        if(_or!=null){
          _orid = _or.getID();
        }
      }
    }
    if(_orid!=0&&_type==3){
      String hql = "";
      Object[] o = null;
      if(st_time!=null&&st_time.length()!=0){
        if(ed_time!=null&&ed_time.length()!=0){
          hql = " from Custmerxf t1 where t1.xfhy.usertype=1 and t1.xfhy.parenttype=? and t1.xfhy.parent.id=? and t1.XFSJ>=str_to_date(?,'%Y-%m-%d %H:%i:%s') and t1.XFSJ<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by t1.XFSJ desc";
          o = new Object[]{_type,_orid,st_time,ed_time};
        }else{
          hql = " from Custmerxf t1 where t1.xfhy.usertype=1 and t1.xfhy.parenttype=? and t1.xfhy.parent.id=? and t1.XFSJ>=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by t1.XFSJ desc";
          o = new Object[]{_type,_orid,st_time};
        }
      }else{
        if(ed_time!=null&&ed_time.length()!=0){
          hql = " from Custmerxf t1 where t1.xfhy.usertype=1 and t1.xfhy.parenttype=? and t1.xfhy.parent.id=? and t1.XFSJ<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by t1.XFSJ desc";
          o = new Object[]{_type,_orid,ed_time};
        }else{
          hql = " from Custmerxf t1 where t1.xfhy.usertype=1 and t1.xfhy.parenttype=? and t1.xfhy.parent.id=? order by t1.XFSJ desc";
          o = new Object[]{_type,_orid};
        }
      }
      PageList list = custmerXFDao.listCustmerXFByConditions(hql, o, pageArgment);
      printJSON(response, list.toJSON());
    }
    return null;
  }
 
  /***
   * 保存会员消费操作
   * @param request
   * @param response
   * @return
   * @throws IOException
   */
  @RequestMapping("/savecustmerxf.do")
  public String saveCustmerXF(HttpServletRequest request,HttpServletResponse response) throws IOException{
    String xfje = request.getParameter("xfje");
    String cardNum = request.getParameter("cardNum");
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    if(xfje==null||xfje.equals("")){
      request.setAttribute("custmerxf", null);
    }
    if(cardNum!=null||cardNum.equals("")){
      request.setAttribute("custmerxf", null);
    }
    //long _orid = 0;
    long _type = 0l;
    Merchant _m = null;
    Custmerxf _xf = null;
    if(rtUser!=null){
      _type = rtUser.getUsertype();
      if(_type==2){
        _m = (Merchant)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
      }
    }
    User _u = userDao.findUserByPhone(cardNum,cardNum);
    if(_u!=null&&_m!=null){
      _xf = custmerService.createCustmerXF(_m, _u, Double.parseDouble(xfje), Double.parseDouble(xfje));
      request.setAttribute("custmerxf", _xf);
    }
    response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();
         out.println("<script type=\"text/javascript\">");
         String url = "/management/merchant/list_cutmerxfxx";
         if(_xf != null)
         {
            out.println("alert(\"操作成功!\");");
            url = "/management/merchant/list_cutmerxfxx";
         }else
         {
            out.println("alert(\"操作失败!\");");
            url = "/management/merchant/create_custmerxf";
         }
         out.println("</script>");
         out.write("");
         out.flush();
   
    return url;
  }
 
  @RequestMapping("/listczxx.do")
  public ModelAndView queryCZXX(HttpServletRequest request,HttpServletResponse response){
    String st_time = request.getParameter("cz_st");
    String ed_time = request.getParameter("cz_ed");
    PageArgument pageArgment = getPageInfo(request);
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    String hql = "";
    Object[] o = null;
    if(st_time!=null&&st_time.length()!=0){
      if(ed_time!=null&&ed_time.length()!=0){
        hql = " from Rechargehistory where czyhbh =? and czsjbh=? and czsj>=str_to_date(?,'%Y-%m-%d %H:%i:%s') and xfsj<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by czsj desc";
        o = new Object[]{rtUser.getId(),rtUser.getOrganizationid(),st_time,ed_time};
      }else{
        hql = " from Rechargehistory where czyhbh =? and czsjbh=? and czsj>=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by czsj desc";
        o = new Object[]{rtUser.getId(),rtUser.getOrganizationid(),st_time};
      }
    }else{
      if(ed_time!=null&&ed_time.length()!=0){
        hql = " from Rechargehistory where czyhbh =? and czsjbh=? and czsj<=str_to_date(?,'%Y-%m-%d %H:%i:%s') order by czsj desc";
        o = new Object[]{rtUser.getId(),rtUser.getOrganizationid(),ed_time};
      }else{
        hql = " from Rechargehistory where czyhbh =? and czsjbh=? order by czsj desc";
        o = new Object[]{rtUser.getId(),rtUser.getOrganizationid()};
      }
    }
    PageList list = rechargehistoryDao.listRechargehistoryByConditions(hql, o, pageArgment);
    printJSON(response, list.toJSON());
    return null;
  }
 
  @RequestMapping("/loadmerchantxx.do")
  public String updateMerchatXX(HttpServletRequest request,HttpServletResponse response){
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    if(rtUser!=null){
      long _mid = rtUser.getOrganizationid();
      Merchant _m = merchantDao.getMerchantById(_mid);
      System.out.println(_mid+">>>>>>>>>>>>>>>>>>>");
      request.setAttribute("merchant", _m);
    }
    return "/management/merchant/update_merchant";
  }
 
  @RequestMapping("/savemerchantxx.do")
  public String saveMerchantXX(HttpServletRequest request,HttpServletResponse response){
    String id = request.getParameter("m_id");
    String sjdz = request.getParameter("sjdz");
    String sjdh = request.getParameter("sjdh");
    String sfshsm = request.getParameter("sfshsm");
    String sj_yykssj = request.getParameter("sj_yykssj");
    String sj_yyjssj = request.getParameter("sj_yyjssj");
    String sjfwxj = request.getParameter("sjfwxj");
    int _mid = id==null?0:Integer.parseInt(id);
    Merchant mr = merchantDao.getMerchantById(_mid);
    if(mr!=null){
      mr.setSJDZ(sjdz);
      mr.setSJDHHM(sjdh);
      mr.setSFSHSM(sfshsm);
      mr.setYYKSSJ(sj_yykssj);
      mr.setYYJSSJ(sj_yyjssj);
      mr.setSJFWXJ(sjfwxj);
      merchantDao.updateMerchant(mr);
      request.setAttribute("merchant", mr);
    }
    return "/management/merchant/result_merchant";
  }
 
  @RequestMapping("/saveaddcount.do")
  public String saveAddCount(HttpServletRequest request,HttpServletResponse response){
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String realName = request.getParameter("realName");
    String sex = request.getParameter("sex");
    String phoneNum = request.getParameter("phoneNum");
    String userName = request.getParameter("userName");
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    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==4||_type==5){
        Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
        if(_or!=null){
          _orid = _or.getID();
        }
      }
    }
    if(_orid!=0&&_type!=0&&_type!=1&&_type!=3){
      User u = new User();
      u.setUsername(userName);
      u.setPassword(password);
      u.setTruename(realName);
      u.setSex(Integer.parseInt(sex));
      u.setMobile(phoneNum);
      u.setRegistertime(new Date());
      u.setLastlogin(new Date());
      u.setEmail(email);
      u.setOpener(rtUser);
      u.setParent(rtUser);
      u.setParenttype(_type);
      u.setUsertype(_type);
      u.setOrganizationid(_orid);

      u.setEnabled(true);
      u.setLocked(false);
      u.setCardEnd("0");
      u.setCardStart("0");
      u.setCurMaxCard("0");
      userCardService.createUser(u);
      request.setAttribute("userXX", u);
    }
    return "/management/merchant/result_merchant_subcount";
  }
 
  @RequestMapping("/listsubcount.do")
  public ModelAndView listaddCount(HttpServletRequest request,HttpServletResponse response){
    String userName = request.getParameter("userName");
    PageArgument pageArgment = getPageInfo(request);
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    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==4||_type==5){
        Organization _or = (Organization)userOrganizationMerchantService.getOrganizationOrMerchantByUser(rtUser);
        if(_or!=null){
          _orid = _or.getID();
        }
      }
    }
    if(_orid!=0){
      String hql = "";
      Object[] o = null;
      if(userName!=null&&userName.length()!=0){
        hql = " from User where usertype=? and organizationid=? and username=?  order by registertime desc";
        o = new Object[]{_type,_orid,userName};
       
      }else{
        hql = " from User where usertype=? and organizationid=? order by registertime desc";
        o = new Object[]{_type,_orid};
      }
      PageList list = userDao.findUserByConditions(hql, o, pageArgment);
      printJSON(response, list.toJSON());
    }
    return null;
  }
 
  @RequestMapping("/initmerchantdmxx.do")
  public String initProuctPage(HttpServletRequest request,HttpServletResponse response) {
    RunTimeUser rt = RunTimeUser.getRunTimeUser(request);
    if(rt!=null){
      long _mid = rt.getOrganizationid();
      Merchant _m = merchantDao.getMerchantById(_mid);
      request.setAttribute("merchant", _m);
    }
    return "/management/merchant/create_merchant_dmxx";
  }

  @RequestMapping("/savemerchantdmxx.do")
  public String saveProductPage(HttpServletRequest request,
      HttpServletResponse response) {
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    Merchant _m = null;
    if(rtUser!=null){
      long _mid = rtUser.getOrganizationid();
      _m = merchantDao.getMerchantById(_mid);
    }
    if(_m!=null){
      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(102400);
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setSizeMax(Constants.UPLOAD_MAX_SIZE);
      try {
        List fileItems = upload.parseRequest(request);
        Iterator iter = fileItems.iterator();
        while (iter.hasNext()) {
          FileItem item = (FileItem) iter.next();
          if (!item.isFormField()){
            String fileName = item.getName();
            if (fileName != null && fileName.length() != 0) {
              BufferedInputStream bis = new BufferedInputStream(item.getInputStream());
              String _fn = fileName.substring(fileName.lastIndexOf(System.getProperty("file.separator"))+1);
              String _ed = _fn.substring(_fn.indexOf('.'));
              String path = ManagementAction.class.getClassLoader().getResource("/").getPath();
              File file = new File(path);
              File p_file = new File(file.getParent());
              String url = p_file.getParent()+System.getProperty("file.separator")+"merchant_picture"+System.getProperty("file.separator")+new Date().getTime()+"_"+_m.getID();
              boolean suc = new File(p_file.getParent()+System.getProperty("file.separator")+"merchant_picture"+System.getProperty("file.separator")+new Date().getTime()+"_"+_m.getID()).mkdirs();
              if(suc){
                File _new = new File(url+System.getProperty("file.separator")+_m.getID()+_ed);
                _new.createNewFile();
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(_new));
                Streams.copy( bis, bos, true);
                Advertisement ad = new Advertisement();
                ad.setMerchant(_m);
                ad.setUser(rtUser);
                ad.setTPLJ(_new.getPath());
                ad.setUrl(null);
                ad.setSJSJ(new Date());
                ad.setQX(10000);
                ad.setTplx("01");
                advertisementDao.createAdvertisement(ad);
                request.setAttribute("advertisement", ad);
              }
            }
          }
        }
      } catch (FileUploadException e) {
        e.printStackTrace();
        System.out.println(e.getMessage() + "结束");
      } catch (Exception e) {
        e.printStackTrace();
      }
      request.setAttribute("merchant", _m);
    }
    return "/management/merchant/result_merchant_dmxx";
  }
 
  @RequestMapping("/initmerchantadvertisement.do")
  public String initMerchantAdvertisement(HttpServletRequest request,HttpServletResponse response){
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    if(rtUser!=null){
      long _mid = rtUser.getOrganizationid();
      Merchant _m = merchantDao.getMerchantById(_mid);
      request.setAttribute("merchant", _m);
    }
    return "/management/merchant/upload_merchantImg";
  }
 
  @RequestMapping("/saveadvertisement.do")
  public String saveMerchantAdvertisement(HttpServletRequest request,HttpServletResponse response){
    RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
    Merchant _m = null;
    if(rtUser!=null){
      long _mid = rtUser.getOrganizationid();
      _m = merchantDao.getMerchantById(_mid);
    }
    if(_m!=null){
      DiskFileItemFactory factory = new DiskFileItemFactory();
      factory.setSizeThreshold(102400);
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setSizeMax(Constants.UPLOAD_MAX_SIZE);
      try {
        List fileItems = upload.parseRequest(request);
        Iterator iter = fileItems.iterator();
        Advertisement ad = new Advertisement();
        ad.setMerchant(_m);
        ad.setUser(rtUser);
        ad.setUrl(null);
        ad.setSJSJ(new Date());
        ad.setQX(10000);
        while (iter.hasNext()) {
          FileItem item = (FileItem) iter.next();
          if (!item.isFormField()){
            String fileName = item.getName();
            if (fileName != null && fileName.length() != 0) {
              BufferedInputStream bis = new BufferedInputStream(item.getInputStream());
              String _fn = fileName.substring(fileName.lastIndexOf(System.getProperty("file.separator"))+1);
              String _ed = _fn.substring(_fn.indexOf('.'));
              String path = ManagementAction.class.getClassLoader().getResource("/").getPath();
              File file = new File(path);
              File p_file = new File(file.getParent());
              String url = p_file.getParent()+System.getProperty("file.separator")+"merchant_picture"+System.getProperty("file.separator")+new Date().getTime()+"_"+_m.getID();
              boolean suc = new File(p_file.getParent()+System.getProperty("file.separator")+"merchant_picture"+System.getProperty("file.separator")+new Date().getTime()+"_"+_m.getID()).mkdirs();
              if(suc){
                File _new = new File(url+System.getProperty("file.separator")+_m.getID()+_ed);
                _new.createNewFile();
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(_new));
                Streams.copy( bis, bos, true);
                ad.setTPLJ(_new.getPath());
                ad.setTplx("02");
              }
            }
          }else{
            String tpmx = item.getString();
            ad.setTpmx(tpmx);
          }
        }
        advertisementDao.createAdvertisement(ad);
      } catch (FileUploadException e) {
        e.printStackTrace();
        System.out.println(e.getMessage() + "结束");
      } catch (Exception e) {
        e.printStackTrace();
      }
      request.setAttribute("merchant", _m);
    }
    return "/management/merchant/upload_merchantImg";
  }
}
TOP

Related Classes of com.lgx8.management.action.ManagementAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.