using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using BLL;
using m = Model;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web.SessionState;
namespace Web.Handler
{
/// <summary>
/// AddOppportunityHandler 的摘要说明
/// </summary>
public class AddOppportunityHandler : IHttpHandler, IRequiresSessionState
{
private HttpContext _httpContext;
public void ProcessRequest(HttpContext context)
{
string result = "0";
if (requestVerify("AddOppportunityHandler", HttpContext.Current.Session["userName"].ToString().Trim()))
{
_httpContext = context;
context.Response.ContentType = "text/plain";
string jihuidianmingcheng = context.Request.Form["CrmOppportunity_Name"].ToString();
string quhao = context.Request.Form["quhao"].ToString();
string nianyue = context.Request.Form["nianyue"].ToString();
string fenxi = context.Request.Form["CrmOppportunity_Analysis"].ToString();
string duijiebumen = context.Request.Form["CrmOppportunity_PurchaseDp"].ToString();
string duijieren = context.Request.Form["CrmOppportunityConactPerson_Name"].ToString();
string zhiwei = context.Request.Form["CrmOppportunityConactPerson_Duty"].ToString();
string dianhua = context.Request.Form["CrmOppportunityConactPerson_Phone"].ToString();
string jine = context.Request.Form["CrmOppportunity_Amount"].ToString();
string qianyueshijian = context.Request.Form["CrmOppportunity_SignDate"].ToString();
string huoqushijian = context.Request.Form["CrmOppportunity_CreatedDateTime"].ToString();
string jinzhanwenti = context.Request.Form["CrmOppportunity_ProblemsAndDeve"].ToString();
Guid clientGuid = Guid.Parse(context.Request.Form["CrmClient_ID"].ToString());
HttpPostedFile upfile = _httpContext.Request.Files["fileppt"];//获取文件
CrmOppportunity bll_opportunity = new CrmOppportunity();
CrmOppportunityConactPerson bll_per = new CrmOppportunityConactPerson();
m.CrmOppportunity m_opportunity = new m.CrmOppportunity();
m.CrmOppportunityConactPerson m_per = new m.CrmOppportunityConactPerson();
m_opportunity.CrmOppportunity_ID = Guid.NewGuid();
m_opportunity.CrmOpportunity_Client_ID = clientGuid;
m_opportunity.CrmOppportunity_Name = jihuidianmingcheng;
string[] array_nianyue = nianyue.Split('-');//将年月分隔成数组
string sql_bianhao = "LH" + quhao + array_nianyue[0] + array_nianyue[1];
int bianhao_count = bll_opportunity.GetBianHaoCount(sql_bianhao);//获取相同编号的记录总数
StringBuilder strcount = new StringBuilder();//获取编号
if ((bianhao_count + 1).ToString().Length==1)
{
strcount.Append("00");
strcount.Append((bianhao_count + 1).ToString());
}
else if ((bianhao_count + 1).ToString().Length == 2)
{
strcount.Append("0");
strcount.Append((bianhao_count + 1).ToString());
}
else if ((bianhao_count + 1).ToString().Length == 3)
{
strcount.Append((bianhao_count + 1).ToString());
}
m_opportunity.CrmOppportunity_Code = sql_bianhao + strcount;
m_opportunity.CrmOppportunity_Analysis = fenxi;
m_opportunity.CrmOppportunity_PurchaseDp = duijiebumen;
if (jine != "")
{
m_opportunity.CrmOppportunity_Amount = Convert.ToDecimal(jine);
}
m_opportunity.CrmOppportunity_Year = Convert.ToInt32(array_nianyue[0].ToString());
m_opportunity.CrmOppportunity_ProblemsAndDeve = jinzhanwenti;
if (qianyueshijian != "")
{
m_opportunity.CrmOppportunity_SignDate = Convert.ToDateTime(qianyueshijian);
}
if (huoqushijian != "")
{
m_opportunity.CrmOppportunity_CreatedDateTime = Convert.ToDateTime(huoqushijian);
}
if (upfile != null)
{
//文件原名称
string oldMediaName = upfile.FileName;
string fengefu = DateTime.Now.ToString("yyyy-MM-dd-HH-ss") + DateTime.Now.Ticks;
upfile.SaveAs(_httpContext.Server.MapPath("~\Upload\Opportunity\" + fengefu + "_" + oldMediaName));
m_opportunity.CrmOppportunity_ClientPlanPPTPath = "~\Upload\Opportunity\" + fengefu + "_" + oldMediaName;
}
bool result_1=bll_opportunity.Add(m_opportunity);//添加crmOpportunity model
m_per.CrmOppportunityConactPerson_ID = Guid.NewGuid();
m_per.CrmOppportunity_ID = m_opportunity.CrmOppportunity_ID;
m_per.CrmOppportunityConactPerson_Name = duijieren;
m_per.CrmOppportunityConactPerson_Phone = dianhua;
m_per.CrmOppportunityConactPerson_Duty = zhiwei;
bool result_2=bll_per.Add(m_per);
if (result_1 && result_2) {
result = "1";
}
else{
result="-1";
}
}
else {
result = "-2";
}
context.Response.Write(result.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
/// <summary>
/// 校验权限
/// </summary>
/// <param name="HandleName"></param>
/// <param name="ClientId"></param>
/// <returns></returns>
private bool requestVerify(string HandleName, string ClientId)
{
bool boVerify = true;
#region 权限校检
if (HttpContext.Current.Session["userName"].ToString().Trim().ToLower() == "admin")
{
boVerify = true;
}
else
{
#region dataToCheck
List<List<KeyValuePair<string, string>>> dataToCheck = new List<List<KeyValuePair<string, string>>>();
List<KeyValuePair<string, string>> tempItem = new List<KeyValuePair<string, string>>();
KeyValuePair<string, string> tempID = new KeyValuePair<string, string>("CrmClient_ID", ClientId);
dataToCheck.Add(tempItem);
tempItem.Add(tempID);
#endregion
KeyValuePair<bool, ECrmDataScop> checkResult = BLL.CrmRequestVerify.HandleRequest(ClientId, HandleName, dataToCheck);
if (!checkResult.Key)
{
boVerify = false; ;
}
}
#endregion
return boVerify;
}
}
}