Propelling.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package com.happy.action;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import com.google.gson.reflect.TypeToken;
  5. import com.happy.Model.*;
  6. import com.happy.Until.ResUtil;
  7. import com.happy.Until.ResponseUtil;
  8. import com.happy.common.http.HttpsClient;
  9. import com.happy.service.ConsumeService;
  10. import com.happy.service.ElecService;
  11. import com.happy.service.PropelService;
  12. import com.opensymphony.xwork2.ActionSupport;
  13. import org.apache.struts2.ServletActionContext;
  14. import org.apache.struts2.interceptor.ServletRequestAware;
  15. import testExport.DataClient;
  16. import testExport.Test;
  17. import javax.annotation.Resource;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.text.DateFormat;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Calendar;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. public class Propelling extends ActionSupport implements ServletRequestAware {
  28. private HttpServletRequest request;
  29. public HttpServletResponse response;
  30. @Resource
  31. public PropelService propelService;
  32. @Resource
  33. public ConsumeService consumeService;
  34. @Resource
  35. public ElecService elecService;
  36. public int page; // 当前页
  37. public int rows;// 每页显示的行数rows
  38. public String dom;
  39. public String dataTime;
  40. public String build;
  41. public HttpServletRequest getRequest() {
  42. return request;
  43. }
  44. public void setRequest(HttpServletRequest request) {
  45. this.request = request;
  46. }
  47. public void setServletRequest(HttpServletRequest request) {
  48. this.request = request;
  49. }
  50. public HttpServletResponse getResponse() {
  51. return response;
  52. }
  53. public void setResponse(HttpServletResponse response) {
  54. this.response = response;
  55. }
  56. public String getDom() {
  57. return dom;
  58. }
  59. public void setDom(String dom) {
  60. this.dom = dom;
  61. }
  62. public String getDataTime() {
  63. return dataTime;
  64. }
  65. public void setDataTime(String dataTime) {
  66. this.dataTime = dataTime;
  67. }
  68. public int getPage() {
  69. return page;
  70. }
  71. public void setPage(int page) {
  72. this.page = page;
  73. }
  74. public int getRows() {
  75. return rows;
  76. }
  77. public void setRows(int rows) {
  78. this.rows = rows;
  79. }
  80. public String getBuild() {
  81. return build;
  82. }
  83. public void setBuild(String build) {
  84. this.build = build;
  85. }
  86. // 热水推送
  87. public String load(){
  88. List<JSONObject> json = propelService.query();
  89. String result = "";
  90. for (int i=0; i<json.size(); i++) {
  91. result = HttpsClient.sendJson2("https://chtech.ncjti.edu.cn/bigdata-api/api/energy/energyDataUpload", json.get(i));
  92. }
  93. JSONObject resultJson = new JSONObject();
  94. resultJson.put("msg", result);
  95. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  96. resultJson.toString());
  97. return null;
  98. }
  99. // 电费更新
  100. public String getElecT() throws Exception {
  101. Gson gson=new Gson();
  102. JSONObject resultJson = new JSONObject();
  103. List<Build_elec> ta = propelService.queryEt();
  104. for (int i=0; i<ta.size(); i++){
  105. System.out.println(ta.get(i).getPointid());
  106. DataClient.getToken();
  107. JSONObject jb = DataClient.getPowerLastHistoryCumulant(ta.get(i).getPointid());
  108. System.out.println(jb);
  109. if (jb == null){
  110. resultJson.put("mess", "未查询到户号!");
  111. ResUtil.write(resultJson, ServletActionContext.getResponse());
  112. return null;
  113. }
  114. try {
  115. Object data = jb.get("data");
  116. HashMap<String, String> userMoney = gson.fromJson(data.toString(), new TypeToken<HashMap<String, String>>() {}.getType());
  117. double elcColumn = Double.parseDouble(userMoney.get("bm"));
  118. propelService.updateUseSize(ta.get(i).getPointid(), elcColumn);
  119. }catch (Exception e){
  120. System.out.println("更新失败");
  121. }
  122. }
  123. resultJson.put("msg", "更新完毕");
  124. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  125. resultJson.toString());
  126. return null;
  127. }
  128. // 电费推送
  129. public String ElecByBuild(){
  130. List<JSONObject> et = propelService.queryElecT();
  131. String result = "";
  132. if (et!=null){
  133. for (int i=0; i<et.size(); i++){
  134. System.out.println(et.get(i));
  135. result = HttpsClient.sendJson2("https://chtech.ncjti.edu.cn/bigdata-api/api/energy/energyDataUpload", et.get(i));
  136. }
  137. }
  138. JSONObject resultJson = new JSONObject();
  139. resultJson.put("msg", result);
  140. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  141. resultJson.toString());
  142. return null;
  143. }
  144. // 水费更新
  145. public String getWaterT() throws Exception {
  146. Gson gson=new Gson();
  147. JSONObject resultJson = new JSONObject();
  148. List<WaterTj> ta = propelService.queryWaterTj();
  149. for (int i=0; i<ta.size(); i++){
  150. System.out.println(ta.get(i).getPointid());
  151. DataClient.getToken();
  152. JSONObject jb = DataClient.getPowerLastHistoryCumulantw(ta.get(i).getPointid());
  153. //System.out.println(jb);
  154. if (jb == null){
  155. System.out.println("未查到:"+ta.get(i).getPointid());
  156. }else {
  157. try {
  158. Object data = jb.get("data");
  159. HashMap<String, String> userMoney = gson.fromJson(data.toString(), new TypeToken<HashMap<String, String>>() {
  160. }.getType());
  161. double elcColumn = Double.parseDouble(userMoney.get("bm"));
  162. propelService.updateWaterUseSize(ta.get(i).getPointid(), elcColumn);
  163. } catch (Exception e) {
  164. System.out.println("更新失败");
  165. }
  166. }
  167. }
  168. resultJson.put("msg", "更新完毕");
  169. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  170. resultJson.toString());
  171. return null;
  172. }
  173. // 水费推送
  174. public String waterByBuild(){
  175. List<JSONObject> et = propelService.queryWaterT();
  176. String result = "";
  177. if (et!=null){
  178. for (int i=0; i<et.size(); i++){
  179. System.out.println(et.get(i));
  180. result = HttpsClient.sendJson2("https://chtech.ncjti.edu.cn/bigdata-api/api/energy/energyDataUpload", et.get(i));
  181. }
  182. }
  183. JSONObject resultJson = new JSONObject();
  184. resultJson.put("msg", result);
  185. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  186. resultJson.toString());
  187. return null;
  188. }
  189. // 下载电费使用详情
  190. public String bm(){
  191. JSONObject resultJson = new JSONObject();
  192. Gson gson = new Gson();
  193. Calendar cal = Calendar.getInstance();
  194. // 往前多少天的访客
  195. cal.add(Calendar.DATE, -1);
  196. Calendar cal2 = Calendar.getInstance();
  197. // 往前多少天的访客
  198. cal2.add(Calendar.DATE, -0);
  199. String startTime = new SimpleDateFormat( "yyyyMMdd").format(cal.getTime());
  200. String endTime = new SimpleDateFormat( "yyyyMMdd").format(cal2.getTime());
  201. System.out.println("开始时间:"+startTime);
  202. System.out.println("结束时间:"+endTime);
  203. List<Build_elec> lists = elecService.queryAll();
  204. for (int i=0; i<lists.size(); i++){
  205. try {
  206. DataClient.getToken();
  207. JSONObject json = DataClient.getHistoryCumulant(lists.get(i).getPointid(), startTime, endTime);
  208. Object obj = json.get("data");
  209. List<HistoryBm> lh = gson.fromJson(obj.toString(), new TypeToken<List<HistoryBm>>() {}.getType());
  210. for (int j = 0; j < lh.size(); j++) {
  211. Detail_elc detail_elc = new Detail_elc();
  212. detail_elc.setBuild(lists.get(i).getBuild());
  213. detail_elc.setDom(lists.get(i).getDom());
  214. detail_elc.setPointid(lists.get(i).getPointid());
  215. detail_elc.setDataTime(lh.get(j).getDataTime().substring(0, 10));
  216. detail_elc.setBm(lh.get(j).getBm());
  217. List<Detail_elc> ld = elecService.queryByTimeAndPoint(lists.get(i).getPointid(), lh.get(j).getDataTime().substring(0, 10));
  218. if (ld == null ) {
  219. elecService.insertDetail(detail_elc);
  220. System.out.println(lists.get(i).getPointid() + "==" + lh.get(j).getDataTime().substring(0, 10));
  221. }
  222. }
  223. }catch (Exception e){
  224. System.out.println(lists.get(i).getPointid()+"未查到");
  225. }
  226. }
  227. resultJson.put("msg", "插入完成");
  228. System.out.println("表码下载完成");
  229. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  230. resultJson.toString());
  231. return null;
  232. }
  233. // 更新每日使用电量
  234. public String updateUse() throws ParseException {
  235. JSONObject resultJson = new JSONObject();
  236. List<Detail_elc> ld = elecService.queryUse();
  237. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  238. Calendar c = Calendar.getInstance();
  239. if (ld!=null) {
  240. for (int i = 0; i < ld.size(); i++) {
  241. // 获取某个日期前一天
  242. String day_time = ld.get(i).getDataTime();
  243. Date date = dateFormat.parse(day_time);
  244. c.setTime(date);
  245. int day = c.get(Calendar.DATE);
  246. c.set(Calendar.DATE, day - 1);
  247. String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  248. List<Detail_elc> use_day = elecService.queryByTimeAndPoint(ld.get(i).getPointid(), ld.get(i).getDataTime());
  249. List<Detail_elc> use_before = elecService.queryByTimeAndPoint(ld.get(i).getPointid(), dayBefore);
  250. if (use_before != null) {
  251. double use_size = use_day.get(0).getBm() - use_before.get(0).getBm();
  252. elecService.updateUse(ld.get(i).getPointid(), ld.get(i).getDataTime(), use_size);
  253. System.out.println(ld.get(i).getPointid() + "====" + ld.get(i).getDataTime());
  254. } else {
  255. elecService.updateUseState(ld.get(i).getPointid(), ld.get(i).getDataTime());
  256. }
  257. }
  258. }
  259. resultJson.put("msg", "更新成功!");
  260. System.out.println("用电量更新完");
  261. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  262. resultJson.toString());
  263. return null;
  264. }
  265. // 更新每日充值
  266. public String updateRe(){
  267. JSONObject resultJson = new JSONObject();
  268. List<Detail_elc> de = elecService.queryRe();
  269. if (de!=null) {
  270. for (int i = 0; i < de.size(); i++) {
  271. List<Recharge_elc> re = elecService.queryByPointAndDay(de.get(i).getPointid(), de.get(i).getDataTime());
  272. if (re != null) {
  273. double b = 0;
  274. for (int j = 0; j < re.size(); j++) {
  275. b = b + re.get(j).getAccount();
  276. }
  277. elecService.updateRe(de.get(i).getPointid(), de.get(i).getDataTime(), b);
  278. } else {
  279. elecService.updateReState(de.get(i).getPointid(), de.get(i).getDataTime());
  280. }
  281. }
  282. }
  283. resultJson.put("msg", "更新成功!");
  284. System.out.println("充值更新完");
  285. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  286. resultJson.toString());
  287. return null;
  288. }
  289. // 更新每日剩余可用电量
  290. public String updateUsable() throws Exception {
  291. JSONObject resultJson = new JSONObject();
  292. Calendar cal = Calendar.getInstance();
  293. // 往前多少天的访客
  294. cal.add(Calendar.DATE, -1);
  295. String day = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
  296. List<Detail_elc> de = elecService.queryUsable();
  297. if (de!=null) {
  298. for (int i = 0; i < de.size(); i++) {
  299. List<Build_elec> re = elecService.queryBuildByPoint(de.get(i).getPointid());
  300. if (re != null) {
  301. String user_id = re.get(0).getUser_id();
  302. DataClient.getToken();
  303. JSONObject jb = DataClient.getAccountInfo(user_id);
  304. if (jb != null) {
  305. try {
  306. Object data = jb.get("data");
  307. Gson gson = new Gson();
  308. HashMap<String, String> userMoney = gson.fromJson(data.toString(), new TypeToken<HashMap<String, String>>() {
  309. }.getType());
  310. double elcMoney = Double.parseDouble(userMoney.get("usablemoney"));
  311. Double amount = 0.00;
  312. List<Price> elcPrice = consumeService.water_price("电费");
  313. amount = elcMoney / elcPrice.get(0).getPrice(); // 剩余电量
  314. elecService.updateUsable(de.get(i).getPointid(), day, amount);
  315. } catch (Exception e) {
  316. e.printStackTrace();
  317. }
  318. } else {
  319. elecService.updateUsableState(de.get(i).getPointid(), day);
  320. }
  321. }
  322. }
  323. }
  324. resultJson.put("msg", "更新成功!");
  325. System.out.println("可用余额更新完");
  326. ResponseUtil.writeJson(ServletActionContext.getResponse(),
  327. resultJson.toString());
  328. return null;
  329. }
  330. public String queryDetail() {
  331. net.sf.json.JSONObject resultJson = new net.sf.json.JSONObject();
  332. StringBuilder sb = new StringBuilder("select * from `detail_elc` where 1=1 ");
  333. StringBuilder sb2 = new StringBuilder("select count(*) from `detail_elc` where 1=1 ");
  334. if (this.build != null){
  335. sb.append(" and build='" + this.build + "'");
  336. sb2.append(" and build='" + this.build + "'");
  337. }
  338. if (this.dom != null) {
  339. sb.append(" and dom='" + this.dom + "'");
  340. sb2.append(" and dom='" + this.dom + "'");
  341. }
  342. if (this.dataTime != null) {
  343. sb.append(" and dataTime = '" + this.dataTime + "'");
  344. sb2.append(" and dataTime = '" + this.dataTime + "'");
  345. }
  346. int start = (page - 1) * rows;// 每页的起始下标
  347. sb.append(" order by dom,dataTime desc limit "+start+", "+rows);
  348. System.out.println(sb);
  349. int total = this.propelService.total(sb2.toString());
  350. System.out.println(total);
  351. List<Detail_elc> pp = this.propelService.findAll(sb.toString());
  352. System.out.println(pp);
  353. if (pp == null) {
  354. resultJson.put("rows", "");
  355. resultJson.put("total", 0);
  356. } else {
  357. resultJson.put("rows", pp);
  358. // int total = listAll.size();
  359. resultJson.put("total", total);// 总记录数
  360. int totalPage = total % rows == 0 ? (total / rows)
  361. : (total / rows) + 1;// 总页数
  362. resultJson.put("totalPage", totalPage);
  363. resultJson.put("currentPage", page);// 当前页
  364. resultJson.put("numPerPage", rows);// 每页数
  365. resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
  366. resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
  367. resultJson.put("hasPreviousPage", true);// 有上一页
  368. resultJson.put("hasNextPage", true);// 有下一页
  369. resultJson.put("firstPage", true);// 首页
  370. resultJson.put("lastPage", true);// 尾页
  371. }
  372. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  373. return null;
  374. }
  375. }