unit U_WechatAuthority;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit, cxPC, cxCheckComboBox, cxDropDownEdit, Menus,
  RM_e_Xls, TeEngine, Series, TeeProcs, Chart, DbChart;

type
  TfrmWechatAuthority = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    username: TEdit;
    RMXLSExport2: TRMXLSExport;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    ADOQueryPrt: TADOQuery;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Label2: TLabel;
    authority: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
  public

    { Public declarations }
     FQX:String;

  end;

var
  frmWechatAuthority: TfrmWechatAuthority;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_UserList;

{$R *.dfm}

 
procedure TfrmWechatAuthority.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from  WechatAuthority  where authority='+quotedstr(Trim(FQX)));

      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmWechatAuthority.FormDestroy(Sender: TObject);
begin
  frmWechatAuthority:=nil;
end;

procedure TfrmWechatAuthority.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmWechatAuthority.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption),Tv2,'权限管理');
  Close;
end;
procedure TfrmWechatAuthority.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption),Tv2,'权限管理');
 authority.Text:=Trim(FQX);
  InitGrid();
end;

procedure TfrmWechatAuthority.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWechatAuthority.ToolButton2Click(Sender: TObject);
var
  sql:string;
begin
  if ADOQueryMain.Active then
  begin
    sql:=SGetFilters(Panel1,1,2);
    SDofilter(ADOQueryMain,sql);
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;


procedure TfrmWechatAuthority.cxTabControl1Change(Sender: TObject);
begin
  InitGrid;
end;

procedure TfrmWechatAuthority.N1Click(Sender: TObject);
begin
  IF  CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmWechatAuthority.N2Click(Sender: TObject);
begin
 IF  CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmWechatAuthority.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmWechatAuthority.v2Column8PropertiesEditValueChanged(
  Sender: TObject);
 var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value:=Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate WechatAuthority ');
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
      SQL.Add(' where WAID='+CDS_HZ.fieldbyname('WAID').AsString);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end
end;

procedure TfrmWechatAuthority.ToolButton4Click(Sender: TObject);
var
  Maxno,MUserId,MUserName:string;
begin
  if GetLSNo(ADOQueryCmd,Maxno,'WA','WechatAuthority',3,1)=False then
  begin

    Application.MessageBox('取纱线入库编号失败!','提示',0);
    Exit;
  end;
  try
    frmUserList:=TfrmUserList.Create(Application);
    with frmUserList do
    begin
      if ShowModal=1 then
      begin
          MUserId:=trim(CDS_User.fieldbyname('UserId').asstring);
          MUserName:=trim(CDS_User.fieldbyname('UserName').asstring);
      end;
    end;
  finally
    frmUserList.Free;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into WechatAuthority(WAID,authority,userid,username) values('+quotedstr(Trim(Maxno))
    +','+    quotedstr(Trim(FQX))
    +','+    quotedstr(Trim(Muserid))
    +','+    quotedstr(Trim(Musername))
    +')');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmWechatAuthority.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete WechatAuthority where WAID='+inttostr(CDS_HZ.FieldByName('WAID').AsInteger));
    ExecSQL;
  end;
  CDS_HZ.Delete;
end;

end.
