package com.iailab.module.mcs.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.common.enums.IsEnableEnum; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.common.utils.UnzipUtils; import com.iailab.module.mcs.dao.StModelDao; import com.iailab.module.mcs.dto.RelationLineDTO; import com.iailab.module.mcs.dto.RelationNodeDTO; import com.iailab.module.mcs.dto.StModelDTO; import com.iailab.module.mcs.entity.StModelEntity; import com.iailab.module.mcs.service.StModelService; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; /** * @author lirm 1343021927@qq.com * @since 1.0.0 2023-05-10 */ @Service public class StModelServiceImpl extends BaseServiceImpl implements StModelService { private final String PATH_ZIP = "/zip/"; @Override public PageData page(Map params) { IPage page = baseDao.selectPage( getPage(params, "create_date", false), getWrapper(params) ); return getPageData(page, StModelDTO.class); } private QueryWrapper getWrapper(Map params) { String modelCode = (String) params.get("modelCode"); String modelName = (String) params.get("modelName"); String modelType = (String) params.get("modelType"); String isEnable = (String) params.get("isEnable"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.like(StringUtils.isNotBlank(modelName), "model_name", modelName) .like(StringUtils.isNotBlank(modelCode), "model_code", modelCode) .eq(StringUtils.isNotBlank(modelType), "model_type", modelType) .eq(StringUtils.isNotBlank(isEnable), "is_enable", isEnable); return wrapper; } @Override public void save(StModelDTO dto) { StModelEntity entity = ConvertUtils.sourceToTarget(dto, StModelEntity.class); insert(entity); } @Override public void update(StModelDTO dto) { StModelEntity entity = ConvertUtils.sourceToTarget(dto, StModelEntity.class); updateById(entity); } @Override public StModelDTO get(String id) { StModelEntity entity = baseDao.selectById(id); return ConvertUtils.sourceToTarget(entity, StModelDTO.class); } @Override public List getListAll(Map params) { String modelLoop = (String)params.get("modelLoop"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("is_enable", IsEnableEnum.ENABLE.value()) .eq(StringUtils.isNotBlank(modelLoop), "model_loop", modelLoop) .orderByAsc("run_sort"); List list = baseDao.selectList(wrapper); return ConvertUtils.sourceToTarget(list, StModelDTO.class); } @Override public StModelDTO getByCode(String code) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("model_code", code); wrapper.eq("is_enable",1); StModelEntity entity = baseDao.selectOne(wrapper); return ConvertUtils.sourceToTarget(entity, StModelDTO.class); } @Override public void deleteByIds(String[] ids) { baseDao.deleteBatchIds(Arrays.asList(ids)); } @Override public void enableByIds(String[] ids) { if (CollectionUtils.isEmpty(Arrays.asList(ids))) { return; } Arrays.asList(ids).forEach(item -> { StModelEntity entity = new StModelEntity(); entity.setId(item); entity.setIsEnable(IsEnableEnum.ENABLE.value()); baseDao.updateById(entity); }); } @Override public void disableByIds(String[] ids) { if (CollectionUtils.isEmpty(Arrays.asList(ids))) { return; } Arrays.asList(ids).forEach(item -> { StModelEntity entity = new StModelEntity(); entity.setId(item); entity.setIsEnable(IsEnableEnum.DISABLE.value()); baseDao.updateById(entity); }); } @Override public void updateTime(String id, Date runTime) { StModelEntity entity = new StModelEntity(); entity.setId(id); entity.setRunTime(runTime); baseDao.updateById(entity); } @Override public Map upload(MultipartFile file) throws IOException { Map result = new HashMap<>(); String modelPath = ""; String modelStart = ""; String uploadDir = modelPath + PATH_ZIP + file.getOriginalFilename(); File uploadFile = new File(uploadDir); if (!uploadFile.exists()) { uploadFile.mkdirs(); } file.transferTo(new File(uploadDir)); // 解压 UnzipUtils.unzipFile(uploadDir, modelPath + "/"); result.put("path", modelPath + "/" + file.getOriginalFilename().substring(0, file.getOriginalFilename().indexOf(".")) + "/" + modelStart); return result; } @Override public Map getRelation() { Map result = new HashMap<>(); List nodes = new ArrayList<>(); List lines = new ArrayList<>(); List modelList = getListAll(new HashMap<>()); if (CollectionUtils.isEmpty(modelList)) { return result; } RelationNodeDTO rootNode = new RelationNodeDTO(); rootNode.setId("root"); rootNode.setText("模型库"); nodes.add(rootNode); List parents = modelList.stream().map(t -> t.getModelParent()).distinct().collect(Collectors.toList()); Map> modelGroup = modelList.stream().collect(Collectors.groupingBy(StModelDTO::getModelParent)); for (int i = 0; i < parents.size(); i++) { RelationNodeDTO pNode = new RelationNodeDTO(); pNode.setId("p-" + i); pNode.setText(parents.get(i)); List groupList = modelGroup.get(parents.get(i)); for (int j = 0; j < groupList.size(); j++) { StModelDTO stModel = groupList.get(j); RelationNodeDTO mNode = new RelationNodeDTO(); mNode.setId(stModel.getId()); mNode.setText(stModel.getModelName()); mNode.setData(stModel); nodes.add(mNode); RelationLineDTO mLine = new RelationLineDTO(); mLine.setFrom(pNode.getId()); mLine.setTo(mNode.getId()); lines.add(mLine); } nodes.add(pNode); RelationLineDTO pLine = new RelationLineDTO(); pLine.setFrom("root"); pLine.setTo(pNode.getId()); lines.add(pLine); } result.put("nodes", nodes); result.put("lines", lines); return result; } }