--tn_common_print_server.lua客户端打印服务
--Copyright(C) 杭州塔网科技有限公司  2015-2018
--更新日志
--        2018-11-23 新建   --by: liang
--------------------------------------------------

local m_pGrid=CLuaGridUtil();
m_pGrid:SetGrid(ui:getDialogName(), 'id_grid');
local m_help = CSelectHelp();
local m_report = CLuaGridReport();
local m_linePrintList = {};--打印任务队列, 防止同时多个请求
local m_bPrinting = false;--是否正在打印中

local function init()
    if _sys_init_print ~= nil then
        return;
    end

    ui:set('id_dt_start', cutil:getPreDay(-7));
    ui:set('id_dt_end', cutil:getTodayEnd());

    ui:run('run_update()', '');

    ui:setClickEvent('id_query', 'query');
    ui:setClickEvent('id_tb_re_print', 'clicked_id_tb_re_print');
    ui:setClickEvent('id_tb_del', 'clicked_id_tb_del');
end

function query(sType)
    local sql = [[select %s a.print_log_id,b.user_cn_name,c.dc_loc_name,a.create_time,a.temp_code,a.print_info,a.def01 from d_print_log a
        left join uums_user b on a.user_id=b.USER_ID
        left join dc_loc c on a.site_id=dc_loc_id %s order by a.create_time desc]];
    if sType == 'freshen' then
        sql = string.format(sql, 'top 200', '');
    else
        local sStartTime = ui:get('id_dt_start');
        local sEndTime = ui:get('id_dt_end');
        sql = string.format(sql, '', string.format("where a.create_time>='%s' and a.create_time<='%s'", sStartTime, sEndTime));
    end
    if db:select(sql, '', m_help) < 0 then
        ui:tip('查询失败', db:error());
        return;
    end
    if sType == 'freshen' then
        ui:threadChangeValue("set_help()");
    else
        m_pGrid:setHelp(m_help);
    end
    return true;
end

function set_help()
    m_pGrid:setHelp(m_help);
end

function run_update()
    local sql = 'select top 1 print_log_id from d_print_log order by create_time desc';
    local help = CSelectHelp();
    local sLast = '';
    local iTimeC = 0;
    while true do
        if ui:allThreadStop() then
            return;
        end
        if iTimeC == 0 then
            db:select(sql, '', help);
            if help:size() > 0 then
                if sLast ~= help:vs(0, 'print_log_id') then
                    if query('freshen') == true then
                        sLast = help:vs(0, 'print_log_id');
                    end
                end
            end
            iTimeC = 0;
        end

        for i=#m_linePrintList,1,-1 do
            m_bPrinting = true;
            ui:threadChangeValue(string.format("print_to('%d','%s', false)", i, m_linePrintList[i]));
            while true do
                if m_bPrinting == false then
                    break;
                end
                cutil:sleep(100);
            end
        end

        cutil:sleep(500);
        iTimeC = iTimeC + 500;
        if iTimeC >= 2000 then
            iTimeC = 0;
        end
    end
end

function set_printer_name(sPrinterName)
    m_report:setPrinterName(sPrinterName);
end

