SaveMaintainancePlanMachineCmd.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2017-2020 吴学文 and java110 team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.java110.community.cmd.maintainancePlan;
  17. import com.alibaba.fastjson.JSONArray;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.java110.core.annotation.Java110Cmd;
  20. import com.java110.core.annotation.Java110Transactional;
  21. import com.java110.core.context.ICmdDataFlowContext;
  22. import com.java110.core.event.cmd.Cmd;
  23. import com.java110.core.event.cmd.CmdEvent;
  24. import com.java110.core.factory.GenerateCodeFactory;
  25. import com.java110.dto.equipmentAccount.EquipmentAccountDto;
  26. import com.java110.dto.maintainancePlanMachine.MaintainancePlanMachineDto;
  27. import com.java110.dto.maintainanceStandardItem.MaintainanceStandardItemDto;
  28. import com.java110.intf.common.IEquipmentAccountV1InnerServiceSMO;
  29. import com.java110.intf.community.IMaintainancePlanMachineV1InnerServiceSMO;
  30. import com.java110.po.maintainancePlanMachine.MaintainancePlanMachinePo;
  31. import com.java110.utils.exception.CmdException;
  32. import com.java110.utils.util.Assert;
  33. import com.java110.utils.util.BeanConvertUtil;
  34. import com.java110.vo.ResultVo;
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. import java.util.List;
  39. /**
  40. * 类表述:保存
  41. * 服务编码:maintainancePlanMachine.saveMaintainancePlanMachine
  42. * 请求路劲:/app/maintainancePlanMachine.SaveMaintainancePlanMachine
  43. * add by 吴学文 at 2022-11-07 02:22:18 mail: 928255095@qq.com
  44. * open source address: https://gitee.com/wuxw7/MicroCommunity
  45. * 官网:http://www.homecommunity.cn
  46. * 温馨提示:如果您对此文件进行修改 请不要删除原有作者及注释信息,请补充您的 修改的原因以及联系邮箱如下
  47. * // modify by 张三 at 2021-09-12 第10行在某种场景下存在某种bug 需要修复,注释10至20行 加入 20行至30行
  48. */
  49. @Java110Cmd(serviceCode = "maintainancePlan.saveMaintainancePlanMachine")
  50. public class SaveMaintainancePlanMachineCmd extends Cmd {
  51. private static Logger logger = LoggerFactory.getLogger(SaveMaintainancePlanMachineCmd.class);
  52. public static final String CODE_PREFIX_ID = "10";
  53. @Autowired
  54. private IMaintainancePlanMachineV1InnerServiceSMO maintainancePlanMachineV1InnerServiceSMOImpl;
  55. @Autowired
  56. private IEquipmentAccountV1InnerServiceSMO equipmentAccountV1InnerServiceSMOImpl;
  57. @Override
  58. public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
  59. Assert.hasKeyAndValue(reqJson, "planId", "请求报文中未包含planId");
  60. Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
  61. if(!reqJson.containsKey("machines")){
  62. throw new CmdException("未包含保养设备");
  63. }
  64. JSONArray items = reqJson.getJSONArray("machines");
  65. if(items.size() < 1){
  66. throw new CmdException("未包含保养设备");
  67. }
  68. String machineId = "";
  69. MaintainancePlanMachineDto maintainancePlanMachineDto = new MaintainancePlanMachineDto();
  70. maintainancePlanMachineDto.setPlanId(reqJson.getString("planId"));
  71. List<MaintainancePlanMachineDto> maintainancePlanMachineDtos = null;
  72. for(int itemIndex = 0; itemIndex < items.size(); itemIndex++) {
  73. machineId = items.getString(itemIndex);
  74. maintainancePlanMachineDto.setMachineId(machineId);
  75. maintainancePlanMachineDtos = maintainancePlanMachineV1InnerServiceSMOImpl.queryMaintainancePlanMachines(maintainancePlanMachineDto);
  76. if (maintainancePlanMachineDtos != null && maintainancePlanMachineDtos.size() >0) {
  77. throw new CmdException(maintainancePlanMachineDtos.get(0).getMachineName()+"已经添加");
  78. }
  79. }
  80. }
  81. @Override
  82. @Java110Transactional
  83. public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
  84. MaintainancePlanMachinePo maintainancePlanMachinePo = null;
  85. int flag = 0;
  86. JSONArray machineIds = reqJson.getJSONArray("machines");
  87. EquipmentAccountDto equipmentAccountDto = null;
  88. List<EquipmentAccountDto> accountDtos = null;
  89. for (int machineIndex = 0; machineIndex < machineIds.size(); machineIndex++) {
  90. maintainancePlanMachinePo = new MaintainancePlanMachinePo();
  91. maintainancePlanMachinePo.setCommunityId(reqJson.getString("communityId"));
  92. maintainancePlanMachinePo.setPlanId(reqJson.getString("planId"));
  93. maintainancePlanMachinePo.setMpmId(GenerateCodeFactory.getGeneratorId("11"));
  94. maintainancePlanMachinePo.setMachineId(machineIds.getString(machineIndex));
  95. equipmentAccountDto = new EquipmentAccountDto();
  96. equipmentAccountDto.setMachineId(machineIds.getString(machineIndex));
  97. accountDtos = equipmentAccountV1InnerServiceSMOImpl.queryEquipmentAccounts(equipmentAccountDto);
  98. Assert.listOnlyOne(accountDtos, "设备不存在");
  99. maintainancePlanMachinePo.setMachineName(accountDtos.get(0).getMachineName());
  100. flag = maintainancePlanMachineV1InnerServiceSMOImpl.saveMaintainancePlanMachine(maintainancePlanMachinePo);
  101. if(flag < 1){
  102. throw new CmdException("未包含保养设备");
  103. }
  104. }
  105. cmdDataFlowContext.setResponseEntity(ResultVo.success());
  106. }
  107. }