# app_script_history

id: 60aae1b35db2f37491f98fb8
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 08:35:52.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


id: 60ab0ab55db2f37491f994a1
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:20:30.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getIntFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(ym);
    if( ym==0) 
    {
        ym =  getYear(newDate())*100+ getInt(newDate());
    }else{
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ym; 


	

}


id: 60ab0ab55db2f37491f994a9
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:20:54.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getIntFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(ym);
    if( ym==0) 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
    }else{
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ym; 


	

}


id: 60ab0ab55db2f37491f994b1
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:21:34.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getIntFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(ym);
    if( ym==0) 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
        println( ym);
    }else{
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ym; 


	

}


id: 60ab0ab55db2f37491f994b9
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:22:26.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getIntFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(ym);
    if( ym==0) 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
        println( ym);
    }else{
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab0ab55db2f37491f994c1
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:23:27.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getIntFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(ym);
    ym=202012;
    if( ym==0) 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
        println( ym);
    }else{
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab0ab55db2f37491f994c9
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:23:57.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getIntFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
   // println(ym);
   
    if( ym==0) 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab0e675db2f37491f99517
scriptid: 60ab0ab55db2f37491f994ce
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:25:30.000
script:
/*
得到指定帐套的最后关帐期

 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getStringFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
   
    return ym ; 
    


	

}


id: 60ab0e675db2f37491f9952b
scriptid: 60aaff095db2f37491f99016
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:31:52.000
script:
/**
 * 表单初始化完成后，执行此事件
 */
//@comment (group=表单事件,caption=表单初始化完成)
function formOpen()
{
    init();
}


function init()
{
    var corporationid= book.sheet0.cells("corporationid").getValueString();
    var ym=  runTask("当前记帐期",{corporationid:corporationid});
    book.sheet0.cells("ym").setValue(ym);


}


function cb_ok_onClick()
{
    var corporationid= book.sheet0.cells("corporationid").getValueString();
    var ym= book.sheet0.cells("ym").getValueString();
    if( ym=='') 
    {
        toastr.error("没有记帐期可以关帐");
        return;
    }

    $confirm("提示","请检查帐套信息及记帐期，确信要关帐吗?" , function()
    {
         showInfoPane("*正在关帐");
          runTask("关帐",{corporationid:corporationid, ym:ym} , function()
          {
              hideInfoPane();
              toastr.success("关帐完成");
              init();
          })  ;
    });

}


id: 60ab11bb5db2f37491f99579
scriptid: 60ab0e675db2f37491f99530
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:43:34.000
script:
function main(arg)
{
    var p= JSON.parse(arg);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var ds=newDataStore("pz", "select * from dzpz_gz");
    ds.setUpdateProperty("dzpz_gz","id","*","id");
    ds.retrieve(" corporationid='"+ corporationid+"'  and ym="+ ym);
    if( ds.getRowCount()==0)
    {
        ds.insertRow(0);
        var newid= newGUID();
        ds.setValue(0,"id", newid);
        ds.setValue(0,"gguid", newid);
        ds.setValue(0,"billno", newid);
        ds.setValue(0,"billdate", newDate());
        ds.setValue(0,"userid", currentUserId());
        ds.setValue(0,"username", currentUserShowName());
        ds.setValue(0,"corporationid", corporationid);
        ds.setValue(0,"ym", ym);
        
    }

    ds.setValue(0,"state", 1);
    ds.setValue(0,"gzsj", newDate()); //关帐时间 
    
    if( ds.update(true)) return {success:true};
    var err= ds.getError().getMessage();
    println( err);
    return {success:false, message:err};

}


id: 60ab12fe5db2f37491f99610
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 10:49:01.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= ''+getStringFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
   // println(ym);
   
    if( ym=='') 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        ym= parseInt(ym);
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab12fe5db2f37491f99618
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 11:06:17.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';
    println( corporationid);

    var ym= ''+getStringFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(ym);
   
    if( ym=='') 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        ym= parseInt(ym);
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab18b35db2f37491f9964c
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 11:29:31.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';
    println( corporationid);

    var ym= ''+getStringFromSQL( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(" ym= "+ ym);
   
    if( ym=='') 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        ym= parseInt(ym);
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab18b35db2f37491f99654
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 11:30:24.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';
    println( corporationid);

    var ym= ''+getStringFromSQL2( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(" ym= "+ ym);
   
    if( ym=='') 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        ym= parseInt(ym);
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab18b35db2f37491f9965c
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 11:31:14.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';
    println( corporationid);

    var ym= ''+getStringFromSQL2( "pz", " select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
    println(" ym= "+ ym);
   
    if( ym=='') 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        ym= parseInt(ym);
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab18b35db2f37491f99664
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 11:32:57.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';
    println( corporationid);

    var sql= " select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ";
    println( sql);
    var ym= ''+getStringFromSQL2( "pz", sql);
    println(" ym= "+ ym);
   
    if( ym=='') 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        ym= parseInt(ym);
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab18b35db2f37491f9966c
scriptid: 60ab09f85db2f37491f99447
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 11:36:49.000
script:
/*
得到指定帐套的当前帐期
原理：  获取上一个关帐的年月，如果不存在，那么直接返回本月
        关帐年月的下一个月就是当前活动年月
 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';
    println( corporationid);

    var sql= " select  max(ym) from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ";
    println( sql);
    var ym= ''+getStringFromSQL2( "pz", sql);
    println(" ym= "+ ym);
   
    if( ym=='') 
    {
        ym =  getYear(newDate())*100+ getMonth(newDate());
     //   println( ym);
    }else{
        ym= parseInt(ym);
        var  y = round( ym /100 ,0);
        var m= ym -  y *100;
        m=m+1;
        if( m==13)
        {
            y++;
            m=1;
        }
        ym=  y*100 + m ;
    }

    return ''+ round(ym,0); 


	

}


id: 60ab2c6e5db2f37491f997c0
scriptid: 60ab2c0d5db2f37491f9970b
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 12:33:05.000
script:
function main(arg)
{
    var p= JSON.parse(arg);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var ds=newDataStore("pz", "select * from dzpz_gz");
    ds.setUpdateProperty("dzpz_gz","id","*","id");
    ds.retrieve(" corporationid='"+ corporationid+"'  and ym="+ ym);
    if( ds.getRowCount()==0)
    {
        ds.insertRow(0);
        var newid= newGUID();
        ds.setValue(0,"id", newid);
        ds.setValue(0,"gguid", newid);
        ds.setValue(0,"billno", newid);
        ds.setValue(0,"billdate", newDate());
        ds.setValue(0,"userid", currentUserId());
        ds.setValue(0,"username", currentUserShowName());
        ds.setValue(0,"corporationid", corporationid);
        ds.setValue(0,"ym", ym);
        
    }

    ds.setValue(0,"state", 0);
    ds.setValue(0,"gzsj", null); //关帐时间 
    
    if( ds.update(true)) return {success:true};
    var err= ds.getError().getMessage();
    println( err);
    return {success:false, message:err};

}


id: 60ab2c6e5db2f37491f997d7
scriptid: 60ab0ab55db2f37491f994ce
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 12:34:45.000
script:
/*
得到指定帐套的最后关帐期

 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getStringFromSQL2( "pz", "  select  max (ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
   
    return ym ; 
    


	

}


id: 60ab2c6e5db2f37491f997df
scriptid: 60ab0ab55db2f37491f994ce
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 12:35:21.000
script:
/*
得到指定帐套的最后关帐期

 */

function main(arg)
{
    var p= JSON.parse( arg);
    var corporationid= p.corporationid ||'';

    var ym= getStringFromSQL2( "pz", "  select  max(ym)  from dzpz_gz where   state = 1 and corporationid='"+ corporationid+"' ");
   
    return ym ; 
    


	

}


id: 60ab2c6e5db2f37491f997ed
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 12:46:05.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet0.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet0.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


id: 60ab2c6e5db2f37491f997f3
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 12:46:58.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


id: 60ab32ab5db2f37491f99921
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 13:30:54.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号，如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];

    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];


    }


}


