OrdinaryController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package com.persagy.controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.persagy.domain.ReservationCustom;
  5. import com.persagy.domain.ReservationVo;
  6. import com.persagy.domain.Room;
  7. import com.persagy.service.ReservationService;
  8. import com.persagy.service.RoomService;
  9. import com.persagy.service.UserService;
  10. import com.persagy.util.PinyinUtils;
  11. import org.apache.http.client.methods.CloseableHttpResponse;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.client.methods.HttpUriRequest;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.impl.client.HttpClients;
  17. import org.apache.http.util.EntityUtils;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.Model;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.annotation.Resource;
  22. import java.io.IOException;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. /**
  28. * Created by Admiral on 2018/1/20.
  29. */
  30. @Controller
  31. @ResponseBody
  32. @RequestMapping("/ordinary")
  33. public class OrdinaryController {
  34. @Resource(name = "roomServiceImpl")
  35. private RoomService roomService;
  36. @Resource(name = "userServiceImpl")
  37. private UserService userService;
  38. @Resource(name = "reservationServiceImpl")
  39. private ReservationService reservationService;
  40. //获取CorpAccessToken URL
  41. private String corpAccessTokenURL = "https://open.fxiaoke.com/cgi/corpAccessToken/get/V2";
  42. //部门列表
  43. private String departmentURL = "https://open.fxiaoke.com/cgi/department/list";
  44. //部门下人员列表
  45. private String userURL = "https://open.fxiaoke.com/cgi/user/list";
  46. //消息
  47. private String messageURL = "https://open.fxiaoke.com/cgi/message/send";
  48. private String corpAccessToken = null;
  49. private String corpId = null;
  50. /**
  51. * 获取会议室列表
  52. * @return
  53. * @throws Exception
  54. */
  55. @RequestMapping(value="/roomList", method=RequestMethod.POST)
  56. @ResponseBody
  57. public List<Room> roomList() throws Exception {
  58. List<Room> list = roomService.findByPaging(1);
  59. return list;
  60. }
  61. /**
  62. * 根据会议室名称模糊搜索会议室
  63. * @param roomName
  64. * @return
  65. * @throws Exception
  66. */
  67. @RequestMapping(value = "/queryRoomByName", method = {RequestMethod.POST,RequestMethod.GET})
  68. private List<Room> queryRoomByName(@RequestBody String roomName) throws Exception {
  69. JSONObject jsonObject = JSONObject.parseObject(roomName);
  70. String name = jsonObject.getString("roomName");
  71. List<Room> list = roomService.findByName(name);
  72. return list;
  73. }
  74. /**
  75. * 根据会议室id查询会议室详情
  76. * @param roomID
  77. * @return
  78. * @throws Exception
  79. */
  80. @RequestMapping(value = "/queryRoomById", method = {RequestMethod.POST,RequestMethod.GET})
  81. private Room queryRoomById(@RequestBody String roomID) throws Exception {
  82. JSONObject jsonObject = JSONObject.parseObject(roomID);
  83. Integer id = jsonObject.getInteger("roomID");
  84. String date = jsonObject.getString("date");
  85. Room room = roomService.findById(id);
  86. return room;
  87. }
  88. /**
  89. * 查询所有的会议室预约记录
  90. * @return
  91. * @throws Exception
  92. */
  93. @RequestMapping("/showRecord")
  94. public List<ReservationVo> findAllReservation() throws Exception {
  95. List<ReservationVo> list = null;
  96. list = reservationService.findAllByPaging(1);
  97. return list;
  98. }
  99. /**
  100. * 根据会议室id查询会议室记录
  101. * @param id
  102. * @return
  103. * @throws Exception
  104. */
  105. @RequestMapping("/showRecordByRoomId")
  106. public List<ReservationVo> findAllReservationByRoomId(@RequestBody String id) throws Exception {
  107. JSONObject jsonObject = JSONObject.parseObject(id);
  108. Integer roomId = jsonObject.getInteger("id");
  109. List<ReservationVo> list = null;
  110. list = reservationService.findByRoomId(roomId);
  111. return list;
  112. }
  113. //搜索借用人
  114. /**
  115. * 搜索我组织的会议
  116. * @param userName
  117. * @return
  118. * @throws Exception
  119. */
  120. @RequestMapping(value = "/queryByUser",method = RequestMethod.POST)
  121. private List<ReservationVo> queryUser(@RequestBody String userName) throws Exception {
  122. JSONObject jsonObject = JSONObject.parseObject(userName);
  123. String name = jsonObject.getString("userName");
  124. List<ReservationVo> list = reservationService.queryByUser(name);
  125. return list;
  126. }
  127. //预约会议室页面跳转
  128. @RequestMapping(value = "/reserveRoom", method = RequestMethod.GET)
  129. public String reserveRoomUI(Model model) throws Exception {
  130. //从数据库查询出所有会议室信息回显到页面
  131. List<Room> list = roomService.nameList();
  132. model.addAttribute("nameList", list);
  133. return "/ordinary/reserveRoom";
  134. }
  135. /**
  136. * 预约会议室功能实现
  137. * @param reservation
  138. * @return
  139. * @throws Exception
  140. */
  141. @RequestMapping(value = "/reserveRoom", method = RequestMethod.POST)
  142. public String reserveRoom(@RequestBody String reservation) throws Exception {
  143. JSONObject jsonObject = JSONObject.parseObject(reservation);
  144. String roomName = jsonObject.getString("roomName");
  145. int roomId = jsonObject.getIntValue("roomId");
  146. int alarmminute = jsonObject.getIntValue("alarmminute");
  147. String userName = jsonObject.getString("userName");
  148. String department = jsonObject.getString("department");
  149. String theme = jsonObject.getString("theme");
  150. JSONArray enterUsers = jsonObject.getJSONArray("enterUsers");
  151. JSONArray dateArray = jsonObject.getJSONArray("dateArray");
  152. for (int i = 0; i <dateArray.size() ; i++) {
  153. JSONObject json = dateArray.getJSONObject(i);
  154. String date = json.getString("date");
  155. String beginTime = json.getString("beginTime");
  156. String endTime = json.getString("endTime");
  157. // String a = date+""+
  158. ReservationCustom reservationCustom = new ReservationCustom();
  159. reservationCustom.setDate(date);
  160. reservationCustom.setBeginTime(beginTime);
  161. reservationCustom.setEndTime(endTime);
  162. reservationCustom.setUser(userName);
  163. reservationCustom.setRoomId(roomId);
  164. reservationCustom.setMark("待审核");
  165. reservationCustom.setName(roomName);
  166. if(enterUsers != null){
  167. reservationCustom.setEnterUsers(enterUsers.toString());
  168. }
  169. reservationCustom.setTheme(theme);
  170. reservationCustom.setDepartment(department);
  171. reservationCustom.setAlarmminute(alarmminute);
  172. reservationService.addReservation(reservationCustom);
  173. }
  174. //发送消息
  175. //0. 获取所有人员
  176. JSONObject result = new JSONObject();
  177. JSONArray users = new JSONArray();
  178. users.add(userService.findUserById(userName).getFsuid());
  179. if(enterUsers !=null){
  180. for (int i = 0; i < enterUsers.size(); i++) {
  181. JSONObject object = enterUsers.getJSONObject(i);
  182. String id = object.getString("id");
  183. users.add(id);
  184. }
  185. }
  186. result.put("users",users);
  187. JSONObject message = new JSONObject();
  188. JSONObject head = new JSONObject();
  189. head.put("title","会议参加提醒");
  190. JSONObject first = new JSONObject();
  191. head.put("first","欢迎参加“"+userName+"”组织的【"+theme+"】会议,会议将于");
  192. // message.put("head,new )
  193. // {
  194. // "head":{
  195. // "title":"会议室提醒"
  196. // },
  197. // "first":{
  198. // "content":"2019-08-24"
  199. // }
  200. result.put("text","theme");
  201. // sendMessage(result.toString());
  202. return "success";
  203. }
  204. //取消预约申请页面跳转
  205. @RequestMapping(value = "/cancelApplication",method = RequestMethod.GET)
  206. public String cancelApplicationUI(String user,Model model) throws Exception{
  207. List<ReservationCustom> list=reservationService.findByUser(user);
  208. model.addAttribute("reserveList",list);
  209. return "/ordinary/cancelApplication";
  210. }
  211. /**
  212. * 根据预约id查看详情
  213. * @param id
  214. * @return
  215. * @throws Exception
  216. */
  217. @RequestMapping(value = "/findByReservationId", method = RequestMethod.POST)
  218. public ReservationVo findByReservationId(@RequestBody String id) throws Exception{
  219. JSONObject jsonObject = JSONObject.parseObject(id);
  220. Integer cancelID = jsonObject.getInteger("id");
  221. ReservationVo reservationVo =reservationService.findByReservationId(cancelID);
  222. return reservationVo;
  223. }
  224. //取消预约申请业务实现
  225. @RequestMapping(value = "/cancelApply", method = RequestMethod.POST)
  226. public ReservationVo cancelApplication(@RequestBody String id) throws Exception{
  227. JSONObject jsonObject = JSONObject.parseObject(id);
  228. Integer cancelID = jsonObject.getInteger("id");
  229. reservationService.cancelApplication(cancelID);
  230. ReservationVo reservationVo =reservationService.findByReservationId(cancelID);
  231. String user = reservationVo.getUser();
  232. String theme =reservationVo.getTheme();
  233. String StringEnterUsers = reservationVo.getEnterUsers();
  234. JSONArray enterUsers = JSONArray.parseArray(StringEnterUsers);
  235. //发送消息
  236. //0. 获取所有人员
  237. JSONObject result = new JSONObject();
  238. JSONArray users = new JSONArray();
  239. users.add(user);
  240. if(enterUsers!=null){
  241. for (int i = 0; i < enterUsers.size(); i++) {
  242. JSONObject object = enterUsers.getJSONObject(i);
  243. String uid= object.getString("id");
  244. users.add(uid);
  245. }
  246. }
  247. result.put("users",users);
  248. result.put("text","取消会议:"+theme);
  249. sendMessage(result.toString());
  250. return reservationVo;
  251. }
  252. //获取数字签名
  253. public void getSignature() throws Exception{
  254. //appId
  255. String appId = "FSAID_131860e";
  256. //appSecret
  257. String appSecret = "e1c53d3c6dff4e96b9ec155a031889af";
  258. //永久授权码
  259. String permanentCode = "B83ED308137C9653E543303740DB2533";
  260. JSONObject corpAccessTokenObj = getCorpAccessToken(appId,appSecret,permanentCode);
  261. corpAccessToken = corpAccessTokenObj.getString("corpAccessToken");
  262. corpId = corpAccessTokenObj.getString("corpId");
  263. }
  264. /**
  265. * 获取所有部门
  266. */
  267. @RequestMapping(value="/departmentList", method=RequestMethod.POST)
  268. @ResponseBody
  269. public String departmentList() throws Exception {
  270. getSignature();
  271. Map<String,Object> obj = new HashMap<String, Object>();
  272. obj.put("corpAccessToken",corpAccessToken);
  273. obj.put("corpId",corpId);
  274. String result = httpPostRequest(departmentURL, obj);
  275. return result;
  276. }
  277. /**
  278. * 获取部门下所有人员
  279. */
  280. @RequestMapping(value="/usertList", method=RequestMethod.POST)
  281. @ResponseBody
  282. public String usertList(@RequestBody String jsonStr) throws Exception {
  283. JSONObject jsonObject = JSONObject.parseObject(jsonStr);
  284. String departmentId = jsonObject.getString("departmentId");
  285. Map<String,Object> obj = new HashMap<String, Object>();
  286. obj.put("corpAccessToken",corpAccessToken);
  287. obj.put("corpId",corpId);
  288. obj.put("departmentId",Integer.valueOf(departmentId));
  289. String result = httpPostRequest(userURL, obj);
  290. return result;
  291. }
  292. private JSONObject getCorpAccessToken(String appId, String appSecret, String permanentCode) throws Exception{
  293. Map<String,Object> obj = new HashMap<String, Object>();
  294. obj.put("appId",appId);
  295. obj.put("appSecret",appSecret);
  296. obj.put("permanentCode",permanentCode);
  297. String result = httpPostRequest(corpAccessTokenURL, obj);
  298. JSONObject jsonObject = JSONObject.parseObject(result);
  299. return jsonObject;
  300. }
  301. /**
  302. * 执行http请求
  303. * @param httpClient
  304. * @param httpRequest
  305. * @return
  306. */
  307. private String executeHttpRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest){
  308. CloseableHttpResponse response = null;
  309. String respContent = null;
  310. try {
  311. response = httpClient.execute(httpRequest);
  312. respContent = EntityUtils.toString(response.getEntity(), "utf-8");
  313. } catch (Exception e) {
  314. }finally {
  315. try {
  316. httpClient.close();
  317. } catch (IOException e) {
  318. }
  319. if(response != null) {
  320. try {
  321. response.close();
  322. } catch (IOException e) {
  323. }
  324. }
  325. }
  326. return respContent;
  327. }
  328. public String httpPostRequest(String url, Map<String, Object> params) throws Exception {
  329. String respContent = null;
  330. CloseableHttpClient httpclient = HttpClients.createDefault();
  331. HttpPost httpPost = new HttpPost(url);
  332. httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
  333. // 设置请求的的参数
  334. JSONObject jsonParam = new JSONObject();
  335. Set<String> keySet = params.keySet();
  336. for (String key : keySet) {
  337. jsonParam.put(key, params.get(key));
  338. }
  339. httpPost.setEntity(new StringEntity(jsonParam.toString(), "utf-8"));
  340. // 执行请求
  341. respContent = executeHttpRequest(httpclient, httpPost);
  342. return respContent;
  343. }
  344. /**
  345. * 所有人员
  346. */
  347. @RequestMapping(value="/usertListFirst", method=RequestMethod.POST)
  348. @ResponseBody
  349. public String usertList() throws Exception {
  350. Map<String,Object> obj = new HashMap<String, Object>();
  351. obj.put("corpAccessToken",corpAccessToken);
  352. obj.put("corpId",corpId);
  353. String result = httpPostRequest(departmentURL, obj);
  354. JSONObject jsonObject = JSONObject.parseObject(result);
  355. JSONArray departments = jsonObject.getJSONArray("departments");
  356. JSONArray jsonArray = new JSONArray();
  357. Integer departmentId = 0;
  358. for (int i = 0; i < departments.size(); i++) {
  359. JSONObject department = (JSONObject)departments.get(i);
  360. Boolean isStop = department.getBoolean("isStop");
  361. if(isStop){
  362. continue;
  363. }
  364. departmentId = department.getInteger("id");
  365. //获取人员
  366. Map<String,Object> personobj = new HashMap<String, Object>();
  367. personobj.put("corpAccessToken",corpAccessToken);
  368. personobj.put("corpId",corpId);
  369. personobj.put("departmentId",departmentId);
  370. String resultStr = httpPostRequest(userURL, personobj);
  371. jsonObject = JSONObject.parseObject(resultStr);
  372. JSONArray userList = jsonObject.getJSONArray("userList");
  373. for (int j = 0; j < userList.size(); j++) {
  374. JSONObject jsonObject1 = new JSONObject();
  375. JSONObject user = (JSONObject)userList.get(j);
  376. Boolean stop = user.getBoolean("isStop");
  377. if(stop){
  378. continue;
  379. }
  380. String name = user.getString("name");
  381. String openUserId = user.getString("openUserId");
  382. String first = name;
  383. jsonObject1.put("name",name);
  384. jsonObject1.put("openUserId",openUserId);
  385. // System.out.println(PinyinUtils.getPinYinHeadChar(name));
  386. jsonObject1.put("first", PinyinUtils.getPinYinHeadChar(name));
  387. jsonArray.add(jsonObject1);
  388. }
  389. }
  390. return jsonArray.toJSONString();
  391. }
  392. /**
  393. * 发送消息
  394. */
  395. @RequestMapping(value="/sendMessage", method=RequestMethod.POST)
  396. @ResponseBody
  397. public String sendMessage(@RequestBody String jsonStr) throws Exception {
  398. getSignature();
  399. Map<String,Object> obj = new HashMap<String, Object>();
  400. obj.put("corpAccessToken",corpAccessToken);
  401. obj.put("corpId",corpId);
  402. JSONObject jsonObject = JSONObject.parseObject(jsonStr);
  403. //消息内容
  404. String text = jsonObject.getString("text");
  405. //发送人
  406. JSONArray users = jsonObject.getJSONArray("users");
  407. // JSONArray users = new JSONArray();
  408. // users.add("FSUID_782A9585B3CEB91B9DC0C709FA4D3119");
  409. obj.put("toUser",users);
  410. obj.put("msgType","text");
  411. JSONObject content = new JSONObject();
  412. content.put("content","luo nb");
  413. obj.put("text",content);
  414. String result = httpPostRequest(messageURL, obj);
  415. return result;
  416. }
  417. }