package com.happy.action; import com.alibaba.fastjson.JSONArray; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.happy.Model.*; import com.happy.Until.GetHttpParam; import com.happy.Until.PwdDefind; import com.happy.Until.ResUtil; import com.happy.Until.UUIDUtil; import com.happy.dto.HotelEto; import com.happy.dto.IPage; import com.happy.service.*; import com.opensymphony.xwork2.ActionSupport; import net.sf.json.JSONObject; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.springframework.beans.BeanUtils; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; public class hotelAction extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; public HttpServletResponse response; @Resource public AdminManagerService adminManagerService; @Resource public HotelService hotelService; @Resource public HotelDictService hotelDictService; @Resource public FileService fileService; public Integer id; public Integer managerId;//所属商家账号id public Integer code;//查询字典编码 public int page; // 当前页 public int rows;// 每页显示的行数rows public String fileListJson; public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public HttpServletResponse getResponse() { return response; } public void setResponse(HttpServletResponse response) { this.response = response; } /** * 描述:维护民宿信息 * @return */ public String updateHotel(){ JSONObject resultJson = new JSONObject(); Gson gson = new Gson(); com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request); if (json == null) { resultJson.put("message", "请传入参数"); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } HotelEto hotelEto = new HotelEto(); try{ hotelEto = gson.fromJson(json.toString(), new TypeToken() {}.getType()); Hotel hotel = new Hotel(); BeanUtils.copyProperties(hotelEto,hotel); int hotelId = hotel.getId()!=null ? hotel.getId():Math.toIntExact(UUIDUtil.generateID()); if(hotelId==0){ hotelId = Math.toIntExact(UUIDUtil.generateID()); } int m = 0; //详细图附件 List fileList = JSONArray.parseArray(fileListJson, FileInfo.class); if(fileList==null){ resultJson.put("message", "请上传详细图"); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } //保存基本信息 Hotel hotel1 = hotelService.getById(hotel.getId()); if(hotel1!= null){ m = hotelService.updateHotel(hotel); }else{ hotel.setId(hotelId); m = hotelService.insertHotel(hotel); } if (m > 0) { if(hotel.getId()!= null){ fileService.delLinkFile(hotel.getId()); } for(FileInfo file : fileList){ FileInfo fileInfo = fileService.getById(file.getId()); if(fileInfo==null){ fileService.insertFile(file); }else{ file.setLinkId(hotelId); fileService.updateFile(file); } } resultJson.put("message", "维护成功"); resultJson.put("code", 200); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } else { resultJson.put("message", "维护失败"); resultJson.put("code", 502); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } } catch (Exception e){ e.printStackTrace(); } resultJson.put("message", "未知异常"); resultJson.put("code", 205); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } /** * 描述:查看民宿详细 * @return */ public String getByHotelInfo(){ JSONObject resultJson = new JSONObject(); StringBuilder s1 = new StringBuilder(""); if (managerId!=null){ s1.append(" and manager_id = '").append(managerId).append("'"); }else{ resultJson.put("message", "请传入managerId"); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } if(id!=null){ s1.append(" and id = '").append(id).append("'"); } //查询商户信息 AdminManager adminManager = adminManagerService.getById(managerId); //查询民宿信息 Hotel hotel = hotelService.getOen(s1.toString()); HotelEto hotelEto = new HotelEto(); if(adminManager!=null){ hotelEto.setHotelName(adminManager.getHotelName()); hotelEto.setHotelTownship(adminManager.getHotelTownship()); hotelEto.setHotelTownshipName(adminManager.getHotelTownshipName()); hotelEto.setCorpnName(adminManager.getCorpnName()); hotelEto.setCorpnPhone(adminManager.getCorpnPhone()); hotelEto.setManagerId(adminManager.getId()); //hotelEto.setBankCard(adminManager.getBankCard()); } if(hotel!=null){ //获取有效民宿配置 if(hotel.getHconfig() != null && !"".equals(hotel.getHconfig())){ hotel.setHconfig(hotelDictService.setConfig(hotel.getHconfig())); } BeanUtils.copyProperties(hotel,hotelEto); hotelEto.setHotelTownshipName(adminManager.getHotelTownshipName()); StringBuilder fileSqlx = new StringBuilder(""); fileSqlx.append("and link_id = '").append(hotel.getId()).append("'"); List fileList = fileService.queryList(fileSqlx.toString()); hotelEto.setFileInfoList(fileList); resultJson.put("message", "查询成功"); resultJson.put("code", 200); resultJson.put("data", hotelEto); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); }else{ hotelEto.setHstatus(1); hotelEto.setIsCanorder(1); hotelEto.setIsOrder(1); hotelEto.setIsCanorder(1); } if(adminManager == null && hotel == null){ resultJson.put("message", "未查到数据请检查参数"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } resultJson.put("message", "查询成功"); resultJson.put("code", 200); resultJson.put("data", hotelEto); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } /** * 描述:新增民宿配置字典 code 1.民宿类型;2.民宿服务;3.房型名称;4.房型服务 * @return */ public String insterHotelDict(){ JSONObject resultJson = new JSONObject(); Gson gson = new Gson(); com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request); if (json == null) { resultJson.put("message", "请传入参数"); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } HotelDict hotelDict = new HotelDict(); try{ hotelDict = gson.fromJson(json.toString(), new TypeToken() {}.getType()); if(hotelDict==null){ resultJson.put("message", "数据为空"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } if(hotelDict.getName()==null || hotelDict.getCode()==null){ resultJson.put("message", "各参数不能为空"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } if(hotelDict.getCode()==4 && hotelDict.getFileUrl()==null){ resultJson.put("message", "服务图标不能为空"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } StringBuilder getOneSqlx = new StringBuilder(""); getOneSqlx.append(" and code = '").append(hotelDict.getCode()).append("'") .append(" and name = '").append(hotelDict.getName()).append("'"); HotelDict listc = hotelDictService.getOen(getOneSqlx.toString()); if(listc != null){ resultJson.put("message", "已新增过相同名称的数据"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } int m = hotelDictService.insertHotelDict(hotelDict); if (m > 0) { resultJson.put("message", "添加成功"); resultJson.put("code", 200); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } else { resultJson.put("message", "添加失败"); resultJson.put("code", 502); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } }catch (Exception e){ e.printStackTrace(); resultJson.put("message", "未知异常:"+ e); resultJson.put("code", 205); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); } resultJson.put("message", "未知异常"); resultJson.put("code", 205); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } /** * 描述:新增民宿配置字典 code 1.民宿类型;2.民宿服务;3.房型名称;4.房型服务 * @return */ public String updateHotelDict(){ JSONObject resultJson = new JSONObject(); Gson gson = new Gson(); com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request); if (json == null) { resultJson.put("message", "请传入参数"); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } HotelDict hotelDict = new HotelDict(); try{ hotelDict = gson.fromJson(json.toString(), new TypeToken() {}.getType()); if(hotelDict==null){ resultJson.put("message", "数据为空"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } if(hotelDict.getId()==null){ resultJson.put("message", "修改Id不能为空"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } if(hotelDict.getName()==null){ resultJson.put("message", "各参数不能为空"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } if(hotelDict.getCode()==4 && hotelDict.getFileUrl()==null){ resultJson.put("message", "服务图标不能为空"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } StringBuilder getOneSqlx = new StringBuilder(""); getOneSqlx.append(" and code = '").append(hotelDict.getCode()).append("'") .append(" and name = '").append(hotelDict.getName()).append("'") .append(" and id != '").append(hotelDict.getId()).append("'"); HotelDict listc = hotelDictService.getOen(getOneSqlx.toString()); if(listc != null){ resultJson.put("message", "已新增过相同名称的数据"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } int m = hotelDictService.updateHotelDict(hotelDict); if (m > 0) { resultJson.put("message", "修改成功"); resultJson.put("code", 200); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } else { resultJson.put("message", "修改失败"); resultJson.put("code", 502); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } }catch (Exception e){ e.printStackTrace(); resultJson.put("message", "未知异常:"+ e); resultJson.put("code", 205); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); } resultJson.put("message", "未知异常"); resultJson.put("code", 205); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } /** * 描述:删除民宿配置字典 * @return */ public String delHotelDict(){ JSONObject resultJson = new JSONObject(); if (id == null) { resultJson.put("message", "请传入id"); resultJson.put("code", 500); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } int m = hotelDictService.delHotelDict(id); if (m > 0) { resultJson.put("message", "删除成功"); resultJson.put("code", 200); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } resultJson.put("message", "未知异常"); resultJson.put("code", 205); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } /** * 描述:查询民宿配置列表 * @return */ public String queryList(){ JSONObject resultJson = new JSONObject(); StringBuilder s1 = new StringBuilder(""); StringBuilder s2 = new StringBuilder(""); StringBuilder s3 = new StringBuilder(""); StringBuilder s4 = new StringBuilder(""); if(code==null){//查询全部 s1.append("and code = 1"); List list1 = hotelDictService.queryList(s1.toString()); s2.append("and code = 2"); List list2 = hotelDictService.queryList(s2.toString()); s3.append("and code = 3"); List list3 = hotelDictService.queryList(s3.toString()); s4.append("and code = 4"); List list4 = hotelDictService.queryList(s4.toString()); resultJson.put("message", "查询成功"); resultJson.put("code", 200); resultJson.put("date1", list1); resultJson.put("date2", list2); resultJson.put("date3", list3); resultJson.put("date4", list4); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; }else{ s1.append("and code = ").append(code); List list1 = hotelDictService.queryList(s1.toString()); resultJson.put("message", "查询成功"); resultJson.put("code", 200); resultJson.put("data", list1); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } } }