package com.happy.action; 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.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 { 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 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()); int m = 0; //保存基本信息 if(hotel.getId()!= null){ m = hotelService.updateHotel(hotel); }else{ hotel.setId(hotelId); m = hotelService.insertHotel(hotel); } //保存详细图附件 if(hotelEto.getFileList()!=null && hotelEto.getFileList().size()>0){ if(hotel.getId()!= null){ fileService.delLinkFile(hotel.getId()); } for(File file : hotelEto.getFileList()){ file.setLinkId(hotelId); fileService.insertFile(file); } }else{ resultJson.put("message", "请上传详细图"); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } 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", "未知异常"); 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", "请传入登陆人账号Id"); ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } //查询商户信息 AdminManager adminManager = adminManagerService.getById(managerId); //查询民宿信息 Hotel hotel = hotelService.getOen(s1.toString()); HotelEto hotelEto = new HotelEto(); BeanUtils.copyProperties(adminManager,hotelEto); BeanUtils.copyProperties(hotel,hotelEto); StringBuilder fileSqlx = new StringBuilder(""); fileSqlx.append("and link_id = '").append(id).append("'"); List fileList = fileService.queryList(fileSqlx.toString()); hotelEto.setFileList(fileList); resultJson.put("message", "查询成功"); resultJson.put("code", 200); resultJson.put("date", 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 || 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("'") .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.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; } /** * 描述:删除民宿配置字典 * @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()); s1.append("and code = '3'"); List list3 = hotelDictService.queryList(s3.toString()); s1.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).append("'"); List list1 = hotelDictService.queryList(s1.toString()); resultJson.put("message", "查询成功"); resultJson.put("code", 200); resultJson.put("date1", list1); return null; } } }