using System;
using System.Collections.Generic;
using System.Common;
using System.IRepositories;
using System.Linq;
using System.Model;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TVShoppingCallCenter_ZLJ.Models.Inputs;
using SqlSugar;
using System.Common.Helpers;
using Microsoft.Extensions.Configuration;
using System.Data;
using System.Reflection;
namespace TVShoppingCallCenter_ZLJ.Controllers.Customer
{
//[Authorize]
[Produces("application/json")]
[Route("api/[controller]")]
public class VipInfoController : BaseController
{
private readonly ICus_VipInfoRepository _cus_vip_infoRepository;
private readonly ISys_UserAccountRepository _sys_user_accountRepository;
private readonly ICus_MsgRepository _cus_msgRepository;
private readonly IConfiguration _configuration;
public VipInfoController(ICus_VipInfoRepository cus_vip_infoRepository, ISys_UserAccountRepository sys_user_accountRepository, ICus_MsgRepository cus_msgRepository,IConfiguration configuration)
{
_cus_vip_infoRepository = cus_vip_infoRepository;
_sys_user_accountRepository = sys_user_accountRepository;
_cus_msgRepository = cus_msgRepository;
_configuration = configuration;
}
[HttpGet]
public IActionResult Index()
{
return Success("成功");
}
///
/// 获取会员列表分页
///
///
///
///
///
[HttpGet("getlistbypage")]
public async Task GetListsByPageAsync(string keyword,string name,string phone,string type, string province,string city,string area,string town,string address,string birthday,string recommender,string saleperson,int pageindex = 1, int pagesize = 20)
{
List conModels = new List();
#region 条件筛选
conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
if (!string.IsNullOrEmpty(phone))
conModels.Add(new ConditionalCollections()
{
ConditionalList = new List>()
{
new KeyValuePair(WhereType.And, new ConditionalModel() { FieldName = "F_Phone", ConditionalType = ConditionalType.Like, FieldValue = phone }),
new KeyValuePair( WhereType.Or , new ConditionalModel() { FieldName = "F_Mobile", ConditionalType = ConditionalType.Like, FieldValue = phone }),
new KeyValuePair( WhereType.Or, new ConditionalModel() { FieldName = "F_Mobile1", ConditionalType = ConditionalType.Like, FieldValue = phone })
}
});
if (!string.IsNullOrEmpty(name))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = name });
}
if (!string.IsNullOrEmpty(type))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Type", ConditionalType = ConditionalType.Like, FieldValue = type });
}
if (!string.IsNullOrEmpty(province))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Province", ConditionalType = ConditionalType.Like, FieldValue = province });
}
if (!string.IsNullOrEmpty(city))
{
conModels.Add(new ConditionalModel() { FieldName = "F_City", ConditionalType = ConditionalType.Like, FieldValue = city });
}
if (!string.IsNullOrEmpty(area))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Area", ConditionalType = ConditionalType.Like, FieldValue = area });
}
if (!string.IsNullOrEmpty(town))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Town", ConditionalType = ConditionalType.Like, FieldValue = town });
}
if (!string.IsNullOrEmpty(address))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Address", ConditionalType = ConditionalType.Like, FieldValue = address });
new KeyValuePair(WhereType.Or, new ConditionalModel() { FieldName = "F_Address1", ConditionalType = ConditionalType.Like, FieldValue = address });
}
if (!string.IsNullOrEmpty(birthday))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Birthday", ConditionalType = ConditionalType.Equal, FieldValue = birthday });
}
if (!string.IsNullOrEmpty(recommender))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Recommender", ConditionalType = ConditionalType.Like, FieldValue = recommender });
}
if (!string.IsNullOrEmpty(saleperson))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Saleperson", ConditionalType = ConditionalType.Like, FieldValue = saleperson });
}
//if (!string.IsNullOrEmpty(contact))
//{
// conModels.Add(new ConditionalModel() { FieldName = "F_Contact", ConditionalType = ConditionalType.Like, FieldValue = contact });
//}
#endregion
int recordCount = 0;
var list = await _cus_vip_infoRepository.GetListViewByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, "F_CreateOn desc");
var obj = new
{
state = "success",
message = "成功",
rows = list,
total = recordCount,
};
return Content(obj.ToJson());
}
///
/// 获取会员信息详情
///
/// id
///
[HttpGet("getdetails")]
public async Task GetDetailsAsync(int id)
{
if (id <= 0)
return Error("参数错误");
var model = await _cus_vip_infoRepository.GetSingleV(x => x.F_ID == id && x.F_State == (int)EnumDelState.Enabled);
var newmodel = new V_Cus_VipInfo();
if (model == null)
{
return Error("获取失败");
}
return Success("获取成功!", model);
}
///
/// 添加会员
///
///
///
[HttpPost("add")]
public async Task AddAsync(VipInfoInput input)
{
if (string.IsNullOrEmpty(input.name))
return Error("请输入名字");
if (string.IsNullOrEmpty(input.phone))
return Error("请输入联系方式");
var model = new T_Cus_VipInfo();
//model.F_VIPCode = input.vipcode;//会员卡号
model.F_Name = input.name;
model.F_Birthday = input.birthday;
model.F_Phone = input.phone;
model.F_Recommender = input.recommender;
model.F_Sex = input.sex;
model.F_Label = input.label;
model.F_Note = input.note;
model.F_City = input.city;
model.F_Nickname = input.nickname;
model.F_Address = input.address;
model.F_Address1 = input.address1;
model.F_Age = input.age;
model.F_Mobile = input.mobile;
model.F_Mobile1 = input.mobile1;
model.F_Province = input.province;
model.F_Area = input.area;
model.F_Town = input.town;
model.F_Postcode = input.postcode;
model.F_Province1 = input.province1;
model.F_Area1 = input.area1;
model.F_Town1 = input.town1;
model.F_Postcode1 = input.postcode1;
model.F_City1 = input.city1;
model.F_ShoeSize = input.shoesize;
model.F_Score = input.score;
model.F_Money = input.money;
model.F_TotalScore = input.totalscore;
model.F_Saleperson = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";input.saleperson;
model.F_Type = input.type;
model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
model.F_CreateOn = DateTime.Now;
model.F_State = (int)EnumDelState.Enabled;
if (await _cus_vip_infoRepository.GetCount(x => x.F_Mobile == model.F_Mobile && x.F_Name == input.name) > 0)
{
return Error("添加失败,存在相同会员信息");
}
var res = await _cus_vip_infoRepository.Add(model);
if (res > 0)
{
return Success("添加成功");
}
else
{
return Error("添加失败");
}
}
///
/// 修改会员信息
///
[HttpPost("update")]
public async Task UpdateAsync(VipInfoInput input)
{
if (string.IsNullOrEmpty(input.name))
return Error("请输入名字");
if (string.IsNullOrEmpty(input.phone))
return Error("请输入联系方式");
var model = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == input.id && x.F_State == (int)EnumDelState.Enabled);
if (model == null)
return Error("操作失败");
//model.F_VIPCode = input.vipcode;//会员卡号
model.F_Name = input.name;
model.F_Birthday = input.birthday;
model.F_Phone = input.phone;
model.F_Recommender = input.recommender;
model.F_Sex = input.sex;
model.F_Label = input.label;
model.F_State = input.state;
model.F_Note = input.note;
model.F_City = input.city;
model.F_Nickname = input.nickname;
model.F_Address = input.address;
model.F_Address1 = input.address1;
model.F_LastModifyOn = DateTime.Now.ToLocalTime();
model.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
model.F_Age = input.age;
model.F_Mobile = input.mobile;
model.F_Mobile1 = input.mobile1;
model.F_Province = input.province;
model.F_Area = input.area;
model.F_Town = input.town;
model.F_Postcode = input.postcode;
model.F_Province1 = input.province1;
model.F_Area1 = input.area1;
model.F_Town1 = input.town1;
model.F_Postcode1 = input.postcode1;
model.F_City1 = input.city1;
model.F_ShoeSize = input.shoesize;
model.F_Score = input.score;
model.F_Money = input.money;
model.F_TotalScore = input.totalscore;
//model.F_Saleperson = input.saleperson;
//model.F_Contact = input.contact;
model.F_Type = input.type;
bool b = await _cus_vip_infoRepository.Update(model);
if (b)
return Success("修改成功");
return Error("修改失败");
}
///
/// 删除会员信息 by ids
///
///
///
[HttpPost("delete")]
public async Task Remove(int[] ids)
{
var res = 0;
if (ids != null && ids.Length > 0)
{
foreach (var item in ids)
{
var ml = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == item);
ml.F_State = (int)EnumDelState.Delete;
ml.F_DeleteOn = DateTime.Now.ToLocalTime();
ml.F_DeleteBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
if (_cus_vip_infoRepository.Update(ml).Result)
res += 1;
}
if (res == ids.Length)
return Success("删除成功");
else if (res > 0 && res < ids.Length)
return Error("部分删除失败,请查看后重新操作");
else
return Error("删除失败,请查看后重新操作");
}
else
return Error("请选择要删除的记录");
}
///
/// 转移会员信息
///
///
///
[HttpPost("transfer")]
public async Task Transfer(int[] ids,string toperson)
{
var res = 0;
if (ids != null && ids.Length > 0)
{
foreach (var item in ids)
{
var ml = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == item);
if (ml != null)
{
#region 创建消息提醒
string cname = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
var model = new T_Cus_Msg();
model.F_Type = 1;
model.F_VipInfoID = item;
model.F_ToPerson = toperson;
model.F_State = 0;
model.F_Notice = cname+"将"+ids .Length +"条会员档案从"+ ml.F_Saleperson+"转移给"+toperson ;
model.F_NotifyPerson = ml.F_Saleperson;
model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
model.F_CreateOn = DateTime.Now;
var su = await _cus_msgRepository.Add(model);
if (su > 0)
{
res += 1;
}
#endregion
}
//ml.F_Saleperson = toperson;
//ml.F_LastModifyOn = DateTime.Now.ToLocalTime();
//ml.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
//if (_cus_vip_infoRepository.Update(ml).Result)
// res += 1;
}
if (res == ids.Length)
return Success("转移消息创建成功");
else if (res > 0 && res < ids.Length)
return Error("部分转移消息创建失败,请查看后重新操作");
else
return Error("转移消息创建失败,请查看后重新操作");
}
else
return Error("请选择要转移的记录");
}
///
/// 转移消息处理
///
///
///
[HttpPost("deal")]
public async Task Deal(int[] ids, int notagree)//同意传0,不同意传1
{
var res = 0;
if (ids != null && ids.Length > 0)
{
foreach (var item in ids)
{
var ml = await _cus_msgRepository.GetSingle(x => x.F_ID == item);
if (ml != null)
{
#region 创建消息提醒
//var model = new T_Cus_Msg();
ml.F_Type = 1;
ml.F_State = notagree +2;
ml.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
ml.F_LastModifyOn = DateTime.Now;
if (_cus_msgRepository.Update(ml).Result)
{
if (notagree == 0)//同意转移时更新会员信息
{
var m = await _cus_vip_infoRepository.GetSingle(x => x.F_ID == ml.F_VipInfoID );
m.F_Saleperson = ml.F_ToPerson ;
m.F_LastModifyOn = DateTime.Now.ToLocalTime();
m.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
if (_cus_vip_infoRepository.Update(m).Result)
{ }
}
res += 1;
}
#endregion
}
}
if (res == ids.Length)
return Success("处理成功");
else if (res > 0 && res < ids.Length)
return Error("部分消息处理失败,请查看后重新操作");
else
return Error("处理消息失败,请查看后重新操作");
}
else
return Error("请选择要处理的记录");
}
#region 导入、导出
//导出
[Authorize]
[HttpGet("exportexcel")]
[AllowAnonymous]
public async Task ExportExcelAsync(string keyword, string name, string phone, string type, string province, string city, string area, string town, string address, string birthday, string recommender, string saleperson)
{
try
{
List conModels = new List();
#region 条件筛选
conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
if (!string.IsNullOrEmpty(phone))
conModels.Add(new ConditionalCollections()
{
ConditionalList = new List>()
{
new KeyValuePair(WhereType.And, new ConditionalModel() { FieldName = "F_Phone", ConditionalType = ConditionalType.Like, FieldValue = phone }),
new KeyValuePair( WhereType.Or , new ConditionalModel() { FieldName = "F_Mobile", ConditionalType = ConditionalType.Like, FieldValue = phone }),
new KeyValuePair( WhereType.Or, new ConditionalModel() { FieldName = "F_Mobile1", ConditionalType = ConditionalType.Like, FieldValue = phone })
}
});
if (!string.IsNullOrEmpty(name))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Name", ConditionalType = ConditionalType.Like, FieldValue = name });
}
if (!string.IsNullOrEmpty(type))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Type", ConditionalType = ConditionalType.Like, FieldValue = type });
}
if (!string.IsNullOrEmpty(province))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Province", ConditionalType = ConditionalType.Like, FieldValue = province });
}
if (!string.IsNullOrEmpty(city))
{
conModels.Add(new ConditionalModel() { FieldName = "F_City", ConditionalType = ConditionalType.Like, FieldValue = city });
}
if (!string.IsNullOrEmpty(area))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Area", ConditionalType = ConditionalType.Like, FieldValue = area });
}
if (!string.IsNullOrEmpty(town))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Town", ConditionalType = ConditionalType.Like, FieldValue = town });
}
if (!string.IsNullOrEmpty(address))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Address", ConditionalType = ConditionalType.Like, FieldValue = address });
new KeyValuePair(WhereType.Or, new ConditionalModel() { FieldName = "F_Address1", ConditionalType = ConditionalType.Like, FieldValue = address });
}
if (!string.IsNullOrEmpty(birthday))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Birthday", ConditionalType = ConditionalType.Like, FieldValue = birthday });
}
if (!string.IsNullOrEmpty(recommender))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Recommender", ConditionalType = ConditionalType.Like, FieldValue = recommender });
}
if (!string.IsNullOrEmpty(saleperson))
{
conModels.Add(new ConditionalModel() { FieldName = "F_Saleperson", ConditionalType = ConditionalType.Like, FieldValue = saleperson });
}
#endregion
int recordCount = 0;
var list = await _cus_vip_infoRepository.GetListALL(conModels, "F_CreateOn desc");
var dtNew = list.ToList();
if (dtNew != null)
{
var tb = ModelConvertHelper.ConvertListToDataTable(dtNew);
//导出dtnew
NPOIHelper npoi = new NPOIHelper();
string dbkeys = _configuration["Import:vipkey"].ToString();
string title = _configuration["Import:vipvalue"].ToString();
string[] cols = dbkeys.Split(",");
string[] colname = title.Split(",");
byte[] sm = npoi.ExportToExcel1(tb, cols, colname);
if (sm != null)
{
return File(sm, "application/vnd.ms-excel", "会员信息.xlsx");
}
else
{
return Error("导出失败3");
}
}
return Error("导出失败2");
//return Success("根据条件获取数据成功", tasklist);
}
catch (Exception ex)
{
//_logger.LogError(ex, "导出异常");
return Error("导出失败1");
}
//return Success("导出成功");
}
///
/// 上传文件并导入数据库
///
///
[HttpPost("importexcel")]
public async Task ImportExcel( int headrow = 0)
{
//string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
NPOIHelper npoi = new NPOIHelper();
var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
int num = dtExcel.Rows.Count;
var cols = dtExcel.Columns;
int colnum = cols.Count;
string dbkeys = _configuration["Import:vipkey"].ToString();
string title = _configuration["Import:vipvalue"].ToString();
string[] dbcols = dbkeys.Split(",");
string[] titlecols = title.Split(",");
string errmsg = string.Empty;
if (num > 0)
{
int index = 1;
foreach (DataRow dr in dtExcel.Rows)
{
var model = new T_Cus_VipInfo();
model.F_State = 1;
//model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
model.F_CreateOn = DateTime.Now;
var dbcolslist = dbcols.ToList();
var titlelist = titlecols.ToList();
Type t = model.GetType();
PropertyInfo[] PropertyList = t.GetProperties();
foreach (PropertyInfo item in PropertyList)
{
if (dbcolslist.Contains(item.Name))
{
Type tType = null;
object o = dr[titlelist[dbcolslist.IndexOf(item.Name)]];
if (item.PropertyType.FullName.Contains(typeof(DateTime).FullName))
{
tType= typeof(DateTime);
}
else if (item.PropertyType.FullName.Contains(typeof(Int64).FullName))
{
tType = typeof(Int64);
}
else if (item.PropertyType.FullName.Contains(typeof(Int32).FullName))
{
tType = typeof(Int32);
}
else if (item.PropertyType.FullName.Contains(typeof(Decimal).FullName))
{
tType = typeof(Decimal);
}
else { tType = item.PropertyType; }
object v = null;
if (o!=null&&o!=DBNull.Value)
v = Convert.ChangeType(dr[titlelist[dbcolslist.IndexOf(item.Name)]].ToString(), tType);
item.SetValue(model, v, null);
}
}
int b = await _cus_vip_infoRepository.Add(model);
if (b<=0)
{
if (!string.IsNullOrEmpty(errmsg))
{
errmsg = errmsg + "\r\n第" + index + "行导入失败!";
}
else
{
errmsg = "第" + index + "行导入失败!";
}
}
index++;
}
}
else
{
return Error("文件中无数据");
}
if (!string.IsNullOrEmpty(errmsg))
{
//删除已导入的部分
return Error(errmsg);
}
return Success("导入成功");
}
#endregion
///
/// 更新会员积分
///
///
///
[HttpPost("updatescore")]
public async Task UpdateScore(string mobile,string money)
{
if (string.IsNullOrEmpty (mobile ))
{
var ml = await _cus_vip_infoRepository.GetSingle(x => x.F_Mobile == mobile||x.F_Mobile1 ==mobile );
ml.F_State = (int)EnumDelState.Delete;
ml.F_DeleteOn = DateTime.Now.ToLocalTime();
ml.F_DeleteBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
if (_cus_vip_infoRepository.Update(ml).Result)
{
return Success("更新成功");
}
else
return Error("更新失败");
}
else
return Error("手机号不能为空");
}
}
}