package com.happy.action; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.happy.Model.*; import com.happy.Until.ResUtil; import com.happy.Until.ResponseUtil; import com.happy.common.http.HttpsClient; import com.happy.service.ConsumeService; import com.happy.service.ElecService; import com.happy.service.PropelService; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import testExport.DataClient; import testExport.Test; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; public class Propelling extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; public HttpServletResponse response; @Resource public PropelService propelService; @Resource public ConsumeService consumeService; @Resource public ElecService elecService; public int page; // 当前页 public int rows;// 每页显示的行数rows public String dom; public String dataTime; public String build; 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; } public String getDom() { return dom; } public void setDom(String dom) { this.dom = dom; } public String getDataTime() { return dataTime; } public void setDataTime(String dataTime) { this.dataTime = dataTime; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public String getBuild() { return build; } public void setBuild(String build) { this.build = build; } // 热水推送 public String load(){ List json = propelService.query(); String result = ""; for (int i=0; i ta = propelService.queryEt(); for (int i=0; i userMoney = gson.fromJson(data.toString(), new TypeToken>() {}.getType()); double elcColumn = Double.parseDouble(userMoney.get("bm")); propelService.updateUseSize(ta.get(i).getPointid(), elcColumn); }catch (Exception e){ System.out.println("更新失败"); } } resultJson.put("msg", "更新完毕"); ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } // 电费推送 public String ElecByBuild(){ List et = propelService.queryElecT(); String result = ""; if (et!=null){ for (int i=0; i ta = propelService.queryWaterTj(); for (int i=0; i userMoney = gson.fromJson(data.toString(), new TypeToken>() { }.getType()); double elcColumn = Double.parseDouble(userMoney.get("bm")); propelService.updateWaterUseSize(ta.get(i).getPointid(), elcColumn); } catch (Exception e) { System.out.println("更新失败"); } } } resultJson.put("msg", "更新完毕"); ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } // 水费推送 public String waterByBuild(){ List et = propelService.queryWaterT(); String result = ""; if (et!=null){ for (int i=0; i lists = elecService.queryAll(); for (int i=0; i lh = gson.fromJson(obj.toString(), new TypeToken>() {}.getType()); for (int j = 0; j < lh.size(); j++) { Detail_elc detail_elc = new Detail_elc(); detail_elc.setBuild(lists.get(i).getBuild()); detail_elc.setDom(lists.get(i).getDom()); detail_elc.setPointid(lists.get(i).getPointid()); detail_elc.setDataTime(lh.get(j).getDataTime().substring(0, 10)); detail_elc.setBm(lh.get(j).getBm()); List ld = elecService.queryByTimeAndPoint(lists.get(i).getPointid(), lh.get(j).getDataTime().substring(0, 10)); if (ld == null ) { elecService.insertDetail(detail_elc); System.out.println(lists.get(i).getPointid() + "==" + lh.get(j).getDataTime().substring(0, 10)); } } }catch (Exception e){ System.out.println(lists.get(i).getPointid()+"未查到"); } } resultJson.put("msg", "插入完成"); System.out.println("表码下载完成"); ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } // 更新每日使用电量 public String updateUse() throws ParseException { JSONObject resultJson = new JSONObject(); List ld = elecService.queryUse(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); if (ld!=null) { for (int i = 0; i < ld.size(); i++) { // 获取某个日期前一天 String day_time = ld.get(i).getDataTime(); Date date = dateFormat.parse(day_time); c.setTime(date); int day = c.get(Calendar.DATE); c.set(Calendar.DATE, day - 1); String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); List use_day = elecService.queryByTimeAndPoint(ld.get(i).getPointid(), ld.get(i).getDataTime()); List use_before = elecService.queryByTimeAndPoint(ld.get(i).getPointid(), dayBefore); if (use_before != null) { double use_size = use_day.get(0).getBm() - use_before.get(0).getBm(); elecService.updateUse(ld.get(i).getPointid(), ld.get(i).getDataTime(), use_size); System.out.println(ld.get(i).getPointid() + "====" + ld.get(i).getDataTime()); } else { elecService.updateUseState(ld.get(i).getPointid(), ld.get(i).getDataTime()); } } } resultJson.put("msg", "更新成功!"); System.out.println("用电量更新完"); ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } // 更新每日充值 public String updateRe(){ JSONObject resultJson = new JSONObject(); List de = elecService.queryRe(); if (de!=null) { for (int i = 0; i < de.size(); i++) { List re = elecService.queryByPointAndDay(de.get(i).getPointid(), de.get(i).getDataTime()); if (re != null) { double b = 0; for (int j = 0; j < re.size(); j++) { b = b + re.get(j).getAccount(); } elecService.updateRe(de.get(i).getPointid(), de.get(i).getDataTime(), b); } else { elecService.updateReState(de.get(i).getPointid(), de.get(i).getDataTime()); } } } resultJson.put("msg", "更新成功!"); System.out.println("充值更新完"); ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } // 更新每日剩余可用电量 public String updateUsable() throws Exception { JSONObject resultJson = new JSONObject(); Calendar cal = Calendar.getInstance(); // 往前多少天的访客 cal.add(Calendar.DATE, -1); String day = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime()); List de = elecService.queryUsable(); if (de!=null) { for (int i = 0; i < de.size(); i++) { List re = elecService.queryBuildByPoint(de.get(i).getPointid()); if (re != null) { String user_id = re.get(0).getUser_id(); DataClient.getToken(); JSONObject jb = DataClient.getAccountInfo(user_id); if (jb != null) { try { Object data = jb.get("data"); Gson gson = new Gson(); HashMap userMoney = gson.fromJson(data.toString(), new TypeToken>() { }.getType()); double elcMoney = Double.parseDouble(userMoney.get("usablemoney")); Double amount = 0.00; List elcPrice = consumeService.water_price("电费"); amount = elcMoney / elcPrice.get(0).getPrice(); // 剩余电量 elecService.updateUsable(de.get(i).getPointid(), day, amount); } catch (Exception e) { e.printStackTrace(); } } else { elecService.updateUsableState(de.get(i).getPointid(), day); } } } } resultJson.put("msg", "更新成功!"); System.out.println("可用余额更新完"); ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } public String queryDetail() { net.sf.json.JSONObject resultJson = new net.sf.json.JSONObject(); StringBuilder sb = new StringBuilder("select * from `detail_elc` where 1=1 "); StringBuilder sb2 = new StringBuilder("select count(*) from `detail_elc` where 1=1 "); if (this.build != null){ sb.append(" and build='" + this.build + "'"); sb2.append(" and build='" + this.build + "'"); } if (this.dom != null) { sb.append(" and dom='" + this.dom + "'"); sb2.append(" and dom='" + this.dom + "'"); } if (this.dataTime != null) { sb.append(" and dataTime = '" + this.dataTime + "'"); sb2.append(" and dataTime = '" + this.dataTime + "'"); } int start = (page - 1) * rows;// 每页的起始下标 sb.append(" order by dom,dataTime desc limit "+start+", "+rows); System.out.println(sb); int total = this.propelService.total(sb2.toString()); System.out.println(total); List pp = this.propelService.findAll(sb.toString()); System.out.println(pp); if (pp == null) { resultJson.put("rows", ""); resultJson.put("total", 0); } else { resultJson.put("rows", pp); // int total = listAll.size(); resultJson.put("total", total);// 总记录数 int totalPage = total % rows == 0 ? (total / rows) : (total / rows) + 1;// 总页数 resultJson.put("totalPage", totalPage); resultJson.put("currentPage", page);// 当前页 resultJson.put("numPerPage", rows);// 每页数 resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页 resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页 resultJson.put("hasPreviousPage", true);// 有上一页 resultJson.put("hasNextPage", true);// 有下一页 resultJson.put("firstPage", true);// 首页 resultJson.put("lastPage", true);// 尾页 } ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString()); return null; } }