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.LqMdFzht;
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_md_fzht;
using NCC.Extend.Entitys.Dto.LqMdFzht;
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;
namespace NCC.Extend.LqMdFzht
{
///
/// 房租合同服务
///
[ApiDescriptionSettings(Tag = "Extend",Name = "LqMdFzht", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqMdFzhtService : ILqMdFzhtService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqMdFzhtRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public LqMdFzhtService(
ISqlSugarRepository lqMdFzhtRepository,
IUserManager userManager)
{
_lqMdFzhtRepository = lqMdFzhtRepository;
_db = _lqMdFzhtRepository.Context;
_userManager = userManager;
}
///
/// 获取房租合同
///
/// 参数
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
var output = entity.Adapt();
return output;
}
///
/// 获取房租合同列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] LqMdFzhtListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryZfsj = input.zfsj != null ? input.zfsj.Split(',').ToObeject>() : null;
DateTime? startZfsj = queryZfsj != null ? Ext.GetDateTime(queryZfsj.First()) : null;
DateTime? endZfsj = queryZfsj != null ? Ext.GetDateTime(queryZfsj.Last()) : null;
List queryHtqdsj = input.htqdsj != null ? input.htqdsj.Split(',').ToObeject>() : null;
DateTime? startHtqdsj = queryHtqdsj != null ? Ext.GetDateTime(queryHtqdsj.First()) : null;
DateTime? endHtqdsj = queryHtqdsj != null ? Ext.GetDateTime(queryHtqdsj.Last()) : null;
List queryHtjssj = input.htjssj != null ? input.htjssj.Split(',').ToObeject>() : null;
DateTime? startHtjssj = queryHtjssj != null ? Ext.GetDateTime(queryHtjssj.First()) : null;
DateTime? endHtjssj = queryHtjssj != null ? Ext.GetDateTime(queryHtjssj.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.mdbm), p => p.Mdbm.Equals(input.mdbm))
.WhereIF(!string.IsNullOrEmpty(input.djmd), p => p.Djmd.Contains(input.djmd))
.WhereIF(!string.IsNullOrEmpty(input.fz), p => p.Fz.Contains(input.fz))
.WhereIF(queryZfsj != null, p => p.Zfsj >= new DateTime(startZfsj.ToDate().Year, startZfsj.ToDate().Month, startZfsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryZfsj != null, p => p.Zfsj <= new DateTime(endZfsj.ToDate().Year, endZfsj.ToDate().Month, endZfsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.jfz), p => p.Jfz.Equals(input.jfz))
.WhereIF(!string.IsNullOrEmpty(input.dfz), p => p.Dfz.Equals(input.dfz))
.WhereIF(!string.IsNullOrEmpty(input.yszkyj), p => p.Yszkyj.Contains(input.yszkyj))
.WhereIF(!string.IsNullOrEmpty(input.zffs), p => p.Zffs.Equals(input.zffs))
.WhereIF(queryHtqdsj != null, p => p.Htqdsj >= new DateTime(startHtqdsj.ToDate().Year, startHtqdsj.ToDate().Month, startHtqdsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryHtqdsj != null, p => p.Htqdsj <= new DateTime(endHtqdsj.ToDate().Year, endHtqdsj.ToDate().Month, endHtqdsj.ToDate().Day, 23, 59, 59))
.WhereIF(queryHtjssj != null, p => p.Htjssj >= new DateTime(startHtjssj.ToDate().Year, startHtjssj.ToDate().Month, startHtjssj.ToDate().Day, 0, 0, 0))
.WhereIF(queryHtjssj != null, p => p.Htjssj <= new DateTime(endHtjssj.ToDate().Year, endHtjssj.ToDate().Month, endHtjssj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.wy), p => p.Wy.Contains(input.wy))
.WhereIF(!string.IsNullOrEmpty(input.jwy), p => p.Jwy.Equals(input.jwy))
.WhereIF(!string.IsNullOrEmpty(input.dwy), p => p.Dwy.Equals(input.dwy))
.Select(it=> new LqMdFzhtListOutput
{
id = it.Id,
mdbm=it.Mdbm,
djmd=it.Djmd,
fz=it.Fz,
zfsj=it.Zfsj,
jfz=it.Jfz,
dfz=it.Dfz,
yszkyj=it.Yszkyj,
zffs=it.Zffs,
htqdsj=it.Htqdsj,
htjssj=it.Htjssj,
wy=it.Wy,
jwy=it.Jwy,
dwy=it.Dwy,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 新建房租合同
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] LqMdFzhtCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
///
/// 获取房租合同无分页列表
///
/// 请求参数
///
[NonAction]
public async Task GetNoPagingList([FromQuery] LqMdFzhtListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryZfsj = input.zfsj != null ? input.zfsj.Split(',').ToObeject>() : null;
DateTime? startZfsj = queryZfsj != null ? Ext.GetDateTime(queryZfsj.First()) : null;
DateTime? endZfsj = queryZfsj != null ? Ext.GetDateTime(queryZfsj.Last()) : null;
List queryHtqdsj = input.htqdsj != null ? input.htqdsj.Split(',').ToObeject>() : null;
DateTime? startHtqdsj = queryHtqdsj != null ? Ext.GetDateTime(queryHtqdsj.First()) : null;
DateTime? endHtqdsj = queryHtqdsj != null ? Ext.GetDateTime(queryHtqdsj.Last()) : null;
List queryHtjssj = input.htjssj != null ? input.htjssj.Split(',').ToObeject>() : null;
DateTime? startHtjssj = queryHtjssj != null ? Ext.GetDateTime(queryHtjssj.First()) : null;
DateTime? endHtjssj = queryHtjssj != null ? Ext.GetDateTime(queryHtjssj.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.mdbm), p => p.Mdbm.Equals(input.mdbm))
.WhereIF(!string.IsNullOrEmpty(input.djmd), p => p.Djmd.Contains(input.djmd))
.WhereIF(!string.IsNullOrEmpty(input.fz), p => p.Fz.Contains(input.fz))
.WhereIF(queryZfsj != null, p => p.Zfsj >= new DateTime(startZfsj.ToDate().Year, startZfsj.ToDate().Month, startZfsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryZfsj != null, p => p.Zfsj <= new DateTime(endZfsj.ToDate().Year, endZfsj.ToDate().Month, endZfsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.jfz), p => p.Jfz.Equals(input.jfz))
.WhereIF(!string.IsNullOrEmpty(input.dfz), p => p.Dfz.Equals(input.dfz))
.WhereIF(!string.IsNullOrEmpty(input.yszkyj), p => p.Yszkyj.Contains(input.yszkyj))
.WhereIF(!string.IsNullOrEmpty(input.zffs), p => p.Zffs.Equals(input.zffs))
.WhereIF(queryHtqdsj != null, p => p.Htqdsj >= new DateTime(startHtqdsj.ToDate().Year, startHtqdsj.ToDate().Month, startHtqdsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryHtqdsj != null, p => p.Htqdsj <= new DateTime(endHtqdsj.ToDate().Year, endHtqdsj.ToDate().Month, endHtqdsj.ToDate().Day, 23, 59, 59))
.WhereIF(queryHtjssj != null, p => p.Htjssj >= new DateTime(startHtjssj.ToDate().Year, startHtjssj.ToDate().Month, startHtjssj.ToDate().Day, 0, 0, 0))
.WhereIF(queryHtjssj != null, p => p.Htjssj <= new DateTime(endHtjssj.ToDate().Year, endHtjssj.ToDate().Month, endHtjssj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.wy), p => p.Wy.Contains(input.wy))
.WhereIF(!string.IsNullOrEmpty(input.jwy), p => p.Jwy.Equals(input.jwy))
.WhereIF(!string.IsNullOrEmpty(input.dwy), p => p.Dwy.Equals(input.dwy))
.Select(it=> new LqMdFzhtListOutput
{
id = it.Id,
mdbm=it.Mdbm,
djmd=it.Djmd,
fz=it.Fz,
zfsj=it.Zfsj,
jfz=it.Jfz,
dfz=it.Dfz,
yszkyj=it.Yszkyj,
zffs=it.Zffs,
htqdsj=it.Htqdsj,
htjssj=it.Htjssj,
wy=it.Wy,
jwy=it.Jwy,
dwy=it.Dwy,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToListAsync();
return data;
}
///
/// 导出房租合同
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] LqMdFzhtListQueryInput 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\":\"mdbm\"},{\"value\":\"单据门店\",\"field\":\"djmd\"},{\"value\":\"房租\",\"field\":\"fz\"},{\"value\":\"支付时间\",\"field\":\"zfsj\"},{\"value\":\"借方\",\"field\":\"jfz\"},{\"value\":\"贷方\",\"field\":\"dfz\"},{\"value\":\"应收账款押金\",\"field\":\"yszkyj\"},{\"value\":\"支付方式\",\"field\":\"zffs\"},{\"value\":\"合同签订时间\",\"field\":\"htqdsj\"},{\"value\":\"合同结束时间\",\"field\":\"htjssj\"},{\"value\":\"物业\",\"field\":\"wy\"},{\"value\":\"借方\",\"field\":\"jwy\"},{\"value\":\"贷方\",\"field\":\"dwy\"},]".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;
}
///
/// 批量删除房租合同
///
/// 主键数组
///
[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);
}
}
}
///
/// 更新房租合同
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] LqMdFzhtUpInput input)
{
var entity = input.Adapt();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
///
/// 删除房租合同
///
///
[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);
}
}
}