using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Extend.Interfaces.LqYaoyjl;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Extend.Entitys.lq_yaoyjl;
using NCC.Extend.Entitys.Dto.LqYaoyjl;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.Common.Model.NPOI;
using NCC.Common.Configuration;
using NCC.DataEncryption;
using NCC.ClayObject;
using NCC.System.Entitys.Permission;
using NCC.Extend.Entitys.lq_mdxx;
namespace NCC.Extend.LqYaoyjl
{
///
/// 邀约记录服务
///
[ApiDescriptionSettings(Tag = "绿纤邀约记录服务", Name = "LqYaoyjl", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqYaoyjlService : ILqYaoyjlService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqYaoyjlRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public LqYaoyjlService(
ISqlSugarRepository lqYaoyjlRepository,
IUserManager userManager)
{
_lqYaoyjlRepository = lqYaoyjlRepository;
_db = _lqYaoyjlRepository.Context;
_userManager = userManager;
}
#region 邀约记录
///
/// 获取邀约记录
///
/// 参数
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
var output = entity.Adapt();
output.storeName = SqlFunc.Subqueryable().Where(u => u.Id == entity.StoreId).Select(u => u.Dm);
return output;
}
#endregion
#region 邀约记录列表
///
/// 获取邀约记录列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] LqYaoyjlListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryYysj = input.yysj != null ? input.yysj.Split(',').ToObeject>() : null;
DateTime? startYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.First()) : null;
DateTime? endYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.Last()) : null;
List queryLxsj = input.lxsj != null ? input.lxsj.Split(',').ToObeject>() : null;
DateTime? startLxsj = queryLxsj != null ? Ext.GetDateTime(queryLxsj.First()) : null;
DateTime? endLxsj = queryLxsj != null ? Ext.GetDateTime(queryLxsj.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.yyr), p => p.Yyr.Equals(input.yyr))
.WhereIF(queryYysj != null, p => p.Yysj >= new DateTime(startYysj.ToDate().Year, startYysj.ToDate().Month, startYysj.ToDate().Day, 0, 0, 0))
.WhereIF(queryYysj != null, p => p.Yysj <= new DateTime(endYysj.ToDate().Year, endYysj.ToDate().Month, endYysj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.yykh), p => p.Yykh.Equals(input.yykh))
.WhereIF(!string.IsNullOrEmpty(input.yykhxm), p => p.Yykhxm.Contains(input.yykhxm))
.WhereIF(!string.IsNullOrEmpty(input.dhsfyx), p => p.Dhsfyx.Equals(input.dhsfyx))
.WhereIF(queryLxsj != null, p => p.Lxsj >= new DateTime(startLxsj.ToDate().Year, startLxsj.ToDate().Month, startLxsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryLxsj != null, p => p.Lxsj <= new DateTime(endLxsj.ToDate().Year, endLxsj.ToDate().Month, endLxsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.lxjl), p => p.Lxjl.Contains(input.lxjl))
.Select(it => new LqYaoyjlListOutput
{
id = it.Id,
yyr = it.Yyr,
yysj = it.Yysj,
yykh = it.Yykh,
yykhxm = it.Yykhxm,
dhsfyx = it.Dhsfyx,
lxsj = it.Lxsj,
lxjl = it.Lxjl,
storeId = it.StoreId,
storeName = SqlFunc.Subqueryable().Where(u => u.Id == it.StoreId).Select(u => u.Dm),
createTime = it.CreateTime,
yyrName = SqlFunc.Subqueryable().Where(u => u.Id == it.Yyr).Select(u => u.RealName),
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
#region 新建邀约记录
///
/// 新建邀约记录
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] LqYaoyjlCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
entity.CreateTime = DateTime.Now;
entity.StoreId = _db.Queryable().Where(u => u.Id == input.yyr).Select(u => u.Mdid).First();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 邀约记录无分页列表
///
/// 获取邀约记录无分页列表
///
/// 请求参数
///
[NonAction]
public async Task GetNoPagingList([FromQuery] LqYaoyjlListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryYysj = input.yysj != null ? input.yysj.Split(',').ToObeject>() : null;
DateTime? startYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.First()) : null;
DateTime? endYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.Last()) : null;
List queryLxsj = input.lxsj != null ? input.lxsj.Split(',').ToObeject>() : null;
DateTime? startLxsj = queryLxsj != null ? Ext.GetDateTime(queryLxsj.First()) : null;
DateTime? endLxsj = queryLxsj != null ? Ext.GetDateTime(queryLxsj.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.yyr), p => p.Yyr.Equals(input.yyr))
.WhereIF(queryYysj != null, p => p.Yysj >= new DateTime(startYysj.ToDate().Year, startYysj.ToDate().Month, startYysj.ToDate().Day, 0, 0, 0))
.WhereIF(queryYysj != null, p => p.Yysj <= new DateTime(endYysj.ToDate().Year, endYysj.ToDate().Month, endYysj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.yykh), p => p.Yykh.Equals(input.yykh))
.WhereIF(!string.IsNullOrEmpty(input.yykhxm), p => p.Yykhxm.Contains(input.yykhxm))
.WhereIF(!string.IsNullOrEmpty(input.dhsfyx), p => p.Dhsfyx.Equals(input.dhsfyx))
.WhereIF(queryLxsj != null, p => p.Lxsj >= new DateTime(startLxsj.ToDate().Year, startLxsj.ToDate().Month, startLxsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryLxsj != null, p => p.Lxsj <= new DateTime(endLxsj.ToDate().Year, endLxsj.ToDate().Month, endLxsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.lxjl), p => p.Lxjl.Contains(input.lxjl))
.Select(it => new LqYaoyjlListOutput
{
id = it.Id,
yyr = it.Yyr,
yysj = it.Yysj,
yykh = it.Yykh,
yykhxm = it.Yykhxm,
dhsfyx = it.Dhsfyx,
lxsj = it.Lxsj,
lxjl = it.Lxjl,
yyrName = SqlFunc.Subqueryable().Where(u => u.MobilePhone == it.Yyr).Select(u => u.RealName),
}).MergeTable().OrderBy(sidx + " " + input.sort).ToListAsync();
return data;
}
#endregion
#region 导出邀约记录
///
/// 导出邀约记录
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] LqYaoyjlListQueryInput input)
{
var userInfo = await _userManager.GetUserInfo();
var exportData = new List();
if (input.dataType == 0)
{
var data = Clay.Object(await this.GetList(input));
exportData = data.Solidify>().list;
}
else
{
exportData = await this.GetNoPagingList(input);
}
List paramList = "[{\"value\":\"邀约编号\",\"field\":\"id\"},{\"value\":\"邀约人\",\"field\":\"yyr\"},{\"value\":\"邀约时间\",\"field\":\"yysj\"},{\"value\":\"邀约客户\",\"field\":\"yykh\"},{\"value\":\"邀约客户姓名\",\"field\":\"yykhxm\"},{\"value\":\"电话是否有效\",\"field\":\"dhsfyx\"},{\"value\":\"联系时间\",\"field\":\"lxsj\"},{\"value\":\"联系记录\",\"field\":\"lxjl\"},]".ToList();
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.FileName = "邀约记录.xls";
excelconfig.HeadFont = "微软雅黑";
excelconfig.HeadPoint = 10;
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnModel = new List();
List selectKeyList = input.selectKey.Split(',').ToList();
foreach (var item in selectKeyList)
{
var isExist = paramList.Find(p => p.field == item);
if (isExist != null)
{
excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field, ExcelColumn = isExist.value });
}
}
var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName;
ExcelExportHelper.Export(exportData, excelconfig, addPath);
var fileName = _userManager.UserId + "|" + addPath + "|xls";
var output = new
{
name = excelconfig.FileName,
url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC")
};
return output;
}
#endregion
#region 批量删除邀约记录
///
/// 批量删除邀约记录
///
/// 主键数组
///
[HttpPost("batchRemove")]
public async Task BatchRemove([FromBody] List ids)
{
var entitys = await _db.Queryable().In(it => it.Id, ids).ToListAsync();
if (entitys.Count > 0)
{
try
{
//开启事务
_db.BeginTran();
//批量删除邀约记录
await _db.Deleteable().In(d => d.Id, ids).ExecuteCommandAsync();
//关闭事务
_db.CommitTran();
}
catch (Exception)
{
//回滚事务
_db.RollbackTran();
throw NCCException.Oh(ErrorCode.COM1002);
}
}
}
#endregion
#region 更新邀约记录
///
/// 更新邀约记录
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] LqYaoyjlUpInput input)
{
var entity = input.Adapt();
entity.StoreId = _db.Queryable().Where(u => u.Id == input.yyr).Select(u => u.Mdid).First();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 删除邀约记录
///
/// 删除邀约记录
///
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
}
#endregion
}
}