From 1b223b109ec65a2934e283bb0be2c0e116b013e4 Mon Sep 17 00:00:00 2001
From: ludc
Date: 星期二, 30 七月 2024 15:47:40 +0800
Subject: [PATCH] 状态池相关接口上传(已测试待联调)

---
 Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/OsStatusServiceImpl.java |  267 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 228 insertions(+), 39 deletions(-)

diff --git a/Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/OsStatusServiceImpl.java b/Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/OsStatusServiceImpl.java
index 7bae860..033c3f4 100644
--- a/Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/OsStatusServiceImpl.java
+++ b/Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/OsStatusServiceImpl.java
@@ -1,19 +1,27 @@
 package com.vci.web.service.impl;
 
 import com.vci.corba.common.PLException;
+import com.vci.corba.omd.atm.AttributeDef;
 import com.vci.corba.omd.stm.StatePool;
+import com.vci.dto.OsAttributeDTO;
+import com.vci.pagemodel.OsEnumVO;
+import com.vci.po.OsAttributePO;
+import com.vci.po.OsStatusPO;
+import com.vci.starter.poi.bo.ReadExcelOption;
+import com.vci.starter.poi.bo.WriteExcelData;
+import com.vci.starter.poi.bo.WriteExcelOption;
+import com.vci.starter.poi.constant.ExcelLangCodeConstant;
+import com.vci.starter.poi.util.ExcelUtil;
 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.BaseResult;
 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.starter.web.util.*;
 import com.vci.dto.OsStatusDTO;
 import com.vci.model.OsStatusDO;
 import com.vci.pagemodel.OsStatusVO;
-import com.vci.starter.web.util.WebThreadLocalUtil;
 import com.vci.web.service.OsLifeCycleServiceI;
 import com.vci.web.service.OsStatusServiceI;
 import com.vci.web.service.WebBoServiceI;
@@ -21,6 +29,7 @@
 import com.vci.web.util.PlatformClientUtil;
 import com.vci.web.util.WebUtil;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.hssf.util.HSSFColor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -28,8 +37,9 @@
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
-import javax.swing.*;
+import java.io.File;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 import static com.vci.constant.FrameWorkBusLangCodeConstant.DATA_OID_NOT_EXIST;
@@ -66,6 +76,11 @@
     @Autowired(required = false)
     @Lazy
     private OsLifeCycleServiceI lifeCycleService;
+
+    /**
+     *  蹇呭~鍒�
+     */
+    private List<Integer> ColumnNameisRed = new ArrayList<Integer>();
 
     /**
      * 鍔犺浇鑷韩
@@ -182,8 +197,9 @@
                     throw WebUtil.getVciBaseException(e);
                 }
             }
+            return true;
         }
-        return true;
+        return false;
     }
 
     /**
@@ -267,24 +283,26 @@
             statusDTO.getId(),"鐘舵�佺殑鑻辨枃鍚嶇О",
             statusDTO.getName(),"鐘舵�佺殑涓枃鍚嶇О"
         );
-        StatePool dbStatePool = platformClientUtil.getStatePoolService().getStatePool(statusDTO.getName());
-        if (Func.isNotEmpty(dbStatePool) || Func.isNotBlank(dbStatePool.oid)) {
+        StatePool dbStatePool = platformClientUtil.getStatePoolService().getStatePool(statusDTO.getId());
+        if (Func.isNotEmpty(dbStatePool) && Func.isNotBlank(dbStatePool.oid)) {
             throw new PLException("500",new String[]{"鍚嶇О閲嶅璇锋洿鎹㈠悕绉帮紒"});
         }
         // 鐘舵�佹睜鍚嶇О鍙兘涓鸿嫳鏂囧瓧姣�
         String regex = "[a-z A-Z]*";
-        if ((!dbStatePool.name.matches(regex))) {
+        if ((!statusDTO.getId().matches(regex))) {
             throw new PLException("500",new String[]{"鍚嶇О鍙兘涓鸿嫳鏂囷紒"});
         }
         //铏界劧浼氳嚜鍔ㄧ敓鎴恛id锛屼絾鏄繖鍎胯缃富閿紝閬垮厤鏀惧叆缂撳瓨鐨勬暟鎹槸娌℃湁oid鐨�
-        statusDTO.setOid(VciBaseUtil.getPk());
-        StatePool pool = statusDTO2DO(statusDTO);
-        List<StatePool> poolList = new ArrayList<>();
-        poolList.add(pool);
-        batchAddStatus(poolList);
-        //clearCache();
-        self.selectAllStatusMap();
-        return true;
+        statusDTO.setOid(VciBaseUtil.getPk().toUpperCase(Locale.ROOT));
+        try {
+            boolean res = batchAddStatus(Arrays.asList(statusDTO2DO(statusDTO)));
+            self.selectAllStatusMap();
+            return res;
+        }catch (Exception e){
+            e.printStackTrace();
+            logger.error(e.getMessage());
+            return false;
+        }
     }
 
     /**
@@ -304,34 +322,53 @@
         StatePool pool = statusDTO2DO(statusDTO);
         pool.creator = statusVO.getCreator();
         pool.createTime = VciDateUtil.getTime(statusVO.getCreateTime());
-        List<StatePool> poolList = new ArrayList<>();
-        poolList.add(pool);
-        batchEditSave(poolList);
-        clearCache();
-        self.selectAllStatusMap();
-        return true;
+        try {
+            boolean res = batchEditSave(Arrays.asList(pool));
+            self.selectAllStatusMap();
+            return res;
+        }catch (Exception e){
+            e.printStackTrace();
+            logger.error(e.getMessage());
+            return false;
+        }
     }
 
     /**
      * 鍒犻櫎鐘舵��
-     * @param oids 涓婚敭
+     * @param osStatusDTOS
      */
     @Override
