package com.persagy.proxy.adm.controller; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.databind.node.ObjectNode; import com.persagy.dmp.basic.utils.JsonNodeUtils; import com.persagy.proxy.adm.model.AdmEquipment; import com.persagy.proxy.adm.request.AdmCreateRequest; import com.persagy.proxy.adm.request.AdmQueryCriteria; import com.persagy.proxy.adm.request.AdmResponse; import com.persagy.proxy.adm.service.IAdmEquipmentService; import com.persagy.proxy.adm.utils.AdmContextUtil; import com.persagy.proxy.adm.utils.ObjectNameUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; /** * Adm 楼层 API * @author lvxy * @date 2021/8/16 */ @RestController @RequestMapping("/object/equip") public class AdmEquipmentController { @Autowired private IAdmEquipmentService service; /** * 统计 * * @param request * @return * @throws Exception */ @PostMapping("/count") public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception { request.setName(AdmEquipment.OBJ_TYPE); request.setOnlyCount(true); return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmEquipment.class); } /** * 创建 * @param createRequest * @return * @throws Exception */ @PostMapping("/create") public AdmResponse create(@RequestBody AdmCreateRequest createRequest) throws Exception { List vos = CollUtil.newArrayList(createRequest.getContent()); if(CollUtil.isEmpty(vos)){ return AdmResponse.success(); } //创建设备 vos.stream().forEach(equipment -> { if (StrUtil.isEmpty(equipment.getName())){ equipment.setName(ObjectNameUtil.objectName("设备-")); } }); vos = service.doInsertEuip(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } /** * 根据ID删除 * @param vos * @return * @throws Exception */ @PostMapping("/delete") public AdmResponse delete( @RequestBody List vos) throws Exception { // 组装上下文条件 service.doDelete(AdmContextUtil.toDmpContext(), vos); return AdmResponse.success(); } /** * 更新 * @param createRequest * @return * @throws Exception */ @PostMapping("/update") public AdmResponse update(@RequestBody AdmCreateRequest createRequest) throws Exception { List vos = CollUtil.newArrayList(createRequest.getContent()); //vos = service.doUpdateEuip(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); //针对设备更新时,所有更新的字段都放到信息点中的情况特殊处理 if(CollUtil.isNotEmpty(vos)){ List equipmentList = new ArrayList<>(vos.size()); vos.stream().forEach(equipment -> { if(CollUtil.isNotEmpty(equipment.getInfos())){ AdmEquipment admEquipment = new AdmEquipment(); admEquipment.setId(equipment.getId()); admEquipment.setInfos(equipment.getInfos()); ObjectNode objectNode = JsonNodeUtils.toObjectNode(admEquipment, "infos", null); AdmEquipment admEquipmentNew = JsonNodeUtils.toEntity(objectNode, AdmEquipment.class, "infos"); equipmentList.add(admEquipmentNew); } }); vos = equipmentList; } vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } /** * 查询 * * @param request * @return * @throws Exception */ @PostMapping("/query") public AdmResponse query(@RequestBody AdmQueryCriteria request) throws Exception { // 组装上下文条件 request.setName(AdmEquipment.OBJ_TYPE); return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmEquipment.class); } /** * 设备绑定的动态信息点或静态信息点 * @param admEquipment * @return * @throws Exception */ @GetMapping("/export") public AdmResponse exportInfos(@RequestBody AdmCreateRequest admEquipment) throws Exception { List vos = CollUtil.newArrayList(admEquipment.getContent()); vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } /** * 设备上传excel识别静态信息点 * @param admEquipment * @return * @throws Exception */ @PostMapping("/import") public AdmResponse importInfos(@RequestBody AdmCreateRequest admEquipment) throws Exception { List vos = CollUtil.newArrayList(admEquipment.getContent()); vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } /** * 根据项目id查询所有的设备和部件 * * @param request * @return * @throws Exception */ @GetMapping("/query-equip") public AdmResponse queryEquip(@RequestBody AdmQueryCriteria request) throws Exception { //request.setName("equipment"); if(StrUtil.isNotEmpty(request.getFilters())){ request.setFilters(String.join(";",request.getFilters(),"objType in ['equipment','component']")); }else{ request.setFilters("objType in ['equipment','component']"); } return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmEquipment.class); } /** * 批量创建设备同时创建资产、创建设备和系统的关系 * @param admEquipment * @return * @throws Exception */ @PostMapping("/creat-equip-pro") public AdmResponse createExt(@RequestBody AdmCreateRequest admEquipment) throws Exception { List vos = CollUtil.newArrayList(admEquipment.getContent()); if(CollUtil.isEmpty(vos)){ return AdmResponse.success(); } //创建设备 vos.stream().forEach(equipment -> { if (StrUtil.isEmpty(equipment.getName())){ equipment.setName(ObjectNameUtil.objectName("设备-")); } }); vos = service.doInsertExt(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos); return AdmResponse.success(vos); } }