package com.vci.web.service.impl; import com.vci.starter.revision.service.RevisionModelUtil; import com.vci.starter.web.exception.VciBaseException; import com.vci.starter.web.pagemodel.BaseResult; import com.vci.starter.web.pagemodel.DataGrid; import com.vci.starter.web.pagemodel.PageHelper; import com.vci.starter.web.pagemodel.SessionInfo; import com.vci.starter.web.util.BeanUtilForVCI; import com.vci.starter.web.util.VciBaseUtil; import com.vci.web.dao.BdSelectInputCharDaoI; import com.vci.dto.BdSelectInputCharDTO; import com.vci.model.BdSelectInputCharDO; import com.vci.pagemodel.BatchCBO; import com.vci.pagemodel.BdSelectInputCharVO; import com.vci.pagemodel.KeyValue; import com.vci.web.service.BdSelectInputCharServiceI; import com.vci.web.service.WebBoServiceI; 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.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.util.*; import static com.vci.constant.FrameWorkLangCodeConstant.*; /** * 可输可选内容服务 * @author weidy * @date 2022-03-09 */ @Service public class BdSelectInputCharServiceImpl implements BdSelectInputCharServiceI{ /** * 日志 */ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 数据操作层 */ @Resource private BdSelectInputCharDaoI bdSelectInputCharMapper; /** * 业务类型操作的服务 */ @Autowired private WebBoServiceI boService; /** * 对象的操作 */ @Autowired private RevisionModelUtil revisionModelUtil; /** * 查询所有的可输可选内容 * @param conditionMap 查询条件 * @param pageHelper 分页和排序 * @return 执行结果 * @throws VciBaseException 查询条件和分页出错的时候会抛出异常 */ @Override public DataGrid gridBdSelectInputChar(Map conditionMap, PageHelper pageHelper) throws VciBaseException{ if (pageHelper == null) { pageHelper = new PageHelper(-1); } pageHelper.addDefaultDesc("createTime"); List doList = bdSelectInputCharMapper.selectByCondition(conditionMap,pageHelper); DataGrid dataGrid=new DataGrid(); if (!CollectionUtils.isEmpty(doList)) { dataGrid.setData(bdSelectInputCharDO2VOs(doList)); dataGrid.setTotal(VciBaseUtil.getInt(String.valueOf(bdSelectInputCharMapper.countByCondition(conditionMap)))); } return dataGrid; } /** * 批量数据对象转换为显示对象 * @param bdSelectInputCharDOs 数据对象列表 * @return 显示对象 * @throws VciBaseException 参数为空或者不存在的时候会抛出异常 */ @Override public List bdSelectInputCharDO2VOs(Collection bdSelectInputCharDOs) throws VciBaseException{ List voList = new ArrayList(); if(!CollectionUtils.isEmpty(bdSelectInputCharDOs)){ for(BdSelectInputCharDO s: bdSelectInputCharDOs){ BdSelectInputCharVO vo = bdSelectInputCharDO2VO(s); if(vo != null){ voList.add(vo); } } } return voList; } /** * 数据对象转换为显示对象 * @param bdSelectInputCharDO 数据对象 * @return 显示对象 * @throws VciBaseException 拷贝属性出错的时候会抛出异常 */ @Override public BdSelectInputCharVO bdSelectInputCharDO2VO(BdSelectInputCharDO bdSelectInputCharDO) throws VciBaseException{ BdSelectInputCharVO vo = new BdSelectInputCharVO(); if(bdSelectInputCharDO != null){ BeanUtilForVCI.copyPropertiesIgnoreCase(bdSelectInputCharDO,vo); //如果有lcstatus的类的话 } return vo; } /** * 增加可输可选内容 * @param bdSelectInputCharDTO 可输可选内容数据传输对象 * @return 执行结果 * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常 */ @Override public BdSelectInputCharVO addSave(BdSelectInputCharDTO bdSelectInputCharDTO) throws VciBaseException{ VciBaseUtil.alertNotNull(bdSelectInputCharDTO,"需要添加的数据对象"); if(StringUtils.isBlank(bdSelectInputCharDTO.getId())){ return null; } boolean persistence = WebUtil.isPersistence(); WebUtil.setPersistence(true); //不论成功不成功,都保存进去 //将DTO转换为DO BdSelectInputCharDO bdSelectInputCharDO = new BdSelectInputCharDO(); BeanUtilForVCI.copyPropertiesIgnoreCase(bdSelectInputCharDTO,bdSelectInputCharDO); if(StringUtils.isBlank(bdSelectInputCharDO.getSourceNameSpace())){ bdSelectInputCharDO.setSourceNameSpace("default"); } if(StringUtils.isBlank(bdSelectInputCharDO.getSourceInputFlag())){ bdSelectInputCharDO.setSourceInputFlag("default"); } //需要判断是否存在 Map conditionMap = new HashMap<>(); conditionMap.put("id",bdSelectInputCharDO.getId()); conditionMap.put("sourcenamespace",bdSelectInputCharDO.getSourceNameSpace()); conditionMap.put("sourceinputflag",bdSelectInputCharDO.getSourceInputFlag()); if(bdSelectInputCharMapper.countByCondition(conditionMap) == 0) { bdSelectInputCharMapper.insert(bdSelectInputCharDO); }else{ return null; } WebUtil.setPersistence(persistence); return bdSelectInputCharDO2VO(bdSelectInputCharDO); } /** * 修改可输可选内容 * @param bdSelectInputCharDTO 可输可选内容数据传输对象 * @return 执行结果 * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常 */ @Override public BdSelectInputCharVO editSave(BdSelectInputCharDTO bdSelectInputCharDTO) throws VciBaseException{ VciBaseUtil.alertNotNull(bdSelectInputCharDTO,"数据对象",bdSelectInputCharDTO.getOid(),"可输可选内容主键"); //将DTO转换为DO BdSelectInputCharDO bdSelectInputCharDO = selectByOid(bdSelectInputCharDTO.getOid()); revisionModelUtil.copyFromDTOIgnore(bdSelectInputCharDTO,bdSelectInputCharDO); bdSelectInputCharMapper.updateByPrimaryKey(bdSelectInputCharDO); return bdSelectInputCharDO2VO(bdSelectInputCharDO); } /** * 校验是否可以删除,如果存在下级,并且下级有数据引用则不能删除 * @param bdSelectInputCharDTO 数据传输对象 * @param bdSelectInputCharDO 数据库中的数据对象 * @return success为true为可以删除,false表示有数据引用,obj为true表示有下级 */ private BaseResult checkIsCanDeleteForDO(BdSelectInputCharDTO bdSelectInputCharDTO, BdSelectInputCharDO bdSelectInputCharDO) { BdSelectInputCharDO tsDO = new BdSelectInputCharDO(); BeanUtilForVCI.copyPropertiesIgnoreCase(bdSelectInputCharDTO,tsDO); boService.checkTs(tsDO); if(!checkIsLinked(bdSelectInputCharDO.getOid())) { return BaseResult.success(); }else{ return BaseResult.fail(DATA_LINKED_NOT_DELETE,new String[]{""}); } } /** * 校验是否被引用 * @param oid 主键 * @throws VciBaseException 被引用的时候会抛出异常 */ private boolean checkIsLinked(String oid) throws VciBaseException{ //TODO 添加需要校验引用的地方 return false; } /** * 删除可输可选内容 * @param bdSelectInputCharDTO 可输可选内容数据传输对象,oid和ts需要传输 * @return 删除结果反馈::success:成功,fail:失败 * @throws VciBaseException 参数为空,被引用时抛出异常 */ @Override public BaseResult deleteBdSelectInputChar(BdSelectInputCharDTO bdSelectInputCharDTO) throws VciBaseException{ VciBaseUtil.alertNotNull(bdSelectInputCharDTO,"可输可选内容数据对象",bdSelectInputCharDTO.getOid(),"可输可选内容的主键"); BdSelectInputCharDO bdSelectInputCharDO = selectByOid(bdSelectInputCharDTO.getOid()); BaseResult baseResult = checkIsCanDeleteForDO(bdSelectInputCharDTO,bdSelectInputCharDO); if(baseResult.isSuccess()) { }else{ return baseResult; } //执行删除操作 BatchCBO batchCBO = bdSelectInputCharMapper.deleteByPrimaryKey(bdSelectInputCharDO.getOid()); return (batchCBO!=null && batchCBO.getDeleteCbos() !=null &&batchCBO.getDeleteCbos().size() > 0)?BaseResult.successMsg(DELETE_SUCCESS):BaseResult.fail(DELETE_FAIL); } /** * 主键获取可输可选内容 * @param oid 主键 * @return 可输可选内容显示对象 * @throws VciBaseException 参数为空,数据不存在时会抛出异常 */ @Override public BdSelectInputCharVO getObjectByOid(String oid) throws VciBaseException{ return bdSelectInputCharDO2VO(selectByOid(oid)); } /** * 主键查询数据对象 * @param oid 主键 * @return 数据对象 * @throws VciBaseException 参数为空,并且数据不存在的时候会抛出异常 */ private BdSelectInputCharDO selectByOid(String oid) throws VciBaseException{ VciBaseUtil.alertNotNull(oid,"主键"); BdSelectInputCharDO bdSelectInputCharDO = bdSelectInputCharMapper.selectByPrimaryKey(oid.trim()); if(bdSelectInputCharDO == null || StringUtils.isBlank(bdSelectInputCharDO.getOid())){ throw new VciBaseException(DATA_OID_NOT_EXIST); } return bdSelectInputCharDO; } /** * 主键批量获取可输可选内容 * @param oidCollections 主键集合,但是受性能影响,建议一次查询不超过10000个 * @return 可输可选内容显示对象 * @throws VciBaseException 查询出现异常时会抛出 */ @Override public Collection listBdSelectInputCharByOids(Collection oidCollections) throws VciBaseException{ VciBaseUtil.alertNotNull(oidCollections,"数据对象主键集合"); List bdSelectInputCharDOList = listBdSelectInputCharDOByOidCollections(oidCollections); return bdSelectInputCharDO2VOs(bdSelectInputCharDOList); } /** * 使用主键集合查询数据对象 * @param oidCollections 主键的集合 * @return 数据对象列表 */ private List listBdSelectInputCharDOByOidCollections(Collection oidCollections){ List bdSelectInputCharDOList = new ArrayList(); if(!CollectionUtils.isEmpty(oidCollections)){ Collection> oidCollectionsList = VciBaseUtil.switchCollectionForOracleIn(oidCollections); for(Collection oids: oidCollectionsList){ List tempDOList = bdSelectInputCharMapper.selectByPrimaryKeyCollection(oids); if(!CollectionUtils.isEmpty(tempDOList)){ bdSelectInputCharDOList.addAll(tempDOList); } } } return bdSelectInputCharDOList; } /** * 参照可输可选内容列表 * @param conditionMap 查询条件 * @param pageHelper 分页和排序 * @return 可输可选内容显示对象列表,生效的内容 * @throws VciBaseException 查询条件和分页出错的时候会抛出异常 */ @Override public DataGrid refDataGridBdSelectInputChar(Map conditionMap, PageHelper pageHelper) throws VciBaseException{ if(conditionMap == null){ conditionMap = new HashMap(); } return gridBdSelectInputChar(conditionMap,pageHelper); } /** * 获取可选择的列表 * * @param flag 标记 * @param namespace 命名空间 * @return 包含的内容 */ @Override public List listByFlag(String namespace,String flag) { if(StringUtils.isBlank(namespace)){ namespace = "default"; } if(StringUtils.isBlank(flag)){ flag = "default"; } Map conditionMap = new HashMap<>(); conditionMap.put("sourceinputflag",flag); conditionMap.put("sourceNameSpace",namespace); PageHelper pageHelper = new PageHelper(-1); pageHelper.addDefaultDesc("createTime"); List charDOS = bdSelectInputCharMapper.selectByCondition(conditionMap, pageHelper); if(CollectionUtils.isEmpty(charDOS)){ return new ArrayList<>(); } List keyValueList = new ArrayList<>(); charDOS.stream().forEach(charDO->{ KeyValue keyValue = new KeyValue(); keyValue.setKey(charDO.getId()); keyValue.setValue(StringUtils.isBlank(charDO.getName())?charDO.getId():(charDO.getId() + " " + charDO.getName())); keyValueList.add(keyValue); }); return keyValueList; } /** * 存储 * * @param namespace 命名空间 * @param flag 标识 * @param value 值 */ @Override @Async public void save(String namespace, String flag, String value,SessionInfo sessionInfo) { VciBaseUtil.setCurrentUserSessionInfo(sessionInfo); BdSelectInputCharDTO sic = new BdSelectInputCharDTO(); sic.setSourcenamespace(namespace); sic.setSourceinputflag(flag); sic.setId(value); addSave(sic); } /** * 相同命名空间的存储 * * @param namespace 命名空间 * @param flagValueMap key是标识,value是值 * @param sessionInfo 会话信息 */ @Override @Async public void saveBySameNamespace(String namespace, Map flagValueMap,SessionInfo sessionInfo) { VciBaseUtil.alertNotNull(flagValueMap,"需要添加的数据对象"); VciBaseUtil.setCurrentUserSessionInfo(sessionInfo); boolean persistence = WebUtil.isPersistence(); WebUtil.setPersistence(true); //不论成功不成功,都保存进去 //将DTO转换为DO List charDOList = new ArrayList<>(); if(StringUtils.isBlank(namespace)){ namespace = "default"; } String finalNamespace = namespace; flagValueMap.forEach((flag, value)-> { BdSelectInputCharDO sic = new BdSelectInputCharDO(); sic.setId(value); sic.setSourceInputFlag(flag); sic.setSourceNameSpace(finalNamespace); Map conditionMap = new HashMap<>(); conditionMap.put("id", sic.getId()); conditionMap.put("sourcenamespace", sic.getSourceNameSpace()); conditionMap.put("sourceinputflag", sic.getSourceInputFlag()); if (bdSelectInputCharMapper.countByCondition(conditionMap) == 0) { charDOList.add(sic); } }); if(!CollectionUtils.isEmpty(charDOList)) { bdSelectInputCharMapper.batchInsert(charDOList); } WebUtil.setPersistence(persistence); } /** * 相同命名空间和相同标识的存储 * * @param namespace 命名空间 * @param flag 标记 * @param values 值集合 * @param sessionInfo 会话信息 */ @Override @Async public void saveBySameNamespaceAndFlag(String namespace, String flag, Collection values,SessionInfo sessionInfo) { VciBaseUtil.alertNotNull(values,"需要添加的数据对象"); boolean persistence = WebUtil.isPersistence(); WebUtil.setPersistence(true); //不论成功不成功,都保存进去 //将DTO转换为DO List charDOList = new ArrayList<>(); if(StringUtils.isBlank(namespace)){ namespace = "default"; } if(StringUtils.isBlank(flag)){ flag = "default"; } String finalNamespace = namespace; String finalFlag = flag; values.stream().forEach(value->{ BdSelectInputCharDO sic = new BdSelectInputCharDO(); sic.setId(value); sic.setSourceInputFlag(finalFlag); sic.setSourceNameSpace(finalNamespace); Map conditionMap = new HashMap<>(); conditionMap.put("id", sic.getId()); conditionMap.put("sourcenamespace", sic.getSourceNameSpace()); conditionMap.put("sourceinputflag", sic.getSourceInputFlag()); if (bdSelectInputCharMapper.countByCondition(conditionMap) == 0) { charDOList.add(sic); } }); if(!CollectionUtils.isEmpty(charDOList)) { bdSelectInputCharMapper.batchInsert(charDOList); } WebUtil.setPersistence(persistence); } }