/*
'' ============================================================================================
'' 项目名称：风越项目范例WinForm单层框架_dll
'' 项目版本：V1.0
'' 项目描述：
'' 文件名称：YongHuXinXi_Add.cs
'' 文件描述：信息增改文件 （用户信息）
'' 公司名称：中极网盛
'' 开发人员：刘怒
'' 创建日期：2008-12-24 21:26:37
'' 修订日期：2009-03-10 11:55:57
'' 版权信息：Copyright (C) 2008

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FypSystem.Common;
using FypSystem.DBManager;

namespace fyp_winform_dll
{
    public partial class YongHuXinXi_Add : Form
    {
        /// <summary>
        /// 当前页面是否编辑状态
        /// </summary>
        private bool bIsEdit = false;
        
        string strReq_USER_ID = ""; 
        string strReq_USER_NAME = ""; 

        public YongHuXinXi_Add()
        {
            Initialize();
        }

        public YongHuXinXi_Add(string strUSER_ID, string strUSER_NAME)
        {
            bIsEdit = true;
            
            strReq_USER_ID = DBString.ConvertInSql(strUSER_ID); 
            strReq_USER_NAME = DBString.ConvertInSql(strUSER_NAME); 

            Initialize();
            //MessageBox.Show(strUSER_ID + "" + strUSER_NAME);
        }

        /// <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);

                string strSql = "select * from [USER] where [USER_ID] = @USER_ID and [USER_NAME] = @USER_NAME";
                
                System.Data.OleDb.OleDbParameter[] parameters = new System.Data.OleDb.OleDbParameter[2];
                
                parameters[0] = new System.Data.OleDb.OleDbParameter("@USER_ID", strReq_USER_ID);
                parameters[1] = new System.Data.OleDb.OleDbParameter("@USER_NAME", strReq_USER_NAME);
				
                
                itemCurrent = dbManager.Read(strSql, parameters);
            }
            return itemCurrent;
        }

        /// <summary>
        /// 导入默认值、数据
        /// </summary>
        private void Initialize()
        {
            InitializeComponent();
            this.Text += " - " + FypSystem.Common.WebPage.GetConfig("fypAppName");

            //默认值
            
            
            
            
            this.USER_BIRTHDAY.Text = DateTime.Now.ToString();
            
            
            

    
            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.btn_Submit.Text = "修改";

                    //编辑时设置只读状态
                    
                    
                    this.USER_NAME.ReadOnly = true;
                    
                    

                    //导入数据
                    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.Text = item["USER_PWD"].ToString();
                            

                            

                            
                            

                            
                            
                            
                            

                            
                            //要求两次输入
                            
                            this.USER_PWD_again.Text = 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 hh:mm:ss");
                            
                            

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

                            

                            
                            if (item["USER_PHOTO"].ToString().Length > 0)
                            {
                                //this.lab_USER_PHOTO_file.Text = "查看 " + item["USER_PHOTO"].ToString() + "";
                                //this.btn_USER_PHOTO_file_del.Text = " >> 删除文件";
                            }
                            
                            

                            
                            
                            
                            

                            
                            
                            

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

                            
                            

                            
                            
                            
                            

                            
                            
                            

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

                            
                            

                            
                            
                            
                            

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

                            

                            
                            

                            
                            
                            
                            

                            
                            
                        }
                        catch (System.Exception err)
                        {
                            MessageBox.Show(err.Message, "出错提示：");
                        }
                    }
                    else
                    {
                        //找不到指定记录时
                        MessageBox.Show("未找到指定记录！", "出错提示：");
                    }
                }
            }
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        #region 检测输入数据
        
        private void USER_NAME_Validated(object sender, EventArgs e)
        {
            CheckInputData_USER_NAME();
        }

        private bool CheckInputData_USER_NAME()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_NAME, "");
            
            string strUSER_NAME = this.USER_NAME.Text;
            
            

            
            if (strUSER_NAME.Length == 0)
            {
                this.errorProvider.SetError(this.label_USER_NAME, "不允许为空值");
                bValue = false;
            }
            

            if (strUSER_NAME.Length < 1 || strUSER_NAME.Length > 200)
            {
                this.errorProvider.SetError(this.label_USER_NAME, "所输入 [姓名]");
                bValue = false;
            }

            

            return bValue;
        }
		

        
        
        
        private void USER_PWD_Validated(object sender, EventArgs e)
        {
            CheckInputData_USER_PWD();
        }

        private bool CheckInputData_USER_PWD()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_PWD, "");
            
            string strUSER_PWD = this.USER_PWD.Text;
            
            

            

            if (strUSER_PWD.Length < 0 || strUSER_PWD.Length > 50)
            {
                this.errorProvider.SetError(this.label_USER_PWD, "所输入 [密码]");
                bValue = false;
            }

            

            return bValue;
        }
		

        
        
        
        private void USER_PWD_again_Validated(object sender, EventArgs e)
        {
            CheckInputData_again_USER_PWD();
        }

        private bool CheckInputData_again_USER_PWD()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_PWD_again, "");
            
            string strUSER_PWD = this.USER_PWD_again.Text;
            
            

            

            if (strUSER_PWD.Length < 0 || strUSER_PWD.Length > 50)
            {
                this.errorProvider.SetError(this.label_USER_PWD_again, "再次输入 [密码");
                bValue = false;
            }

            

            return bValue;
        }
        
        
        
        private void USER_BIRTHDAY_Validated(object sender, EventArgs e)
        {
            CheckInputData_USER_BIRTHDAY();
        }

        private bool CheckInputData_USER_BIRTHDAY()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_BIRTHDAY, "");
            
            string strUSER_BIRTHDAY = this.USER_BIRTHDAY.Text;
            
            

            

            if (strUSER_BIRTHDAY.Length < 0 || strUSER_BIRTHDAY.Length > 23)
            {
                this.errorProvider.SetError(this.label_USER_BIRTHDAY, "所输入 [生日]");
                bValue = false;
            }

            
            if (!FypSystem.Common.RegExp.RegCheck(@"^(\d{4}[\-]?\d{1,2}[\-]?\d{1,2})|(\d{1,2}[:]?\d{1,2}[:]?\d{1,2})$", strUSER_BIRTHDAY))
            {
                this.errorProvider.SetError(this.label_USER_BIRTHDAY, "所输入 [生日]");
                bValue = false;
            }
            

            return bValue;
        }
		

        
        
        
        private void USER_PHOTO_Validated(object sender, EventArgs e)
        {
            CheckInputData_USER_PHOTO();
        }

        private bool CheckInputData_USER_PHOTO()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_PHOTO, "");
            
            string strUSER_PHOTO = this.USER_PHOTO.Text;
            
            

            

            if (strUSER_PHOTO.Length < 0 || strUSER_PHOTO.Length > 65535)
            {
                this.errorProvider.SetError(this.label_USER_PHOTO, "所输入 [相片]");
                bValue = false;
            }

            

            return bValue;
        }
		

        
        
        
        private void USER_COMPANY_Validated(object sender, EventArgs e)
        {
            CheckInputData_USER_COMPANY();
        }

        private bool CheckInputData_USER_COMPANY()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_COMPANY, "");
            
            
            string strUSER_COMPANY = "";
            if (this.USER_COMPANY.SelectedValue != null)
                strUSER_COMPANY = this.USER_COMPANY.SelectedValue.ToString();
            

            

            if (strUSER_COMPANY.Length < 0 || strUSER_COMPANY.Length > 10)
            {
                this.errorProvider.SetError(this.label_USER_COMPANY, "所输入 [公司]");
                bValue = false;
            }

            
            if (!FypSystem.Common.RegExp.RegCheck(@"^[\d]+$", strUSER_COMPANY))
            {
                this.errorProvider.SetError(this.label_USER_COMPANY, "所输入 [公司]");
                bValue = false;
            }
            

            return bValue;
        }
		

        
        
        
        private void USER_DEPT_Validated(object sender, EventArgs e)
        {
            CheckInputData_USER_DEPT();
        }

        private bool CheckInputData_USER_DEPT()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_DEPT, "");
            
            
            string strUSER_DEPT = "";
            if (this.USER_DEPT.SelectedValue != null)
                strUSER_DEPT = this.USER_DEPT.SelectedValue.ToString();
            

            

            if (strUSER_DEPT.Length < 0 || strUSER_DEPT.Length > 10)
            {
                this.errorProvider.SetError(this.label_USER_DEPT, "所输入 [部门]");
                bValue = false;
            }

            
            if (!FypSystem.Common.RegExp.RegCheck(@"^[\d]+$", strUSER_DEPT))
            {
                this.errorProvider.SetError(this.label_USER_DEPT, "所输入 [部门]");
                bValue = false;
            }
            

            return bValue;
        }
		

        
        
        
        private void USER_INFO_Validated(object sender, EventArgs e)
        {
            CheckInputData_USER_INFO();
        }

        private bool CheckInputData_USER_INFO()
        {
            bool bValue = true;
            this.errorProvider.SetError(this.label_USER_INFO, "");
            
            string strUSER_INFO = this.USER_INFO.Text;
            
            

            

            if (strUSER_INFO.Length < 0 || strUSER_INFO.Length > 65535)
            {
                this.errorProvider.SetError(this.label_USER_INFO, "所输入 [用户信");
                bValue = false;
            }

            

            return bValue;
        }
		

        
        
        

        /// <summary>
        /// 检测输入数据的正确性
        /// </summary>
        /// <returns></returns>
        private bool CheckInputData()
        { 
            return CheckInputData_USER_NAME() && CheckInputData_USER_PWD() && CheckInputData_again_USER_PWD() && CheckInputData_USER_BIRTHDAY() && CheckInputData_USER_PHOTO() && CheckInputData_USER_COMPANY() && CheckInputData_USER_DEPT() && CheckInputData_USER_INFO();
        }
        #endregion 检测输入数据

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            bool bSaveSta = false;
            string strMsg = "";
            if (CheckInputData())
            {
    
                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.Text.Length > 0 && this.USER_PHOTO.Text.IndexOf(":\\") > 0)
                        {
                            //使用原文件名上载
                            //item["USER_PHOTO"] = strSavePath + this.USER_PHOTO.Text;
                            //使用系统生成的新文件名
                            item["USER_PHOTO"] = strSavePath + MathExtend.NumToStrJZ("datetime", 36) + System.IO.Path.GetExtension(this.USER_PHOTO.Text);
                            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.Text);
                                    System.IO.File.Delete(fileSys.GetAbsPath(strUSER_PHOTO_old));
                                }
                            }
                            
                            
                            //上载图片到服务器，并添加水印
                            fileSys.UploadFile(this.USER_PHOTO.Text, false, item["USER_PHOTO"].ToString(), strAllowExt, iMaxSize, true, true, strWaterMarkPath, 9);
                            
                        }
                        

                        
                        
                        

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

                        
                        
                        

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

                        
                        
                        
                        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 = "保存数据成功";
                        bSaveSta = true;
                    }
                    catch (System.Exception err)
                    {
                        strMsg = "保存数据时出错：" + err.Message;
                    }
                }
            }
            else
            {
                strMsg = "输入信息不正确";
            }

            if (strMsg.Length > 0)
                MessageBox.Show(strMsg, "出错提示：");

            if (bSaveSta)
            {
                ((YongHuXinXi_Search)this.Owner).FillGrid();
                this.Close();
            }
        }

    
        
        

        
        private void btn_SelectFile_USER_PHOTO_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog.ShowDialog(this) == DialogResult.OK)
                this.USER_PHOTO.Text = this.openFileDialog.FileName;
        }
        

    
        
        
        
        private void USER_COMPANY_SelectedIndexChanged(object sender, EventArgs e)
        {
            //填充下拉框
            if (this.USER_COMPANY.SelectedValue.ToString().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", "--请选择--", "");
                }
            }
        }
        
        

        

    
    }
}