MouldServiceImpl.java 17 KB

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