using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CallCenterApi.Interface.Controllers.Base;
using System.Data;
using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.DB;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Newtonsoft.Json;
using CallCenterApi.Interface.Models.Input;
using System.Text.RegularExpressions;
namespace CallCenterApi.Interface.Controllers
{
public class ConversationController : BaseController
{
BLL.T_Con_Conversation bll = new BLL.T_Con_Conversation();
BLL.T_Sys_Department bll_Dep = new BLL.T_Sys_Department();
///
/// 获取内外线列表
///
///
public ActionResult GetList()
{
ActionResult res = NoToken("未知错误,请重新登录");
if (Request.IsAuthenticated)
{
string sql = " and F_IsDelete=0";
DataTable dt = new DataTable();
string keyword = RequestString.GetQueryString("keyword");
//电话
string strtel = RequestString.GetQueryString("tel");
//部门id
int department = RequestString.GetInt("department", 0);
//科室id
int section = RequestString.GetInt("section", 0);
//医师
string physician = RequestString.GetQueryString("physician");
string stringtype = RequestString.GetQueryString("type");
int type = 0;
string strpageindex = RequestString.GetQueryString("pageindex");
int pageindex = 1;
string strpagesize = RequestString.GetQueryString("pagesize");
int pagesize = 10;
if (keyword.Trim() != "" && keyword != "undefined")
{
var modelfept = new BLL.T_Sys_Department().GetModelList("F_DeptName='"+ keyword.Trim()+"'").FirstOrDefault ();
if (modelfept!=null )
{
sql += $" and (F_Department ='" + modelfept.F_DeptId + "' or F_Section= '" + modelfept.F_DeptId + "') ";
}
else
sql += $" and ( F_Telephone like'%" + keyword.Trim() + "%' or F_Title like'%" + keyword.Trim() + "%'" + "or F_Physician like'%" + keyword.Trim() + "%') ";
}
if (strtel.Trim() != "" && strtel != "undefined")
{
sql += " and F_Telephone like'%" + strtel+"%'";
}
if (department > 0)
{
sql += " and F_Department=" + department;
}
if (section > 0)
{
sql += " and F_Section=" + section;
}
if (physician.Trim() != "" && physician != "undefined")
{
sql += " and F_Physician=" + physician;
}
if (strpageindex.Trim() != "")
{
pageindex = Convert.ToInt32(strpageindex);
}
if (strpagesize.Trim() != "")
{
pagesize = Convert.ToInt32(strpagesize);
}
if (stringtype .Trim() != "")
{
type = Convert.ToInt32(stringtype);
sql += " and F_Type=" + type;
}
int recordCount = 0;
dt = BLL.PagerBLL.GetListPager(
"T_Con_Conversation",
"T_Con_Conversation.ID",
"*,(SELECT F_DeptName FROM dbo.T_Sys_Department WHERE F_DeptId = dbo.T_Con_Conversation.F_Department) AS Department_Name,(SELECT F_DeptName FROM dbo.T_Sys_Department WHERE F_DeptId = dbo.T_Con_Conversation.F_Section) AS Section_Name ",
sql,
"ORDER BY T_Con_Conversation.ID desc",
pagesize,
pageindex,
true,
out recordCount);
//List modlelist = new BLL.T_Con_Conversation().DataTableToList(dt);
var obj = new
{
state = "success",
message = "成功",
rows = dt,
total = recordCount
};
res = Content(obj.ToJson());
}
return res;
}
///
/// 获取内外线列表详情
///
///
public ActionResult GetDetails()
{
ActionResult res = NoToken("未知错误,请重新登录");
if (Request.IsAuthenticated)
{
int cid = Utils.StrToInt(RequestString.GetQueryString("id"), 0);
if (cid != 0)
{
Model.T_Con_Conversation model = new BLL.T_Con_Conversation().GetModel(cid);
if (model != null)
{
res = Success("获取成功", model);
}
else
{
res = Error("获取失败");
}
}
else
{
res = Error("参数传输失败");
}
}
return res;
}
public class Conversation
{
public int ID { get; set; }
public int F_Type { get; set; }// 1内线2外线
public int F_Department { get; set; }// 部门
public int F_Section { get; set; }//科室
public string F_Telephone { get; set; }//电话
public string F_Physician { get; set; }//医师
public string F_Title { get; set; }//职称
}
///
/// 验证客户电话是否唯一
///
private bool getunphone(int id, string phone)
{
var sql = " F_IsDelete=0 ";
sql += "and (F_Telephone='" + phone + "')";
if (id > 0)
sql += " and ID<>" + id;
var count = bll.GetModelList(sql).Count();
return count > 0;
}
///
/// 添加一键转接三方通话
///
///
public ActionResult Add(Conversation input)
{
#region 获取部门id by 科室id
var model_Dep = new Model.T_Sys_Department();
int dep_Sectionid = input.F_Section;
if (dep_Sectionid > 0)
model_Dep = bll_Dep.GetModel(dep_Sectionid);
#endregion
#region 添加验证判断
if (string.IsNullOrEmpty(input.F_Telephone))
return Error("电话不能为空!");
if (getunphone(0, input.F_Telephone))
return Error("电话重复请勿重复添加");
if (input.F_Type<=0)
return Error("请选择电话类别");
#endregion
var model = new Model.T_Con_Conversation();
model.F_Type = input.F_Type; //1内线2外线
model.F_Department = model_Dep != null ? model_Dep.F_ParentId : input.F_Department; //部门id
model.F_Section = input.F_Section; // 科室id
model.F_Telephone = input.F_Telephone; // 电话
model.F_Physician = input.F_Physician; //医师
model.F_Title = input.F_Title;//职称
model.F_IsDelete = 0;
int n = bll.Add(model);
if (n > 0)
return Success("新增成功!", model);
else
return Error("新增失败!");
}
///
/// 修改一键转接三方通话
///
///
public ActionResult Update(Conversation input)
{
int userId = CurrentUser.UserData.F_UserId;
#region 获取部门id by 科室id
var model_Dep = new Model.T_Sys_Department();
int dep_Sectionid = input.F_Section;
if(dep_Sectionid > 0)
model_Dep = bll_Dep.GetModel(dep_Sectionid);
#endregion
#region 添加验证判断
if (input.ID <= 0)
return Error("参数错误!");
if (string.IsNullOrEmpty(input.F_Telephone))
return Error("电话不能为空!");
if (input.F_Type <= 0)
return Error("请选择电话类别");
#endregion
var model = bll.GetModel(input.ID);
if (getunphone(0, input.F_Telephone) && input.F_Telephone != model.F_Telephone)
return Error("电话重复请勿重复添加");
model.F_Type = input.F_Type; //1内线2外线
model.F_Department = model_Dep != null ? model_Dep.F_ParentId : input.F_Department; //部门id
model.F_Section = input.F_Section; // 科室id
model.F_Telephone = input.F_Telephone; // 电话
model.F_Physician = input.F_Physician; //医师
model.F_Title = input.F_Title;//职称
model.F_IsDelete = 0;
bool n = bll.Update(model);
if (n)
return Success("保存成功!", model);
else
return Error("保存失败!");
}
///
/// 删除一键转接三方通话
///
///
///
public ActionResult DelConver(string[] ids)
{
ActionResult res = NoToken("未知错误,请重新登录");
if (Request.IsAuthenticated)
{
if (ids != null && ids.Length > 0)
{
string idd = " ";
foreach (string str in ids)
{
idd += str + ",";
}
string sql = "update T_Con_Conversation set F_IsDelete=1 where ID in (" + idd.TrimEnd(',') + ")";
if (!string.IsNullOrEmpty(idd.Trim()))
{
if (DbHelperSQL.ExecuteSql(sql) > 0)
{
res = Success("设置成功");
}
else
{
res = Error("设置失败");
}
}
else
{
res = Error("请选择用户");
}
}
else
{
res = Error("获取参数失败");
}
}
return res;
}
///
/// 导入excel
///
/// 区域id
/// 项目id
/// 楼号id
///
public ActionResult ImportExcel(int type=0)
{
string usercode = CurrentUser.UserData.F_UserCode;
string ip = DTRequest.GetIP();
if (!string.IsNullOrWhiteSpace(usercode))
{
HttpPostedFile _upFile = RequestString.GetFile("upFile");
if (_upFile != null)
{
if (type <= 0)
return Error("请选择上传类别");
int headrow = 0;
#region 上传文件
string filepath = "";
string datepath = DateTime.Now.ToString("yyyyMMddHHMMss");
string aLastName = Path.GetExtension(_upFile.FileName);
string oriname = Path.GetFileNameWithoutExtension(_upFile.FileName);
if (aLastName != ".xls" && aLastName != ".xlsx")
{
return Error("文件类型错误,请选择Excel文件");
}
string newpath = datepath + "_" + _upFile.FileName;
if (!Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData")))
{
Directory.CreateDirectory(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData"));
}
filepath = this.Request.ApplicationPath + "/ExcelData/" + newpath;
string PhysicalPath = Server.MapPath(filepath);
_upFile.SaveAs(PhysicalPath);
#endregion
#region 添加附件日志
Model.T_Sys_Accessories model_T_Sys_Accessories = new Model.T_Sys_Accessories();
model_T_Sys_Accessories.F_AddTime = DateTime.Now;//上传时间
model_T_Sys_Accessories.F_FileName = newpath;//附件名称
model_T_Sys_Accessories.F_FileType = aLastName;//附件类型
model_T_Sys_Accessories.F_FileUrl = filepath;//附件地址
model_T_Sys_Accessories.F_UserCode = usercode;//上传人
int fid = new BLL.T_Sys_Accessories().Add(model_T_Sys_Accessories);
#endregion
NPOIHelper np = new NPOIHelper();
DataTable dt = np.ExcelToTable(_upFile, headrow);
string msg = string.Empty;
if (dt == null || dt.Rows.Count == 0)
return Error("文件没有数据");
else
{
Model.T_Con_Conversation dModel = new Model.T_Con_Conversation();
dModel.F_IsDelete = 0;
dModel.F_FileId = fid;
dModel.F_Type = type ;
//获取之前所有客户档案信息
var list_Dep = bll_Dep.DataTableToList(bll_Dep.GetList(" 1=1").Tables[0]).ToList();
foreach (DataRow dr in dt.Rows)
{
headrow = headrow + 1;
if (dr["电话"].ToString() != "" )
{
if (!getunphone(0, dr["电话"].ToString()))
{
var model_Dept = list_Dep.SingleOrDefault(x => x.F_DeptName.Equals(dr["部门"].ToString()));
var model_Sec = list_Dep.SingleOrDefault(x => x.F_DeptName.Equals(dr["科室"].ToString()));
dModel.F_Department = model_Dept != null ? model_Dept.F_DeptId : 0; //dr["部门"].ToString();
dModel.F_Section = model_Sec != null ? model_Sec.F_DeptId : 0; //dr["科室"].ToString();
dModel.F_Telephone = dr["电话"].ToString();
if (type == 2)
{
dModel.F_Physician = dr["医师"].ToString();
dModel.F_Title = dr["职称"].ToString();
}
var res = bll.Add(dModel);
if (res <= 0)
{
msg = msg + "第" + headrow + "行,导入失败
";
}
}
else
msg = msg + "第" + headrow + "行,电话重复,未导入
";
}
else
msg = msg + "第" + headrow + "行,电话为空,未导入
";
}
if (string.IsNullOrEmpty(msg))
return Success("导入成功 ");
else
return Error(msg);
}
}
return Error("数据源上传失败");
}
return Error("用户登录失败,请重新登录");
}
}
}