using OASystem.API.OAMethodLib;
using OASystem.Domain.AesEncryption;
using OASystem.Domain.Dtos.CRM;
using OASystem.Domain.Entities.Customer;
using OASystem.Infrastructure.Repositories.CRM;
using System.Collections;
using System.Diagnostics;
namespace OASystem.API.Controllers
{
///
/// 市场客户资料
///
[Route("api/[controller]/[action]")]
public class MarketCustomerResourcesController : ControllerBase
{
private readonly NewClientDataRepository _clientDataRepository;
private readonly SqlSugarClient _sqlSugar;
///
/// 初始化
///
public MarketCustomerResourcesController(NewClientDataRepository clientDataRepository, SqlSugarClient sqlSugar)
{
this._clientDataRepository = clientDataRepository;
_sqlSugar = sqlSugar;
}
///
/// 客户资料数据
/// 基础数据
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task MarketCustomerInit(MarketCustomerInitDto dto)
{
JsonView jw = new JsonView();
try
{
Result resultData = await _clientDataRepository._Init(dto);
if (resultData.Code == 0)
{
jw = JsonView(true, "查询成功!", resultData.Data);
}
else
{
jw = JsonView(false, resultData.Msg);
}
}
catch (Exception)
{
jw = JsonView(false, "程序错误!");
}
return Ok(jw);
}
///
/// 查询客户资料数据
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task QueryNewClientData(NewClientDataQueryDto dto)
{
#region 参数验证
if (dto.OperationUserId < 0)
return Ok(JsonView(false, "请传入有效的OperationUserId参数!"));
if (dto.PortType < 0)
return Ok(JsonView(false, "请传入有效的PortType参数!"));
#endregion
JsonView jw = new JsonView();
try
{
Result resultData = await _clientDataRepository.QueryNewClientData(dto);
if (resultData.Code == 0)
{
#region 客户资料表操作记录
await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.NoOperation, dto.OperationUserId, 0, "");
#endregion
jw = JsonView(true, resultData.Msg, resultData.Data);
}
else
{
jw = JsonView(false, resultData.Msg);
}
}
catch (Exception)
{
jw = JsonView(false, "程序错误!");
}
return Ok(jw);
}
///
/// 客户资料数据
/// Details
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task PostNewClientDataDetails(NewClientDataDetailsDto dto)
{
#region 参数验证
if (dto.Id < 0)
return Ok(JsonView(false, "请传入有效的Id参数!"));
if (dto.UserId < 0)
return Ok(JsonView(false, "请传入有效的UserId参数!"));
if (dto.PortType < 0)
return Ok(JsonView(false, "请传入有效的PortType参数!"));
#endregion
JsonView jw = new JsonView();
try
{
Result resultData = await _clientDataRepository._Details(dto.PortType, dto.Id);
if (resultData.Code == 0)
{
#region 客户资料表操作记录
await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Details, dto.UserId, dto.Id, "");
#endregion
jw = JsonView(true, "查询成功!", resultData.Data);
}
else
{
jw = JsonView(false, resultData.Msg);
}
}
catch (Exception)
{
jw = JsonView(false, "程序错误!");
}
return Ok(jw);
}
///
/// 客户资料操作(Status:1.新增,2.修改)
///
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task NewClientOp(NewClientOpDto dto)
{
#region 参数验证
if (dto.CreateUserId < 0)
{
return Ok(JsonView(false, "请传入有效的CreateUserId参数!"));
}
if (dto.PortType < 0)
{
return Ok(JsonView(false, "请传入有效的PortType参数!"));
}
#endregion
try
{
Domain.Result result = await _clientDataRepository.NewClientOp(dto);
if (result.Code != 0)
{
return Ok(JsonView(false, result.Msg));
}
#region 客户资料操作记录
OperationEnum operationEnum = OperationEnum.NoOperation;
if (dto.Status == 1)
{
operationEnum = OperationEnum.Add;
dto.Id = Convert.ToInt32(result.Data);
}
else if (dto.Status == 2) operationEnum = OperationEnum.Edit;
await GeneralMethod.NewClientOperationRecord(dto.PortType, operationEnum, dto.CreateUserId, dto.Id, "");
#endregion
return Ok(JsonView(true, result.Msg + "Id:" + dto.Id));
}
catch (Exception ex)
{
return Ok(JsonView(false, "程序错误!Msg:" + ex.Message));
}
}
///
/// 新客户资料操作(删除)
///
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task NewClientDel(DelBaseDto dto)
{
#region 参数验证
if (dto.Id < 0)
{
return Ok(JsonView(false, "请传入有效的Id参数!"));
}
if (dto.DeleteUserId < 0)
{
return Ok(JsonView(false, "请传入有效的DeleteUserId参数!"));
}
if (dto.PortType < 0)
{
return Ok(JsonView(false, "请传入有效的PortType参数!"));
}
#endregion
var res = await _clientDataRepository.DelNewClientData(dto);
if (res.Code != 0)
{
return Ok(JsonView(false, "删除失败"));
}
#region 客户资料表操作记录
await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Del, dto.DeleteUserId, dto.Id, "");
#endregion
return Ok(JsonView(true, "删除成功!"));
}
///
/// 获取下拉列表数据和单条数据信息
///
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task QuerySelectAndSingleData(QuerySingleDto dto)
{
JsonView jw = new JsonView();
var result = await _clientDataRepository.QuerySelectAndSingleData(dto);
if (result.Code == 0)
{
#region 客户资料表操作记录
await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Details, dto.UserId, dto.Id, "");
#endregion
jw = JsonView(true, result.Msg, result.Data);
}
else
{
jw = JsonView(false, result.Msg);
}
return Ok(jw);
}
///
/// 获取现有负责人
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task QueryUserSelect()
{
try
{
Result resTable = _clientDataRepository.QueryUserSelect();
return Ok(JsonView(true, resTable.Msg, resTable.Data));
}
catch (Exception)
{
return Ok(JsonView(false, "程序错误!"));
}
}
///
/// 获取出团数据
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task QueryNumberGroups()
{
var result = await _clientDataRepository.QueryNumberGroups();
if (result.Code != 0)
{
return Ok(JsonView(false, result.Msg));
}
return Ok(JsonView(true, result.Msg, result.Data));
}
///
/// 新客户资料操作
/// 批量分配
///
///
///
[HttpPost]
[ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
public async Task PostBatchAssignment(BatchAssignmentDto dto)
{
#region 参数验证
if (dto.UserId < 0)
{
return Ok(JsonView(false, "请传入有效的UserId参数!"));
}
if (dto.PortType < 0)
{
return Ok(JsonView(false, "请传入有效的PortType参数!"));
}
#endregion
var res = await _clientDataRepository._BatchAssignment(dto);
if (res.Code != 0)
{
return Ok(JsonView(false, res.Msg));
}
return Ok(JsonView(true, "操作成功!"));
}
private Dictionary> MCRSetting()
{
Dictionary> keyValuePairs = new Dictionary>();
keyValuePairs.Add(419, new List() //四川
{
376,377,378,381,382,387,388,389,390,753,754,1296,1303
});
keyValuePairs.Add(420, new List() //云南
{
407,408,409,410,449,451,452,453,567,754,1297,1304
});
keyValuePairs.Add(421, new List() { 424, 425, 426, 427, 428, 429, 754,1298, 1305,1307,1306 }); // 贵州
keyValuePairs.Add(422, new List() { 415, 416, 754 }); // 西藏
keyValuePairs.Add(423, new List() { 417, 418, 454, 456, 754 }); // 重庆
keyValuePairs.Add(578, new List() { 581, 582, 583, 754,1299,1300 }); // 青海
keyValuePairs.Add(605, new List() { 588, 589, 590, 591, 592, 593, 754,1301,1310,1309,1308 }); // 陕西
keyValuePairs.Add(606, new List() { 597, 598, 599, 600, 601, 602, 603, 604, 754 }); // 宁夏
keyValuePairs.Add(625, new List() { 617, 618, 619, 620, 621, 622, 622, 623, 624, 754,1302,1312,1311 }); // 甘肃
keyValuePairs.Add(634, new List() { 455, 630, 631, 632, 633, 754 }); // 新疆
return keyValuePairs;
}
[HttpPost]
public IActionResult QueryClientType(QueryClientTypeDto Dto)
{
var jw = JsonView(true,"获取成功!");
var keyValuePairs = MCRSetting();
ArrayList arr = new ArrayList();
var ids = new List();
foreach (var item in Dto.SetDataIdArr)
{
if (keyValuePairs.Keys.Contains(item))
{
ids.AddRange(keyValuePairs[item]);
}
}
arr.AddRange(_sqlSugar.Queryable()
.Where(u => ids.Contains(u.Id) && u.IsDel == 0)
.Select(x => new { x.Id, x.Name , x.Remark })
.ToList());
if (arr.Count == 0)
{
arr.AddRange(_sqlSugar.Queryable()
.Where(u => u.STid == 37 && u.IsDel == 0)
.Select(x => new { x.Id, x.Name, x.Remark }).ToList());
}
// 创建比较器实例
IComparer remakeComparer = new RemakeComparer();
// 使用ArrayList.Sort方法和自定义比较器对arr进行排序
arr.Sort(remakeComparer);
jw.Data = arr;
return Ok(jw);
}
[HttpPost]
public IActionResult SynchronizationData()
{
var keyValuePairs = new Dictionary>
{
{ 5, new List { "省级部门", "省级单位" } },
{ 10, new List { "市州" } },
{ 15, new List { "省属国企" } },
{ 20, new List { "省属学校", "省属高校" } },
{ 25, new List { "省级行业协会", "省级社团", "省级联合会" , "省属行业协会", "民营企业", "地方国企" } },
{ 30, new List { "市级部门", "市级单位" } },
{ 35, new List { "区市县" } },
{ 40, new List { "市级国企", "市属国企" } },
{ 45, new List { "市属学校", "市属高校" } },
{ 50, new List { "市级行业协会", "市级社团", "市级联合会" } }
};
var setting = MCRSetting();
var arr = new List>();
foreach (var item in setting.Keys)
{
var setDataList_DB = _sqlSugar.Queryable()
.Where(x => x.IsDel == 0 && setting[item].Contains(x.Id))
.ToList();
foreach (var setData in setDataList_DB)
{
foreach (var lv in keyValuePairs.Keys)
{
var islv = keyValuePairs[lv].Where(x => setData.Name.Contains(x)).Count() > 0;
if (islv)
{
setData.Remark = lv.ToString();
}
}
}
arr.Add(setDataList_DB);
}
_sqlSugar.Updateable(arr.SelectMany(x => x).ToList()).UpdateColumns(x => new
{
x.Remark
}).ExecuteCommand();
return Ok(arr);
}
}
}