xj qian
2024-07-16 7a91c3a1ed37b0332d4ff0ebeae60bd6da3665c0
jrj/project/business/BasicCode/SKUTemplate.cpp
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,427 @@
#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) SKUTemplate : public xframe
{
public:
   xdwgrid  dw_list;
   xdwtable dw_maint;
   xdwtable dw_category;
   int rowno;
   xnode   m_agentNode;
public:
   SKUTemplate(void* implPtr, HWND hWnd) :xframe(implPtr, hWnd) {}
public:
   static SKUTemplate* CreateInstance(void* implPtr, void* hWnd)
   {
      SKUTemplate* pWin = new SKUTemplate(implPtr, (HWND)hWnd);
      return pWin;
   }
   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 OnSetFocus(TEvent* evt, LPARAM p)
   {
      SetAgent();
      return 1;
   }
   int  OnReTrieve()
   {
      xstring query = L"[SKUTemplate.list.xq]";
      xml x;
      xaserverarg arg;
      if (xaserver::ExecXQuery(GetServerUrl(), query, arg.GetString(), x) != 1)
      {
         trace(x.text());
         return -1;
      }
      else
      {
         dw_list.Retrieve(x);
         dw_list.Redraw();
      }
      return 1;
   }
   int OnRowChanged(TEvent* evt, LPARAM p)
   {
      DWNMHDR& hdr = *(DWNMHDR*)evt->notify.pnmh;
      int ll_row = hdr.row;
      rowno = ll_row;
      xstring TemplateContent = dw_list.GetItemString(ll_row, L"TemplateContent");
      xstring Category = dw_list.GetItemString(ll_row, L"Category");
      if (TemplateContent == L"")
      {
         dw_maint.openUrl(L"其他设置.vface/template/other/TemplateContent");
         dw_maint.SetColHeaderHeight(0);
         dw_maint.SetRowSelectorWidth(0);
      }
      if (Category == L"")
      {
         dw_category.openUrl(L"其他设置.vface/template/other/TemplateCategory");
         dw_category.SetColHeaderHeight(0);
         dw_category.SetRowSelectorWidth(0);
      }
      xml x;
      x.loadXML(TemplateContent);
      xml x1;
      x1.loadXML(Category);
      dw_maint.Retrieve(x);
      dw_maint.Redraw();
      dw_category.Retrieve(x1);
      dw_category.Redraw();
      return 1;
   }
   int OnSave()
   {
      dw_list.AcceptText();
      dw_maint.AcceptText();
      dw_category.AcceptText();
      xml x;
      dw_list.DwUpdateAllToEx(x);
      trace(x.xml());
      xaserverarg arg;
      arg.AddArg(L"content", x.xml());
      arg.AddArg(L"dbmap", L"SKUTemplate.dbmap");
      if (xaserver::ExecXAction(GetServerUrl(), L"[onSave.xa]", arg.GetString(), x) != 1)
      {
         trace(x.documentElement().text());
         alert(L"保存失败");
      }
      else {
         alert(L"保存成功");
      }
      dw_list.ResetUpdateStatus();
      dw_maint.ResetUpdateStatus();
      dw_category.ResetUpdateStatus();
      dw_list.Redraw();
      dw_maint.Redraw();
      dw_category.Redraw();
      return 1;
   }
   int OnSetValue(int row) {
      dw_list.SetItemString(row, L"TemplateID", publiccode::GetGuid());
      return 1;
   }
   int RemoveAttribute(xml x, xstring nodename)//根据名称删除属性节点
   {
      KXMLDOMElement e;
      var list = x.getElementsByTagName(L"" + nodename + L"");
      if (list)
      {
         int nlen = list.length;
         int i;
         for (i = 0; i < nlen; i++)
         {
            e = list.item(i);
            //trace(e.xml);
            e.removeAttribute(L"update.new");
            e.removeAttribute(L"update.modify");
            e.removeAttribute(L"update.delete");
            e.removeAttribute(L"guid");
            e.removeAttribute(L"update.origin");
            e.removeAttribute(L"isnull");
            //trace(e.xml);
         }
      }
   }
   int setMaint()
   {
      xml x;
      dw_maint.DwUpdateAllTo(x);
      int row = dw_list.GetRow();
      trace(x.xml());
      RemoveAttribute(x, L"TemplateContent");
      RemoveAttribute(x, L"Item");
      RemoveAttribute(x, L"ItemName");
      RemoveAttribute(x, L"Remark");
      RemoveAttribute(x, L"SeqNo");
      //trace(x.xml());
      dw_list.SetItemString(row, L"TemplateContent", x.xml());
      dw_list.Redraw();
      return 1;
   }
   int setItemCategory()
   {
      xml x;
      dw_category.DwUpdateAllTo(x);
      int row = dw_list.GetRow();
      //trace(row);
      dw_list.SetItemString(row, L"Category", x.xml());
      return 1;
   }
   int OnAddRow()
   {
      int row = dw_list.InsertRow(0);
      int rows = dw_list.GetRowCount();
      OnSetValue(rows);
      dw_list.SetRow(dw_list.GetRowCount());
      //trace(row.toString());
      dw_maint.openUrl(L"其他设置.vface/template/other/TemplateContent");
      dw_maint.SetColHeaderHeight(0);
      dw_maint.SetRowSelectorWidth(0);
      dw_category.openUrl(L"其他设置.vface/template/other/TemplateCategory");
      dw_category.SetColHeaderHeight(0);
      dw_category.SetRowSelectorWidth(0);
      return 1;
   }
   int OnInsertRow()
   {
      int row = dw_list.GetRow();
      if (row<0 || row>dw_list.GetRowCount()) return 0;
      dw_list.InsertRow(row);
      OnSetValue(row);
      dw_list.SetRow(row);
      dw_maint.openUrl(L"其他设置.vface/template/other/TemplateContent");
      dw_maint.SetColHeaderHeight(0);
      dw_maint.SetRowSelectorWidth(0);
      dw_category.openUrl(L"其他设置.vface/template/other/TemplateCategory");
      dw_category.SetColHeaderHeight(0);
      dw_category.SetRowSelectorWidth(0);
      return 1;
   }
   int OnChangeRowNo(int row)
   {
      xdwtable dw = new xdwtable;
      dw.setNativePointer(dw_maint.FindDwTable(L"Item", 0));
      int i;
      for (i = row; i <= dw.GetRowCount(); i++)
         dw.SetItemString(i, L"SeqNo", xstring(i));
      return 1;
   }
   int OnDeleteRow()
   {
      int row = dw_list.GetRow();
      if (row<0 || row>dw_list.GetRowCount()) return 0;
      dw_list.DeleteRow(row);
      dw_maint.openUrl(L"其他设置.vface/template/other/TemplateContent");
      dw_maint.SetColHeaderHeight(0);
      dw_maint.SetRowSelectorWidth(0);
      dw_category.openUrl(L"其他设置.vface/template/other/TemplateCategory");
      dw_category.SetColHeaderHeight(0);
      dw_category.SetRowSelectorWidth(0);
      return 1;
   }
   int RemoveChild(xml x, xstring nodename)//根据名称删除节点
   {
      KXMLDOMElement e;
      var list = x.selectNodes(L"//Item[@update.delete='1']");
      if (list)
      {
         int nlen = list.length;
         int i;
         for (i = 0; i < nlen; i++)
         {
            e = list.item(i);
            //trace(e.xml);
            e.parentNode.removeChild(e);
            //trace(e.xml);
         }
      }
   }
   int setMaint_removeChild()
   {
      xml x;
      dw_maint.DwUpdateAllTo(x);
      int row = dw_list.GetRow();
      //trace(x.xml());
      RemoveChild(x, L"Item");
      RemoveAttribute(x, L"TemplateContent");
      RemoveAttribute(x, L"Item");
      RemoveAttribute(x, L"ItemName");
      RemoveAttribute(x, L"Remark");
      RemoveAttribute(x, L"SeqNo");
      //trace(x.xml());
      dw_list.SetItemString(row, L"TemplateContent", x.xml());
      dw_list.Redraw();
      return 1;
   }
   int setItemCategory_removeChild()
   {
      xml x;
      dw_category.DwUpdateAllTo(x);
      int row = dw_list.GetRow();
      RemoveChild(x, L"Item");
      //trace(row);
      dw_list.SetItemString(row, L"Category", x.xml());
      dw_list.Redraw();
      return 1;
   }
   int del(xdwtable dw_1)
   {
      xdwtable dw = new xdwtable;
      dw.setNativePointer(dw_1.GetCurTable());
      int row = dw.GetRow();
      if (row < 1 || row > dw.GetRowCount()) return -1;
      dw.DeleteRow(row);
      //trace(dw.GetDwName());
      if (dw_1.GetDwName() == L"TemplateContent")
      {
         OnChangeRowNo(row);
         if (dw.GetRowCount() == 1 && dw.GetItemString(1, L"ItemName") == L"")
         {
            dw_list.SetItemString(rowno, L"TemplateContent", L"");
            dw_list.Redraw();
         }
         else
         {
            setMaint_removeChild();
         }
      }
      if (dw_1.GetDwName() == L"Categorys")
      {
         //trace(dw.GetRowCount());
         if (dw.GetRowCount() == 1 && dw.GetItemString(1, L"CategoryID") == L"")
         {
            dw_list.SetItemString(rowno, L"Category", L"");
            dw_list.Redraw();
         }
         else
         {
            setItemCategory_removeChild();
         }
      }
      return 1;
   }
   int SKUItem()
   {
      xaserverarg arg;
      arg.AddArg(L"dw_maint", dw_maint.nativept.toString());
      openUrl(L"其他设置.vface/xpage/SKUItem/select", arg);
      return 1;
   }
   int SKUItemcategory()
   {
      xaserverarg arg;
      arg.AddArg(L"dw_category", dw_category.nativept.toString());
      openUrl(L"其他设置.vface/xpage/SKUItemcategory/select", arg);
      return 1;
   }
   int SKUItemModify()
   {
      openUrl(L"其他设置.vface/xpage/SKUItem/list");
      return 1;
   }
   //命令发布函数
   int OnCmdDispatch(xstring comdid)
   {
      if (comdid == L"save")  OnSave();
      else if (comdid == L"AddRow") OnAddRow();
      else if (comdid == L"InsertRow") OnInsertRow();
      else if (comdid == L"DeleteRow") OnDeleteRow();
      else if (comdid == L"SKUItem") SKUItem();
      else if (comdid == L"SKUItemcategory") SKUItemcategory();
      else if (comdid == L"del_1") del(dw_maint);
      else if (comdid == L"del_2") del(dw_category);
      else if (comdid == L"SKUItemModify") SKUItemModify();
      return 0;
   }
   //命令处理事件
   int OnXCommand(TEvent* evt, LPARAM param)
   {
      return OnCmdDispatch(evt->xcommand.pStrID);
   }
   int OnMaintChanged(TEvent* evt, LPARAM p)
   {
      //trace(L"hello");
      setMaint();
      return 1;
   }
   int OnCategoryChanged(TEvent* evt, LPARAM p)
   {
      setItemCategory();
      return 1;
   }
   int OnAttachEvent()
   {
      //绑定工具条点击事件
      AttachEvent(L"WM_XCOMMAND", (FEvent)&SKUTemplate::OnXCommand);
      //获取焦点事件,用于重置工具条
      AttachEvent(L"WM_SETFOCUS", (FEvent)&SKUTemplate::OnSetFocus);
      AttachEvent(L"dw_maint", L"DWV_ITEMCHANGED", (FEvent)&SKUTemplate::OnMaintChanged);
      AttachEvent(L"dw_category", L"DWV_ITEMCHANGED", (FEvent)&SKUTemplate::OnCategoryChanged);
      AttachEvent(L"dw_list", L"DWV_ROWFOCUSCHANGED", (FEvent)&SKUTemplate::OnRowChanged);
      return 1;
   }
   int OnInitial()
   {
      SetAgent();
      OnAttachEvent();
      return 1;
   }
   int onload()
   {
      dw_list = new xdwgrid;
      dw_list.setNativePointer(GetControl(L"dw_list"));
      dw_list.openUrl(L"其他设置.vface/template/other/SKUTemplate");
      dw_maint = new xdwtable;
      dw_maint.setNativePointer(GetControl(L"dw_maint"));
      dw_maint.openUrl(L"其他设置.vface/template/other/TemplateContent");
      dw_maint.SetColHeaderHeight(0);
      dw_maint.SetRowSelectorWidth(0);
      dw_category = new xdwtable;
      dw_category.setNativePointer(GetControl(L"dw_category"));
      dw_category.openUrl(L"其他设置.vface/template/other/TemplateCategory");
      dw_category.SetColHeaderHeight(0);
      dw_category.SetRowSelectorWidth(0);
      dw_category.SetReadOnly(true);
      OnInitial();
      OnReTrieve();
      xdwtable dw = new xdwtable;
      dw.setNativePointer(dw_category.FindDwTable(L"Item", 0));
      //trace(dw.GetRowCount());
      return 1;
   }
};