⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conaddctrl.ascx.cs

📁 CRM管理系统 CRM管理系统
💻 CS
字号:
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 WYX.Dataport;

public partial class Contact_Ctrls_ConAddCtrl : PageBaseUserCtrl
{
    private Dataport dp = new Dataport();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (CurrentUser == null)
            return;

        if (!Page.IsPostBack)
        {
            SetData();
        }
    }

    #region 初始化页面

    /// <summary>
    /// 初始化页面

    /// </summary>
    private void SetData()
    {
        BindddlConType();
        BindConPriod();
        BindAndItem();
        BindCustomer();
        BindPerson();

        this.txtDateTime.Text = DateTime.Now.ToShortDateString();
        this.ddlPerson.SelectedValue = CurrentUser.user_id.ToString();
    }
    #endregion

    #region 读取字典表

    /// <summary>
    /// 邦定联络方式
    /// </summary>
    private void BindddlConType()
    {
        this.BindDictionary(ddlConType, "101", true);
    }

    /// <summary>
    /// 邦定联络阶段
    /// </summary>
    private void BindConPriod()
    {
        this.BindDictionary(ddlConPriod, "102", true);
    }

    /// <summary>
    /// 邦定相关项目
    /// </summary>
    private void BindAndItem()
    {
        string sql = "SELECT * FROM dictionary where parent_id = 103 order by dict_name";
        DataTable dt = null;
        try
        {
            dt = dp.GetTableResult(sql);
        }
        catch
        {
            dt = null;
        }
        if (dt == null || dt.Rows.Count == 0)
        {
            this.ddlAndItem.Items.Insert(0, new ListItem("请选择..."));
            return;
        }

        this.ddlAndItem.DataSource = dt;
        this.ddlAndItem.DataTextField = "dict_name";
        this.ddlAndItem.DataValueField = "dict_code";
        this.ddlAndItem.DataBind();
        this.ddlAndItem.Items.Insert(0, new ListItem("请选择..."));
        //this.BindDictionary(ddlAndItem, "103", true);
    }

    /// <summary>
    /// 邦定客户
    /// </summary>
    private void BindCustomer()
    {
        string sql = "SELECT code, name FROM ITSV_CustomerManager where is_delete = 0 and is_use= 0 order by name";
        DataTable dt = null;
        try
        {
            dt = dp.GetTableResult(sql);
        }
        catch 
        {
            dt = null;
        }
        if (dt == null || dt.Rows.Count == 0)
        {
            this.ddlCustomNo.Items.Insert(0, new ListItem("请选择..."));
            return;
        }

        this.ddlCustomNo.DataSource = dt;
        this.ddlCustomNo.DataTextField = "name";
        this.ddlCustomNo.DataValueField = "code";
        this.ddlCustomNo.DataBind();
        this.ddlCustomNo.Items.Insert(0, new ListItem("请选择..."));
    }

    /// <summary>
    /// 设置客户联系人
    /// </summary>
    /// <param name="custom_id"></param>
    private void BindCustPerson(string custom_id)
    {
        string sql = @"select code,first_name from iTSV_customer_person where customer_id = " + custom_id;
        DataTable dt = null;
        try
        {
            dt = dp.GetTableResult(sql);
        }
        catch
        {
            dt = null;
        }
        if (dt == null || dt.Rows.Count == 0)
        {
            this.ddlCustPerson.Items.Clear();
            this.ddlCustPerson.Items.Insert(0, new ListItem("请选择..."));
            return;
        }

        this.ddlCustPerson.DataSource = dt;
        this.ddlCustPerson.DataTextField = "first_name";
        this.ddlCustPerson.DataValueField = "code";
        this.ddlCustPerson.DataBind();
        this.ddlCustPerson.Items.Insert(0, new ListItem("请选择..."));
    }

    /// <summary>
    /// 邦定我方人员
    /// </summary>
    private void BindPerson()
    {
        string sql = "SELECT account, user_id, name FROM Users WHERE (inuse = 1)";
        DataTable dt = null;
        try
        {
            dt = dp.GetTableResult(sql);
        }
        catch
        {
            dt = null;
        }
        if (dt == null || dt.Rows.Count == 0)
        {
            this.ddlPerson.Items.Insert(0, new ListItem("请选择..."));
            return;
        }

        this.ddlPerson.DataSource = dt;
        this.ddlPerson.DataTextField = "name";
        this.ddlPerson.DataValueField = "user_id";
        this.ddlPerson.DataBind();
        this.ddlPerson.Items.Insert(0, new ListItem("请选择..."));
    }
    #endregion

    #region 数据验证
    /// <summary>
    /// 数据验证
    /// </summary>
    /// <returns></returns>
    private bool CheckData()
    {
        this.msg = "";
        if (ddlCustomNo.SelectedItem == null || ddlCustomNo.SelectedIndex <= 0)
            msg += "请选择客户编号\\r\\n";
        if (ddlCustPerson.SelectedItem == null || ddlCustPerson.SelectedIndex == 0)
            msg += "请选择客户人员\\r\\n";
        if (txtTitle.Text.Trim().Length == 0)
            msg += "联络主题不能为空\\r\\n";
        if (ddlConType.SelectedItem == null || ddlConType.SelectedIndex == 0)
            msg += "请选择联络方式\\r\\n";
        if (ddlConPriod.SelectedItem == null || ddlConPriod.SelectedIndex == 0)
            msg += "请选择联络阶段\\r\\n";
        if (ddlAndItem.SelectedItem == null || ddlAndItem.SelectedIndex == 0)
            msg += "请选择相关项目\\r\\n";
        if (txtConMoney.Text.Trim().Length == 0)
            msg += "联络费用不能为空\\r\\n";
        else if (!IsNuberic(txtConMoney.Text.Trim()))
            msg += "联络费用必须为数字\\r\\n";
        else if (Convert.ToDouble(txtConMoney.Text.Trim()) < 0)
            msg += "联络费用必须为大于等于0的数字\\r\\n";

        if (ddlPerson.SelectedItem == null || ddlPerson.SelectedIndex == 0)
            msg += "请选择我方人员\\r\\n";
        if (txtDateTime.Text.Trim().Length == 0)
            msg += "联络日期不能为空\\r\\n";
        else if (!IsDate(txtDateTime.Text.Trim()))
            msg += "联络日期格式不正确\\r\\n";

        if (txtContent.Value.Trim().Length == 0)
            msg += "联络内容不能为空\\r\\n";

        if (txtNote.Value.Trim().Length > 250)
            msg += "备注最大长度不能大于250个汉字和250个字符\\r\\n";

        HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        int accCount = 0;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile postFile = files[i];

            if (postFile.FileName.Trim().Length > 0)
            {
                string fileName, fileExtension;
                fileName = System.IO.Path.GetFileName(postFile.FileName);
                if (fileName != "")
                {
                    fileExtension = System.IO.Path.GetExtension(fileName).Replace(".","");
                    if (!CheckExtension(fileExtension))
                    {
                        msg += "存在不允许上传的文件类型:" + fileExtension + "\\r\\n";
                    }
                    else if (postFile.ContentLength > 10240000)
                    {
                        msg += fileName + "文件大小超过了上传的最大值:10240k";
                    }
                    else
                    {
                        accCount++;
                    }
                }
                else
                {
                    msg += "文件路径不正确\\r\\n";
                }
            }
        }
        if (accCount > 5)
            msg += "最多上传5个附件\\r\\n";

        if (msg != "")
        {
            return false;
        }
        return true;
    }

    private bool CheckExtension(string exten)
    {
        ArrayList array = new ArrayList();
        array.Add("doc");
        array.Add("txt");
        array.Add("jpg");
        array.Add("gif");
        array.Add("ppt");
        array.Add("csv");
        array.Add("xls");
        array.Add("rar");
        array.Add("zip");
        array.Add("docx");

        foreach (string str in array)
        {
            if (exten == str)
            {
                return true;
            }
        }

        return false;
    }
    #endregion

    #region 数据提交
    /// <summary>
    /// 数据提交
    /// </summary>
    /// <returns></returns>
    private bool SubmitData()
    {
        if (!UploadFiles())
        {
            return false;
        }

        String customNo = ddlCustomNo.SelectedValue.Split(',')[0];
        string customName = ddlCustPerson.SelectedValue.Replace("'", "\"");
        string conTitle = txtTitle.Text.Trim().Replace("'", "\"");
        string conType = ddlConType.SelectedValue;
        string conPriod = ddlConPriod.SelectedValue;
        string andItem = ddlAndItem.SelectedValue;
        double conMoney = Convert.ToDouble(txtConMoney.Text.Trim());
        long userid = Convert.ToInt64(ddlPerson.SelectedValue);
        DateTime dateTime = DateTime.Parse(txtDateTime.Text.Trim());
        string conContent = txtContent.Value.Trim().Replace("'", "\"");
        string note = txtNote.Value.Trim().Replace("'", "\"");
        string document = lblAcc.Text;

        string sql = @"INSERT INTO ITSV_CustomerContract
                      (customer_id, customer_person_id, title, type, period, project, money, contract_person, 
                      contract_time, content, note, document, write_person)";
        sql += " VALUES ('" + customNo + "','" + customName + "','" + conTitle + "','" + conType + "','" + conPriod + "','" + andItem + "','" + conMoney + "','" + userid + "',";
        sql += "'" + dateTime.ToShortDateString() + "','" + conContent + "','" + note + "','" + document + "','" + CurrentUser.user_id + "')";

        if (dp.ExeSql(sql))
        {
            this.WriteUserAction("添加联络\"" + conTitle + "\"");
            return true;
        }

        msg += "数据提交失败,请您重试!!\\r\\n";
        return false;
    }

    /// <summary>
    /// 多文件上传
    /// </summary>
    /// <returns></returns>
    private bool UploadFiles()
    {
        HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

        System.Random rand = new Random(DateTime.Now.Millisecond);
        string result = "";
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile postFile = files[i];

            if (postFile.FileName.Trim().Length > 0)
            {
                int seed = rand.Next(1000, 9999);
                string fileName, fileExtension;
                fileName = System.IO.Path.GetFileName(postFile.FileName);
                if (fileName != "")
                {
                    try
                    {
                        fileExtension = System.IO.Path.GetExtension(fileName).Replace(".","");
                        string uploadName = "" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + seed + "." + fileExtension;
                        postFile.SaveAs(Server.MapPath("~/upload/") + uploadName);
                        result += "[" + fileName + "]" + uploadName + "|";
                    }
                    catch
                    {
                        msg += fileName + "上传失败\\r\\n";
                        return false;
                    }
                }
            }
        }
        this.lblAcc.Text = result;
        return true;
    }
    #endregion

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!CheckData())
        {
            ShowMessage(msg);
            return;
        }

        if (!SubmitData())
        {
            ShowMessage(msg);
        }
        else
        {
            ShowAndRedirect(Page, "数据提交成功!!", "ConIndex.aspx");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ConIndex.aspx");
    }
    protected void ddlCustomNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCustomNo.SelectedItem != null && ddlCustomNo.SelectedIndex != 0)
        {
            string coutId = ddlCustomNo.SelectedValue;
            BindCustPerson(coutId);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -