MouldServiceImpl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package com.hssx.cloudmodel.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.github.pagehelper.PageHelper;
  6. import com.github.pagehelper.PageInfo;
  7. import com.hssx.cloudmodel.constant.Constant;
  8. import com.hssx.cloudmodel.entity.*;
  9. import com.hssx.cloudmodel.entity.vo.MouldEquipmentVO;
  10. import com.hssx.cloudmodel.entity.vo.MouldVO;
  11. import com.hssx.cloudmodel.entity.vo.ProjectVO;
  12. import com.hssx.cloudmodel.entity.vo.UserVO;
  13. import com.hssx.cloudmodel.mapper.*;
  14. import com.hssx.cloudmodel.service.MouldService;
  15. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  16. import com.hssx.cloudmodel.util.HttpKit;
  17. import com.hssx.cloudmodel.util.HttpRespMsg;
  18. import com.hssx.cloudmodel.util.ListUtil;
  19. import com.hssx.cloudmodel.util.WechatTemplateMessage;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.commons.lang3.StringEscapeUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. import javax.annotation.Resource;
  25. import java.io.IOException;
  26. import java.security.KeyManagementException;
  27. import java.security.NoSuchAlgorithmException;
  28. import java.security.NoSuchProviderException;
  29. import java.text.SimpleDateFormat;
  30. import java.time.LocalDateTime;
  31. import java.util.*;
  32. /**
  33. * <p>
  34. * 服务实现类
  35. * </p>
  36. *
  37. * @author 吴涛涛
  38. * @since 2019-07-30
  39. */
  40. @Service
  41. @Slf4j
  42. public class MouldServiceImpl extends ServiceImpl<MouldMapper, Mould> implements MouldService {
  43. @Resource
  44. MouldMapper mouldMapper;
  45. @Resource
  46. UserMapper userMapper;
  47. @Resource
  48. PowerMapper powerMapper;
  49. @Resource
  50. ProjectMapper projectMapper;
  51. @Resource
  52. ProjectApproveMapper projectApproveMapper;
  53. @Resource
  54. CustomCompanyMapper customCompanyMapper;
  55. @Resource
  56. CompanyMapper companyMapper;
  57. @Resource
  58. ProjectUserMapper projectUserMapper;
  59. @Resource
  60. NewsNoticeMapper newsNoticeMapper;
  61. @Resource
  62. NewsNoticeUserMapper newsNoticeUserMapper;
  63. @Override
  64. public HttpRespMsg addAndUpdateMould(Mould mould, User user) {
  65. HttpRespMsg msg = new HttpRespMsg();
  66. if (mould.getId() != null) {
  67. //修改
  68. Mould m = mouldMapper.selectOne(new QueryWrapper<Mould>().eq("model_no", mould.getModelNo()));
  69. if ((m != null && m.getId() == mould.getId()) || m == null) {
  70. if(mould.getProduceCompanyId() != null){
  71. Company company = companyMapper.selectById(mould.getProduceCompanyId());
  72. mould.setArea(company.getCompanyAddress());
  73. }
  74. mouldMapper.updateById(mould);
  75. } else {
  76. msg.setError("当前模具编号已存在,请重新输入其他模具编号");
  77. }
  78. } else {
  79. //查询当前模具编号的模具是否存在
  80. Integer count = mouldMapper.selectCount(new QueryWrapper<Mould>().eq("model_no", mould.getModelNo()));
  81. if (count > 0) {
  82. msg.setError("当前模具编号已存在,请重新输入其他模具编号");
  83. } else {
  84. mould.setCreatorId(user.getId());
  85. mould.setCompanyId(user.getCompanyId());
  86. mouldMapper.insert(mould);
  87. }
  88. }
  89. return msg;
  90. }
  91. @Override
  92. public HttpRespMsg getMoildDetail(MouldVO mouldVO) {
  93. HttpRespMsg msg = new HttpRespMsg();
  94. Map<String, Object> map = new HashMap<>();
  95. MouldVO mould = mouldMapper.getDetailById(mouldVO);
  96. map.put("vo", mould);
  97. ProjectVO vo = projectMapper.getProjectById(mould.getProjectId());
  98. User currentUser = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", mouldVO.getToken()));
  99. if (null == mould.getProjectId()) {
  100. map.put("update", 0);
  101. map.put("download", 0);
  102. map.put("view", 0);
  103. map.put("approve", 0);
  104. } else {
  105. List<CustomCompany> list = customCompanyMapper.selectList(new QueryWrapper<CustomCompany>().eq("project_id", mould.getProjectId()));
  106. map.put("customCompany", list);
  107. if (Constant.SYS_PARENT_ID == currentUser.getParentId()) {
  108. //当前人超级管理员 ,对项目只可以浏览
  109. map.put("update", 0);
  110. map.put("download", 0);
  111. map.put("view", 1);
  112. map.put("approve", 0);
  113. } else if (Constant.SYS_ID == currentUser.getParentId()) {
  114. //当前为系统管理员
  115. map.put("update", 1);
  116. map.put("download", 1);
  117. map.put("view", 1);
  118. map.put("approve", 0);
  119. } else if (vo.getManagerId() == currentUser.getId()) {
  120. //当前人是该项目的项目经理
  121. Integer approve = 0;
  122. if (projectApproveMapper.selectCount(new QueryWrapper<ProjectApprove>().eq("approver_id", currentUser.getId())) > 0) {
  123. //查看当前项目经理是否为审批人
  124. approve = 1;
  125. }
  126. map.put("update", 1);
  127. map.put("download", 1);
  128. map.put("view", 1);
  129. map.put("approve", approve);
  130. } else {
  131. //该项目的参与人
  132. Integer update = 0;
  133. Integer download = 0;
  134. Integer view = 0;
  135. Integer approve = 0;
  136. List<Power> powers = powerMapper.selectList(new QueryWrapper<Power>().eq("project_id", mould.getProjectId()).eq("user_id", currentUser.getId()));
  137. if (powers.size() > 0) {
  138. for (Power power : powers) {
  139. if (power.getPowerType() == 0) {
  140. update = 1;
  141. } else if (power.getPowerType() == 1) {
  142. download = 1;
  143. } else if (power.getPowerType() == 2) {
  144. view = 1;
  145. } else {
  146. approve = 1;
  147. }
  148. }
  149. }
  150. map.put("update", update);
  151. map.put("download", download);
  152. map.put("view", view);
  153. map.put("approve", approve);
  154. }
  155. }
  156. msg.data = map;
  157. return msg;
  158. }
  159. @Override
  160. public HttpRespMsg maintenanceReminder() throws Exception {
  161. HttpRespMsg msg = new HttpRespMsg();
  162. //查询所有被分配到项目的模具
  163. List<Mould> moulds = mouldMapper.selectList(new QueryWrapper<Mould>().isNotNull("project_id").isNotNull("maintain_count"));
  164. for (Mould mould : moulds) {
  165. List<Integer> counts = ListUtil.convertIntegerIdsArrayToList(mould.getMaintainCount());
  166. Collections.sort(counts);
  167. Integer noticeCount = 0;
  168. if (counts.size() > 0) {
  169. for (Integer count : counts) {
  170. if (count <= mould.getRunTimes()) {
  171. noticeCount = count;
  172. }
  173. }
  174. }
  175. if (noticeCount != 0) {
  176. //需要保养
  177. Mould m = new Mould();
  178. m.setId(mould.getId());
  179. m.setIsMaintain(1);
  180. mouldMapper.updateById(m);
  181. // //提示保养,向模具资产方人员公众号推送消息
  182. // User user = userMapper.selectOne(new QueryWrapper<User>().eq("parent_id", Constant.SYS_ID).eq("company_id", mould.getCompanyId()));
  183. // MouldEquipmentVO mouldEquipmentVO = new MouldEquipmentVO();
  184. // mouldEquipmentVO.setPlanType(Constant.PLAN_TYPE);
  185. // mouldEquipmentVO.setArea(mould.getArea());
  186. // mouldEquipmentVO.setName(mould.getModelName());
  187. // msg = sendMaintainTemplateMessage(Constant.MAINTAIN_NOTICE_TEMPLATE_ID, user.getOpenid(), Constant.WECHAT_APPID, Constant.WECHAT_SECRET, mouldEquipmentVO);
  188. //添加通知消息
  189. Project project = projectMapper.selectById(mould.getProjectId());
  190. //查询到参与该项目的人
  191. List<Integer> userIds = new ArrayList<>();
  192. userIds.add(project.getCreatorId());
  193. userIds.add(project.getManagerId());
  194. List<ProjectUser> projectUsers = projectUserMapper.selectList(new QueryWrapper<ProjectUser>().eq("project_id", project.getId()));
  195. for (ProjectUser projectUser : projectUsers) {
  196. userIds.add(projectUser.getUserId());
  197. }
  198. List<ProjectApprove> projectApproves = projectApproveMapper.selectList(new QueryWrapper<ProjectApprove>().eq("project_id", project.getId()));
  199. for (ProjectApprove projectApprove : projectApproves) {
  200. userIds.add(projectApprove.getApproverId());
  201. }
  202. List<User> users = userMapper.selectList(new QueryWrapper<User>().in("id", userIds));
  203. //查询到之前模具保养的消息
  204. NewsNotice oldNews = newsNoticeMapper.selectOne(new QueryWrapper<NewsNotice>().eq("ref_id", mould.getId()).eq("notice_type", 1));
  205. NewsNotice newsNotice = new NewsNotice();
  206. if(oldNews != null){
  207. NewsNotice newNews = new NewsNotice();
  208. newNews.setId(oldNews.getId());
  209. oldNews.setIndate(LocalDateTime.now());
  210. newsNoticeMapper.updateById(oldNews);
  211. newsNoticeUserMapper.updateNewsNoticeUserByNewsNoticeId(oldNews.getId());
  212. // for (User u : users) {
  213. // //添加通知的消息
  214. // NewsNoticeUser newsNoticeUser = new NewsNoticeUser();
  215. // newsNoticeUser.setNewsId(oldNews.getId());
  216. // newsNoticeUser.setUserId(u.getId());
  217. // newsNoticeUser.setIsRead(0);
  218. // newsNoticeUserMapper.updateById(newsNoticeUser);
  219. // }
  220. }else{
  221. newsNotice.setNoticeType(Constant.MAINTAIN_TYPE);
  222. newsNotice.setProjectId(project.getId());
  223. newsNotice.setProjectName(project.getProjectName());
  224. newsNotice.setRefId(mould.getId());
  225. newsNotice.setContent(Constant.MAIN_TAIN_NOTICE);
  226. newsNoticeMapper.insert(newsNotice);
  227. for (User u : users) {
  228. //添加通知的消息
  229. NewsNoticeUser newsNoticeUser = new NewsNoticeUser();
  230. newsNoticeUser.setNewsId(newsNotice.getId());
  231. newsNoticeUser.setUserId(u.getId());
  232. newsNoticeUserMapper.insert(newsNoticeUser);
  233. }
  234. }
  235. }
  236. }
  237. return msg;
  238. }
  239. @Override
  240. public HttpRespMsg delMoule(Mould mould) {
  241. HttpRespMsg msg = new HttpRespMsg();
  242. Mould newMould = mouldMapper.selectById(mould.getId());
  243. if (null != mould.getProjectId()) {
  244. msg.setError("该模具已被应用到项目,不提供删除操作");
  245. } else {
  246. mouldMapper.deleteById(newMould.getId());
  247. }
  248. return msg;
  249. }
  250. @Override
  251. public HttpRespMsg isMaintain(Mould mould) {
  252. HttpRespMsg msg = new HttpRespMsg();
  253. if (mould.getId() != null) {
  254. mouldMapper.updateById(mould);
  255. } else {
  256. msg.setError("模具id不存在");
  257. }
  258. return msg;
  259. }
  260. @Override
  261. public HttpRespMsg maintainMouldList(UserVO userVO) {
  262. HttpRespMsg msg = new HttpRespMsg();
  263. User currentUser = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", userVO.getToken()));
  264. List<Integer> set = new ArrayList<>();
  265. set.add(-1);
  266. List<Mould> moulds = new ArrayList<>();
  267. if (currentUser.getId() != null) {
  268. //资产方管理员,获取他公司下的所有模具
  269. if (Constant.SYS_ID == currentUser.getParentId()) {
  270. List<Project> projects = projectMapper.selectList(new QueryWrapper<Project>().eq("creator_id", currentUser.getId()));
  271. for (Project project : projects) {
  272. set.add(project.getId());
  273. }
  274. } else {
  275. //普通用户或者项目经理
  276. //充当项目经理参与的项目
  277. QueryWrapper<Project> qw = new QueryWrapper<>();
  278. qw.eq("manager_id", currentUser.getId());
  279. List<Project> projects = projectMapper.selectList(qw);
  280. if (projects.size() > 0) {
  281. for (Project project : projects) {
  282. set.add(project.getId());
  283. }
  284. }
  285. // //充当普通人员参与的项目
  286. List<ProjectUser> projectUsers = projectUserMapper.selectList(new QueryWrapper<ProjectUser>().eq("user_id", currentUser.getId()));
  287. if (projectUsers.size() > 0) {
  288. for (ProjectUser projectUser : projectUsers) {
  289. set.add(projectUser.getProjectId());
  290. }
  291. }
  292. // //充当审批人员参与的项目
  293. List<ProjectApprove> projectss = projectApproveMapper.selectList(new QueryWrapper<ProjectApprove>().eq("approver_id", currentUser.getId()));
  294. if (projectss.size() > 0) {
  295. for (ProjectApprove projectUser : projectss) {
  296. set.add(projectUser.getProjectId());
  297. }
  298. }
  299. }
  300. moulds = mouldMapper.selectList(new QueryWrapper<Mould>().in("project_id",set));
  301. msg.data = moulds;
  302. } else {
  303. msg.setError("用户不存在,或者未登录!");
  304. }
  305. return msg;
  306. }
  307. //告警模板推送通用接口
  308. /**
  309. * templateId 模板id,touserOpenId 被推送者的openId,appId微信公众号的appId
  310. * firstData 推送标题
  311. */
  312. public HttpRespMsg sendEmergencyTemplateMessage(String templateId, String touserOpenId, String appId, String secret, MouldEquipmentVO mouldEquipmentVO) throws Exception {
  313. HttpRespMsg msg = new HttpRespMsg();
  314. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
  315. + appId + "&secret=" + secret;
  316. String resp;
  317. String resp1;
  318. resp1 = HttpKit.get(url, true);
  319. resp1 = StringEscapeUtils.unescapeJava(resp1);
  320. JSONObject json = (JSONObject) JSON.parse(resp1);
  321. // 获取值赋值给全局变量
  322. if (!json.containsKey("errcode")) {
  323. String newAccessToken = json.getString("access_token");
  324. String url1 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
  325. + newAccessToken;
  326. WechatTemplateMessage wechat = new WechatTemplateMessage();
  327. wechat.setTemplate_id(templateId);
  328. wechat.setTouser(touserOpenId);
  329. wechat.setAppid(appId);
  330. Map<String, Map<String, String>> data = new HashMap<>();
  331. Map<String, String> first = new HashMap<>();
  332. Map<String, String> value1 = new HashMap<>();
  333. Map<String, String> value2 = new HashMap<>();
  334. Map<String, String> value3 = new HashMap<>();
  335. Map<String, String> value4 = new HashMap<>();
  336. Map<String, String> remark = new HashMap<>();
  337. // 推送信息主体
  338. first.put("value", "告警通知");//firstData推送标题
  339. data.put("first", first);
  340. value1.put("value", mouldEquipmentVO.getEquipmentName());
  341. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  342. data.put("keyword1", value1);
  343. value2.put("value", mouldEquipmentVO.getEmergencyType());
  344. data.put("keyword2", value2);
  345. value3.put("value", sdf.format(new Date()));
  346. data.put("keyword3", value3);
  347. value4.put("value", mouldEquipmentVO.getEmergencyContent());
  348. data.put("keyword4", value4);
  349. remark.put("value", "请尽快检查该设备");
  350. data.put("remark", remark);
  351. wechat.setData(data);
  352. String jsonString = JSONObject.toJSONString(wechat);
  353. // System.out.println("jsonString"+jsonString);
  354. resp = HttpKit.post(url1, jsonString);
  355. // System.out.println("resp0"+resp);
  356. resp = StringEscapeUtils.unescapeJava(resp);
  357. // System.out.println("resp"+resp);
  358. json = (JSONObject) JSON.parse(resp);
  359. }
  360. return msg;
  361. }
  362. //保养
  363. /**
  364. * templateId 模板id,touserOpenId 被推送者的openId,appId微信公众号的appId
  365. * firstData 推送标题
  366. */
  367. public HttpRespMsg sendMaintainTemplateMessage(String templateId, String touserOpenId, String appId, String secret, MouldEquipmentVO mouldEquipmentVO) throws Exception {
  368. HttpRespMsg msg = new HttpRespMsg();
  369. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
  370. + appId + "&secret=" + secret;
  371. String resp;
  372. String resp1;
  373. resp1 = HttpKit.get(url, true);
  374. resp1 = StringEscapeUtils.unescapeJava(resp1);
  375. JSONObject json = (JSONObject) JSON.parse(resp1);
  376. // 获取值赋值给全局变量
  377. if (!json.containsKey("errcode")) {
  378. String newAccessToken = json.getString("access_token");
  379. String url1 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
  380. + newAccessToken;
  381. WechatTemplateMessage wechat = new WechatTemplateMessage();
  382. wechat.setTemplate_id(templateId);
  383. wechat.setTouser(touserOpenId);
  384. wechat.setAppid(appId);
  385. Map<String, Map<String, String>> data = new HashMap<>();
  386. Map<String, String> first = new HashMap<>();
  387. Map<String, String> value1 = new HashMap<>();
  388. Map<String, String> value2 = new HashMap<>();
  389. Map<String, String> value3 = new HashMap<>();
  390. Map<String, String> value4 = new HashMap<>();
  391. Map<String, String> remark = new HashMap<>();
  392. // 推送信息主体
  393. first.put("value", "你好,你有新的保养通知");//firstData推送标题
  394. data.put("first", first);
  395. value1.put("value", mouldEquipmentVO.getEquipmentName());
  396. SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
  397. data.put("keyword1", value1);
  398. value2.put("value", mouldEquipmentVO.getArea());
  399. data.put("keyword2", value2);
  400. value3.put("value", sdf.format(new Date()));
  401. data.put("keyword3", value3);
  402. value4.put("value", mouldEquipmentVO.getPlanType());
  403. data.put("keyword4", value4);
  404. remark.put("value", "模具初始模次不满足运行了");
  405. data.put("remark", remark);
  406. wechat.setData(data);
  407. String jsonString = JSONObject.toJSONString(wechat);
  408. // System.out.println("jsonString"+jsonString);
  409. resp = HttpKit.post(url1, jsonString);
  410. // System.out.println("resp0"+resp);
  411. resp = StringEscapeUtils.unescapeJava(resp);
  412. // System.out.println("resp"+resp);
  413. json = (JSONObject) JSON.parse(resp);
  414. }
  415. return msg;
  416. }
  417. }