MouldServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.mapper.*;
  11. import com.hssx.cloudmodel.service.MouldService;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import com.hssx.cloudmodel.util.HttpKit;
  14. import com.hssx.cloudmodel.util.HttpRespMsg;
  15. import com.hssx.cloudmodel.util.WechatTemplateMessage;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.StringEscapeUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import javax.annotation.Resource;
  21. import java.io.IOException;
  22. import java.security.KeyManagementException;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.security.NoSuchProviderException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  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. @Override
  56. public HttpRespMsg addAndUpdateMould(Mould mould, User user) {
  57. HttpRespMsg msg = new HttpRespMsg();
  58. if (mould.getId() != null) {
  59. //修改
  60. Mould m = mouldMapper.selectOne(new QueryWrapper<Mould>().eq("model_no", mould.getModelNo()));
  61. if ((m != null && m.getId() == mould.getId()) || m == null) {
  62. Company company = companyMapper.selectById(mould.getProduceCompanyId());
  63. mould.setArea(company.getCompanyAddress());
  64. mouldMapper.updateById(mould);
  65. } else {
  66. msg.setError("当前模具编号已存在,请重新输入其他模具编号");
  67. }
  68. } else {
  69. //查询当前模具编号的模具是否存在
  70. Integer count = mouldMapper.selectCount(new QueryWrapper<Mould>().eq("model_no", mould.getModelNo()));
  71. if (count > 0) {
  72. msg.setError("当前模具编号已存在,请重新输入其他模具编号");
  73. } else {
  74. mould.setCreatorId(user.getId());
  75. mould.setCompanyId(user.getCompanyId());
  76. mouldMapper.insert(mould);
  77. }
  78. }
  79. return msg;
  80. }
  81. @Override
  82. public HttpRespMsg getMoildDetail(MouldVO mouldVO) {
  83. HttpRespMsg msg = new HttpRespMsg();
  84. Map<String, Object> map = new HashMap<>();
  85. MouldVO mould = mouldMapper.getDetailById(mouldVO);
  86. map.put("vo",mould);
  87. ProjectVO vo = projectMapper.getProjectById(mould.getProjectId());
  88. User currentUser = userMapper.selectOne(new QueryWrapper<User>().eq("head_imgurl", mouldVO.getToken()));
  89. if (null == mould.getProjectId()) {
  90. map.put("update", 0);
  91. map.put("download", 0);
  92. map.put("view", 0);
  93. map.put("approve", 0);
  94. } else {
  95. List<CustomCompany> list = customCompanyMapper.selectList(new QueryWrapper<CustomCompany>().eq("project_id",mould.getProjectId()));
  96. map.put("customCompany",list);
  97. if (Constant.SYS_PARENT_ID == currentUser.getParentId()) {
  98. //当前人超级管理员 ,对项目只可以浏览
  99. map.put("update", 0);
  100. map.put("download", 0);
  101. map.put("view", 1);
  102. map.put("approve", 0);
  103. } else if (Constant.SYS_ID == currentUser.getParentId()) {
  104. //当前为系统管理员
  105. map.put("update", 1);
  106. map.put("download", 1);
  107. map.put("view", 1);
  108. map.put("approve", 0);
  109. } else if (vo.getManagerId() == currentUser.getId()) {
  110. //当前人是该项目的项目经理
  111. Integer approve = 0;
  112. if (projectApproveMapper.selectCount(new QueryWrapper<ProjectApprove>().eq("approver_id", currentUser.getId())) > 0) {
  113. //查看当前项目经理是否为审批人
  114. approve = 1;
  115. }
  116. map.put("update", 1);
  117. map.put("download", 1);
  118. map.put("view", 1);
  119. map.put("approve", approve);
  120. } else {
  121. //该项目的参与人
  122. Integer update = 0;
  123. Integer download = 0;
  124. Integer view = 0;
  125. Integer approve = 0;
  126. List<Power> powers = powerMapper.selectList(new QueryWrapper<Power>().eq("project_id", mould.getProjectId()).eq("user_id", currentUser.getId()));
  127. if (powers.size() > 0) {
  128. for (Power power : powers) {
  129. if (power.getPowerType() == 0) {
  130. update = 1;
  131. } else if (power.getPowerType() == 1) {
  132. download = 1;
  133. } else if (power.getPowerType() == 2) {
  134. view = 1;
  135. } else {
  136. approve = 1;
  137. }
  138. }
  139. }
  140. map.put("update", update);
  141. map.put("download", download);
  142. map.put("view", view);
  143. map.put("approve", approve);
  144. }
  145. }
  146. msg.data = map;
  147. return msg;
  148. }
  149. @Override
  150. public HttpRespMsg maintenanceReminder() throws Exception {
  151. HttpRespMsg msg = new HttpRespMsg();
  152. //查询所有被分配到项目的模具
  153. List<Mould> moulds = mouldMapper.selectList(new QueryWrapper<Mould>().isNotNull("project_id"));
  154. for (Mould mould : moulds) {
  155. if(mould.getInitialModulus()<mould.getRunTimes()){
  156. //提示保养,向模具资产方人员公众号推送消息
  157. User user = userMapper.selectOne(new QueryWrapper<User>().eq("parent_id", Constant.SYS_ID).eq("company_id", mould.getCompanyId()));
  158. MouldEquipmentVO mouldEquipmentVO = new MouldEquipmentVO();
  159. mouldEquipmentVO.setPlanType(Constant.PLAN_TYPE);
  160. mouldEquipmentVO.setArea(mould.getArea());
  161. mouldEquipmentVO.setName(mould.getModelName());
  162. msg = sendMaintainTemplateMessage(Constant.MAINTAIN_NOTICE_TEMPLATE_ID,user.getOpenid(),Constant.WECHAT_APPID,Constant.WECHAT_SECRET,mouldEquipmentVO);
  163. //添加通知消息
  164. Project project = projectMapper.selectById(mould.getProjectId());
  165. //查询到参与该项目的人
  166. }
  167. }
  168. return null;
  169. }
  170. //告警模板推送通用接口
  171. /**
  172. * templateId 模板id,touserOpenId 被推送者的openId,appId微信公众号的appId
  173. * firstData 推送标题
  174. */
  175. public HttpRespMsg sendEmergencyTemplateMessage(String templateId, String touserOpenId, String appId, String secret, MouldEquipmentVO mouldEquipmentVO) throws Exception {
  176. HttpRespMsg msg = new HttpRespMsg();
  177. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
  178. + appId + "&secret=" + secret;
  179. String resp;
  180. String resp1;
  181. resp1 = HttpKit.get(url, true);
  182. resp1 = StringEscapeUtils.unescapeJava(resp1);
  183. JSONObject json = (JSONObject) JSON.parse(resp1);
  184. // 获取值赋值给全局变量
  185. if (!json.containsKey("errcode")) {
  186. String newAccessToken = json.getString("access_token");
  187. String url1 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
  188. + newAccessToken;
  189. WechatTemplateMessage wechat = new WechatTemplateMessage();
  190. wechat.setTemplate_id(templateId);
  191. wechat.setTouser(touserOpenId);
  192. wechat.setAppid(appId);
  193. Map<String, Map<String, String>> data = new HashMap<>();
  194. Map<String, String> first = new HashMap<>();
  195. Map<String, String> value1 = new HashMap<>();
  196. Map<String, String> value2 = new HashMap<>();
  197. Map<String, String> value3 = new HashMap<>();
  198. Map<String, String> value4 = new HashMap<>();
  199. Map<String, String> remark = new HashMap<>();
  200. // 推送信息主体
  201. first.put("value", "告警通知");//firstData推送标题
  202. data.put("first", first);
  203. value1.put("value", mouldEquipmentVO.getEquipmentName());
  204. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  205. data.put("keyword1", value1);
  206. value2.put("value", mouldEquipmentVO.getEmergencyType());
  207. data.put("keyword2", value2);
  208. value3.put("value", sdf.format(new Date()));
  209. data.put("keyword3", value3);
  210. value4.put("value", mouldEquipmentVO.getEmergencyContent());
  211. data.put("keyword4", value4);
  212. remark.put("value", "请尽快检查该设备");
  213. data.put("remark", remark);
  214. wechat.setData(data);
  215. String jsonString = JSONObject.toJSONString(wechat);
  216. // System.out.println("jsonString"+jsonString);
  217. resp = HttpKit.post(url1, jsonString);
  218. // System.out.println("resp0"+resp);
  219. resp = StringEscapeUtils.unescapeJava(resp);
  220. // System.out.println("resp"+resp);
  221. json = (JSONObject) JSON.parse(resp);
  222. }
  223. return msg;
  224. }
  225. //保养
  226. /**
  227. * templateId 模板id,touserOpenId 被推送者的openId,appId微信公众号的appId
  228. * firstData 推送标题
  229. */
  230. public HttpRespMsg sendMaintainTemplateMessage(String templateId, String touserOpenId, String appId, String secret, MouldEquipmentVO mouldEquipmentVO) throws Exception {
  231. HttpRespMsg msg = new HttpRespMsg();
  232. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
  233. + appId + "&secret=" + secret;
  234. String resp;
  235. String resp1;
  236. resp1 = HttpKit.get(url, true);
  237. resp1 = StringEscapeUtils.unescapeJava(resp1);
  238. JSONObject json = (JSONObject) JSON.parse(resp1);
  239. // 获取值赋值给全局变量
  240. if (!json.containsKey("errcode")) {
  241. String newAccessToken = json.getString("access_token");
  242. String url1 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
  243. + newAccessToken;
  244. WechatTemplateMessage wechat = new WechatTemplateMessage();
  245. wechat.setTemplate_id(templateId);
  246. wechat.setTouser(touserOpenId);
  247. wechat.setAppid(appId);
  248. Map<String, Map<String, String>> data = new HashMap<>();
  249. Map<String, String> first = new HashMap<>();
  250. Map<String, String> value1 = new HashMap<>();
  251. Map<String, String> value2 = new HashMap<>();
  252. Map<String, String> value3 = new HashMap<>();
  253. Map<String, String> value4 = new HashMap<>();
  254. Map<String, String> remark = new HashMap<>();
  255. // 推送信息主体
  256. first.put("value", "你好,你有新的保养通知");//firstData推送标题
  257. data.put("first", first);
  258. value1.put("value", mouldEquipmentVO.getEquipmentName());
  259. SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
  260. data.put("keyword1", value1);
  261. value2.put("value", mouldEquipmentVO.getArea());
  262. data.put("keyword2", value2);
  263. value3.put("value", sdf.format(new Date()));
  264. data.put("keyword3", value3);
  265. value4.put("value", mouldEquipmentVO.getPlanType());
  266. data.put("keyword4", value4);
  267. remark.put("value", "模具初始模次不满足运行了");
  268. data.put("remark", remark);
  269. wechat.setData(data);
  270. String jsonString = JSONObject.toJSONString(wechat);
  271. // System.out.println("jsonString"+jsonString);
  272. resp = HttpKit.post(url1, jsonString);
  273. // System.out.println("resp0"+resp);
  274. resp = StringEscapeUtils.unescapeJava(resp);
  275. // System.out.println("resp"+resp);
  276. json = (JSONObject) JSON.parse(resp);
  277. }
  278. return msg;
  279. }
  280. }