TG.WXCRM.V4/WEB/Controllers/Res/CustomerLabelController.cs

469 lines
17 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CRM.Core.DTO.Ord;
using Ninject;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WX.CRM.BLL.Base;
using WX.CRM.BLL.Util;
using WX.CRM.Common;
using WX.CRM.IBLL.Res;
using WX.CRM.IBLL.Soft;
using WX.CRM.IBLL.Util;
using WX.CRM.Model.Entity;
using WX.CRM.Model.Enum;
using WX.CRM.WebHelper;
using WX.CRM.WebHelper.RedisFactory;
namespace WX.CRM.WEB.Controllers.Res
{
public class CustomerLabelController : BaseController
{
[Inject]
public IRES_CUSTOMER_Q _customer { get; set; }
[Inject]
public IRES_CUSTOMER_LABEL _label { get; set; }
[Inject]
public IRES_RESOURCEMOBILE_Q _RES_RESOURCEMOBILE_Q { get; set; }
[Inject]
public ICACHE_Q cache { get; set; }
[Inject]
public ISecurityHelper sHelper { get; set; }
[Inject]
public ISOFT_USER_Q _softUserQ { get; set; }
[Inject]
public IRES_CUSTOMERUSER_Q customeruser_q { get; set; }
[HttpGet]
[AuthorizeRedirect(Roles = InitRights.CONST_风险客户列表)]
public ActionResult Index()
{
Pager pager = new Pager() { page = 1, rows = 20 };
string tableId = "tablist";
Table tab = new Table(tableId);
tab.AddHeadCol("RESID", "", "客户ID");
tab.AddHeadCol("REMARK", "", "备注");
tab.AddHeadCol("CTIME", "", "添加时间");
tab.AddHeadCol("UNAME", "", "添加人");
tab.AddHeadRow();
ViewBag.gridTable = tab.GetHead() + Pagination.GetPage(pager, tableId, "20,50,100");
return View();
}
[HttpPost]
[AuthorizeRedirect(Roles = InitRights.CONST_风险客户列表)]
public JsonResult Index(Pager pager, QueryDto_RES_CUSTOMER_LABEL queryDto, string columns)
{
var list = _label.List(pager, queryDto);
Table table = new Table(columns, true)
{
gridPager = pager
};
foreach (var item in list)
{
table.AddCol(item.RESID);
table.AddCol(item.REAMRK);
table.AddCol(item.CTIME);
table.AddCol(item.EID + "-" + item.UNAME);
table.AddRow();
}
var json = new
{
totalPages = pager.totalPages,
totalRows = pager.totalRows,
rowsList = table.GetRows()
};
return Json(json, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult Set(string resId)
{
var customerId = _customer.getResCustomerByResId(resId).CUSTOMERID;
var ResCustomerList = new RedisFactory().GetList_Rescustomer(null, customerId).Where(n => n.TYPE != 2 && n.TYPE != 3).Select(p => p.RESID);
var model = _label.Get(p => ResCustomerList.Contains(p.RESID));
if (model == null)
{
model = new RES_CUSTOMER_LABEL
{
RESID = resId
};
}
return View(model);
}
[HttpPost]
public JsonResult Set(string resId, decimal type, string remark)
{
try
{
var model = _label.Get(p => p.RESID == resId);
if (model != null)
{
model.RFMTYPE = type;
model.REAMRK = remark;
_label.Update(model);
if (model.RFMTYPE == 0)
{
CloseFreeOrder(model.RESID);
}
else
{
OpenFreeOrder(model.RESID);
}
}
else
{
var ret = OpenFreeOrder(resId);
if (ret)
{
var info = new RES_CUSTOMER_LABEL
{
ID = new SEQUENCES_BL().Seq_base_get(),
RESID = resId,
RFMTYPE = type,
RFMLABEL = type == 1 ? "风险客户" : "未知",
REAMRK = remark,
CTIME = DateTime.Now,
INNERUSERID = UserId,
EID = Eid,
UNAME = UserName
};
_label.Add(info);
}
}
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
return Json(new { ret = false, code = 500, message = "error" }, JsonRequestBehavior.AllowGet);
}
return Json(new { ret = true, code = 200, message = "success" }, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult Import()
{
return View();
}
[HttpPost]
public string Import(string FileType)
{
string msg = "ok";
var errmsg = string.Empty;
try
{
var mbs = GetMiblesByExcelFile();
foreach (var item in mbs)
{
try
{
var model = _label.Get(p => p.RESID == item.Key);
if (model == null)
{
var ret = OpenFreeOrder(item.Key);
if (ret)
{
var info = new RES_CUSTOMER_LABEL
{
ID = new SEQUENCES_BL().Seq_base_get(),
RESID = item.Key,
RFMTYPE = 1,
RFMLABEL = "风险客户",
REAMRK = item.Value,
CTIME = DateTime.Now,
INNERUSERID = UserId,
EID = Eid,
UNAME = UserName
};
_label.Add(info);
}
}
}
catch (Exception exx)
{
errmsg += item + ",";
}
}
}
catch (Exception ex)
{
msg = ex.Message;
LogHelper.Error("ImportResController:【UploadFile】:" + ex.Message + ex.StackTrace);
}
if (!string.IsNullOrEmpty(errmsg))
{
msg = "部分执行失败,请重试:" + errmsg;
}
return msg;
}
[HttpPost]
public JsonResult Delete(string resId)
{
var isOk = _label.Delete(p => p.RESID == resId && p.RFMTYPE == 1);
if (isOk)
{
CloseFreeOrder(resId);
return Json(new { result = true, code = 200, message = "success" }, JsonRequestBehavior.AllowGet);
}
return Json(new { result = false, code = 500, message = "error" }, JsonRequestBehavior.AllowGet);
}
private bool OpenFreeOrder(string resid)
{
var customer = _customer.getResCustomerByResId(resid);
if (customer == null)
{
return false;
}
var list = customeruser_q.GetListByCustomerId(customer.CUSTOMERID);
var softuser = _softUserQ.GetUserList_userName(list.Select(m => m.USERNAME).ToArray());
foreach (var item in softuser)
{
PhoneLogModel phoneLogModel = new PhoneLogModel
{
Method = System.Reflection.MethodBase.GetCurrentMethod().Name,
userid = 0
};
var theModel = new freeordermodel
{
mobile = _RES_RESOURCEMOBILE_Q.GetNumberByResId(resid, phoneLogModel),
needpay = 0,
opendays = 1,
ordertype = 1,
productId = 1092,
remark = "风险客户",
saledeptid = companyId,
source = "1",
softusername = item.USERNAME,
productcode = "UPCFJC_20201209093318_MFTY",//小类代码
teamserve = 1,
userid = 600000207,
count = 365 * 3,//产品数 --免费产品始终一天,这里天数作为数量传递,达到天数效果
productType = 0,//产品分类
opuser = UserId,
ctime = DateTimeTool.ConvertDateTimeInt(DateTime.Now).ToString(),
//theModel.companycode = companyCode;
companycode = Utility.GetSettingOrNullByKey("DataClientCode"),
days = 365 * 3
};
var url = cache.GetValue_Parameter(Parameter.Core_ZxdService_FreeOrder);//内部接口··开通免费订单接口
var json = Utility.ConvertToJSON<freeordermodel>(theModel);
json = sHelper.createSignEncodingStr(json, SecurityHelper.OrderClientIdKey);
//LogHelper.Info("投顾订单" + model.ORDERID + (string.IsNullOrEmpty(model.WEBORDERID) ? "" : string.Format("({0})", model.WEBORDERID)) + "审核:" + url + "?" + json);
string retmsg = Utility.PostData(url + "?" + json, Encoding.UTF8);
retmsg = sHelper.decyptData(SecurityHelper.OrderClientIdKey, retmsg);
CreateOrderRsp retmessage = JsonHelper.JsonDivertToObj<CreateOrderRsp>(retmsg);//返回信息
if (retmessage.retCode == (int)NodeJsOrderEnum.)//成功
{
LogHelper.Info("写入风险客户权限成功!" + resid + "---" + item.USERNAME);
}
else
{
LogHelper.Error("写入风险客户权限失败!" + resid + "---" + item.USERNAME);
}
}
return true;
}
private void CloseFreeOrder(string resid)
{
var customer = _customer.getResCustomerByResId(resid);
if (customer == null)
{
return;
}
var list = customeruser_q.GetListByCustomerId(customer.CUSTOMERID);
var softuser = _softUserQ.GetUserList_userName(list.Select(m => m.USERNAME).ToArray());
foreach (var item in softuser)
{
var theModel = new freeOrderClose
{
orderId = "1092",//订单ID
userName = item.USERNAME
};
var url = cache.GetValue_Parameter(Parameter.Core_ZxdService_CloseFreeOrder);//内部接口··开通免费订单接口
var json = Utility.ConvertToJSON<freeOrderClose>(theModel);
json = sHelper.createSignEncodingStr(json, SecurityHelper.OrderClientIdKey);
//LogHelper.Info("投顾订单" + model.ORDERID + (string.IsNullOrEmpty(model.WEBORDERID) ? "" : string.Format("({0})", model.WEBORDERID)) + "关闭:" + url + "?" + json);
string retmsg = Utility.PostData(url + "?" + json, Encoding.UTF8);
retmsg = sHelper.decyptData(SecurityHelper.OrderClientIdKey, retmsg);
freeOrderCloseRes retmessage = JsonHelper.JsonDivertToObj<freeOrderCloseRes>(retmsg);//返回信息
if (retmessage.iRet == 0)
{
LogHelper.Info("删除风险客户权限成功!" + resid + "---" + item.USERNAME);
}
else
{
LogHelper.Error("删除风险客户权限失败!" + resid + "---" + item.USERNAME);
}
}
}
private List<string> GetMiblesByTxtFile()
{
string vPath = System.Configuration.ConfigurationManager.AppSettings["UploadTemFileDic"];
List<string> lis = new List<string>();
HttpFileCollectionBase files = Request.Files;
HttpPostedFileBase file = files["FileMobile"];
if (file != null && file.ContentLength > 0)
{
string fileName = file.FileName;
//判断文件名字是否包含路径名,如果有则提取文件名
if (fileName.LastIndexOf("\\") > -1)
{
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
}
if (fileName.ToLower().IndexOf(".txt") > -1)
{
if (!System.IO.Directory.Exists(Server.MapPath(vPath)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(vPath));
}
string path = Server.MapPath((vPath + "/") + fileName);
file.SaveAs(path);
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
while (!sr.EndOfStream)
{
string number = sr.ReadLine();
if (!string.IsNullOrEmpty(number))
lis.Add(number);
}
sr.Close();
fs.Close();
System.IO.File.Delete(path);
}
else
{
throw new Exception("文件格式不正确!");
}
}
else
{
throw new Exception("文件不存在!");
}
return lis;
}
private Dictionary<string, string> GetMiblesByExcelFile()
{
var vPath = System.Configuration.ConfigurationManager.AppSettings["UploadTemFileDic"];
var lis = new Dictionary<string, string>();
HttpFileCollectionBase files = Request.Files;
HttpPostedFileBase file = files["FileMobile2"];
if (file != null && file.ContentLength > 0)
{
string fileName = file.FileName;
//判断文件名字是否包含路径名,如果有则提取文件名
if (fileName.LastIndexOf("\\") > -1)
{
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
}
if (!System.IO.Directory.Exists(Server.MapPath(vPath)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(vPath));
}
string path = Server.MapPath((vPath + "/") + fileName);
file.SaveAs(path);
DataTable dt = null;
try
{
if (Path.GetExtension(file.FileName).ToLower().Equals(".xls"))
{
NPOIHelper.InitializeWorkbook_xls(path);
dt = NPOIHelper.ConvertToDataTable();
}
else if (Path.GetExtension(file.FileName).ToLower().Equals(".xlsx"))
{
NPOIHelper.InitializeWorkbook_xlsx(path);
dt = NPOIHelper.ConvertToDataTable_xlsx();
}
else
{
throw new Exception("文件格式不正确!");
}
}
catch (Exception ex)
{
LogHelper.Error(ex);
throw new Exception("解析excel文件错误");
}
if (dt.Rows.Count > 0 || dt.Columns.Count == 3)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
var model = dt.Rows[i];
var number = model[0] != null ? model[0].ToString() : "";
var remark = model[1] != null ? model[1].ToString() : "";
if (!string.IsNullOrEmpty(number))
lis.Add(number, remark);
}
}
System.IO.File.Delete(path);
}
else
{
throw new Exception("文件不存在!");
}
return lis;
}
private List<string> GetMiblesByStr()
{
List<string> lis = new List<string>();
string mobileStr = string.Format("{0}", Request.Form["txtMobiles"]);
mobileStr = mobileStr.Replace("\r\n", "&");
mobileStr = mobileStr.Replace("\r", "&");
mobileStr = mobileStr.Replace("\n", "&");
string[] mbs = mobileStr.Trim().Split('&');
for (int i = 0; i < mbs.Length; i++)
{
if (!string.IsNullOrEmpty(mbs[i]))
lis.Add(mbs[i]);
}
return lis;
}
}
}