-    public boolean delete(String oids){
-        VciBaseUtil.alertNotNull(oids,"涓婚敭");
-        //鍒ゆ柇鏄惁琚紩鐢�
-        List<OsStatusVO> statusVOList1 = selectByOidCollection(VciBaseUtil.str2List(oids));
-        if(statusVOList1.stream().anyMatch(statusVO -> lifeCycleService.checkStatusUsed(statusVO))){
+    public boolean deleteStatus(List<OsStatusDTO> osStatusDTOS) throws PLException {
+        VciBaseUtil.alertNotNull(osStatusDTOS,"寰呭垹闄ょ殑灞炴�у垪琛�");
+        Set<String> oids = osStatusDTOS.stream().map(OsStatusDTO::getOid).collect(Collectors.toSet());
+        List<OsStatusVO> osStatusVOList = selectByOidCollection(oids);
+        //鍒ゆ柇鏋氫妇鏄惁鏈夎寮曠敤
+        if(osStatusVOList.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);
+        //骞冲彴鐨刣eleteStatus鏂规硶蹇呬紶涓変釜鍙傛暟锛宱id銆乶ame鍜宼s
+        List<StatePool> statePoolList = new ArrayList<>();
+        for(OsStatusDTO statusDTO : osStatusDTOS){
+            //oid鍜宼s鍒ょ┖
+            String oid = statusDTO.getOid();
+            //name涓昏鐢ㄦ潵瀵圭紦瀛樻暟鎹垹闄�
+            String name = statusDTO.getName();
+            //鍚庡彴浼氱敤ts杩涜鏁版嵁涓�鑷存�ф牎楠�
+            Date ts = statusDTO.getTs();
+            if(Func.isBlank(oid) || Func.isBlank(name) || Func.isEmpty(ts)){
+                throw new PLException("500",new String[]{"寰呭垹闄ょ殑鐘舵�佸垪琛ㄤ腑涓婚敭銆恛id銆戙�佽皟鏁存椂闂淬�恡s銆戙�佺姸鎬佸悕绉般�恘ame銆戜笉鑳戒负绌猴紒"});
             }
-        });
-        clearCache();
+            StatePool statePool = new StatePool();
+            statePool.oid = oid;
+            statePool.name = name;
+            statePool.ts = Func.format(ts,VciDateUtil.DateTimeMillFormat);
+            statePoolList.add(statePool);
+            boolean res = platformClientUtil.getStatePoolService().deleteStatePool(statePool);
+            if(!res){
+                throw new PLException("500",new String[]{"鍒犻櫎鐘舵�佸悕绉颁负銆�" + name + "銆戠殑鏁版嵁鏃跺嚭鐜伴敊璇�!"});
+            }
+        }
+        //clearCache();
         self.selectAllStatusMap();
         return true;
     }
@@ -351,13 +388,167 @@
         statePool.description = statusDTO.getDescription()==null?"":statusDTO.getDescription();
         String userId = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();
         long now = VciDateUtil.getNowTime();
-        statePool.creator = userId;
+        statePool.creator = Func.isBlank(statusDTO.getCreator()) ? userId:statusDTO.getCreator();
         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;
