/*
'' ============================================================================================
'' 项目名称：风越项目范例ASPX单层框架
'' 项目版本：V1.0
'' 项目描述：
'' 文件名称：YongHuXinXi_add.aspx
'' 文件描述：信息增改文件 （用户信息）
'' 公司名称：中极网盛
'' 开发人员：刘怒
'' 创建日期：2008-8-7 21:34:32
'' 修订日期：2009-03-10 07:13:09
'' 版权信息：Copyright (C) 2008

'' 项目升级时需读取以下生成工具的版权信息
'' 生成工具：风越代码生成器 [FireCode Creator]
'' 当前版本：通用版 V1.8
'' 官方网站：http://www.sino8848.com
'' ============================================================================================
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FypSystem.Common;
using FypSystem.DBManager;

public partial class YongHuXinXi_add : System.Web.UI.Page
{
    /// <summary>
    /// 当前页面是否编辑状态
    /// </summary>
    private bool bIsEdit = false;
    
    string strReq_USER_ID = ""; 
    string strReq_USER_NAME = ""; 
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = WebPage.GetConfig("fypWebName");
        
        strReq_USER_ID = DBString.ConvertInSql(this.Request.QueryString["USER_ID"]); 
        strReq_USER_NAME = DBString.ConvertInSql(this.Request.QueryString["USER_NAME"]); 

        if (!StringExtend.IsEmpty(strReq_USER_ID) && !StringExtend.IsEmpty(strReq_USER_NAME))
            bIsEdit = true;

        if (!this.Page.IsPostBack)
        {
            Initialize();
        }
    }

    /// <summary>
    /// 编辑记录时，读取数据库指定记录
    /// </summary>
    /// <returns></returns>
    
    private FypSystem.DBManager.SortedItem GetRecordData(FypSystem.DBManager.Access dbManager)
    
    {
        FypSystem.DBManager.SortedItem itemCurrent = new SortedItem();
        if (bIsEdit)
        {
            //string strSql = "select [USER_ID], [USER_NAME], [USER_PWD], [USER_BIRTHDAY], [USER_PHOTO], [USER_COMPANY], [USER_DEPT], [USER_INFO] from [USER] where [USER_ID] = " + strReq_USER_ID + " and [USER_NAME] = '" + strReq_USER_NAME + "'";

            string strSql = "select * from [USER] where [USER_ID] = " + strReq_USER_ID + " and [USER_NAME] = '" + strReq_USER_NAME + "'";
            itemCurrent = dbManager.Read(strSql);
        }
        return itemCurrent;
    }

    /// <summary>
    /// 导入默认值、数据
    /// </summary>
    private void Initialize()
    {
        //绑定属性
        
        
        
        this.USER_BIRTHDAY.Attributes.Add("onFocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
        
        
        
        

        //默认值
        
        
        
        
        
        this.USER_BIRTHDAY.Text = DateTime.Now.ToString("yyyy-MM-dd");
        
        

        using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())


        {

            //直接填充下拉框

    
            
            
            string strSql_USER_COMPANY = "select [COMPANY_NAME], [COMPANY_ID] from [COMPANY]";
            dbManager.FillListControl(this.USER_COMPANY, strSql_USER_COMPANY, "COMPANY_NAME", "COMPANY_ID", "--请选择--", "");
        
    


            //编辑记录时导入数据，并设置相应属性
            if (bIsEdit)
            {
                //设置相应属性
                this.btnSubmit.Text = "修改";

                //编辑时设置只读状态
            

                //导入数据
                FypSystem.DBManager.SortedItem item = GetRecordData(dbManager);
                if (item.Count > 0)
                {
                    try
                    {
        
                        //根据条件填充下拉框
            
                
                    
                        
                        
                        if (item.ContainsKey("USER_COMPANY") && item["USER_COMPANY"].ToString().Length > 0)
                        {
                            string strWhere_USER_DEPT = "";
                            strWhere_USER_DEPT = " Where [COMPANY_ID]=" + item["USER_COMPANY"].ToString() + "";
                            string strSql_USER_DEPT = "select [DEPARTMENT_NAME], [DEPARTMENT_ID] from [DEPARTMENT] " + strWhere_USER_DEPT;
                            dbManager.FillListControl(this.USER_DEPT, strSql_USER_DEPT, "DEPARTMENT_NAME", "DEPARTMENT_ID", "--请选择--", "");
                        }
                        

                        
                        

                    
                
            
        

                        //导入数据库中保存的信息
                        
                        
                        this.USER_NAME.Text = item["USER_NAME"].ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        

                        
                        

                        
                        //密码框
                        this.USER_PWD.Attributes.Add("value", item["USER_PWD"].ToString());
                        

                        
                        
                        
                        

                        
                        
                        

                        
                        

                        

                        
                        
                        
                        
                        //显示日期
                        if (item["USER_BIRTHDAY"].ToString().Length > 0)
                            this.USER_BIRTHDAY.Text = FypSystem.Common.StringExtend.FormatDateTime(item["USER_BIRTHDAY"], "yyyy-MM-dd");
                        

                        
                        
                        

                        
                        this.USER_PHOTO.ToolTip = item["USER_PHOTO"].ToString();
                        if (item["USER_PHOTO"].ToString().Length > 0)
                        {
                            this.lab_USER_PHOTO_file.Text = "<br><a href='" + item["USER_PHOTO"].ToString() + "' target='_blank'> >> 查看 " + item["USER_PHOTO"].ToString() + "</a>";
                            this.btn_USER_PHOTO_file_del.Text = " >> 删除文件";
                            this.btn_USER_PHOTO_file_del.CommandArgument = item["USER_PHOTO"].ToString();
                        }
                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_COMPANY.Text = item["USER_COMPANY"].ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_DEPT.Text = item["USER_DEPT"].ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_INFO.Text = item["USER_INFO"].ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                    }
                    catch (System.Exception err)
                    {
                        FypSystem.Common.WebPage.JsAlert(this, err.Message);
                    }
                }
                else
                {
                    //找不到指定记录时
                    FypSystem.Common.WebPage.JsAlert(this, "未找到指定记录！");
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SaveData();
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    private void SaveData()
    {
        bool bSta = false;
        string strMsg = "";
        string strUrl = this.Request.Url.ToString();


if (this.Page.IsValid)
        {

            using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())


            {
                try
                {

                    //读取保存的记录信息
                    FypSystem.DBManager.SortedItem itemOld = GetRecordData(dbManager);
                    //读取保存上载文件的设置
                    FypSystem.Common.FileSys fileSys = new FypSystem.Common.FileSys();
                    string strSavePath = WebPage.GetConfig("fypSavePath");
                    string strWaterMarkPath = WebPage.GetConfig("fypWaterMarkPath");
                    string strAllowExt = WebPage.GetConfig("fypAllowExt");
                    int iMaxSize = Convert.ToInt32(WebPage.GetConfig("fypMaxSize"));


 

                    FypSystem.DBManager.SortedItem item = new SortedItem();
                    
                    
                    item["USER_NAME"] = this.USER_NAME.Text;
                    

                    

                    
                    
                    
                    
                    item["USER_PWD"] = this.USER_PWD.Text;
                    

                    

                    
                    
                    
                    
                    item["USER_BIRTHDAY"] = this.USER_BIRTHDAY.Text;
                    

                    

                    
                    
                    
                    

                    
                    //选择文件后才进行上载
                    if (this.USER_PHOTO.FileName.Length > 0)
                    {
                        //使用原文件名上载
                        //item["USER_PHOTO"] = strSavePath + this.USER_PHOTO.FileName;
                        //使用系统生成的新文件名
                        item["USER_PHOTO"] = strSavePath + MathExtend.NumToStrJZ("datetime", 36) + System.IO.Path.GetExtension(this.USER_PHOTO.FileName);
                        if (bIsEdit)
                        {
                            //保存文件时，如已经上载文件，则使用旧文件名
                            string strUSER_PHOTO_old = itemOld["USER_PHOTO"].ToString();
                            if (strUSER_PHOTO_old.Length > 0)
                            {
                                item["USER_PHOTO"] = fileSys.RemoveExtName(strUSER_PHOTO_old) + System.IO.Path.GetExtension(this.USER_PHOTO.FileName);
                                System.IO.File.Delete(fileSys.GetAbsPath(strUSER_PHOTO_old));
                            }
                        }
                        
                        //上载文件到服务器
                        fileSys.UploadFile(this.USER_PHOTO, false, item["USER_PHOTO"].ToString(), strAllowExt, iMaxSize, true);
                        
                        
                    }
                    

                    
                    
                    
                    
                    item["USER_COMPANY"] = this.USER_COMPANY.Text;
                    

                    

                    
                    
                    
                    
                    item["USER_DEPT"] = this.USER_DEPT.Text;
                    

                    

                    
                    
                    
                    
                    item["USER_INFO"] = this.USER_INFO.Text;
                    

                    

                    
                    
                    

                    if (bIsEdit)
                    {
                        dbManager.Update(item, "USER", "[USER_ID] = " + strReq_USER_ID + " and [USER_NAME] = '" + strReq_USER_NAME + "'");
                    }
                    else
                    {
                        dbManager.Insert(item, "USER");
                    }





                    strMsg = "保存数据成功";
                    bSta = true;
                }
                catch (System.Exception err)
                {
                    strMsg = "保存数据时出错：" + err.Message;
                }
            }
        }
        else
        {
            strMsg = "输入信息不正确";
        }
        if (bSta)
            FypSystem.Common.WebPage.JsRun(this, "ShowMsg(\"" + FypSystem.Common.WebPage.FormatJsStr(strMsg) + "\");try{window.opener.location.reload(); window.close();}catch(e){GoUrl('" + strUrl + "')}");
        else
            FypSystem.Common.WebPage.JsAlert(this, strMsg, "");
    }






    protected void btn_USER_PHOTO_file_del_Command(object sender, CommandEventArgs e)
    {
        DelFile("USER_PHOTO", e.CommandArgument.ToString());
    }





    protected void USER_COMPANY_SelectedIndexChanged(object sender, EventArgs e)
    {
        //填充下拉框
        if (this.USER_COMPANY.SelectedValue.Length > 0)
        {

            using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())


            {
                string strWhere = "";
                
                
                strWhere = " Where [COMPANY_ID]=" + this.USER_COMPANY.SelectedValue + "";
                

                

                dbManager.FillListControl(this.USER_DEPT, "select [DEPARTMENT_NAME], [DEPARTMENT_ID] from [DEPARTMENT] " + strWhere, "DEPARTMENT_NAME", "DEPARTMENT_ID", "--请选择--", "");
            }
        }
    }







    /// <summary>
    /// 删除保存于数据库或磁盘文件
    /// </summary>
    /// <param name="strFieldName">保存文件名或文件字节信息的字段名</param>
    /// <param name="strFileName">文件名，如文件保存于数据库，传入空值</param>
    private void DelFile(string strFieldName, string strFileName)
    {
        string strMsg = "";
        string strUrl = "";
        string strSql = "Update [USER] set [" + strFieldName + "] = null where [USER_ID] = " + strReq_USER_ID + " and [USER_NAME] = '" + strReq_USER_NAME + "'";
        try
        {
            if (strFileName.Length > 0)
            {
                FypSystem.Common.FileSys fileSys = new FileSys();
                string strFilePath = fileSys.GetAbsPath(strFileName);
                System.IO.File.Delete(strFilePath);
            }

            using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())


            {
                dbManager.ExecNonQuery(strSql);
            }
            strMsg = "成功删除文件";
            strUrl = this.Request.Url.ToString();
        }
        catch (System.Exception err)
        {
            strMsg = "出错提示：" + err.Message;
        }
        FypSystem.Common.WebPage.JsAlert(this, strMsg, strUrl);
    }


}