package fi.gl.task; import kd.bos.context.RequestContext; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.entity.AppInfo; import kd.bos.entity.AppMetadataCache; import kd.bos.entity.param.AppParam; import kd.bos.exception.KDException; import kd.bos.orm.query.QCP; import kd.bos.orm.query.QFilter; import kd.bos.schedule.api.TaskInfo; import kd.bos.schedule.executor.AbstractTask; import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.servicehelper.parameter.SystemParamServiceHelper; import kd.bos.servicehelper.schedule.ScheduleServiceHelper; import kd.bos.servicehelper.user.UserServiceHelper; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; /** * 自动日结调度任务插件 * 2024-06-28 wangj * 主要功能:日结执行按钮逻辑 */ public class AutoDailTask extends AbstractTask { @Override public void execute(RequestContext requestContext, Map map) throws KDException { TaskInfo taskInfo = ScheduleServiceHelper.queryTask(this.taskId); String scheduleId = taskInfo.getScheduleId(); DynamicObject schedule = BusinessDataServiceHelper.loadSingleFromCache(scheduleId,"sch_schedule"); DynamicObject schprincipal = schedule.getDynamicObject("schprincipal"); Long userId = schprincipal.getLong("id"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String now = sdf.format(new Date()); Date nowDate = null; try { nowDate = sdf.parse(now); } catch (ParseException e) { throw new RuntimeException(e); } QFilter qFilter_enable = new QFilter("enable", QCP.equals,"1"); QFilter qFilter_status = new QFilter("status", QCP.equals, "C"); QFilter qFilter_fisaccounting = new QFilter("fisaccounting", QCP.equals, "1"); QFilter qFilter_number = new QFilter("number", QCP.equals, "A002"); DynamicObject[] orgCol = BusinessDataServiceHelper.load("bos_org","id", new QFilter[]{qFilter_enable,qFilter_status,qFilter_fisaccounting,qFilter_number}); for(DynamicObject orgObj : orgCol){ //根据应用编码从缓存中获取应用信息 AppInfo cgfwAppInfo = AppMetadataCache.getAppInfo("gl"); //获取应用的主键 String appId = cgfwAppInfo.getId(); AppParam apm = new AppParam(); apm.setAppId(appId); apm.setOrgId(orgObj.getLong("id")); Map paramWhole = SystemParamServiceHelper.loadAppParameterFromCache(apm); Object nckd_isautodaily = paramWhole.get("nckd_isautodaily"); if("true".equals(nckd_isautodaily.toString())){ dailyData(orgObj,nowDate,userId); } } } private void dailyData(DynamicObject org, Date dailydate, Long userId) { //创建日志对象 DynamicObject dailybillLogObj = BusinessDataServiceHelper.newDynamicObject("nckd_gl_autodaillog"); String messageInfo = ""; QFilter qFilter_org = new QFilter("org", QCP.equals,org.getPkValue()); QFilter qFilter_bizdate = new QFilter("bizdate",QCP.equals,dailydate); DynamicObject[] voucherCol = BusinessDataServiceHelper.load("gl_voucher","id,ispost,billno,bizdate,org,billstatus", new QFilter[]{qFilter_org,qFilter_bizdate}); for(DynamicObject voucherObj : voucherCol){ boolean ispost = voucherObj.getBoolean("ispost"); if(ispost!=true){//是否过账等于否 messageInfo = "所选日期存在未过账凭证"; break; } } if("".equals(messageInfo)){ QFilter qFilter_nckd_org = new QFilter("nckd_org", QCP.equals,org.getPkValue()); QFilter qFilter_nckd_dailydate = new QFilter("nckd_dailydate",QCP.equals,dailydate); DynamicObject[] dailybillCol = BusinessDataServiceHelper.load("nckd_gl_dailybill","id", new QFilter[]{qFilter_nckd_org,qFilter_nckd_dailydate}); if(dailybillCol!=null && dailybillCol.length>0){ messageInfo = "所选日期已日结"; } } if("".equals(messageInfo)){ //写入日结记录 messageInfo = writeDailybill(org,dailydate,userId); } //写入日志 dailybillLogObj.set("nckd_orgfield",org.getPkValue()); dailybillLogObj.set("nckd_dailydate",dailydate); dailybillLogObj.set("enable","1"); dailybillLogObj.set("status","C"); if("".equals(messageInfo)){ dailybillLogObj.set("nckd_dailstatus","0"); }else{ dailybillLogObj.set("nckd_dailstatus","1"); dailybillLogObj.set("nckd_errorinfo",messageInfo); } DynamicObject[] logDatalist = new DynamicObject[]{dailybillLogObj}; SaveServiceHelper.save(logDatalist); } private String writeDailybill(DynamicObject orgObjReal, Date dailydate, Long userId) { String messageInfo = ""; try { DynamicObject dailybillObj = BusinessDataServiceHelper.newDynamicObject("nckd_gl_dailybill"); dailybillObj.set("nckd_org", orgObjReal); dailybillObj.set("nckd_dailydate", dailydate); dailybillObj.set("status", "A"); dailybillObj.set("enable", "1"); dailybillObj.set("creator", userId); DynamicObject[] datalist = new DynamicObject[]{dailybillObj}; SaveServiceHelper.save(datalist); }catch(Exception e){ messageInfo = e.getMessage(); } return messageInfo; } }