package com.vci.web.service.impl; import com.vci.client.common.providers.ServiceProvider; import com.vci.corba.common.PLException; import com.vci.corba.omd.stm.StatePool; import com.vci.starter.web.annotation.log.VciUnLog; import com.vci.starter.web.exception.VciBaseException; import com.vci.starter.web.pagemodel.BaseQueryObject; import com.vci.starter.web.pagemodel.DataGrid; import com.vci.starter.web.pagemodel.PageHelper; import com.vci.starter.web.util.LangBaseUtil; import com.vci.starter.web.util.VciBaseUtil; import com.vci.starter.web.util.VciDateUtil; import com.vci.web.dto.OsStatusDTO; import com.vci.web.model.OsStatusDO; import com.vci.web.pageModel.OsStatusVO; import com.vci.web.service.OsLifeCycleServiceI; import com.vci.web.service.OsStatusServiceI; import com.vci.web.service.WebBoServiceI; import com.vci.web.util.PlatformClientUtil; import com.vci.web.util.WebUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.*; import java.util.stream.Collectors; import static com.vci.frameworkcore.constant.FrameWorkBusLangCodeConstant.DATA_OID_NOT_EXIST; /** * 状态的服务 * @author weidy * @date 2021-2-14 */ @Service public class OsStatusServiceImpl implements OsStatusServiceI { /** * 日志 */ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 平台的客户端 */ @Autowired private PlatformClientUtil platformClientUtil; /** * 业务 */ @Autowired private WebBoServiceI boService; /** * 生命周期的服务 */ @Autowired(required = false) @Lazy private OsLifeCycleServiceI lifeCycleService; /** * 加载自身 */ @Autowired(required = false) @Lazy private OsStatusServiceI self; /** * 数据对象转换为显示对象 * * @param statePool 状态池的数据对象 * @return 显示对象 */ @Override public OsStatusVO statusDO2VO(com.vci.corba.omd.stm.StatePool statePool) { OsStatusVO statusVO = new OsStatusVO(); if(statePool!=null){ statusVO.setOid(statePool.oid); statusVO.setCreator(statePool.creator); statusVO.setLastModifier(statePool.modifier); try { statusVO.setCreateTime(new Date(statePool.createTime)); statusVO.setLastModifyTime(new Date(statePool.modifyTime)); statusVO.setTs(VciDateUtil.str2Date(statePool.ts,VciDateUtil.DateTimeMillFormat)); } catch (Exception e) { e.printStackTrace(); } statusVO.setDescription(statePool.description); statusVO.setId(statePool.name); statusVO.setName(statePool.tag); } return statusVO; } /** * 数据对象转换为显示对象 * * @param statePools 状态池的数据对象 集合 * @return 显示对象 */ @Override public List statusDO2VOs(Collection statePools) { List statusVOS = new ArrayList<>(); if(!CollectionUtils.isEmpty(statePools)){ statePools.stream().forEach(statePool -> { OsStatusVO statusVO = statusDO2VO(statePool); statusVOS.add(statusVO); }); } return statusVOS; } /** * 查询所有的状态 * * @return 状态的显示对象 */ @Override @VciUnLog public List selectAllStatus() { try { // return statusDO2VOs(Arrays.stream(platformClientUtil.getStatePoolService().getStatePools()).collect(Collectors.toList())); return statusDO2VOs(Arrays.stream(ServiceProvider.getOMDService().getStateService().getStatePools()).collect(Collectors.toList())); } catch (PLException vciError) { throw WebUtil.getVciBaseException(vciError); } } /** * 清除缓存 */ @Override public void clearCache(){ // } /** * 查询全部的状态映射 * @return key是状态的英文名称 */ @Override @VciUnLog public Map selectAllStatusMap(){ return Optional.ofNullable(self.selectAllStatus()).orElseGet(()->new ArrayList<>()).stream().collect(Collectors.toMap(s->s.getId(),t->t,(o1,o2)->o1)); } /** * 状态转换为显示文本 * * @param status 状态 * @return 显示文本 */ @Override public String getStatusTextByValue(String status) { if(StringUtils.isBlank(status)){ return ""; } return self.selectAllStatusMap().getOrDefault(status,new OsStatusVO()).getName(); } /** * 批量添加状态 * * @param statePoolList 状态内容 */ @Override public void batchAddStatus(List statePoolList) { if(!CollectionUtils.isEmpty(statePoolList)){ for(StatePool statePool : statePoolList) { try { platformClientUtil.getStatePoolService().addStatePool(statePool); } catch (PLException e) { throw WebUtil.getVciBaseException(e); } } } } /** * 批量修改状态 * @param statePoolList 状态内容 */ @Override public void batchEditSave(List statePoolList) { if(!CollectionUtils.isEmpty(statePoolList)){ for(StatePool statePool : statePoolList) { try { platformClientUtil.getStatePoolService().modifyStatePool(statePool); } catch (PLException e) { throw WebUtil.getVciBaseException(e); } } } } /** * 状态列表 * * @param conditionMap 查询对象 * @param pageHelper 分页列表 * @return 显示对象 */ @Override public DataGrid gridStatus(Map conditionMap, PageHelper pageHelper) { BaseQueryObject baseQueryObject = new BaseQueryObject(); baseQueryObject.setConditionMap(conditionMap); if (pageHelper == null) { pageHelper = new PageHelper(-1); } baseQueryObject.setPage(pageHelper.getPage()); baseQueryObject.setLimit(pageHelper.getLimit()); baseQueryObject.setOrder(pageHelper.getOrder()); baseQueryObject.setSort(pageHelper.getSort()); return gridObject(baseQueryObject, OsStatusDO.class,self.selectAllStatusMap(),OsStatusVO.class); } /** * 使用主键获取显示对象 * * @param oid 主键 * @return 状态的显示对象 */ @Override public OsStatusVO getObjectByOid(String oid) { List statusVOList = self.selectAllStatusMap().values().stream().filter(status -> status.getOid().equalsIgnoreCase(oid)).collect(Collectors.toList()); if(CollectionUtils.isEmpty(statusVOList)){ throw new VciBaseException(DATA_OID_NOT_EXIST); } return statusVOList.get(0); } /** * 使用主键集合查询 * @param oidCollection 主键集合 * @return 状态的内容 */ @Override public List selectByOidCollection(Collection oidCollection){ List statusVOList = self.selectAllStatus().stream().filter(status -> oidCollection.contains(status.getOid())).collect(Collectors.toList()); if(CollectionUtils.isEmpty(statusVOList)){ throw new VciBaseException(DATA_OID_NOT_EXIST); } return statusVOList; } /** * 添加状态 * * @param statusDTO 状态的数据传输对象 */ @Override public void addSave(OsStatusDTO statusDTO) { VciBaseUtil.alertNotNull(statusDTO,"状态的信息",statusDTO.getId(),"状态的英文名称",statusDTO.getName(),"状态的中文名称"); statusDTO.setOid(VciBaseUtil.getPk()); StatePool pool = statusDTO2DO(statusDTO); List poolList = new ArrayList<>(); poolList.add(pool); batchAddStatus(poolList); clearCache(); self.selectAllStatusMap(); } /** * 编辑状态 * @param statusDTO 状态的数据传输对象 */ @Override public void editSave(OsStatusDTO statusDTO){ VciBaseUtil.alertNotNull(statusDTO,"状态的信息",statusDTO.getId(),"状态的英文名称",statusDTO.getName(),"状态的中文名称",statusDTO.getOid(),"主键"); OsStatusVO statusVO = getObjectByOid(statusDTO.getOid()); StatePool pool = statusDTO2DO(statusDTO); pool.creator = statusVO.getCreator(); pool.createTime = VciDateUtil.getTime(statusVO.getCreateTime()); List poolList = new ArrayList<>(); poolList.add(pool); batchEditSave(poolList); clearCache(); self.selectAllStatusMap(); } /** * 删除状态 * @param oids 主键 */ @Override public void delete(String oids){ VciBaseUtil.alertNotNull(oids,"主键"); //判断是否被引用 List statusVOList1 = selectByOidCollection(VciBaseUtil.str2List(oids)); if(statusVOList1.stream().anyMatch(statusVO -> lifeCycleService.checkStatusUsed(statusVO))){ throw new VciBaseException("状态在生命周期中被使用,不能删除"); } statusVOList1.stream().forEach(statusVO -> { try { platformClientUtil.getStatePoolService().deleteStatePool(statusVO2DO(statusVO)); }catch (Throwable e){ throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{},e); } }); clearCache(); self.selectAllStatusMap(); } /** * 状态的数据传输对象转换为数据对象 * @param statusDTO 数据传输对象 * @return 平台的数据对象 */ @Override public StatePool statusDTO2DO(OsStatusDTO statusDTO){ StatePool statePool = new StatePool(); statePool.oid = statusDTO.getOid(); statePool.id = ""; statePool.name = statusDTO.getId(); statePool.description = statusDTO.getDescription()==null?"":statusDTO.getDescription(); String userId = VciBaseUtil.getCurrentUserId(); long now = VciDateUtil.getNowTime(); statePool.creator = userId; statePool.createTime = now; statePool.modifier = userId; statePool.modifyTime = now; statePool.ts = statusDTO.getTs()==null?VciDateUtil.getNowString(VciDateUtil.DateTimeMillFormat):VciDateUtil.date2Str(statusDTO.getTs(),VciDateUtil.DateTimeMillFormat); statePool.tag = statusDTO.getName(); return statePool; } /** * 状态的显示对象转换为DO对象 * @param statusVO 显示对象 * @return 数据对象 */ public StatePool statusVO2DO(OsStatusVO statusVO){ StatePool statePool = new StatePool(); statePool.oid = statusVO.getOid(); statePool.id = ""; statePool.name = statusVO.getId(); statePool.description = statusVO.getDescription()==null?"":statusVO.getDescription(); String userId = VciBaseUtil.getCurrentUserId(); String now = VciDateUtil.getNowString(VciDateUtil.DateTimeMillFormat); statePool.creator = statusVO.getCreator(); statePool.createTime = VciDateUtil.getTime(statusVO.getCreateTime()); statePool.modifier = statusVO.getLastModifier(); statePool.modifyTime = VciDateUtil.getTime(statusVO.getLastModifyTime()); statePool.ts = statusVO.getTs()==null?now:VciDateUtil.date2Str(statusVO.getTs(),VciDateUtil.DateTimeMillFormat); statePool.tag = statusVO.getName(); return statePool; } }