//package jj.tech.paolu.biz.webadmin.service;
//
//import com.alibaba.fastjson2.JSON;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import jj.tech.paolu.bcn.model.bo.*;
//import jj.tech.paolu.bcn.service.ArchivesFileService;
//import jj.tech.paolu.bcn.service.CatalogService;
//import jj.tech.paolu.bcn.service.FileService;
//import jj.tech.paolu.bcn.service.ProviderService;
//import jj.tech.paolu.biz.webadmin.dao.SelectMapper;
//import jj.tech.paolu.config.enums.SharedConfigStatus;
//import jj.tech.paolu.config.enums.SharedStatus;
//import jj.tech.paolu.config.enums.SynStatus;
//import jj.tech.paolu.config.enums.SynType;
//import jj.tech.paolu.repository.mybatis.dao.SharedSynAllMapper;
//import jj.tech.paolu.repository.mybatis.dao.SynDirectoryFileMapper;
//import jj.tech.paolu.repository.mybatis.dao.SynDirectoryMapper;
//import jj.tech.paolu.repository.mybatis.dao.SynFileMapper;
//import jj.tech.paolu.repository.mybatis.dao.support.SharedSynAllDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.dao.support.SynDirectoryDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.dao.support.SynDirectoryFileDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.dao.support.SynFileDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.entity.*;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang3.ObjectUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.fisco.bcos.sdk.model.TransactionReceipt;
//import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;
//import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
//import org.mybatis.dynamic.sql.render.RenderingStrategies;
//import org.mybatis.dynamic.sql.select.render.DefaultSelectStatementProvider;
//import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.math.BigInteger;
//import java.time.LocalDateTime;
//import java.time.ZoneOffset;
//import java.time.format.DateTimeFormatter;
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static org.mybatis.dynamic.sql.SqlBuilder.*;
//
///**
// * 目录分享服务
// */
//@Service
//public class SharedService {
//    private static final Logger logger = LoggerFactory.getLogger(SharedService.class);
//    /**
//     * 链上表前缀
//     */
//    public static String TABLE_PREFIX;
//
//    @Value("${system.publicKeyAddress}")
//    private String publicKeyAddress;
//    @Resource
//    private ProviderService providerService;
//    @Resource
//    private SharedSynAllMapper sharedSynAllMapper;
//    @Resource
//    private SynDirectoryMapper synDirectoryMapper;
//    @Resource
//    private SelectMapper selectMapper;
//    @Resource
//    private SynDirectoryFileMapper synDirectoryFileMapper;
//    @Resource
//    private SynFileMapper synFileMapper;
//    @Resource
//    private CatalogService catalogService;
//    @Resource
//    private ArchivesFileService archivesFileService;
//    @Resource
//    private FileService fileService;
//    @Resource
//    private ObjectMapper objectMapper;
//
//    @PostConstruct
//    public void init() {
//        //查找当前用户链上表前缀
//        CallResponse callResponse = null;
//        try {
//            callResponse = providerService.selectPrefixByAddr(new ProviderSelectPrefixByAddrInputBO(publicKeyAddress));
//            if (callResponse.getReturnObject() != null) {
//                TABLE_PREFIX = callResponse.getReturnObject().getFirst().toString();
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//
//    /**
//     * 上链数据
//     *
//     * @return
//     */
//    public Object share(String id) throws Exception {
//        Optional<SharedSynAll> sharedSynAllOptional = sharedSynAllMapper.selectByPrimaryKey(id);
//        if (sharedSynAllOptional.isEmpty()) {
//            return null;
//        }
//        SharedSynAll sharedSynAll = sharedSynAllOptional.get();
//        if (sharedSynAll.getSyn_status() != null && sharedSynAll.getSyn_status().equals(SynStatus.SYN.getCode())) {
//            return null;
//        }
//
//        //需要上链的目录数据列表
//        if (sharedSynAll.getSyn_type().equals(SynType.DIR.getCode())) {
//            if (sharedSynAll.getIs_config_rule().equals(SharedConfigStatus.DELETE.getCode())) {
//                //目录下的所有子元素都下链
//                downDirChildBcn(sharedSynAll.getSyn_id());
//                //下链
//                downDirBcn(sharedSynAll);
//            } else if (handleDir(sharedSynAll)) {
//                //上链
//                upDirBcn(sharedSynAll);
//            }
//
//
//        }
//
//
//        //需要上链的目录文件数据列表
//        if (sharedSynAll.getSyn_type().equals(SynType.DIRFILE.getCode())) {
//            if (sharedSynAll.getIs_config_rule().equals(SharedConfigStatus.DELETE.getCode())) {
//                //目录下的所有子元素都下链
//                downFileDirChildBcn(sharedSynAll.getSyn_id());
//                //下链
//                downFileDirBcn(sharedSynAll);
//            }
//            if (handleDirFile(sharedSynAll)) {
//                upFileDirBcn(sharedSynAll);
//            }
//
//        }
//
//
//        //需要上链的文件数据列表
//        if (sharedSynAll.getSyn_type().equals(SynType.FILE.getCode())) {
//            if (sharedSynAll.getIs_config_rule().equals(SharedConfigStatus.DELETE.getCode())) {
//                //目录下的所有子元素都下链
//                downFileChildBcn(sharedSynAll.getSyn_id());
//                //下链
//                downFileBcn(sharedSynAll);
//            }
//            if (handleFile(sharedSynAll)) {
//                upFileBcn(sharedSynAll);
//            }
//        }
//        sharedSynAll.setSyn_status(SynStatus.SYN.getCode());
//        return sharedSynAllMapper.updateByPrimaryKey(sharedSynAll);
//    }
//
//    /**
//     * 目录下的所有子元素都下链
//     *
//     * @param synId
//     */
//    private void downDirChildBcn(String synId) throws Exception {
//
//        //所有子分类目录ids
//        ArrayList<String> dirIds = new ArrayList<>();
//        selectChildDirBcn(synId, dirIds);
//
//        //所有子文件目录ids
//        ArrayList<String> fileDirIds = new ArrayList<>();
//        for (String dirId : dirIds) {
//            selectChildFileDirBcn(dirId, fileDirIds);
//        }
//
//        //所有子文件ids
//        ArrayList<String> fileIds = new ArrayList<>();
//        for (String fileDirId : fileDirIds) {
//            selectChildFileBcn(fileDirId, fileIds);
//        }
//
//        //下链顺序，先文件，文件目录，目录
//        FileRemoveInputBO bo = new FileRemoveInputBO();
//        bo.setFile_id(fileIds);
//        fileService.remove(bo);
//
//        ArchivesFileRemoveInputBO abo = new ArchivesFileRemoveInputBO();
//        abo.setArchives_file_id(fileDirIds);
//        archivesFileService.remove(abo);
//
//        for (String id : dirIds) {
//            CatalogRemoveInputBO cbo = new CatalogRemoveInputBO();
//            cbo.setCatalog_id(id);
//            catalogService.remove(cbo);
//        }
//
//
//    }
//
//    /**
//     * 文件目录下的所有子元素都下链
//     *
//     * @param synId
//     */
//    private void downFileDirChildBcn(String synId) throws Exception {
//
//        //所有子文件目录ids
//        ArrayList<String> fileDirIds = new ArrayList<>();
//        selectChildFileDirBcn(synId.toString(), fileDirIds);
//
//        //所有子文件ids
//        ArrayList<String> fileIds = new ArrayList<>();
//        for (String id : fileDirIds) {
//            selectChildFileBcn(id, fileIds);
//        }
//
//        //下链顺序，先文件，文件目录，目录
//        FileRemoveInputBO bo = new FileRemoveInputBO();
//        bo.setFile_id(fileIds);
//        fileService.remove(bo);
//
//        ArchivesFileRemoveInputBO abo = new ArchivesFileRemoveInputBO();
//        abo.setArchives_file_id(fileDirIds);
//        archivesFileService.remove(abo);
//
//
//    }
//
//    /**
//     * 文件目录下的所有子元素都下链
//     *
//     * @param synId
//     */
//    private void downFileChildBcn(String synId) {
//        try {
//
//            //所有子文件ids
//            ArrayList<String> fileIds = new ArrayList<>();
//            selectChildFileBcn(synId.toString(), fileIds);
//
//            //下链顺序，先文件，文件目录，目录
//            FileRemoveInputBO bo = new FileRemoveInputBO();
//            bo.setFile_id(fileIds);
//            fileService.remove(bo);
//
//
//        } catch (Exception e) {
//            logger.error("目录下链失败！{}", e.getMessage());
//            throw new RuntimeException(e);
//        }
//
//
//    }
//
//    /**
//     * 目录id在链上所有子目录id（不包括下下级）
//     */
//    private void selectChildDirBcn(String id, ArrayList<String> ids) throws Exception {
//        CatalogSelectByIdInputBO bo = new CatalogSelectByIdInputBO();
//        bo.setTable_prefix(TABLE_PREFIX);
//        bo.setId(id.toString());
//        CallResponse select = null;
//
//        select = catalogService.selectById(bo);
//        ArrayList<ArrayList<ArrayList>> arrayList = objectMapper.readValue(select.getValues(), ArrayList.class);
//        for (ArrayList<ArrayList> list : arrayList) {
//            for (ArrayList l : list) {
//                ids.add(l.get(0).toString());
//                selectChildDirBcn(l.get(0).toString(), ids);
//            }
//
//        }
//
//
//    }
//
//    /**
//     * 目录id在链上所有文件目录id
//     *
//     * @param id 目录id
//     */
//    private void selectChildFileDirBcn(String id, ArrayList<String> ids) throws Exception {
//        ArchivesFileSelectByCatalogIdInputBO bo = new ArchivesFileSelectByCatalogIdInputBO();
//        bo.setTable_prefix(TABLE_PREFIX);
//        bo.setCatalog_id(id);
//        CallResponse select = null;
//
//        select = archivesFileService.selectByCatalogId(bo);
//        ArrayList<ArrayList<ArrayList>> arrayList = objectMapper.readValue(select.getValues(), ArrayList.class);
//        arrayList.forEach(list -> {
//            list.forEach(l -> {
//                ids.add(l.get(0).toString());
//            });
//
//        });
//
//    }
//
//    /**
//     * 文件目录id在链上所有文件id
//     *
//     * @param id 文件目录id
//     */
//    private void selectChildFileBcn(String id, ArrayList<String> ids) throws Exception {
//        FileSelectByCatalogIdInputBO bo = new FileSelectByCatalogIdInputBO();
//        bo.setTable_prefix(TABLE_PREFIX);
//        bo.setArchive_file_id(id);
//        CallResponse select = null;
//        select = fileService.selectByCatalogId(bo);
//        ArrayList<ArrayList<ArrayList>> arrayList = objectMapper.readValue(select.getValues(), ArrayList.class);
//        arrayList.forEach(list -> {
//            list.forEach(l -> {
//                ids.add(l.get(0).toString());
//            });
//
//        });
//
//    }
//
//
//    /**
//     * 共享目录：解析出哪些可以上链
//     *
//     * @return
//     */
//    public Boolean handleDir(SharedSynAll sharedSynAll) {
//        if (ObjectUtils.isEmpty(sharedSynAll)) {
//            return false;
//        }
//        //查询所有文件目录id的父类,包含自己本身
//        String select_sql =
//                """
//                                WITH RECURSIVE temp_cet AS
//                                (
//                                   SELECT *
//                                    FROM syn_directory sun
//                                    WHERE id IN (%s)
//                                  UNION
//                                  SELECT p.* FROM temp_cet
//                                  INNER JOIN syn_directory AS p
//                                  ON temp_cet.p_id = p.id
//                                ) SELECT id FROM temp_cet
//                        """;
//        String sql = String.format(select_sql, StringUtils.join(List.of("'"+sharedSynAll.getId()+"'"), ','));
//
//        SelectStatementProvider provider = DefaultSelectStatementProvider
//                .withSelectStatement(sql)
//                .build();
//
//        List<String> all_dir_ids = selectMapper.selectList(provider);
//        Boolean isShare = true;
//        for (String id : all_dir_ids) {
//            Optional<SharedSynAll> sharedSynAllOptional = sharedSynAllMapper.selectByPrimaryKey(id);
//            if (sharedSynAllOptional.isPresent()) {
//                SharedSynAll sharedSyn = sharedSynAllOptional.get();
//                //已配置(下级需要全选)
//                if (sharedSyn.getIs_config_rule().equals(SharedConfigStatus.ADDAll)) {
//                    if (selectAllByDir(sharedSyn.getSyn_id()).equals(false)) {
//                        isShare = false;
//                        break;
//                    }
//                }
//                //已禁用
//                if (sharedSyn.getIs_config_rule().equals(SharedConfigStatus.DELETE)) {
//                    isShare = false;
//                    break;
//                }
//            }
//        }
//        return isShare;
//    }
//
//    /**
//     * 判断目录下级是否有全选
//     */
//    public Boolean selectAllByDir(String id) {
//        //获取全部下级目录
//        var sql = select(SynDirectoryMapper.selectList)
//                .from(SynDirectoryDynamicSqlSupport.synDirectory)
//                .applyWhere(whereFinisher -> whereFinisher.and(SynDirectoryDynamicSqlSupport.p_id, isEqualTo(id)))
//                .build().render(RenderingStrategies.MYBATIS3);
//        List<SynDirectory> synDirectories = synDirectoryMapper.selectMany(sql);
//        //没有下级目录(出口)
//        if (CollectionUtils.isEmpty(synDirectories)) {
//            long count = sharedSynAllMapper.count(c -> c.applyWhere(whereFinisher -> whereFinisher
//                    .and(SharedSynAllDynamicSqlSupport.syn_id, isEqualTo(id))
//                    .and(SharedSynAllDynamicSqlSupport.syn_type, isEqualTo(SynType.DIR.getCode()))
//                    .and(SharedSynAllDynamicSqlSupport.is_config_rule, isIn(SharedConfigStatus.DELETE.getCode(), SharedConfigStatus.NULL.getCode()))
//            ));
//            if (count > 0) {
//                return false;
//            }
//            //叶目录下判断文件目录下级是否全选
//            //获取全部文件目录ids
//            var fileDirSql = select(SynDirectoryFileDynamicSqlSupport.id)
//                    .from(SynDirectoryFileDynamicSqlSupport.synDirectoryFile)
//                    .applyWhere(whereFinisher -> whereFinisher.and(SynDirectoryFileDynamicSqlSupport.p_id, isEqualTo(id)))
//                    .build().render(RenderingStrategies.MYBATIS3);
//            List<SynDirectoryFile> synDirectoryFiles = synDirectoryFileMapper.selectMany(fileDirSql);
//            Boolean selectAll = true;
//            for (SynDirectoryFile synDirectoryFile : synDirectoryFiles) {
//                if (!selectAllByFileDir(synDirectoryFile.getId())) {
//                    selectAll = false;
//                    break;
//                }
//            }
//            return selectAll;
//
//        } else {
//            Boolean selectAll = true;
//            for (SynDirectory synDirectorie : synDirectories) {
//                if (!selectAllByDir(synDirectorie.getId())) {
//                    selectAll = false;
//                    break;
//                }
//            }
//            return selectAll;
//        }
//    }
//
//    /**
//     * 判断文件目录下级是否有全选
//     */
//    public Boolean selectAllByFileDir(String id) {
//        //当前文件目录
//        var fileDirSql = select(SharedSynAllMapper.selectList)
//                .from(SharedSynAllDynamicSqlSupport.sharedSynAll)
//                .applyWhere(whereFinisher -> whereFinisher
//                        .and(SharedSynAllDynamicSqlSupport.syn_type, isEqualTo(SynType.DIRFILE.getCode()))
//                        .and(SharedSynAllDynamicSqlSupport.syn_id, isEqualTo(id))
//                        .and(SharedSynAllDynamicSqlSupport.is_config_rule, isIn(SharedConfigStatus.ADDAll.getCode(), SharedConfigStatus.ADDPART.getCode()))
//                )
//                .limit(1)
//                .build().render(RenderingStrategies.MYBATIS3);
//
//        Optional<SharedSynAll> sharedSynAllOptional = sharedSynAllMapper.selectOne(fileDirSql);
//        if (sharedSynAllOptional.isEmpty()) {
//            return false;
//        }
//
//        //查找目录下所有的文件id
//        var fileSql = select(SynFileDynamicSqlSupport.id)
//                .from(SynFileDynamicSqlSupport.synFile)
//                .applyWhere(whereFinisher -> whereFinisher
//                        .and(SynFileDynamicSqlSupport.p_id, isEqualTo(id))
//                )
//                .build().render(RenderingStrategies.MYBATIS3);
//        List<String> ids = synFileMapper.selectMany(fileSql).stream().map(SynFile::getId).collect(Collectors.toList());
//        long count = sharedSynAllMapper.count(c -> c.applyWhere(whereFinisher -> whereFinisher
//                .and(SharedSynAllDynamicSqlSupport.is_config_rule, isIn(SharedConfigStatus.ADDAll.getCode(), SharedConfigStatus.ADDPART.getCode()))
//                .and(SharedSynAllDynamicSqlSupport.syn_type, isEqualTo(SynType.FILE.getCode()))
//                .and(SharedSynAllDynamicSqlSupport.syn_id, isIn(ids))
//        ));
//        return count == ids.size();
//
//    }
//
//
//    /**
//     * 共享目录文件：解析出哪些可以上链
//     *
//     * @return
//     */
//    public Boolean handleDirFile(SharedSynAll sharedSynAll) {
//        //禁止
//        if (sharedSynAll.getIs_config_rule().equals(SharedConfigStatus.DELETE.getCode())) {
//            return false;
//        }
//        //父级
//        Optional<SynDirectoryFile> synDirectoryFileOpt = synDirectoryFileMapper.selectByPrimaryKey(sharedSynAll.getSyn_id());
//        if (synDirectoryFileOpt.isEmpty()) {
//            return false;
//        }
//        SynDirectoryFile synDirectoryFile = synDirectoryFileOpt.get();
//
//        SharedSynAll sharedSynAllBySynId = getSharedSynAllBySynId(synDirectoryFile.getP_id());
//        if (sharedSynAllBySynId != null) {
//            Boolean dirBool = handleDir(sharedSynAllBySynId);
//            if (dirBool.equals(false)) {
//                return false;
//            }
//        }
//
//
//        //全选
//        if (sharedSynAll.getIs_config_rule().equals(SharedConfigStatus.ADDAll.getCode())) {
//            Boolean fileDirBool = selectAllByFileDir(sharedSynAll.getSyn_id());
//            return !fileDirBool.equals(false);
//        }
//
//        return true;
//
//    }
//
//    /**
//     * 共享文件：解析出哪些可以上链
//     *
//     * @return
//     */
//    public Boolean handleFile(SharedSynAll sharedSynAll) {
//        //禁止
//        if (sharedSynAll.getIs_config_rule().equals(SharedConfigStatus.DELETE.getCode())) {
//            return false;
//        }
//        //父级
//        Optional<SynFile> synFileOpt = synFileMapper.selectByPrimaryKey(sharedSynAll.getSyn_id());
//        if (synFileOpt.isEmpty()) {
//            return false;
//        }
//        SynFile synFile = synFileOpt.get();
//
//        SharedSynAll sharedSynAllBySynId = getSharedSynAllBySynId(synFile.getP_id());
//        if (sharedSynAllBySynId != null) {
//            Boolean dirBool = handleDirFile(sharedSynAllBySynId);
//            return !dirBool.equals(false);
//        }
//        return true;
//    }
//
//
//    /**
//     * 根据syn_id获取sharedSynAll
//     */
//    public SharedSynAll getSharedSynAllBySynId(String synId) {
//        Optional<SharedSynAll> sharedSynAllOptional = sharedSynAllMapper.selectOne(c -> c.applyWhere(whereFinisher -> whereFinisher
//                .and(SharedSynAllDynamicSqlSupport.syn_id, isEqualTo(synId))));
//        if (sharedSynAllOptional.isPresent()) {
//            return sharedSynAllOptional.get();
//        }
//        return null;
//    }
//
//    public Object getTest(Long id) {
////        try {
////            ArrayList<String> ids = new ArrayList<>();
////            selectChildFileBcn(id.toString(), ids);
////            System.out.println(ids);
////        } catch (Exception e) {
////            throw new RuntimeException(e);
////        }
//        return TABLE_PREFIX;
//    }
//
//    public BigInteger getNowTime() {
//        return BigInteger.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
//    }
//
//    /**
//     * 目录上链
//     */
//    public void upDirBcn(SharedSynAll sharedSynAll) {
//        Optional<SynDirectory> synDirectoryOpt = synDirectoryMapper.selectByPrimaryKey(sharedSynAll.getSyn_id());
//        if (synDirectoryOpt.isPresent()) {
//            SynDirectory synDirectory = synDirectoryOpt.get();
//            CatalogInsertInputBO bo = new CatalogInsertInputBO();
//            try {
//                bo.setCreate_time(getNowTime());
//                bo.setId(sharedSynAll.getSyn_id().toString());
//                bo.setUpdate_time(getNowTime());
//                bo.setParent_id(synDirectory.getP_id().toString());
//                bo.setOther_params(objectMapper.writeValueAsString(synDirectory));
//                TransactionResponse insert = catalogService.insert(bo);
//                List<Object> returnObject = insert.getReturnObject();
//                if (CollectionUtils.isEmpty(returnObject)) {
//                    throw new RuntimeException();
//                }
//                TransactionReceipt transactionReceipt = insert.getTransactionReceipt();
//                sharedSynAll.setShared_status(SharedStatus.SUCESS.getCode());
//                sharedSynAll.setBlock_hash(transactionReceipt.getBlockHash());
//                sharedSynAll.setBlock_number(Integer.parseInt(transactionReceipt.getBlockNumber().substring(2), 16) + "");
//                sharedSynAll.setBlock_time(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//
//            } catch (Exception e) {
//                logger.error("目录上链失败！{}", e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
//
//    }
//
//    /**
//     * 目录下链
//     */
//    public void downDirBcn(SharedSynAll sharedSynAll) {
//        Optional<SynDirectory> synDirectoryOpt = synDirectoryMapper.selectByPrimaryKey(sharedSynAll.getSyn_id());
//        if (synDirectoryOpt.isPresent()) {
//            CatalogRemoveInputBO bo = new CatalogRemoveInputBO();
//            try {
//                bo.setCatalog_id(sharedSynAll.getSyn_id().toString());
//                TransactionResponse remove = catalogService.remove(bo);
//                List<Object> returnObject = remove.getReturnObject();
//                if (CollectionUtils.isEmpty(returnObject)) {
//                    throw new RuntimeException();
//                }
//                TransactionReceipt transactionReceipt = remove.getTransactionReceipt();
//                sharedSynAll.setShared_status(SharedStatus.INIT.getCode());
//                sharedSynAll.setBlock_hash(transactionReceipt.getBlockHash());
//                sharedSynAll.setBlock_number(Integer.parseInt(transactionReceipt.getBlockNumber().substring(2), 16) + "");
//                sharedSynAll.setBlock_time(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//            } catch (Exception e) {
//                logger.error("目录下链失败！{}", e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
//    }
//
//    /**
//     * 文件目录上链
//     */
//    public void upFileDirBcn(SharedSynAll sharedSynAll) {
//        Optional<SynDirectoryFile> synDirectoryFileOpt = synDirectoryFileMapper.selectByPrimaryKey(sharedSynAll.getSyn_id());
//        if (synDirectoryFileOpt.isPresent()) {
//            SynDirectoryFile synDirectoryFile = synDirectoryFileOpt.get();
//            ArchivesFileInsertInputBO bo = new ArchivesFileInsertInputBO();
//            try {
//                bo.setId(sharedSynAll.getSyn_id().toString());
//                bo.setCatalog_id(synDirectoryFile.getP_id().toString());
//                //字符类型属性
////                List<String> attStrNames = new ArrayList<>();
////                List<String> attStrValues = new ArrayList<>();
////                attStrNames.add("code");
////                attStrValues.add(synDirectoryFile.getCode());
////                attStrNames.add("name");
////                attStrValues.add(synDirectoryFile.getName());
////                attStrNames.add("author");
////                attStrValues.add(synDirectoryFile.getAuthor());
////                attStrNames.add("file_date");
////                attStrValues.add(synDirectoryFile.getFile_date());
////                attStrNames.add("archives_code");
////                attStrValues.add(synDirectoryFile.getArchives_code());
////                attStrNames.add("other_params");
////                attStrValues.add("");
////                attStrNames.add("url");
////                attStrValues.add(synDirectoryFile.getUrl());
////
////                //int类型属性
////                List<String> attIntNames = new ArrayList<>();
////                List<BigInteger> attIntValues = new ArrayList<>();
////                attIntNames.add("index");
////                attIntValues.add(BigInteger.valueOf(Optional.ofNullable(synDirectoryFile.getSort()).orElse(0)));
////                attIntNames.add("audit_status");
////                attIntValues.add(BigInteger.valueOf(Optional.ofNullable(synDirectoryFile.getAudit_status()).orElse(0)));
////                attIntNames.add("archives_index");
////                attIntValues.add(BigInteger.valueOf(Optional.ofNullable(synDirectoryFile.getArchives_index()).orElse(0)));
////                attIntNames.add("page_count");
////                attIntValues.add(BigInteger.valueOf(Optional.ofNullable(synDirectoryFile.getPage_count()).orElse(0)));
////                attIntNames.add("create_time");
////                attIntValues.add(getNowTime());
////                attIntNames.add("update_time");
////                attIntValues.add(getNowTime());
////
////                bo.setStr_attr_name(attStrNames);
////                bo.setStr_attr_value(attStrValues);
////                bo.setInt_attr_name(attIntNames);
////                bo.setInt_attr_value(attIntValues);
////
////                TransactionResponse insert = archivesFileService.insert(bo);
////                List<Object> returnObject = insert.getReturnObject();
////                if (CollectionUtils.isEmpty(returnObject)) {
////                    throw new RuntimeException();
////                }
////                TransactionReceipt transactionReceipt = insert.getTransactionReceipt();
//                sharedSynAll.setShared_status(SharedStatus.SUCESS.getCode());
////                sharedSynAll.setBlock_hash(transactionReceipt.getBlockHash());
////                sharedSynAll.setBlock_number(Integer.parseInt(transactionReceipt.getBlockNumber().substring(2), 16) + "");
//                sharedSynAll.setBlock_time(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//            } catch (Exception e) {
//                logger.error("文件目录上链失败！{}", e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
//
//    }
//
//    /**
//     * 文件目录下链
//     */
//    public void downFileDirBcn(SharedSynAll sharedSynAll) {
//        Optional<SynDirectoryFile> synDirectoryFileOpt = synDirectoryFileMapper.selectByPrimaryKey(sharedSynAll.getId());
//        if (synDirectoryFileOpt.isPresent()) {
//            ArchivesFileRemoveInputBO bo = new ArchivesFileRemoveInputBO();
//            try {
//                bo.setArchives_file_id(List.of(sharedSynAll.getId().toString()));
//                TransactionResponse remove = archivesFileService.remove(bo);
//                List<Object> returnObject = remove.getReturnObject();
//                if (CollectionUtils.isEmpty(returnObject)) {
//                    throw new RuntimeException();
//                }
//                TransactionReceipt transactionReceipt = remove.getTransactionReceipt();
//                sharedSynAll.setShared_status(SharedStatus.INIT.getCode());
//                sharedSynAll.setBlock_hash(transactionReceipt.getBlockHash());
//                sharedSynAll.setBlock_number(Integer.parseInt(transactionReceipt.getBlockNumber().substring(2), 16) + "");
//                sharedSynAll.setBlock_time(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//            } catch (Exception e) {
//                logger.error("文件目录下链失败！{}", e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
//    }
//
//    /**
//     * 文件上链
//     */
//    public void upFileBcn(SharedSynAll sharedSynAll) {
//        Optional<SynFile> synFileOpt = synFileMapper.selectByPrimaryKey(sharedSynAll.getSyn_id());
//        if (synFileOpt.isPresent()) {
//            SynFile synFile = synFileOpt.get();
//            FileInsertInputBO bo = new FileInsertInputBO();
//            try {
//                bo.setId(sharedSynAll.getSyn_id().toString());
//                bo.setArchive_file_id(synFile.getP_id().toString());
//                //字符类型属性
////                List<String> attStrNames = new ArrayList<>();
////                List<String> attStrValues = new ArrayList<>();
////                attStrNames.add("code");
////                attStrValues.add(synFile.getCode());
////                attStrNames.add("name");
////                attStrValues.add(synFile.getName());
////                attStrNames.add("author");
////                attStrValues.add(synFile.getAuthor());
////                attStrNames.add("file_date");
////                attStrValues.add(synFile.getFile_date());
////                attStrNames.add("other_params");
////                attStrValues.add("");
////                attStrNames.add("url");
////                attStrValues.add(synFile.getUrl());
////
////                //int类型属性
////                List<String> attIntNames = new ArrayList<>();
////                List<BigInteger> attIntValues = new ArrayList<>();
////                attIntNames.add("index");
////                attIntValues.add(BigInteger.valueOf(Optional.ofNullable(synFile.getSort()).orElse(0)));
////                attIntNames.add("file_size");
////                attIntValues.add(BigInteger.valueOf(Optional.ofNullable(synFile.getFile_size()).orElse(0)));
////                attIntNames.add("page_count");
////                attIntValues.add(BigInteger.valueOf(Optional.ofNullable(synFile.getPage_count()).orElse(0)));
////                attIntNames.add("create_time");
////                attIntValues.add(getNowTime());
////                attIntNames.add("update_time");
////                attIntValues.add(getNowTime());
////
////                bo.setStr_attr_name(attStrNames);
////                bo.setStr_attr_value(attStrValues);
////                bo.setInt_attr_name(attIntNames);
////                bo.setInt_attr_value(attIntValues);
////                TransactionResponse insert = fileService.insert(bo);
////                List<Object> returnObject = insert.getReturnObject();
////                if (CollectionUtils.isEmpty(returnObject)) {
////                    throw new RuntimeException();
////                }
////                TransactionReceipt transactionReceipt = insert.getTransactionReceipt();
//                sharedSynAll.setShared_status(SharedStatus.SUCESS.getCode());
////                sharedSynAll.setBlock_hash(transactionReceipt.getBlockHash());
////                sharedSynAll.setBlock_number(Integer.parseInt(transactionReceipt.getBlockNumber().substring(2), 16) + "");
//                sharedSynAll.setBlock_time(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//            } catch (Exception e) {
//                logger.error("文件目录上链失败！{}", e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
//
//    }
//
//    /**
//     * 文件下链
//     */
//    public void downFileBcn(SharedSynAll sharedSynAll) {
//        Optional<SynFile> synFileOpt = synFileMapper.selectByPrimaryKey(sharedSynAll.getSyn_id());
//        if (synFileOpt.isPresent()) {
//            FileRemoveInputBO bo = new FileRemoveInputBO();
//            try {
//                bo.setFile_id(List.of(sharedSynAll.getSyn_id().toString()));
//                TransactionResponse remove = fileService.remove(bo);
//                List<Object> returnObject = remove.getReturnObject();
//                if (CollectionUtils.isEmpty(returnObject)) {
//                    throw new RuntimeException();
//                }
//                TransactionReceipt transactionReceipt = remove.getTransactionReceipt();
//                sharedSynAll.setShared_status(SharedStatus.INIT.getCode());
//                sharedSynAll.setBlock_hash(transactionReceipt.getBlockHash());
//                sharedSynAll.setBlock_number(Integer.parseInt(transactionReceipt.getBlockNumber().substring(2), 16) + "");
//                sharedSynAll.setBlock_time(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//            } catch (Exception e) {
//                logger.error("文件目录下链失败！{}", e.getMessage());
//                throw new RuntimeException(e);
//            }
//        }
//    }
//
//}
//