function print_log(sPrintLogID, bView)--引用此脚本,外部可以调用该函数
    local sql = "select temp_code,print_param from d_print_log where print_log_id='%s'"
    local help = CSelectHelp();
    if db:select(string.format(sql, sPrintLogID), '', help) < 0 then
        ui:tip('查询失败');
        m_bPrinting = false;
        return;
    elseif help:size() <= 0 then
        ui:tip('打印任务不存在');
        m_bPrinting = false;
        return;
    end
    local sTempCode = help:vs(0, 'temp_code');
    local sPrintParam = help:vs(0, 'print_param');
    if #sTempCode <= 0 then
        ui:tip('打印模板code不能为空');
        m_bPrinting = false;
        return;
    end

    sql = "select len(temp_value) as temp_value_len";
    for i=0,18 do
        sql=sql..string.format(',substring(temp_value, %d, 32760) as temp_value_%d', 32760*i, i);
    end
    sql=sql.." from d_print_temp with(nolock) where temp_code='%s'";
    if db:select(string.format(sql, sTempCode), '', help) < 0 then
        ui:tip('查询模板失败');
        m_bPrinting = false;
        return;
    elseif help:size() <= 0 then
        ui:tip('模板不存在');
        m_bPrinting = false;
        return;
    elseif tonumber(help:vs(0,'temp_value_len')) <= 0 then
        ui:tip('模板为空');
        m_bPrinting = false;
        return;
    end

    local sTempValue='';
    for j=0,18 do
        sTempValue=sTempValue..help:vs(0,'temp_value_'..j);
    end
    if #sTempValue ~= tonumber(help:vs(0,'temp_value_len')) then
        m_bPrinting = false;
        return ui:tip('模板长度有误');
    end
    sTempValue = cutil:decodeSrc(sTempValue);
    m_report:openFromStr(sTempValue);

    local itPrint = CIntent();
    itPrint:fromString(cutil:fromBase64(sPrintParam));

    local line = CVector();
    itPrint:getKeysByVector(line);
    local sTmp = '';
    local itOne = CIntent();
    for i=0,line:size()-1 do
    while true do
        sKey = line:get(i);
        if sKey == 'setPrinterName' then
            m_report:setPrinterName(itPrint:get(sKey));
            break;
        end
        itOne:clear();
        itPrint:getIntent(sKey, itOne);
        if itOne:get('cmd') == 'setDetailGridHelp' then
            local help = CSelectHelp();
            itOne:getHelp('help', help);
            local bNest = false;
            if itOne:get('bNest') == '1' then
                bNest = true;
            end
            if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
                m_report:setDetailGridHelp(help, bNest);
            else
                m_report:setSubDetailGridHelp(itOne:get('sSubReportCode'), help, bNest);
            end
        elseif itOne:get('cmd') == 'setFreeGridHelp' then
            local help = CSelectHelp();
            itOne:getHelp('help', help);
            local bSetField = false;
            if itOne:get('bSetField') == '1' then
                bSetField = true;
            end
            if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
                m_report:setFreeGridHelp(itOne:get('sCid'), help, bSetField, tonumber(itOne:get('iStartRow')), tonumber(itOne:get('iStartCol')));
            else
                m_report:setSubFreeGridHelp(itOne:get('sSubReportCode'), itOne:get('sCid'), help, bSetField, tonumber(itOne:get('iStartRow')), tonumber(itOne:get('iStartCol')));
            end
        elseif itOne:get('cmd') == 'setParam' then
            if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
                m_report:setParam(itOne:get('sParamName'), itOne:get('sValue'), itOne:get('sDataType'));
            else
                m_report:setSubParam(itOne:get('sSubReportCode'), itOne:get('sParamName'), itOne:get('sValue'), itOne:get('sDataType'));
            end
        elseif itOne:get('cmd') == 'setProp' then
            if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
                m_report:setProp(itOne:get('sCid'), itOne:get('sPropertyName'), itOne:get('sValue'));
            else
                m_report:setSubProp(itOne:get('sSubReportCode'), itOne:get('sCid'), itOne:get('sPropertyName'), itOne:get('sValue'));
            end
        end
        break;
    end
    end

    if bView == false then--不预览, 直接打印
        m_report:print(false);
    else
        m_report:printPreview(true);
    end
end

function print_to(idx, sPrintLogID, bView)
    m_bPrinting = true;

    print_log(sPrintLogID, bView);
    if idx ~= nil then
        table.remove(m_linePrintList, idx);
    end

    m_bPrinting = false;
    return true;
end

function process_command(sCmd, param)
    if sCmd == 'process_print_task' then
        print('process_print_task print_log: '..param);
        ui:tip('发现新任务到队列', param);
        table.insert(m_linePrintList, param);
    end
end
--处理函数
function process_print(itPrint)

end

function clicked_id_tb_re_print()
    local iRow = m_pGrid:getSelectedRow();
    if iRow < 0 then
        ui:tip('未选择');
        return;
    end
    local sPrintLogID = m_pGrid:getValue(iRow, 'print_log_id');
    if #sPrintLogID <= 0 then
        ui:tip('数据错误');
        return;
    end
    print_to(nil, sPrintLogID);
end

function clicked_id_tb_del()
    local iRow = m_pGrid:getSelectedRow();
    if iRow < 0 then
        ui:tip('未选择');
        return;
    end
    if ui:msgConfirm('确认删除'..m_pGrid:getValue(iRow, 'print_info')) == false then
        return;
    end
    local sPrintLogID = m_pGrid:getValue(iRow, 'print_log_id');
    if #sPrintLogID <= 0 then
        ui:tip('数据错误');
        return;
    end
    if db:execSQL(string.format("delete from d_print_log where print_log_id='%s'", sPrintLogID), '') == false then
        ui:tip('删除失败', db:error());
    else
        m_pGrid:DeleteRow(iRow);
        ui:tip('删除成功');
    end
end


init();
Copyright © TouchNet 2015 all right reserved,powered by Gitbook最后修订时间: 2022-06-21 09:21:04

results matching ""

    No results matching ""