|  | @@ -0,0 +1,210 @@
 | 
	
		
			
				|  |  | +package sys.sc.formplugin;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.alibaba.druid.support.logging.Log;
 | 
	
		
			
				|  |  | +import com.alibaba.druid.support.logging.LogFactory;
 | 
	
		
			
				|  |  | +import kd.bos.context.RequestContext;
 | 
	
		
			
				|  |  | +import kd.bos.dataentity.entity.DynamicObject;
 | 
	
		
			
				|  |  | +import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
 | 
	
		
			
				|  |  | +import kd.bos.entity.EntityMetadataCache;
 | 
	
		
			
				|  |  | +import kd.bos.exception.KDException;
 | 
	
		
			
				|  |  | +import kd.bos.orm.query.QCP;
 | 
	
		
			
				|  |  | +import kd.bos.orm.query.QFilter;
 | 
	
		
			
				|  |  | +import kd.bos.schedule.api.MessageHandler;
 | 
	
		
			
				|  |  | +import kd.bos.schedule.executor.AbstractTask;
 | 
	
		
			
				|  |  | +import kd.bos.servicehelper.BusinessDataServiceHelper;
 | 
	
		
			
				|  |  | +import kd.bos.servicehelper.operation.SaveServiceHelper;
 | 
	
		
			
				|  |  | +import sys.sc.opplugin.utils.SftpClient;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.io.BufferedReader;
 | 
	
		
			
				|  |  | +import java.io.IOException;
 | 
	
		
			
				|  |  | +import java.io.InputStreamReader;
 | 
	
		
			
				|  |  | +import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * @author cjz
 | 
	
		
			
				|  |  | + * @date 2024/9/10 9:00
 | 
	
		
			
				|  |  | + * @description:日常更新行名行号调度计划
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +public class UpdateBankDailyTask extends AbstractTask {
 | 
	
		
			
				|  |  | +    private static final Log log = LogFactory.getLog(UpdateBankDailyTask.class);
 | 
	
		
			
				|  |  | +    public static String host="192.168.10.69";//服务器ip地址
 | 
	
		
			
				|  |  | +    public static String username="root";//用户名
 | 
	
		
			
				|  |  | +    public static int port = 22;//端口号
 | 
	
		
			
				|  |  | +    public static String password="Kd@86262007";//用户密码
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public MessageHandler getMessageHandle() {
 | 
	
		
			
				|  |  | +        return super.getMessageHandle();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
 | 
	
		
			
				|  |  | +        log.info("----------------------获取服务器连接------------------------");
 | 
	
		
			
				|  |  | +        //获取服务器连接
 | 
	
		
			
				|  |  | +        SftpClient sftpClient=new SftpClient(host,username,password,port);
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            sftpClient.connect();
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException(e);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        String mes="-----------------正在执行任务,读取行名行号---------------------";
 | 
	
		
			
				|  |  | +        log.info(mes+requestContext);
 | 
	
		
			
				|  |  | +        //行名行号表字段
 | 
	
		
			
				|  |  | +        List<String> selector = Arrays.asList(
 | 
	
		
			
				|  |  | +                "number", "name"
 | 
	
		
			
				|  |  | +        );
 | 
	
		
			
				|  |  | +        //行名行号表标识
 | 
	
		
			
				|  |  | +        String bd_bebank="bd_bebank";
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            List<DynamicObject> createdataList = new ArrayList<>();
 | 
	
		
			
				|  |  | +            List<DynamicObject> updatedataList = new ArrayList<>();
 | 
	
		
			
				|  |  | +            //文件路径
 | 
	
		
			
				|  |  | +            String fileurl = getFileUrl("hmhhurl");
 | 
	
		
			
				|  |  | +            //基础资料
 | 
	
		
			
				|  |  | +            DynamicObjectType dynamicObjectType = EntityMetadataCache.getDataEntityType(bd_bebank);
 | 
	
		
			
				|  |  | +            //国家基础资料(中国)
 | 
	
		
			
				|  |  | +            DynamicObject country= BusinessDataServiceHelper
 | 
	
		
			
				|  |  | +                    .loadSingle("bd_country","number", new QFilter[]{new QFilter("number", QCP.equals, "001")});
 | 
	
		
			
				|  |  | +            //查出所有现有的数据
 | 
	
		
			
				|  |  | +            DynamicObject[] existingData = BusinessDataServiceHelper
 | 
	
		
			
				|  |  | +                    .load(dynamicObjectType.getName(), String.join(",", selector)+",enable,nckd_datasource", null);
 | 
	
		
			
				|  |  | +            //查找文件更新的数据
 | 
	
		
			
				|  |  | +            Map<String, DynamicObject> existingDataMap = new HashMap<>();
 | 
	
		
			
				|  |  | +            for (DynamicObject obj : existingData) {
 | 
	
		
			
				|  |  | +                //单据号
 | 
	
		
			
				|  |  | +                existingDataMap.put(obj.getString(selector.get(0)), obj);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            Set<String> processeddubilidSet = new HashSet<>();
 | 
	
		
			
				|  |  | +            //读取服务器文件并写入
 | 
	
		
			
				|  |  | +            try (BufferedReader reader = new BufferedReader(new InputStreamReader(sftpClient.fileInputStream(fileurl)))) {
 | 
	
		
			
				|  |  | +                String line;
 | 
	
		
			
				|  |  | +                while ((line = reader.readLine()) != null) {
 | 
	
		
			
				|  |  | +                    //去掉换行符|$|
 | 
	
		
			
				|  |  | +                    line = line.replace("|$|", "");
 | 
	
		
			
				|  |  | +                    //空格符号,根据这个符号分割
 | 
	
		
			
				|  |  | +                    String[] fields = line.split("\u0001");
 | 
	
		
			
				|  |  | +                    //如果获取的数据行不等于预设字段的行,则跳过这行数据
 | 
	
		
			
				|  |  | +//                        if (fields.length != selector.size()) {
 | 
	
		
			
				|  |  | +//                            continue;
 | 
	
		
			
				|  |  | +//                        }
 | 
	
		
			
				|  |  | +                    //获取单据号
 | 
	
		
			
				|  |  | +                    String dubil_id = fields[0];
 | 
	
		
			
				|  |  | +                    boolean isUpdated = false;
 | 
	
		
			
				|  |  | +                    DynamicObject data;
 | 
	
		
			
				|  |  | +                    processeddubilidSet.add(dubil_id);
 | 
	
		
			
				|  |  | +                    if (existingDataMap.containsKey(dubil_id)) {
 | 
	
		
			
				|  |  | +                        //获取对应单据号的数据
 | 
	
		
			
				|  |  | +                        data = existingDataMap.get(dubil_id);
 | 
	
		
			
				|  |  | +                        for (int i = 0; i < selector.size(); i++) {
 | 
	
		
			
				|  |  | +                            String fieldName = selector.get(i);
 | 
	
		
			
				|  |  | +                            String newValue = fields[i];
 | 
	
		
			
				|  |  | +                            String existingValue = data.getString(fieldName);
 | 
	
		
			
				|  |  | +                            //判断数据是否与之前的数据相同,不相同则更新
 | 
	
		
			
				|  |  | +                            if (!Objects.equals(existingValue, newValue)) {
 | 
	
		
			
				|  |  | +                                data.set(fieldName, newValue);
 | 
	
		
			
				|  |  | +                                isUpdated = true;
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        //更新了数据则设置数据更新的时间
 | 
	
		
			
				|  |  | +                        if (isUpdated) {
 | 
	
		
			
				|  |  | +                            updatedataList.add(data);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        //不存在这条数据则新增一条到data中
 | 
	
		
			
				|  |  | +                        data = new DynamicObject(dynamicObjectType);
 | 
	
		
			
				|  |  | +                        for (int i = 0; i < selector.size(); i++) {
 | 
	
		
			
				|  |  | +                            data.set(selector.get(i), fields[i]);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        //国家
 | 
	
		
			
				|  |  | +                        data.set("country",country);
 | 
	
		
			
				|  |  | +                        //省
 | 
	
		
			
				|  |  | +                        data.set("provincetxt","江西");
 | 
	
		
			
				|  |  | +                        //市
 | 
	
		
			
				|  |  | +                        data.set("citytxt","南昌");
 | 
	
		
			
				|  |  | +                        //联行号等于行号
 | 
	
		
			
				|  |  | +                        data.set("union_number",fields[0]);
 | 
	
		
			
				|  |  | +                        //设置可用状态
 | 
	
		
			
				|  |  | +                        data.set("enable","1");
 | 
	
		
			
				|  |  | +                        //设置数据来源,0为手动更新,1为文件自动更新
 | 
	
		
			
				|  |  | +                        data.set("nckd_datasource","1");
 | 
	
		
			
				|  |  | +                        createdataList.add(data);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //关闭连接
 | 
	
		
			
				|  |  | +            sftpClient.disconnect();
 | 
	
		
			
				|  |  | +            //获取自动添加的数据,与手动添加的数据做区分,nckd_datasource:1为读取数据添加,0为手动添加
 | 
	
		
			
				|  |  | +            DynamicObject[] autoData = BusinessDataServiceHelper
 | 
	
		
			
				|  |  | +                    .load(dynamicObjectType.getName()
 | 
	
		
			
				|  |  | +                            , String.join(",", selector)+",enable,nckd_datasource",
 | 
	
		
			
				|  |  | +                            new QFilter[]{new QFilter("nckd_datasource", QCP.equals, "1")});
 | 
	
		
			
				|  |  | +            for (DynamicObject autoDatum : autoData) {
 | 
	
		
			
				|  |  | +                //如果导入的数据在系统中没有则禁用
 | 
	
		
			
				|  |  | +                if (!processeddubilidSet.contains(autoDatum.getString("number"))) {
 | 
	
		
			
				|  |  | +                    //设置禁用
 | 
	
		
			
				|  |  | +                    autoDatum.set("enable", "0");
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            SaveServiceHelper.save(autoData);
 | 
	
		
			
				|  |  | +            //查找更新数据
 | 
	
		
			
				|  |  | +            for (Map.Entry<String, DynamicObject> entry : existingDataMap.entrySet()) {
 | 
	
		
			
				|  |  | +                String dubil_id = entry.getKey();
 | 
	
		
			
				|  |  | +                DynamicObject data = entry.getValue();
 | 
	
		
			
				|  |  | +                if (!processeddubilidSet.contains(dubil_id)) {
 | 
	
		
			
				|  |  | +//                        data.set("nckd_isdelete", true);
 | 
	
		
			
				|  |  | +                    updatedataList.add(data);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //新增数据不为空则更新到数据库中
 | 
	
		
			
				|  |  | +            if (!createdataList.isEmpty()) {
 | 
	
		
			
				|  |  | +                SaveServiceHelper.save(dynamicObjectType, createdataList.toArray(new DynamicObject[0]));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //更新的数据不为空,则更新到数据库中
 | 
	
		
			
				|  |  | +            if (!updatedataList.isEmpty()) {
 | 
	
		
			
				|  |  | +                SaveServiceHelper.save(updatedataList.get(0).getDynamicObjectType(), updatedataList.toArray(new DynamicObject[0]));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            log.info("------------------数据已成功保存------------------");
 | 
	
		
			
				|  |  | +        } catch (IOException ex) {
 | 
	
		
			
				|  |  | +            ex.printStackTrace();
 | 
	
		
			
				|  |  | +            log.info("----------------------读取文件时发生错误-----------------");
 | 
	
		
			
				|  |  | +        } catch (Exception ex) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException(ex);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public boolean isSupportReSchedule() {
 | 
	
		
			
				|  |  | +        return super.isSupportReSchedule();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //根据接口配置信息获取组织人员,拼接服务器文件路径url,参数为urlcode接口配置信息编码
 | 
	
		
			
				|  |  | +    public String getFileUrl(String urlcode)
 | 
	
		
			
				|  |  | +    {
 | 
	
		
			
				|  |  | +        //组织人员接口配置信息获取
 | 
	
		
			
				|  |  | +        DynamicObject nckd_jkpzxx= BusinessDataServiceHelper
 | 
	
		
			
				|  |  | +                .loadSingle("nckd_jkpzxx", new QFilter[]{new QFilter("number", "=", urlcode)});
 | 
	
		
			
				|  |  | +        //取文件名
 | 
	
		
			
				|  |  | +        String nckd_filename=nckd_jkpzxx.getString("nckd_filename");
 | 
	
		
			
				|  |  | +        //获取文件路径
 | 
	
		
			
				|  |  | +        String nckd_url=nckd_jkpzxx.getString("nckd_url");
 | 
	
		
			
				|  |  | +        //当前日期
 | 
	
		
			
				|  |  | +        Date currentDate=new Date();
 | 
	
		
			
				|  |  | +        Calendar calendar = Calendar.getInstance();
 | 
	
		
			
				|  |  | +        calendar.setTime(currentDate);
 | 
	
		
			
				|  |  | +        calendar.add(Calendar.DATE, -1); // 将日期减少一天
 | 
	
		
			
				|  |  | +        //日期减少一天
 | 
	
		
			
				|  |  | +        Date newDate = calendar.getTime();
 | 
	
		
			
				|  |  | +        //转换日期格式
 | 
	
		
			
				|  |  | +        SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
 | 
	
		
			
				|  |  | +        String datestr=sf.format(newDate);
 | 
	
		
			
				|  |  | +        //文件路径拼接
 | 
	
		
			
				|  |  | +        //真实路径
 | 
	
		
			
				|  |  | +        if (nckd_url.equals("/var/appstatic/")) {
 | 
	
		
			
				|  |  | +            nckd_url="/home/kingdee/cosmic/nginx-appstatic/store/appstatic/";
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        String realPath = nckd_url+datestr+"/"+nckd_filename;
 | 
	
		
			
				|  |  | +        return realPath;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |