| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- 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<JSONObject> json = propelService.query();
- String result = "";
- for (int i=0; i<json.size(); i++) {
- result = HttpsClient.sendJson2("https://chtech.ncjti.edu.cn/bigdata-api/api/energy/energyDataUpload", json.get(i));
- }
- JSONObject resultJson = new JSONObject();
- resultJson.put("msg", result);
- ResponseUtil.writeJson(ServletActionContext.getResponse(),
- resultJson.toString());
- return null;
- }
- // 电费更新
- public String getElecT() throws Exception {
- Gson gson=new Gson();
- JSONObject resultJson = new JSONObject();
- List<Build_elec> ta = propelService.queryEt();
- for (int i=0; i<ta.size(); i++){
- System.out.println(ta.get(i).getPointid());
- DataClient.getToken();
- JSONObject jb = DataClient.getPowerLastHistoryCumulant(ta.get(i).getPointid());
- System.out.println(jb);
- if (jb == null){
- resultJson.put("mess", "未查询到户号!");
- ResUtil.write(resultJson, ServletActionContext.getResponse());
- return null;
- }
- try {
- Object data = jb.get("data");
- HashMap<String, String> userMoney = gson.fromJson(data.toString(), new TypeToken<HashMap<String, String>>() {}.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<JSONObject> et = propelService.queryElecT();
- String result = "";
- if (et!=null){
- for (int i=0; i<et.size(); i++){
- System.out.println(et.get(i));
- result = HttpsClient.sendJson2("https://chtech.ncjti.edu.cn/bigdata-api/api/energy/energyDataUpload", et.get(i));
- }
- }
- JSONObject resultJson = new JSONObject();
- resultJson.put("msg", result);
- ResponseUtil.writeJson(ServletActionContext.getResponse(),
- resultJson.toString());
- return null;
- }
- // 水费更新
- public String getWaterT() throws Exception {
- Gson gson=new Gson();
- JSONObject resultJson = new JSONObject();
- List<WaterTj> ta = propelService.queryWaterTj();
- for (int i=0; i<ta.size(); i++){
- System.out.println(ta.get(i).getPointid());
- DataClient.getToken();
- JSONObject jb = DataClient.getPowerLastHistoryCumulantw(ta.get(i).getPointid());
- //System.out.println(jb);
- if (jb == null){
- System.out.println("未查到:"+ta.get(i).getPointid());
- }else {
- try {
- Object data = jb.get("data");
- HashMap<String, String> userMoney = gson.fromJson(data.toString(), new TypeToken<HashMap<String, String>>() {
- }.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<JSONObject> et = propelService.queryWaterT();
- String result = "";
- if (et!=null){
- for (int i=0; i<et.size(); i++){
- System.out.println(et.get(i));
- result = HttpsClient.sendJson2("https://chtech.ncjti.edu.cn/bigdata-api/api/energy/energyDataUpload", et.get(i));
- }
- }
- JSONObject resultJson = new JSONObject();
- resultJson.put("msg", result);
- ResponseUtil.writeJson(ServletActionContext.getResponse(),
- resultJson.toString());
- return null;
- }
- // 下载电费使用详情
- public String bm(){
- JSONObject resultJson = new JSONObject();
- Gson gson = new Gson();
- Calendar cal = Calendar.getInstance();
- // 往前多少天的访客
- cal.add(Calendar.DATE, -1);
- Calendar cal2 = Calendar.getInstance();
- // 往前多少天的访客
- cal2.add(Calendar.DATE, -0);
- String startTime = new SimpleDateFormat( "yyyyMMdd").format(cal.getTime());
- String endTime = new SimpleDateFormat( "yyyyMMdd").format(cal2.getTime());
- System.out.println("开始时间:"+startTime);
- System.out.println("结束时间:"+endTime);
- List<Build_elec> lists = elecService.queryAll();
- for (int i=0; i<lists.size(); i++){
- try {
- DataClient.getToken();
- JSONObject json = DataClient.getHistoryCumulant(lists.get(i).getPointid(), startTime, endTime);
- Object obj = json.get("data");
- List<HistoryBm> lh = gson.fromJson(obj.toString(), new TypeToken<List<HistoryBm>>() {}.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<Detail_elc> 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<Detail_elc> 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<Detail_elc> use_day = elecService.queryByTimeAndPoint(ld.get(i).getPointid(), ld.get(i).getDataTime());
- List<Detail_elc> 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<Detail_elc> de = elecService.queryRe();
- if (de!=null) {
- for (int i = 0; i < de.size(); i++) {
- List<Recharge_elc> 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<Detail_elc> de = elecService.queryUsable();
- if (de!=null) {
- for (int i = 0; i < de.size(); i++) {
- List<Build_elec> 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<String, String> userMoney = gson.fromJson(data.toString(), new TypeToken<HashMap<String, String>>() {
- }.getType());
- double elcMoney = Double.parseDouble(userMoney.get("usablemoney"));
- Double amount = 0.00;
- List<Price> 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<Detail_elc> 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;
- }
- }
|