id: 60ab32ab5db2f37491f99927
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 13:31:05.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];

    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];


    }


}


id: 60ab32ab5db2f37491f9992d
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 13:38:16.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lasPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.fj=[];
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        
    });


}


id: 60ab32ab5db2f37491f99933
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 13:39:14.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lasPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.fj=[];
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

function  do_pzgd( pzGroup)
{
    
}


id: 60ab32ab5db2f37491f99939
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 13:39:34.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lasPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.fj=[];
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{

}


id: 60ab32ab5db2f37491f9993f
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 13:41:53.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.fj=[];
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{

}


id: 60ab513d5db26b6db2c28a87
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 16:08:45.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.fj=[];
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", pzGroup , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}


id: 60ab5fa75db26b6db2c28b13
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 16:32:55.000
script:
/*
归档处理

*/

function main(arg)
{
    var p= JSON.parse(arg);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0d);
    

}


id: 60ab5fa75db26b6db2c28b1b
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 16:33:00.000
script:
/*
归档处理

*/

function main(arg)
{
    var p= JSON.parse(arg);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);
    

}


id: 60ab68505db26b6db2c28b23
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 16:48:16.000
script:
/*
归档处理

*/

function main(arg)
{
    var p= JSON.parse(arg);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        dzpz_imgpool_ds.retrieve(" id='"+config.fj[i]+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
      
        dzpz_pzfj_ds.setValeu(row,"gguid", id);
        dzpz_pzfj_ds.setValeu(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValeu(row,"corporationid", corporationid);

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds);
    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ab6fd65db2a86e7ea8859e
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:31:21.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    return obj.text();
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", pzGroup , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}


id: 60ab6fd65db2a86e7ea885a9
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:36:57.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
    var p= JSON.parse(arg);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValeu(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValeu(row,"gguid", id);
        dzpz_pzfj_ds.setValeu(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValeu(row,"corporationid", corporationid);

        //删除源
        dzpz_pzfj_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupdate where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ab74555db2a86e7ea886d9
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:42:03.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
    println( arg);
    var p= JSON.parse(arg);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValeu(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValeu(row,"gguid", id);
        dzpz_pzfj_ds.setValeu(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValeu(row,"corporationid", corporationid);

        //删除源
        dzpz_pzfj_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupdate where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ab74555db2a86e7ea886e1
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:42:22.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValeu(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValeu(row,"gguid", id);
        dzpz_pzfj_ds.setValeu(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValeu(row,"corporationid", corporationid);

        //删除源
        dzpz_pzfj_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupdate where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ab74555db2a86e7ea886e7
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:44:03.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid= book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", pzGroup , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}


id: 60ab74555db2a86e7ea886f3
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:47:21.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}


id: 60ab74555db2a86e7ea886fb
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:49:09.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValue(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValeu(row,"gguid", id);
        dzpz_pzfj_ds.setValeu(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValeu(row,"corporationid", corporationid);

        //删除源
        dzpz_pzfj_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupdate where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ab76c25db2a86e7ea88703
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 17:49:55.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValue(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        dzpz_pzfj_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupdate where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ab9dd75db2a837c83ac50a
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 20:42:42.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length );
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValue(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60aba1855db2a837c83ac5dd
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 21:30:52.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length -1 ); //注意图片中有一个是凭证，所以附件数要减1 
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValue(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60aba1855db2a837c83ac5e3
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 21:31:54.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}


id: 60aba1855db2a837c83ac5e9
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 21:32:53.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);
        
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}


id: 60aba1855db2a837c83ac5f9
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 21:38:33.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id)
{
    
}


id: 60abac6c5db2a837c83ac668
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 21:43:05.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
}


id: 60abbe055db2a837c83ac7ce
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 23:02:39.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
   // book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
}


id: 60abbe055db2a837c83ac7db
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 23:09:19.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}

//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}

/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    book.sheet3.getBrick('FJ').buildGridly( getPZFJ(id));
}


id: 60abbe055db2a837c83ac7e8
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 23:09:49.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


function getPZFJ( id)
{
    
}


id: 60abbe055db2a837c83ac7f0
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 23:14:50.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}


id: 60abc7755db2a837c83ac7fb
scriptid: 60a31c3c5db27838e0c012a2
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.24 23:34:13.000
script:
/**
 * 表单初始化完成后，执行此事件
 */
//@comment (group=表单事件,caption=表单初始化完成)
function formOpen()
{
book.showTabOnWhere="top";

}


id: 60abc7755db2a837c83ac821
scriptid: 60ab513d5db26b6db2c28aa6
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 00:24:36.000
script:
/*
归档处理
  1 生成凭证主记录
  2 生成凭证附件记录

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var corporationid= p.corporationid;
    var ym= p.ym;
    var pzGroup= p.pzGroup;  //pzGroup 的结构是 [   {pzh: '0001', imgID:'xxxx', fj:['xxxx', 'xxxxx'...] } , { ... } ] 

    for( var i=0;i< pzGroup.length;i++)
    {
        ret=gd( corporationid, ym , pzGroup[i] );    
        if( ! ret.success) return ret; 
    }

    return {success:true};

}


function  gd( corporationid, ym,  config)
{
    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.setUpdateProperty("dzpz_pz", "id","*" ,"id");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    

    var id= newGUID();
    dzpz_pz_ds.insertRow(0);
    dzpz_pz_ds.setValue(0,"id",id);
    dzpz_pz_ds.setValue(0,"gguid",id);
    dzpz_pz_ds.setValue(0,"billno",id);
    dzpz_pz_ds.setValue(0,"billdate",newDate());
    dzpz_pz_ds.setValue(0,"billtype","");
    dzpz_pz_ds.setValue(0,"open_bill_with_url","billedit?action=view&gguid="+id+"&id=dzpz");
    dzpz_pz_ds.setValue(0,"userid",currentUserId() );
    dzpz_pz_ds.setValue(0,"username",currentUserShowName());
    dzpz_pz_ds.setValue(0,"pzh", config.pzh);
    dzpz_pz_ds.setValue(0,"attachmentcount", config.fj.length -1 ); //注意图片中有一个是凭证，所以附件数要减1 
    dzpz_pz_ds.setValue(0,"ym",ym);
    dzpz_pz_ds.setValue(0,"imgid", config.imgID);
    
    dzpz_pz_ds.setValue(0,"corporationid",corporationid);
    dzpz_pz_ds.setValue(0,"deleteflag",0);

    // 附件清单
    for( var i=0;i< config.fj.length;i++)
    {
        var fromID = config.fj[i];
        if( fromID==config.imgID)  //如果这个图片是凭证图片，那么主记录上也记录一下
        {

        }

        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整

    
        if( fromID==config.imgID)  
        {
            ////如果这个图片是凭证图片，那么主记录上也记录一下
            dzpz_pz_ds.setValue(0,"url_img", dzpz_imgpool_ds.getString(0,"url_img") );
            dzpz_pz_ds.setValue(0,"url_thumbnail", dzpz_imgpool_ds.getString(0,"url_thumbnail") );
            //附件表上的附件类型
            dzpz_pzfj_ds.setValue(row,"groupname", '凭证');  //把类型改成 凭证
        }
      
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    var err =  ''+updateAll( dzpz_pz_ds , dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac58145db2f5d22859c3bf
scriptid: 609d3cfc5db2b0a331d6bb4f
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 10:01:11.000
script:
/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    return { success:false, messge:"禁止删除"};
}


id: 60ac58145db2f5d22859c3c5
scriptid: 609d3cfc5db2b0a331d6bb4f
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 10:04:38.000
script:
/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    return { success:false, message:"禁止删除"};
}


id: 60ac58145db2f5d22859c3d6
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 10:55:55.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}


id: 60ac58145db2f5d22859c3dc
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 10:59:15.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {

    }
    return { success:false, message:"禁止删除"};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    book.sheet3.getBrick('FJ').buildGridly( getPZFJ(id));
}


id: 60ac58145db2f5d22859c3e2
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:02:22.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            putImageBackToPool ( data.id);
            return {success:true};
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    book.sheet3.getBrick('FJ').buildGridly( getPZFJ(id));
}


id: 60ac69375db2f5d22859c42d
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:10:36.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            putImageBackToPool ( data.id);
            return {success:true};
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    
    let action=  ( $one_ds.getString(0,"verifiername")=='')?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac6cb75db2f5d22859c49d
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:28:49.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            putImageBackToPool ( data.id);
            return {success:true};
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==1 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac6cb75db2f5d22859c4a3
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:29:47.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            putImageBackToPool ( data.id);
            return {success:true};
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac6cb75db2f5d22859c4a9
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:53:04.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            return  putImageBackToPool ( data.id);
            
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac6cb75db2f5d22859c4b1
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:53:26.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



function putImageBackToPool( id )
{

}


id: 60ac750d5db2f5d22859c4b9
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:54:54.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


id: 60ac750d5db2f5d22859c518
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:04:12.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.id;  // dzpz_pzfj.id 
    
    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid= JSON.parse(t).gguid;


    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //待归档的图片池
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");

        
    var row= dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow( dzpz_pzfj_ds , dzpz_imgpool_fileupload_ds , row);

        //数据调整

  
        dzpz_imgpool_fileupload_ds.setValue(row,"groupname", '附件');  //把类型改成 凭证
        
        dzpz_imgpool_fileupload_ds.setValue(row,"gguid", gguid);
        dzpz_imgpool_fileupload_ds.setValue(row,"tablename", "dzpz_imgpool");
        dzpz_imgpool_fileupload_ds.setValue(row,"corporationid", null);

        //删除源
        //当 
        dzpz_pzfj_ds.deleteRow(0);
 

    var err =  ''+updateAll(  dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow(  dzpz_pzfj_ds , dzpz_imgpool_fileupload_ds , row)
{
    for( var i=0;i<dzpz_pzfj_ds.getColumnCount();i++)
    {
        var col= dzpz_pzfj_ds.getColumnName(i);
        var value= dzpz_pzfj_ds.getValue(0 , i);
        dzpz_imgpool_fileupload_ds.setValue(row, col , value);  
    }
}


id: 60ac77825db2f5d22859c520
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:05:23.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    var p = JSON.parse(arg);
    println(p.rawdata);
    var id = p.id;  // dzpz_pzfj.id 

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac77825db2f5d22859c533
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:11:15.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


id: 60ac77825db2f5d22859c549
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:15:00.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

println( arg); 
    var p = JSON.parse(arg);
    println(p.rawdata);
    var id = p.id;  // dzpz_pzfj.id 

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac77825db2f5d22859c551
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:16:11.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac77825db2f5d22859c557
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:37:44.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            var ret= putImageBackToPool ( data.id);
            if( ret.success) cb_refresh_onClick();
            
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac77825db2f5d22859c55d
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:25:54.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            var ret= putImageBackToPool ( data.id);
            if( ret.success) cb_refresh_onClick();
            
        }
    }
    return  {success:true};
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet , name ) {

   

    if( name=='FJ')
    {
          var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

   
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh!='')
        {
            toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
            return;
        }
    }
 


    }

    
    return { success:false, message:"禁止上传"};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac77825db2f5d22859c563
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:30:04.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) cb_refresh_onClick();

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false}; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false}; // 此返回，阻止了默认的上传操作
            }
        }

        var ret= addImageToPZ( $one_ds.getString(0,"id") , selection);
        if( ret.success)
        {
            //刷新图片区
            cb_refresh_onClick(); 
            //刷新凭证
            loadPZ( $one_ds.getString(0,"id") , $one_ds.getString(0,"verifiername")!=""?1:0 );
        }

    }


    return { success: false}; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac77825db2f5d22859c56b
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:31:50.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("",{pzid:pzid, images:images}));
}


id: 60ac77825db2f5d22859c573
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:32:07.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("",{pzid:pzid, images:images}));
}


id: 60ac77825db2f5d22859c57b
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:32:18.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("追加图片到凭证中",{pzid:pzid, images:images}));
}


id: 60ac8bf55db2f5d22859c633
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:40:10.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) where id='"+ pzid+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c649
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:47:15.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) where id='"+ id+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c651
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:48:05.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ id+"' ) where id='"+ id+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c65d
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:54:50.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ id+"' ) -1  where id='"+ id+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c673
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:55:52.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ id+"' ) -1  where id='"+ id+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac8f625db2f5d22859c679
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:57:44.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success)
            {
                 smartRetrieve(book.sheet3, "one");   
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false}; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false}; // 此返回，阻止了默认的上传操作
            }
        }

        var ret= addImageToPZ( $one_ds.getString(0,"id") , selection);
        if( ret.success)
        {
            //刷新图片区
            cb_refresh_onClick(); 
            //刷新凭证
            loadPZ( $one_ds.getString(0,"id") , $one_ds.getString(0,"verifiername")!=""?1:0 );
        }

    }


    return { success: false}; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac8f625db2f5d22859c67f
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:57:47.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac8f625db2f5d22859c687
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:00:40.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");
    var pzid = dzpz_pzfj_ds.getStrig(0,"gguid");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) -1  where id='"+ pzid+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac8f625db2f5d22859c69d
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:02:13.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");
    var pzid = dzpz_pzfj_ds.getString(0,"gguid");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) -1  where id='"+ pzid+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