+    }
+
+    /**
+     * 瀵煎嚭閫変腑鐨勭姸鎬�
+     * @param exportFileName 瀵煎嚭鐨勬枃浠跺悕
+     * @param statusOids 闇�瑕佸鍑虹殑灞炴�у悕绉�
+     * @return
+     */
+    @Override
+    public String exportStatus(String exportFileName, String statusOids) throws PLException {
+        if(Func.isBlank(statusOids)){
+            throw new PLException("500",new String[]{"璇峰嬀閫夎瀵煎嚭鐨勫睘鎬�!"});
+        }
+        //鐣岄潰娌′紶鍚嶇О锛屼娇鐢ㄩ粯璁ゅ鍑哄悕绉�
+        exportFileName = Func.isBlank(exportFileName) ?  "鐘舵�佹睜鐘舵�佸鍑篲" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName;
+        //璁剧疆鍒楀悕
+        List<String> columns = new ArrayList<>(Arrays.asList("鍚嶇О", "鏍囩", "鍥剧墖", "鎻忚堪"));
+
+        //鍐檈xcel
+        String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName +  ".xls";
+        try {
+            new File(excelPath).createNewFile();
+        } catch (Throwable e) {
+            throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e);
+        }
+        //璁剧疆鍒�
+        List<WriteExcelData> excelDataList = new ArrayList<>();
+        //璁剧疆鍒楀ご
+        for (int index = 0; index < columns.size(); index++) {
+            excelDataList.add(new WriteExcelData(0,index, columns.get(index)));
+        }
+        //鎸夌収灞炴�у悕鏌ヨ灞炴�э紝鐒跺悗澶勭悊灞炴�у鍑�
+        List<String> statusOidList = Func.toStrList(statusOids);
+        AtomicInteger i = new AtomicInteger(1);
+        statusOidList.stream().forEach(oid->{
+            OsStatusVO osStatusVO = this.getObjectByOid(oid);
+            excelDataList.add(new WriteExcelData(i.get(),0, osStatusVO.getId()));
+            excelDataList.add(new WriteExcelData(i.get(),1, osStatusVO.getName()));
+            excelDataList.add(new WriteExcelData(i.get(),2, osStatusVO.getImagePath()));
+            excelDataList.add(new WriteExcelData(i.get(),3, osStatusVO.getDescription()));
+            i.getAndIncrement();
+        });
+        WriteExcelOption excelOption = new WriteExcelOption(excelDataList);
+        ExcelUtil.writeDataToFile(excelPath, excelOption);
+        return excelPath;
+    }
+
+    /**
+     * 涓嬭浇鐘舵�佸鍏ユā鏉�
+     * @param exportFileName
+     * @return
+     * @throws PLException
+     */
+    @Override
+    public String downloadStatusTemplate(String exportFileName) throws Exception {
+        //鐣岄潰娌′紶鍚嶇О锛屼娇鐢ㄩ粯璁ゅ鍑哄悕绉�
+        exportFileName = Func.isBlank(exportFileName) ?  "鐘舵�佹睜瀵煎叆妯℃澘_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName;
+        //璁剧疆鍒楀悕
+        List<String> columns = new ArrayList<>(
+                Arrays.asList("鍚嶇О(涓嶈兘涓虹┖)", "鏍囩(瀵瑰悕绉扮殑瑙i噴)","鍥剧墖(璺緞)", "鎻忚堪(鍙互涓虹┖)")
+        );
+        //璁剧疆蹇呭~鍒�
+        ColumnNameisRed.clear();
+        ColumnNameisRed.add(0);
+
+        //鍐檈xcel
+        String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName +  ".xls";
+        try {
+            new File(excelPath).createNewFile();
+        } catch (Throwable e) {
+            throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e);
+        }
+        //璁剧疆鍒�
+        List<WriteExcelData> excelDataList = new ArrayList<>();
+        //璁剧疆鍒楀ご
+        for (int index = 0; index < columns.size(); index++) {
+            //鍒ゆ柇鏄惁涓哄繀濉垪锛岀粰蹇呭~鍒楄缃鑹�
+            if(ColumnNameisRed.contains(index)){
+                WriteExcelData excelData = new WriteExcelData(0, index, columns.get(index));
+                excelData.setFontColor(String.valueOf(HSSFColor.HSSFColorPredefined.RED.getIndex()));
+                excelDataList.add(excelData);
+            }else{
+                excelDataList.add(new WriteExcelData(0,index, columns.get(index)));
+            }
+        }
+        WriteExcelOption excelOption = new WriteExcelOption(excelDataList);
+        ExcelUtil.writeDataToFile(excelPath, excelOption);
+        return excelPath;
+    }
+
+    /**
+     * 瀵煎叆鐘舵��
+     * @param file
+     * @return
+     */
+    @Override
+    public BaseResult importStatus(File file) throws Exception {
+        VciBaseUtil.alertNotNull(file,"excel鏂囦欢");
+        if(!file.exists()){
+            throw new VciBaseException("瀵煎叆鐨別xcel鏂囦欢涓嶅瓨鍦�,{0}",new String[]{file.getPath()});
+        }
+        try{
+            //1銆佽鍙杄xcel涓殑鏁版嵁锛岀粍鎴愬璞�
+            ReadExcelOption excelOption = new ReadExcelOption();
+            List<OsStatusPO> poList = ExcelUtil.readDataObjectFromExcel(file, OsStatusPO.class,excelOption,(value, po, fieldName)->{});
+            //鍘婚櫎閮芥槸绌虹殑鎯呭喌
+            if(CollectionUtils.isEmpty(poList)){
+                return BaseResult.fail(ExcelLangCodeConstant.IMPORT_CONTENT_NULL,new String[]{});
+            }
+            //excel鍒ら噸锛屾暟鎹牎楠岋紝dto瀵硅薄杞崲锛屽瓨鍌ㄥ璞¤浆鎹紝鎵ц淇濆瓨
+            List<StatePool> statePoolList = new ArrayList<>();
+            //褰撳墠excel涓槸鍚﹂噸澶嶇敤鐨勫垽閲峂ap:锛坘ey锛氬垽閲嶅睘鎬э紝value锛氳鍙凤級
+            Map<String, String> excelReapeat = new HashMap<>();
+            //鍒ゆ柇蹇呭~灞炴�ф槸鍚︿负绌猴紝鐢ㄦ埛鏄惁宸插瓨鍦紝浠ュ強閮ㄩ棬鏄惁濉敊绛夋牎楠岄�昏緫
+            poList.stream().forEach(osStatusPO -> {
+                try {
+                    StatePool dbStatePool = platformClientUtil.getStatePoolService().getStatePool(osStatusPO.getId());
+                    if(Func.isNotEmpty(dbStatePool) && Func.isNotBlank(dbStatePool.oid)){
+                        throw new VciBaseException("绗��"+osStatusPO.getRowIndex()+"銆戣锛屽悕绉板湪绯荤粺涓凡缁忓瓨鍦�,璇蜂慨鏀�!");
+                    }
+                } catch (PLException e) {
+                    e.printStackTrace();
+                    logger.error(e.getMessage());
+                    throw new VciBaseException(e.getMessage());
+                }
+                if(Func.isBlank(osStatusPO.getId())){//灞炴�у悕鍒ょ┖
+                    throw new VciBaseException("绗��"+osStatusPO.getRowIndex()+"銆戣锛宔numnameerror");
+                }else if(!osStatusPO.getId().matches("[a-z A-Z]*")){
+                    // 鐘舵�佹睜鍚嶇О鍙兘涓鸿嫳鏂囧瓧姣�
+                    throw new VciBaseException("鍚嶇О鍙兘涓鸿嫳鏂囷紒");
+                }else if(excelReapeat.containsKey(osStatusPO.getId())){//灞炴�у悕琛ㄦ牸涓垽閲�
+                    throw new VciBaseException("绗��"+excelReapeat.get(osStatusPO.getId())+"銆戣鍜岀銆�"+osStatusPO.getRowIndex()+"銆戣鏁版嵁锛屽悕绉伴噸澶�");
+                }
+                //鐘舵�佸悕excel涓垽閲嶅鐞�
+                excelReapeat.put(osStatusPO.getId(),osStatusPO.getRowIndex());
+                OsStatusDTO osStatusDTO = new OsStatusDTO();
+                osStatusDTO.setOid(VciBaseUtil.getPk().toUpperCase(Locale.ROOT));
+                osStatusDTO.setId(osStatusPO.getId());
+                osStatusDTO.setName(osStatusPO.getName());
+                osStatusDTO.setImagePath(osStatusPO.getImagePath());
+                osStatusDTO.setDescription(osStatusPO.getDescription());
+                statePoolList.add(statusDTO2DO(osStatusDTO));
+
+            });
+            //鎵ц淇濆瓨鎿嶄綔
+            batchAddStatus(statePoolList);
+        }catch (Exception e){
+            if(logger.isErrorEnabled()){
+                logger.error("璇诲彇excel鍐呭鏃舵垨淇濆瓨鐢ㄦ埛淇℃伅鏃跺嚭鐜颁簡閿欒锛屽叿浣撳師鍥狅細",VciBaseUtil.getExceptionMessage(e));
+            }
+            e.printStackTrace();
+            return BaseResult.fail(VciBaseUtil.getExceptionMessage(e),new String[]{},e);
+        }
+        return BaseResult.success("鏋氫妇瀵煎叆鎴愬姛锛�");
     }
 
     /**
@@ -381,7 +572,5 @@
         statePool.tag = statusVO.getName();
         return statePool;
     }
-
-
 
 }

--
Gitblit v1.9.3