/*
'' ============================================================================================
'' 项目名称：风越项目范例ASPX普通三层结构
'' 项目版本：V1.0
'' 项目描述：风越项目范例，用于建立ASPX普通三层结构
'' 文件名称：YongHuXinXi_add.aspx
'' 文件描述：信息增改文件 （用户信息）
'' 公司名称：中极网盛
'' 开发人员：刘怒
'' 创建日期：2008-12-25 20:23:50
'' 修订日期：2009-03-10 06:08:24
'' 版权信息：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;

namespace Fyp_3TierCommon.Web
{
    public partial class YongHuXinXi_add : System.Web.UI.Page
    {
        /// <summary>
        /// 当前页面是否编辑状态
        /// </summary>
        private bool bIsEdit = false;
        
        int req_USER_ID; 
        string req_USER_NAME; 
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = WebPage.GetConfig("fypWebName");
            if (!StringExtend.IsEmpty(this.Request.QueryString["USER_ID"]) && !StringExtend.IsEmpty(this.Request.QueryString["USER_NAME"]))
            {
            
                
                
                req_USER_ID = int.Parse(this.Request.QueryString["USER_ID"]);
                

                
                
            
                
                

                
                
                req_USER_NAME = this.Request.QueryString["USER_NAME"];
                
            
                bIsEdit = true;
            }

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

        /// <summary>
        /// 编辑记录时，读取数据库指定记录
        /// </summary>
        /// <returns></returns>
        private Fyp_3TierCommon.Model.Model_USER GetModelData()
        {
            Fyp_3TierCommon.Model.Model_USER model_USER = new Fyp_3TierCommon.Model.Model_USER();
            if (bIsEdit)
                model_USER = new Fyp_3TierCommon.BLL.BLL_USER().GetModel(req_USER_ID, req_USER_NAME);

            return model_USER;
        }

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


            //直接填充下拉框
            FypSystem.DBManager.FillCtrlData fillCtrlData = new FillCtrlData();
    
        
            
            
            
            

            Fyp_3TierCommon.BLL.BLL_NATION bll_NATION = new Fyp_3TierCommon.BLL.BLL_NATION();
            DataTable dt_NATION = bll_NATION.GetDataTable("");
            fillCtrlData.FillListControl(this.USER_NATION, dt_NATION, "NATION_NAME", "NATION_ID", "--请选择--", "");
            dt_NATION.Dispose();
            
        
    
        
            
            
            
            

            Fyp_3TierCommon.BLL.BLL_COUNTRY bll_COUNTRY = new Fyp_3TierCommon.BLL.BLL_COUNTRY();
            DataTable dt_COUNTRY = bll_COUNTRY.GetDataTable("");
            fillCtrlData.FillListControl(this.USER_COUNTRY, dt_COUNTRY, "COUNTRY_CN", "COUNTRY_ID", "--请选择--", "");
            dt_COUNTRY.Dispose();
            
        
    


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

                //编辑时设置只读状态
            

                //导入数据
                Fyp_3TierCommon.Model.Model_USER model_USER = GetModelData();
                if (model_USER != null)
                {
                    try
                    {
        
                        //根据条件填充下拉框
            
                
                    
                        
                        

                        if (model_USER.USER_COUNTRY != null && model_USER.USER_COUNTRY.ToString().Length > 0)
                        {
                        
                        
                            Fyp_3TierCommon.BLL.BLL_PROVINCE bll_PROVINCE = new Fyp_3TierCommon.BLL.BLL_PROVINCE();
                            DataTable dt_PROVINCE = bll_PROVINCE.GetDataTable("COUNTRY_ID=" + model_USER.USER_COUNTRY.ToString() + "");
                            fillCtrlData.FillListControl(this.USER_PROVINCE, dt_PROVINCE, "PROVINCE_CN", "PROVINCE_ID", "--请选择--", "");
                            dt_PROVINCE.Dispose();
                        

                        
                        
                        }
                    
                
            
        
                        
                        
                        this.USER_NAME.Text = model_USER.USER_NAME.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_PWD.Text = model_USER.USER_PWD.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_EMAIL.Text = model_USER.USER_EMAIL.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        

                        
                        

                        

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

                        
                        
                        

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

                        

                        
                        
                        
                        

                        
                        
                        

                        
                        
                        if (!FypSystem.Common.StringExtend.IsEmpty(model_USER.USER_PHOTOS2))
                        {
                            this.btn_USER_PHOTOS2_file.Text = "<br> >> 下载文件";
                            this.btn_USER_PHOTOS2_file_del.Text = " >> 删除文件";
                        }
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_NATION.Text = model_USER.USER_NATION.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_COUNTRY.Text = model_USER.USER_COUNTRY.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.USER_PROVINCE.Text = model_USER.USER_PROVINCE.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)
            {
                try
                {
                
                    //读取保存的记录信息
                    Fyp_3TierCommon.Model.Model_USER model_USER_Old = GetModelData();
                    //读取保存上载文件的设置
                    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"));
                

                    Fyp_3TierCommon.Model.Model_USER model_USER = GetModelData();
                
                
                    

                    
                    if (this.USER_NAME.Text.Length > 0)
                        model_USER.USER_NAME = this.USER_NAME.Text;
                    
                

                

                    
                    
                
                
                    

                    
                    if (this.USER_PWD.Text.Length > 0)
                        model_USER.USER_PWD = this.USER_PWD.Text;
                    
                

                

                    
                    
                
                
                    

                    
                    if (this.USER_EMAIL.Text.Length > 0)
                        model_USER.USER_EMAIL = this.USER_EMAIL.Text;
                    
                

                

                    
                    
                
                
                    
                    if (this.USER_BIRTHDAY.Text.Length > 0)
                        model_USER.USER_BIRTHDAY = DateTime.Parse(this.USER_BIRTHDAY.Text);
                    else
                        model_USER.USER_BIRTHDAY = null;
                    

                    
                

                

                    
                    
                
                

                
                    //选择文件后才进行上载
                    if (this.USER_PHOTO.FileName.Length > 0)
                    {
                        //使用原文件名上载
                        //model_USER.USER_PHOTO = strSavePath + this.USER_PHOTO.FileName;
                        //使用系统生成的新文件名
                        model_USER.USER_PHOTO = strSavePath + MathExtend.NumToStrJZ("datetime", 36) + System.IO.Path.GetExtension(this.USER_PHOTO.FileName);
                        if (bIsEdit)
                        {
                            //保存文件时，如已经上载文件，则使用旧文件名
                            string strUSER_PHOTO_old = model_USER_Old.USER_PHOTO.ToString();
                            if (strUSER_PHOTO_old.Length > 0)
                            {
                                model_USER.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, model_USER.USER_PHOTO.ToString(), strAllowExt, iMaxSize, true);
                        
                        
                    }
                

                    
                    
                
                

                

                    
                    
                    //上载图片到数据库中，并添加水印
                    if (this.USER_PHOTOS2.FileBytes.Length > 0)
                    {
                        FypSystem.Common.ImageExtend image = new ImageExtend();
                        model_USER.USER_PHOTOS2 = image.AddWaterMark(strWaterMarkPath, 9, 0.5f, this.USER_PHOTOS2.FileBytes, this.USER_PHOTOS2.FileName);
                    }
                    
                
                
                    
                    if (this.USER_NATION.Text.Length > 0)
                        model_USER.USER_NATION = int.Parse(this.USER_NATION.Text);
                    else
                        model_USER.USER_NATION = null;
                    

                    
                

                

                    
                    
                
                
                    
                    if (this.USER_COUNTRY.Text.Length > 0)
                        model_USER.USER_COUNTRY = int.Parse(this.USER_COUNTRY.Text);
                    else
                        model_USER.USER_COUNTRY = null;
                    

                    
                

                

                    
                    
                
                
                    
                    if (this.USER_PROVINCE.Text.Length > 0)
                        model_USER.USER_PROVINCE = int.Parse(this.USER_PROVINCE.Text);
                    else
                        model_USER.USER_PROVINCE = null;
                    

                    
                

                

                    
                    
                

                    Fyp_3TierCommon.BLL.BLL_USER bll_USER = new Fyp_3TierCommon.BLL.BLL_USER();
                    if (bIsEdit)
                        bll_USER.Update(model_USER);
                    else
                        bll_USER.Add(model_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 btn_USER_PHOTOS2_file_Click(object sender, EventArgs e)
        {
            DownLoadFile("USER_PHOTOS2_" + req_USER_ID + "_" + req_USER_NAME, "USER_PHOTOS2");
        }

        protected void btn_USER_PHOTOS2_file_del_Click(object sender, EventArgs e)
        {
            DelFile("USER_PHOTOS2", "");
        }
    
    
    
    
    
        
        protected void USER_COUNTRY_SelectedIndexChanged(object sender, EventArgs e)
        {
            //选择列表项目时，填充下级下拉框
            if (this.USER_COUNTRY.SelectedValue.Length > 0)
            {
                Fyp_3TierCommon.BLL.BLL_PROVINCE bll_PROVINCE = new Fyp_3TierCommon.BLL.BLL_PROVINCE();
                DataTable dtTable = bll_PROVINCE.GetDataTable("COUNTRY_ID=" + this.USER_COUNTRY.SelectedValue + "");
                FypSystem.DBManager.FillCtrlData fillCtrlData = new FillCtrlData();
                fillCtrlData.FillListControl(this.USER_PROVINCE, dtTable, "PROVINCE_CN", "PROVINCE_ID", "--请选择--", "");
            }
        }
        
    

    
    
    

    
        /// <summary>
        /// 删除保存于数据库或磁盘文件
        /// </summary>
        /// <param name="strFieldName">保存文件名或文件字节信息的字段名</param>
        /// <param name="strFileName">文件名，如文件保存于数据库，传入空值</param>
        private void DelFile(string strFieldName, string strFileName)
        {
            string strMsg = "";
            string strUrl = "";
            Fyp_3TierCommon.Model.Model_USER model_USER = GetModelData();
            if (model_USER != null)
            {
                try
                {
                    if (strFileName.Length > 0)
                    {
                        FypSystem.Common.FileSys fileSys = new FileSys();
                        string strFilePath = fileSys.GetAbsPath(strFileName);
                        System.IO.File.Delete(strFilePath);
                    }
                
                    
                    if (strFieldName == "USER_PHOTO")
                        model_USER.USER_PHOTO = null;
                    
                
                    
                    if (strFieldName == "USER_PHOTOS2")
                        model_USER.USER_PHOTOS2 = null;
                    
                
                    new Fyp_3TierCommon.BLL.BLL_USER().Update(model_USER);

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

        
        /// <summary>
        /// 下载保存于数据库中的文件
        /// </summary>
        /// <param name="strFileName">指定的下载文件名</param>
        /// <param name="strFieldName">字段名</param>
        private void DownLoadFile(string strFileName, string strFieldName)
        {
            Fyp_3TierCommon.Model.Model_USER model_USER = GetModelData();
            if (model_USER != null)
            {
                strFileName = this.Server.UrlPathEncode(strFileName);
                FypSystem.Common.FileSys fileSys = new FileSys();
            
                
                if (strFieldName == "USER_PHOTOS2")
                    fileSys.DownloadFileInDB(strFileName, (byte[])model_USER.USER_PHOTOS2);
                
            
            }
        }
        
    
    }
}