xj qian
2024-07-16 7a91c3a1ed37b0332d4ff0ebeae60bd6da3665c0
jrj/project/business/BasicCode/QuickCode.cpp
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,678 @@
#include <wobject/xstring.hpp>
#include <xcontrol/xtreeview.hpp>
#include <xcontrol/xdwgrid.hpp>
#include <wobject/xdouble.hpp>
#include <xcontrol/xlayersheet.hpp>
#include "vbusiness/vframe/listwin.vframe.vbusiness.hpp"
#include "viewobject/view.base.hpp"
using xml = KXMLDOMDocument;
class __declspec(dllexport) QuickCode : public xwin
{
public:
   xdwgrid   dw_list;
   xstring m_config;
   xstring m_agent;
   xstring m_id;
   xstring m_dbmap;
   xstring m_name;
   xstring strconfig;
   KXMLDOMDocument m_configDoc;
   xnode   m_agentNode;
public:
   QuickCode(void* implPtr, HWND hWnd) :xwin(implPtr, hWnd) {}
public:
   static QuickCode* CreateInstance(void* implPtr, void* hWnd)
   {
      QuickCode* pWin = new QuickCode(implPtr, (HWND)hWnd);
      return pWin;
   }
   int alert(xstring str)
   {
      //win::MessageBox(GetHWND(),str,L"提示:",0);
      TApp::alert(this, str);
      return 0;
   }
   int SetAgent()
   {
      xstring xfNodeAgentArea = L"agentarea";
      xnode anode = GetAgentNode(xfNodeAgentArea);
      if (m_agentNode)
      {
         SetAgentNode(anode, m_agentNode);
      }
      else
      {
         KXMLDOMElement xframeElement = GetElement();
         KXMLDOMElement agent = xframeElement.selectSingleNode(L"agent/" + xfNodeAgentArea + L"[1]/*");
         if (agent)
         {
            xstring s = agent.xml();
            m_agentNode = SetAgentNode(anode, s);
         }
      }
      return 1;
   }
   int OnCmdDispatch(xstring comdid)
   {
      HCURSOR hCursor = xutil::SetCursorWait();
      if (comdid == L"save") {
         //type
         xaserverarg args;
         args.AddArg(L"id", m_id);
         args.AddArg(L"name", m_name);
         xml y;
         //TDataSet::exec(m_config,L"savetype",args,y);
         //url::post(L"QuickCode/pref/quickcode_type.save",args.GetString(),y);
         //save
         xml x;
         xaserverarg arg;
         dw_list.AcceptText();
         dw_list.DwUpdateAllToEx(x);
         KXMLDOMElement e = x.documentElement();
         e.setAttribute(L"id", m_id);
         //arg.AddArg(L"id",m_id);
         arg.AddArg(L"content", e.xml());
         arg.AddArg(L"dbmap", m_dbmap);
         //trace(L"-->",m_id);
         //trace(L"-->",m_dbmap);
         //trace(e.xml());
         /*if(TDataSet::exec(m_config,L"save",arg,x)!=1)
         {
            trace(L"error:"+x.xml());
            alert(L"保存出错!");
            return 0;
         }*/
         //trace(m_dbmap);
         trace(x.xml());
         if (xurl::get(L"QuickCode/pref/quickcode.save", arg.GetString(), x) != 1)
         {
            xstring error = x.text();
            trace(error);
         }
         trace(x.xml());
         if (x.selectSingleNode(L"error"))
         {
            alert(L"保存出错2!");
            return 0;
         }
         dw_list.ResetUpdateStatus();
         alert(L"保存成功!");
      }
      else if (comdid == L"add") {
         dw_list.InsertRow(0);
         int rows = dw_list.GetRowCount();
         dw_list.SetRow(rows);
         dw_list.SetItemString(rows, L"ToCurrencyCode", L"RMB");
         dw_list.SetItemDisplayString(rows, L"ToCurrencyCode", L"人民币");
      }
      else if (comdid == L"insert") {
         int row = dw_list.GetRow();
         if (row<1 || row>dw_list.GetRowCount()) return 0;
         dw_list.InsertRow(row);
         dw_list.SetItemString(row, L"ToCurrencyCode", L"RMB");
         dw_list.SetItemDisplayString(row, L"ToCurrencyCode", L"人民币");
      }
      else if (comdid == L"delet") {
         /*int rows = dw_list.GetRowCount();
         int row2 = dw_list.GetPrevSelectRow(rows);
         while(row2 > 0){
            dw_list.DeleteRow(row2);
            row2 = dw_list.GetPrevSelectRow(row2 - 1);
         }
         dw_list.SelectRow(0, false);*/
         int row2 = dw_list.GetRow();
         if (row2<1 || row2>dw_list.GetRowCount()) return 0;
         dw_list.DeleteRow(row2);
      }
      else if (comdid == L"import") {
         if (m_id == L"FND_ExchangeRate")
         {
            int ll_delq = MessageBox(GetHWND(), L"确定要从老系统导入汇率数据吗?", L"提示", 1);
            if (ll_delq != 1)
               return 0;
            xml xi;
            xaserverarg argi;
            argi.setNativePointer(argi.CreateInstance());
            if (xurl::get(L"QuickCode/ExchangeRate/import", argi.GetString(), xi) != 1)
            {
               trace(xi.text());
               return -1;
            }
            alert(L"导入成功!");
            dw_list.Redraw();
         }
      }
      xutil::RestoreCursor(hCursor);
      return 0;
   }
   int OnXCommand(TEvent* evt, LPARAM param)
   {
      return OnCmdDispatch(evt->xcommand.pStrID);
   }
   int OnSetFocus(TEvent* evt, LPARAM p) {
      SetAgent();
      return 1;
   }
   int OnClicked(TEvent* evt, LPARAM p)
   {
      DWNMHDR& hdr = *(DWNMHDR*)evt->notify.pnmh;
      int row = hdr.row;
      int col = hdr.col + 1;
      xstring colname = dw_list.GetColumnName(col);
      trace(colname);
      xaserverarg arg;
      xstring comdid;
      if (colname == L"CategoryID")
      {
         openUrl(L"产品资料.vface/xpage/product/category/dialog", arg);
         comdid = arg.GetArgString(L"comdid");
         if (comdid == L"ok")
         {
            xstring CategoryID = arg.GetArgString(L"CategoryID");
            xstring CName = arg.GetArgString(L"CName");
            dw_list.SetItemDisplayString(row, colname, CName);
            dw_list.SetItemString(row, colname, CategoryID);
            dw_list.Redraw();
         }
      }
      else if (colname == L"MkOrgId")
      {
         openUrl(L"组织架构.vface/xpage/HrOrg/Chart", arg);
         comdid = arg.GetArgString(L"comdid");
         if (comdid == L"xmOk")
         {
            xstring OrgName = arg.GetArgString(L"OrgName");
            xstring OrgId = arg.GetArgString(L"OrgId");
            dw_list.SetItemDisplayString(row, colname, OrgName);
            dw_list.SetItemString(row, colname, OrgId);
            dw_list.Redraw();
         }
      }
      else if (colname == L"Cid")
      {
         openUrl(L"组织架构.vface/xpage/hremployee/select", arg);
         comdid = arg.GetArgString(L"comdid");
         if (comdid == L"xmOk")
         {
            xstring sPersonID = arg.GetArgString(L"sPersonID");
            trace(sPersonID);
            xstring sPersonName = arg.GetArgString(L"sPersonName");
            dw_list.SetItemDisplayString(row, colname, sPersonName);
            dw_list.SetItemString(row, colname, sPersonID);
            dw_list.Redraw();
         }
      }
      return 1;
   }
   int OnDoubleClicked(TEvent* evt, LPARAM p)
   {
      DWNMHDR& hdr = *(DWNMHDR*)evt->notify.pnmh;
      int row = hdr.row;
      if (row<1 || row > dw_list.GetRowCount()) return 0;
      xaserverarg arg;
      //xstring str = hdr.data;
      xstring colname = hdr.colname;
      xstring str = dw_list.GetItemString(row, colname);
      arg.AddArg(L"value", str);
      openUrl(L"/sale/view/view.dlg/xpage/memo/edit", arg);
      xstring comdid = arg.GetArgString(L"comdid");
      if (comdid == L"xmOK")
      {
         xstring value = arg.GetArgString(L"value");
         dw_list.SetItemString(row, colname, value);
         dw_list.Redraw();
      }
      return 1;
   }
   int ItemOutput()
   {
      xstring d = xaserver::GetCurrentDate();
      d = d.left(10) + L"";
      dw_list.SaveAs(d);
      return 1;
   }
   int OnSelectAllRow()
   {
      int rows = dw_list.GetRowCount();
      if (rows < 1) return -1;
      dw_list.SelectRow(1, rows, true);
      dw_list.Redraw();
      return 1;
   }
   int OnRowCopy()
   {
      int rows = dw_list.GetRowCount();
      if (rows < 1) return -1;
      int cols = dw_list.GetColumnCount();
      xstring str = L"";
      int row = dw_list.GetNextSelectRow(1);
      int i;
      while (row > 0)
      {
         xstring tmp1 = L"";
         for (i = 1; i < cols; i++)
         {
            int w = dw_list.GetColumnWidth(i);
            if (w < 5) continue;
            xstring tmp;
            if (dw_list.GetItemDisplayString(row, i + 1))
               tmp = dw_list.GetItemDisplayString(row, i + 1);
            else
               tmp = dw_list.GetItemString(row, i + 1);
            tmp1 += tmp + L"\t";
         }
         str += tmp1 + L"\r\n";
         row = dw_list.GetNextSelectRow(row + 1);
      }
      dw_list.SetDataToClip(str);
      return 1;
   }
   int OnColCopy(xstring col)
   {
      int rows = dw_list.GetRowCount();
      if (rows < 1) return -1;
      xstring str = L"";
      int row = dw_list.GetNextSelectRow(1);
      while (row > 0)
      {
         xstring tmp;
         if (dw_list.GetItemDisplayString(row, col))
            tmp = dw_list.GetItemDisplayString(row, col);
         else
            tmp = dw_list.GetItemString(row, col);
         str += tmp + L"\r\n";
         row = dw_list.GetNextSelectRow(row + 1);
      }
      dw_list.SetDataToClip(str);
      return 1;
   }
   int OnRClick(TEvent* evt, LPARAM p)
   {
      DWNMHDR& hdr = *(DWNMHDR*)evt->notify.pnmh;
      int row = hdr.row;
      int m = win::CreatePopupMenu();
      int MF_STRING = 0x00000000;
      int MF_DISABLED = 0x00000002;
      int TPM_RIGHTBUTTON = 0x0002;
      int TPM_RETURNCMD = 0x0100;
      int MF_CHECKED = 0x8;
      int MF_SEPARATOR = 0x0800;
      win::AppendMenu(m, MF_STRING, 1, L"复制行");
      win::AppendMenu(m, MF_STRING, 101, L"复制列");
      win::AppendMenu(m, MF_SEPARATOR, -1, L"");
      win::AppendMenu(m, MF_STRING, 2, L"ȫѡ");
      win::AppendMenu(m, MF_SEPARATOR, -1, L"");
      win::AppendMenu(m, MF_STRING, 4, L"导出到Excel");
      win::AppendMenu(m, MF_SEPARATOR, -1, L"");
      win::AppendMenu(m, MF_STRING, 6, L"显示列设置");
      xrect xr;
      xpoint pt;
      win::GetCursorPos(pt);
      int ret = win::TrackPopupMenu(m, TPM_RIGHTBUTTON | TPM_RETURNCMD, pt.x, pt.y, 0, GetHWND(), xr);
      //trace(ret);
      HCURSOR hCursor = xutil::SetCursorWait();
      if (ret == 1) { OnRowCopy(); }
      else if (ret == 101) {
         xstring colname = hdr.colname;
         if (colname != L"")
            OnColCopy(colname);
      }
      else if (ret == 2) { OnSelectAllRow(); }
      else if (ret == 4) { ItemOutput(); }
      else if (ret == 6) {}
      xutil::RestoreCursor(hCursor);
      return 1;
   }
   int OnAttachEvent()
   {
      //获取焦点事件,用于重置工具条
      AttachEvent(L"WM_SETFOCUS", (FEvent)&QuickCode::OnSetFocus);
      //绑定工具条点击事件
      AttachEvent(L"WM_XCOMMAND", (FEvent)&QuickCode::OnXCommand);
      AttachEvent(L"dw_list", L"DWV_DOUBLECLICKED", (FEvent)&QuickCode::OnDoubleClicked);//行双击
      AttachEvent(L"dw_list", L"DWV_ITEMBEINGEDIT", (FEvent)&QuickCode::OnClicked);
      AttachEvent(L"dw_list", L"DWV_RCLICKED", (FEvent)&QuickCode::OnRClick);//鼠标右键
      return 1;
   }
   int OnRetrieve()
   {
      xml x;
      xaserverarg arg;
      trace(L"asdasd");
      if (m_id.find(L"", 0) > 0)
      {
         m_id = m_id.mid(0, m_id.find(L"", 0));
         arg.AddArg(L"id", m_id);
         //trace(m_id);
         /*if (TDataSet::exec(m_config, L"query_remark", arg.GetString(),x) != 1)
         {
            trace(x.text());
            return -1;
         }*/
         if (xurl::get(L"QuickCode/entity/list_remark", arg.GetString(), x) != 1)
         {
            trace(x.text());
            return -1;
         }
      }
      else
      {
         trace(L"asdasd");
         arg.AddArg(L"id", m_id);
         arg.AddArg(L"querytxt", L"");
         /*if (TDataSet::exec(m_config, L"query", arg.GetString(),x) != 1)
         {
            trace(x.text());
            return -1;
         }*/
         if (xurl::get(L"QuickCode/entity/list", arg.GetString(), x) != 1)
         {
            trace(x.text());
            return -1;
         }
      }
      dw_list.Retrieve(x);
      dw_list.Redraw();
      return 1;
   }
   xstring jxParam(xstring p, xstring n)
   {
      if (p != L"")
      {
         p += L"&";
         int i = 1;
         while (p.find(L"=L", 0) > 0)
         {
            xstring name = p.mid(0, p.find(L"=L", 0));
            xstring value = p.mid(p.find(L"=L", 0) + 1, p.find(L"&", 0) - p.find(L"=L", 0) - 1);
            if (name == n) return L"" + value;
            i += 1;
            p = p.mid(p.find(L"&", 0) + 1, p.length());
         }
      }
      return "";
   }
   int getURLParam()
   {
      if (GetWinParam())
      {
         xpage xp = new xpage;
         xp.setNativePointer(GetXPage());
         xstring aurl = xp.GetWkUrl();
         xaserverarg arg;
         if (aurl && aurl.find(L"native=true", 0) > 0)
         {
            int p = GetParam();
            arg;
            arg.setNativePointer(p);
         }
         else
         {
            arg = GetParam();
         }
         xstring config = arg.GetArgString(L"config");
         xml x;
         if (config == L"")
         {
            xstring configXml = arg.GetArgString(L"configxml");
            x.loadXML(configXml);
         }
         else
         {
            getUrl(config, L"", x);
         }
         m_configDoc = x;
      }
      return 1;
   }
   int loaded()
   {
      getURLParam();
      KXMLDOMDocument    ee = m_configDoc;
      xstring id = m_id;
      var n = ee.selectSingleNode(L"//list/dwview[@type='dwgrid'][@id='" + id + L"']");
      xstring vface = m_configDoc.selectSingleNode(L"//vface[1]").text();
      xstring dwname = L"";//e.selectsingleNode(L"@name").text();
      xstring tpl = L"";//e.selectsingleNode(L"@url").text();
      xstring dbmap = L"";
      if (n)
      {
         KXMLDOMElement e = n;
         //trace(e.xml());
         if (e.selectSingleNode(L"@name")) dwname = e.selectSingleNode(L"@name").text();
         if (e.selectSingleNode(L"@url")) tpl = e.selectSingleNode(L"@url").text();
         if (e.selectSingleNode(L"@dbmap")) dbmap = e.selectSingleNode(L"@dbmap").text();
         //trace(vface +L"/"+ tpl);
         //trace(selectmode);
      }
      else
      {
         var m = ee.selectSingleNode(L"//list");
         KXMLDOMElement e1 = m;
         //trace(e.xml());
         if (e1.selectSingleNode(L"@name")) dwname = e1.selectSingleNode(L"@name").text();
         if (e1.selectSingleNode(L"@url")) tpl = e1.selectSingleNode(L"@url").text();
         if (e1.selectSingleNode(L"@dbmap")) dbmap = e1.selectSingleNode(L"@dbmap").text();
      }
      m_dbmap = dbmap;
      dw_list = new xdwgrid;
      dw_list.setNativePointer(GetControl(dwname));
      dw_list.openUrl(vface + L"/" + tpl);
      //dw_list.SetSelectionMode(4);
   }
   int Onital()
   {
      //地区
      xml x1;
      xaserverarg arg1;
      arg1.setNativePointer(arg1.CreateInstance());
      arg1.AddArg(L"id", L"地区");
      if (xurl::get(L"/sale/data/QuickCode/entity/list", arg1.GetString(), x1) != 1)
      {
         trace(x1.text());
         return -1;
      }
      var list = x1.selectNodes(L"root/item");
      int i;
      int s = list.length;
      if (s > 0)
      {
         xstring str = L"<cellprop celltype='' cellformatex='' cellformat='' cellprotectex='' cellprotect='保护' cellvisibleex='' cellvisible='' cellvalid='' cellinitial=''><edit edittype='ddlb' ddlbxml='&lt;data>";
         for (i = 0; i < s; i++)
         {
            KXMLDOMElement xitem = list.item(i);
            xstring id = L"";
            if (xitem.selectSingleNode(L"Code"))
               id = xitem.selectSingleNode(L"Code").text();
            xstring name = L"";
            if (xitem.selectSingleNode(L"Meaning"))
               name = xitem.selectSingleNode(L"Meaning").text();
            xstring tmp = L"&lt;row>&lt;显示列>" + name + L"&lt;/显示列>&lt;数据列>" + id + L"&lt;/数据列>&lt;/row>";
            str += tmp;
         }
         str += L"&lt;/data>'/></cellprop>";
         dw_list.SetColumnProps(L"AreaCode", str);
      }
      //币别
      xml x2;
      xaserverarg arg2;
      arg2.setNativePointer(arg2.CreateInstance());
      arg2.AddArg(L"id", L"FND_Currency");
      if (xurl::get(L"/sale/data/QuickCode/entity/list", arg2.GetString(), x2) != 1)
      {
         trace(x2.text());
         return -1;
      }
      var list1 = x2.selectNodes(L"root/item");
      int j;
      int s1 = list1.length;
      if (s1 > 0)
      {
         xstring str1 = L"<cellprop celltype='' cellformatex='' cellformat='' cellprotectex='' cellprotect='保护' cellvisibleex='' cellvisible='' cellvalid='' cellinitial=''><edit edittype='ddlb' ddlbxml='&lt;data>";
         for (j = 0; j < s1; j++)
         {
            KXMLDOMElement xitem1 = list1.item(j);
            xstring id1 = L"";
            if (xitem1.selectSingleNode(L"Code"))
               id1 = xitem1.selectSingleNode(L"Code").text();
            xstring name1 = L"";
            if (xitem1.selectSingleNode(L"Name"))
               name1 = xitem1.selectSingleNode(L"Name").text();
            xstring tmp1 = L"&lt;row>&lt;显示列>" + name1 + L"&lt;/显示列>&lt;数据列>" + id1 + L"&lt;/数据列>&lt;/row>";
            str1 += tmp1;
         }
         str1 += L"&lt;/data>'/></cellprop>";
         dw_list.SetColumnProps(L"FromCurrencyCode", str1);
      }
      //国家
      xml x3;
      xaserverarg arg3;
      arg3.setNativePointer(arg3.CreateInstance());
      arg3.AddArg(L"id", L"FND_Country");
      if (xurl::get(L"/sale/data/QuickCode/entity/list", arg3.GetString(), x3) != 1)
      {
         trace(x3.text());
         return -1;
      }
      var list2 = x3.selectNodes(L"root/item");
      int k;
      int s2 = list2.length;
      if (s2 > 0)
      {
         xstring str2 = L"<cellprop celltype='' cellformatex='' cellformat='' cellprotectex='' cellprotect='保护' cellvisibleex='' cellvisible='' cellvalid='' cellinitial=''><edit edittype='ddlb' ddlbxml='&lt;data>";
         for (k = 0; k < s2; k++)
         {
            KXMLDOMElement xitem2 = list2.item(k);
            xstring id2 = L"";
            if (xitem2.selectSingleNode(L"Code"))
               id2 = xitem2.selectSingleNode(L"Code").text();
            xstring name2 = L"";
            if (xitem2.selectSingleNode(L"Name"))
               name2 = xitem2.selectSingleNode(L"Name").text();
            xstring tmp2 = L"&lt;row>&lt;显示列>" + name2 + L"&lt;/显示列>&lt;数据列>" + id2 + L"&lt;/数据列>&lt;/row>";
            str2 += tmp2;
         }
         str2 += L"&lt;/data>'/></cellprop>";
         dw_list.SetColumnProps(L"MkCountryId", str2);
      }
      dw_list.SetReadOnlyColumn(L"ToCurrencyCode");
   }
   int onload()
   {
      SetArg();
      m_config = L"QuickCode.config";
      m_agent = L"";
      //dw_list = new xdwgrid;
      //dw_list.setNativePointer(GetControl(L"dw_list"));
      //dw_list.SetSelectionMode(4);
      //dw_list.AllowSort(false);
      if (GetWinParam())
      {
         xaserverarg arg = GetArg();
         xstring id = arg.GetArgString(L"id");
         if (id == L"") return 0;
         m_id = id;
         loaded();
         /*xstring tpl = arg.GetArgString(L"tpl");
         if(tpl == L"" && (m_id.find(L"",0) >0))
            tpl = L"QuickCode.tpl/QuickCode_Remark";
         else if(tpl == L"" )
            tpl = L"QuickCode.tpl/QuickCode";
         dw_list.SetDataObject(GetServerUrl(),L"dev:sheet["+tpl+L"]");
         xstring dbmap = arg.GetArgString(L"dbmap");
         if(dbmap == L""  && (m_id.find(L"",0)  >0) )
            dbmap = L"QuickCode_Remark.dbmap";
         else if(dbmap == L"")
            dbmap =L"QuickCode.dbmap";
         m_dbmap = dbmap;
         xstring name = arg.GetArgString(L"name");
         if(name == L"")
            name = id;
         m_name = name;
         xstring title = arg.GetArgString(L"title");
         if(title != L"")
            win::SetWindowText(GetHWND(),title);*/
            /*
            param p = GetParam();
            xstring pa = L""+p;
            //xstring pa = L"id=HrOrgType&title=客户类型";
            xstring id = jxParam(pa,L"id");
            if(id == L"") return 0;
            m_id = id;
            xstring tpl = jxParam(pa,L"tpl");
            if(tpl == L"" && (m_id.find(L"",0) >0))
               tpl = L"QuickCode.tpl/QuickCode_Remark";
            else if(tpl == L"" )
               tpl = L"QuickCode.tpl/QuickCode";
            dw_list.SetDataObject(GetServerUrl(),L"dev:sheet["+tpl+L"]");
            xstring dbmap = jxParam(pa,L"dbmap");
            if(dbmap == L""  && (m_id.find(L"",0)  >0) )
               dbmap = L"QuickCode_Remark.dbmap";
            else if(dbmap == L"")
               dbmap =L"QuickCode.dbmap";
            m_dbmap = dbmap;
            //trace(m_dbmap);
            xstring name = jxParam(pa,L"name");
            if(name == L"")
               name = id;
            m_name = name;
            xstring title = jxParam(pa,L"title");
            if(title != L"")
               win::SetWindowText(GetHWND(),title);*/
         OnRetrieve();
      }
      OnAttachEvent();
      SetAgent();
      Onital();
      return 1;
   }
};