1540 lines
67 KiB
C#
1540 lines
67 KiB
C#
using BaseOUDAL;
|
|
using MoreLinq.Extensions;
|
|
using Org.BouncyCastle.Ocsp;
|
|
using YLErp.DBModels;
|
|
using YLErp.DBModels.Consts;
|
|
using YLErp.Helpers;
|
|
using YLErp.Models;
|
|
using YLErp.Modules.EodModule.QueryModule;
|
|
using YLErp.Modules.SystemModule;
|
|
|
|
namespace YLErp.Modules.ClientModule
|
|
{
|
|
public class ClientFileService : ClientBaseService
|
|
{
|
|
readonly UploadClientFileProcessManager fileProcessManage;
|
|
|
|
public ClientFileService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
fileProcessManage ??= GetFileProcessManage();
|
|
}
|
|
|
|
public ClientFileService(ClientBaseService baseService) : base(baseService)
|
|
{
|
|
fileProcessManage ??= GetFileProcessManage();
|
|
}
|
|
|
|
private UploadClientFileProcessManager GetFileProcessManage()
|
|
{
|
|
var steps = new List<FileProcessUnit>
|
|
{
|
|
new FileProcessUnit() { statusId = 0, statusName = "新增未提交", thisUnitOpreate = SaveFile, thisUnitBack = RecoverFile },
|
|
new FileProcessUnit() { statusId = 1, statusName = "新增已提交", thisUnitOpreate = ModifyFile },
|
|
new FileProcessUnit() { statusId = 2, statusName = "修改未提交", thisUnitOpreate = ModifyFile, thisUnitBack = RecoverFile },
|
|
new FileProcessUnit() { statusId = 3, statusName = "修改已提交", thisUnitOpreate = ModifyFile },
|
|
new FileProcessUnit() { statusId = 4, statusName = "废弃未提交", thisUnitBack = RecoverFile },
|
|
new FileProcessUnit() { statusId = 5, statusName = "废弃已提交" }
|
|
};
|
|
|
|
return new UploadClientFileProcessManager(steps);
|
|
}
|
|
|
|
public string UploadFile(client_file file, string clientName = null, bool isDirectSubApp = false)
|
|
{
|
|
var ret = string.Empty;
|
|
var ClientFiles = DbContext.client_file.Where(O => O.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && O.ClientId == file.ClientId && O.IsValid);
|
|
client_file ClientFile;
|
|
|
|
fileProcessManage.File = file;
|
|
|
|
if (file.FlagStr == "FromWebAPI" && file.id > 0)
|
|
{
|
|
ClientFile = ClientFiles.Where(O => O.id == file.id).FirstOrDefault();
|
|
}
|
|
else if (string.IsNullOrWhiteSpace(file.ProtocolNumber))
|
|
{
|
|
ClientFile = ClientFiles.Where(O => O.FileName == file.FileName).FirstOrDefault();
|
|
ClientFile ??= ClientFiles.Where(O => O.FileName == (file.FileName + "-" + file.FileDesc)).FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
ClientFile = ClientFiles.Where(O => O.ProtocolNumber == file.ProtocolNumber).FirstOrDefault();
|
|
}
|
|
|
|
if (ClientFile != null)
|
|
{
|
|
if (ClientFile.FileTypeName != file.FileTypeName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(ClientFile.ProtocolNumber))
|
|
{
|
|
return " 文件" + file.FileName + "已存在,且文件类型为" + ClientFile.FileTypeName + ",文件类型无法修改。";
|
|
}
|
|
else
|
|
{
|
|
return " 协议编号" + file.ProtocolNumber + "已存在,且文件类型为" + ClientFile.FileTypeName + ",文件类型无法修改。";
|
|
}
|
|
}
|
|
|
|
ret = fileProcessManage.Excute(ConsReport.OptFlagsEnum.U, isDirectSubApp);
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(clientName))
|
|
{
|
|
clientName = DbContext.client.Where(n => n.id == file.ClientId).Select(n => n.Name).FirstOrDefault();
|
|
}
|
|
|
|
fileProcessManage.File.HasSent = CheckProtocolFileSentSatus(file, clientName);
|
|
|
|
ret = fileProcessManage.Excute(ConsReport.OptFlagsEnum.A, isDirectSubApp);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public bool CheckProtocolFileSentSatus(client_file file, string clientName)
|
|
{
|
|
var InfoTag = string.Empty;
|
|
if (file.FileTypeName == "主协议附件(PDF)")
|
|
{
|
|
InfoTag = $"A1001_{file.ProtocolNumber.Replace("_", "-")}_A";
|
|
}
|
|
else if (file.FileTypeName == "代签产品附件(PDF)")
|
|
{
|
|
InfoTag = $"A1002_{file.MainProtocolNumber.Replace("_", "-")}_{clientName.Replace("_", "-")}_A";
|
|
}
|
|
else if (file.FileTypeName == "补充协议附件(PDF)")
|
|
{
|
|
InfoTag = $"A1003_{file.MainProtocolNumber.Replace("_", "-")}_{file.ProtocolNumber.Replace("_", "-")}_A";
|
|
}
|
|
else if (file.FileTypeName == "履约协议附件(PDF)")
|
|
{
|
|
var info = DbContext.client_file.Where(O => O.ApprovalOrder < 1 && O.ProtocolNumber == file.MainProtocolNumber && O.IsValid && O.FileTypeName == "补充协议附件(PDF)").FirstOrDefault();
|
|
if (info == null)
|
|
{
|
|
return false;
|
|
}
|
|
InfoTag = $"A1008_{info.MainProtocolNumber.Replace("_", "-")}_{file.MainProtocolNumber.Replace("_", "-")}_A";
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return yldb.sac_report_notes.Any(o => o.InfoTag.Contains(InfoTag) && o.ReportResponse && o.IsValid);
|
|
}
|
|
|
|
public string RecoverFile(int clientId, string fileName, string protocol, bool isDirectSubApp)
|
|
{
|
|
client_file clientFile;
|
|
if (!string.IsNullOrWhiteSpace(protocol))
|
|
{
|
|
clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.ProtocolNumber == protocol && o.IsValid).FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.FileName == fileName && o.IsValid).FirstOrDefault();
|
|
}
|
|
if (clientFile == null)
|
|
{
|
|
return "无法回退,现记录已被删除";
|
|
}
|
|
fileProcessManage.File = clientFile;
|
|
return fileProcessManage.Recover(isDirectSubApp);
|
|
}
|
|
|
|
public string RecoverFile(client_file clientFile, bool isDirectSubApp)
|
|
{
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.D)
|
|
{
|
|
return DeabandonFile(clientFile.ClientId, clientFile.ProtocolNumber);
|
|
}
|
|
if (clientFile.HasSent)
|
|
{
|
|
return "已报送的记录无法恢复";
|
|
}
|
|
|
|
var clientFileBack = getBackRecordV2(clientFile.ClientId, clientFile.ProtocolNumber);
|
|
if (clientFileBack == null)
|
|
{
|
|
return "无法回退,不存在旧记录";
|
|
}
|
|
var SameFileDiffProto = DbContext.client_file.Where(o => o.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && o.ClientId == clientFileBack.ClientId && o.ProtocolNumber != clientFileBack.ProtocolNumber && o.FileName == clientFileBack.FileName && o.IsValid);
|
|
if (SameFileDiffProto.Count() > 0)
|
|
{
|
|
return "无法回退,有其他协议正在使用该文件/文件名";
|
|
}
|
|
|
|
var sourcePath = OtcAppContext.MapPath(clientFileBack.FilePath);
|
|
var targetPath = OtcAppContext.MapPath(clientFile.FilePath);
|
|
if (!File.Exists(sourcePath))
|
|
{
|
|
var historyFileName = sourcePath.Split('\\', '/').LastOrDefault();
|
|
sourcePath = GetPathFromElderWay(clientFile.ClientId, historyFileName, true);
|
|
if (!File.Exists(sourcePath))
|
|
{
|
|
return "无法回退,旧记录文件已被删除";
|
|
}
|
|
}
|
|
|
|
if (!resetClientProcess(clientFileBack, ApprovalOrderEnum.文件还原))
|
|
{
|
|
if (File.Exists(targetPath))
|
|
{
|
|
File.Delete(targetPath);
|
|
}
|
|
var newPath = targetPath.Replace(clientFile.FileName, clientFileBack.FileName);
|
|
var path= OperatingSystem.IsWindows()? OtcAppContext.MapPath(sourcePath):sourcePath;
|
|
if (File.Exists(path))
|
|
{
|
|
File.Move(sourcePath, newPath);
|
|
}
|
|
clientFileBack.FilePath = clientFile.FilePath.Replace(clientFile.FileName, clientFileBack.FileName);
|
|
clientFileBack.IsValid = true;
|
|
DbContext.client_file.Remove(clientFile);
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = clientFile.ClientId,
|
|
OptType = "文件还原",
|
|
Changes = string.Empty,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
}
|
|
else
|
|
{
|
|
clientFile.ApprovalOrder = (int)ApprovalOrderEnum.文件中转删除;
|
|
clientFile.SourceId = null;
|
|
clientFileBack.SourceId = clientFile.id;
|
|
if (isDirectSubApp)
|
|
{
|
|
SubmitApproval(clientFile.ClientId);
|
|
}
|
|
}
|
|
;
|
|
DbContext.SaveChanges();
|
|
return "还原成功";
|
|
}
|
|
|
|
public string RecoverFile_ApprovalOrder(client_file clientFile)
|
|
{
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.D)
|
|
{
|
|
return DeabandonFile(clientFile.ClientId, clientFile.ProtocolNumber, true);
|
|
}
|
|
if (clientFile.HasSent)
|
|
{
|
|
return "已报送的记录无法恢复";
|
|
}
|
|
|
|
var clientFileBack = getBackRecordV2(clientFile.ClientId, clientFile.ProtocolNumber);
|
|
if (clientFileBack == null)
|
|
{
|
|
return "无法回退,不存在旧记录";
|
|
}
|
|
var SameFileDiffProto = DbContext.client_file.Where(o => o.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && o.ClientId == clientFileBack.ClientId && o.ProtocolNumber != clientFileBack.ProtocolNumber && o.FileName == clientFileBack.FileName && o.IsValid);
|
|
if (SameFileDiffProto.Count() > 0)
|
|
{
|
|
return "无法回退,有其他协议正在使用该文件/文件名";
|
|
}
|
|
|
|
var sourcePath = OtcAppContext.MapPath(clientFileBack.FilePath);
|
|
var targetPath = OtcAppContext.MapPath(clientFile.FilePath);
|
|
if (!File.Exists(sourcePath))
|
|
{
|
|
var historyFileName = sourcePath.Split('\\', '/').LastOrDefault();
|
|
sourcePath = GetPathFromElderWay(clientFile.ClientId, historyFileName, true);
|
|
if (!File.Exists(sourcePath))
|
|
{
|
|
return "无法回退,旧记录文件已被删除";
|
|
}
|
|
}
|
|
clientFileBack.ApprovalOrder = 0;
|
|
if (File.Exists(targetPath))
|
|
{
|
|
File.Delete(targetPath);
|
|
}
|
|
var newPath = targetPath.Replace(clientFile.FileName, clientFileBack.FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(sourcePath)))
|
|
{
|
|
File.Move(sourcePath, newPath);
|
|
}
|
|
clientFileBack.FilePath = clientFile.FilePath.Replace(clientFile.FileName, clientFileBack.FileName);
|
|
clientFileBack.IsValid = true;
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = clientFile.ClientId,
|
|
OptType = "文件还原",
|
|
Changes = string.Empty,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
DbContext.SaveChanges();
|
|
return "还原成功";
|
|
}
|
|
|
|
public string AbandonFile(int clientId, string protocol)
|
|
{
|
|
try
|
|
{
|
|
var clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId
|
|
&& o.ProtocolNumber == protocol
|
|
&& o.OptState != ConsReport.OptFlagsEnum.D
|
|
&& o.IsValid == true).FirstOrDefault();
|
|
if (clientFile == null)
|
|
{
|
|
return "未找到文件,无法废弃";
|
|
}
|
|
fileProcessManage.File = clientFile;
|
|
return fileProcessManage.Excute(ConsReport.OptFlagsEnum.D, false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogFactory.GetLogger("废弃客户文件").Error(ex);
|
|
return "废弃文件出错";
|
|
}
|
|
}
|
|
|
|
public string DeabandonFile(int clientId, string protocol, bool isskipApp = false, bool isDirectSubApp = false)
|
|
{
|
|
try
|
|
{
|
|
var clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.ProtocolNumber == protocol && o.IsValid).FirstOrDefault();
|
|
if (clientFile.HasSent)
|
|
{
|
|
return "废弃已报送文件无法解除废弃";
|
|
}
|
|
if (!checkProtocoalState(clientId, clientFile.MainProtocolNumber))
|
|
{
|
|
return "解除废弃出错,相关联的主协议或补充协议未先解除";
|
|
}
|
|
|
|
var oldClientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.ProtocolNumber == protocol && !o.IsValid).FirstOrDefault();
|
|
|
|
if (!resetClientProcess(oldClientFile, ApprovalOrderEnum.文件解除废弃) || isskipApp)
|
|
{
|
|
if (clientFile != null)
|
|
{
|
|
clientFile.ApprovalOrder = 0;
|
|
DbContext.client_file.Remove(clientFile);
|
|
}
|
|
oldClientFile.ApprovalOrder = 0;
|
|
oldClientFile.IsValid = true;
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = clientId,
|
|
OptType = "解除废弃",
|
|
Changes = string.Empty,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
}
|
|
else
|
|
{
|
|
clientFile.ApprovalOrder = (int)ApprovalOrderEnum.文件中转删除;
|
|
clientFile.SourceId = oldClientFile.SourceId;
|
|
if (isDirectSubApp)
|
|
{
|
|
SubmitApproval(clientFile.ClientId);
|
|
}
|
|
}
|
|
|
|
DbContext.SaveChanges();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogFactory.GetLogger("解除废弃").Error(ex);
|
|
return "解除废弃出错";
|
|
}
|
|
return "废弃解除";
|
|
}
|
|
|
|
public HandleResult DeleteFile(int clientId, string fileName, string protocolNumber, bool isskipApp = false, bool isDirectSubApp = false)
|
|
{
|
|
try
|
|
{
|
|
if (!DbContext.client.Any(n => n.id == clientId))
|
|
{
|
|
return "未找到客户信息";
|
|
}
|
|
|
|
var clientFile = new client_file();
|
|
if (string.IsNullOrWhiteSpace(protocolNumber))
|
|
{
|
|
clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.FileName == fileName && o.IsValid).FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.ProtocolNumber == protocolNumber && o.IsValid).FirstOrDefault();
|
|
}
|
|
|
|
if (clientFile == null)
|
|
{
|
|
return "文件已删除";
|
|
}
|
|
|
|
if (clientFile.HasSent && !(clientFile.OptState == ConsReport.OptFlagsEnum.D))
|
|
{
|
|
return "文件已报送,无法修改";
|
|
}
|
|
|
|
if (HasSideprotocols(clientId, clientFile.ProtocolNumber, out var sideProtocolNumbers, true))
|
|
{
|
|
return "无法删除,请先废弃报送相关子协议: " + string.Join(", ", sideProtocolNumbers.Select(o => o.ProtocolNumber));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(clientFile.FilePath))
|
|
{
|
|
clientFile.FilePath = GetPathFromElderWay(clientId, clientFile.FileName);
|
|
}
|
|
|
|
var ret = "删除成功";
|
|
|
|
if (!resetClientProcess(clientFile, clientFile.OptState == ConsReport.OptFlagsEnum.D ? ApprovalOrderEnum.失效 : ApprovalOrderEnum.删除) || isskipApp)
|
|
{
|
|
clientFile.ApprovalOrder = (int)ApprovalOrderEnum.无需审批;
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.D)
|
|
{
|
|
clientFile.IsValid = false;
|
|
}
|
|
else
|
|
{
|
|
DbContext.client_file.Remove(clientFile);
|
|
}
|
|
|
|
var InvalidClientFile = new List<client_file>();
|
|
if (string.IsNullOrWhiteSpace(clientFile.ProtocolNumber))
|
|
{
|
|
InvalidClientFile = DbContext.client_file.Where(o => o.ClientId == clientId && o.FileName == clientFile.FileName && !o.IsValid).ToList();
|
|
}
|
|
else
|
|
{
|
|
InvalidClientFile = DbContext.client_file.Where(o => o.ClientId == clientId && o.ProtocolNumber == clientFile.ProtocolNumber && !o.IsValid).ToList();
|
|
}
|
|
if (InvalidClientFile != null && InvalidClientFile.Count() > 0)
|
|
{
|
|
foreach (var record in InvalidClientFile)
|
|
{
|
|
if (File.Exists(OtcAppContext.MapPath(record.FilePath)))
|
|
{
|
|
File.Delete(OtcAppContext.MapPath(record.FilePath));
|
|
}
|
|
}
|
|
DbContext.client_file.RemoveRange(InvalidClientFile);
|
|
}
|
|
|
|
var path = clientFile.FilePath;
|
|
var fullPath = OtcAppContext.MapPath(path);
|
|
if (!string.IsNullOrWhiteSpace(path) && File.Exists(fullPath))
|
|
{
|
|
File.Delete(fullPath);
|
|
}
|
|
else
|
|
{ //客户名修改的时候 文件路径改变 重新组路径看有没有
|
|
try
|
|
{
|
|
path = GetPathFromElderWay(clientFile.ClientId, clientFile.FileName);
|
|
fullPath = OtcAppContext.MapPath(path);
|
|
if (File.Exists(fullPath))
|
|
{
|
|
File.Delete(fullPath);
|
|
}
|
|
}
|
|
catch (ServiceException) { }
|
|
}
|
|
|
|
var changes = new List<List<string>>
|
|
{
|
|
new List<string>() {"FileName","文件名",clientFile.FileName,""} };
|
|
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = clientId,
|
|
OptType = "删除文件",
|
|
Changes = changes.ToJson(),
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now
|
|
});
|
|
|
|
if (clientFile.FileTypeName == "主协议附件(PDF)")//此时,主协议的子协议都为废弃已报送
|
|
{
|
|
var sideClientFiles = GetAllSideProtocols(clientId, clientFile.ProtocolNumber);
|
|
foreach (var sideClientFile in sideClientFiles)
|
|
{
|
|
sideClientFile.IsValid = false;
|
|
if (File.Exists(OtcAppContext.MapPath(sideClientFile.FilePath)))
|
|
{
|
|
File.Delete(OtcAppContext.MapPath(sideClientFile.FilePath));
|
|
}
|
|
}
|
|
ret = "已将该主协议及其相关协议(均已报送废弃)删除。";
|
|
}
|
|
}
|
|
else if (isDirectSubApp)
|
|
{
|
|
SubmitApproval(clientFile.ClientId);
|
|
}
|
|
|
|
DbContext.SaveChanges();
|
|
return new HandleResult(0, ret);
|
|
}
|
|
catch (ServiceException ex)
|
|
{
|
|
return "删除文件出错:" + ex.Message;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogFactory.GetLogger("删除客户文件").Error(ex);
|
|
return "删除文件出错:" + ex.Message;
|
|
}
|
|
}
|
|
|
|
public void ChangeFileSentStates(List<int> clientFileIds)
|
|
{
|
|
foreach (var id in clientFileIds)
|
|
{
|
|
var clientFile = DbContext.client_file.Where(o => o.id == id).FirstOrDefault();
|
|
clientFile.HasSent = true;
|
|
if (clientFile != null)
|
|
{
|
|
if (clientFile.HasSent)
|
|
{
|
|
RemoveUnreachableBackRecord(clientFile.ClientId, clientFile.ProtocolNumber);
|
|
}
|
|
|
|
}
|
|
}
|
|
DbContext.SaveChanges();
|
|
}
|
|
|
|
public void RemoveUnreachableBackRecord(int clientId, string Protocol)
|
|
{
|
|
var unreachableRecords = DbContext.client_file.Where(
|
|
o => o.ClientId == clientId && o.ProtocolNumber == Protocol
|
|
&& !o.IsValid);
|
|
//避免删除当前文件 -- 当废弃时,原纪录的文件地址不变
|
|
var NowRecord = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.ProtocolNumber == Protocol
|
|
&& o.IsValid).FirstOrDefault();
|
|
//删除历史文件
|
|
foreach (var record in unreachableRecords)
|
|
{
|
|
if (NowRecord.FilePath != record.FilePath && File.Exists(OtcAppContext.MapPath(record.FilePath)))
|
|
{
|
|
File.Delete(OtcAppContext.MapPath(record.FilePath));
|
|
}
|
|
}
|
|
DbContext.client_file.RemoveRange(unreachableRecords);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否存在关联的子级协议
|
|
/// </summary>
|
|
/// <param name="clientId"></param>
|
|
/// <param name="protocolNumber"></param>
|
|
/// <param name="sideProtocolNumbers"></param>
|
|
private bool HasSideprotocols(int clientId, string protocolNumber, out List<client_file> sideProtocolNumbers, bool expectSent = false)
|
|
{
|
|
sideProtocolNumbers = new List<client_file>();
|
|
if (string.IsNullOrWhiteSpace(protocolNumber))
|
|
{
|
|
sideProtocolNumbers = null;
|
|
return false;
|
|
}
|
|
var clientFiles = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId
|
|
&& o.MainProtocolNumber == protocolNumber
|
|
&& o.IsValid && !(o.OptState == ConsReport.OptFlagsEnum.D && o.HasSent));
|
|
if (!expectSent)
|
|
{
|
|
clientFiles = clientFiles.Where(o => o.OptState != ConsReport.OptFlagsEnum.D);
|
|
}
|
|
sideProtocolNumbers.AddRange(clientFiles);
|
|
//如果有没废弃的
|
|
return sideProtocolNumbers.Any();
|
|
}
|
|
|
|
private List<client_file> GetAllSideProtocols(int clientId, string protocolNumber)
|
|
{
|
|
var files = new List<client_file>();
|
|
if (string.IsNullOrWhiteSpace(protocolNumber))
|
|
{
|
|
return files;
|
|
}
|
|
var clientFiles = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId
|
|
&& o.MainProtocolNumber == protocolNumber
|
|
&& o.IsValid).ToList();
|
|
files.AddRange(clientFiles);
|
|
foreach (var clientFile in clientFiles)
|
|
{
|
|
files.AddRange(GetAllSideProtocols(clientFile.ClientId, clientFile.ProtocolNumber));
|
|
}
|
|
return files;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查父级是不是已经解锁废弃
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private bool checkProtocoalState(int clientId, string protocol)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(protocol))
|
|
{
|
|
return true;
|
|
}
|
|
var clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.ProtocolNumber == protocol && o.IsValid).FirstOrDefault();
|
|
|
|
if (clientFile == null)
|
|
{
|
|
var parentclientId = DbContext.client.Where(x => x.id == clientId).FirstOrDefault()?.ParentId;
|
|
clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == parentclientId && o.ProtocolNumber == protocol && o.IsValid).FirstOrDefault();
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(clientFile.MainProtocolNumber) && clientFile.OptState != ConsReport.OptFlagsEnum.D)
|
|
{
|
|
return true;
|
|
}
|
|
else if (clientFile.OptState == ConsReport.OptFlagsEnum.D && !clientFile.HasSent)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return checkProtocoalState(clientId, clientFile.MainProtocolNumber);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取上一步
|
|
/// </summary>
|
|
public client_file getBackRecordV2(int clientId, string protocolNumber)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(protocolNumber))
|
|
{
|
|
return null;
|
|
}
|
|
var clientFile = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.ProtocolNumber == protocolNumber && (!o.IsValid))
|
|
.OrderByDescending(o => o.OptDate).FirstOrDefault();
|
|
|
|
return clientFile;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 废弃产生的新的文件纪录
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <returns></returns>
|
|
private client_file setNewRecord(client_file file)
|
|
{
|
|
var abandonRecord = new client_file
|
|
{
|
|
ClientId = file.ClientId,
|
|
FileName = file.FileName,
|
|
FileDesc = file.FileDesc,
|
|
BookTime = file.BookTime,
|
|
FileTypeName = file.FileTypeName,
|
|
ProtocolNumber = file.ProtocolNumber,
|
|
MainProtocolNumber = file.MainProtocolNumber,
|
|
SignDate = file.SignDate,
|
|
SignType = file.SignType,
|
|
ReportorRole = file.ReportorRole,
|
|
OptState = ConsReport.OptFlagsEnum.D,
|
|
createDate = file.createDate,
|
|
HasSent = false,
|
|
FilePath = file.FilePath,
|
|
IsValid = true,
|
|
OptId = OptUser.UserId,
|
|
OptName = OptUser.UserName,
|
|
OptDate = DateTime.Now
|
|
};
|
|
|
|
return abandonRecord;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 兼容老版本的filePath没数据的情况
|
|
/// </summary>
|
|
/// <param name="IsSuitAble"></param>
|
|
/// <param name="intid">客户Id</param>
|
|
/// <param name="FileName"></param>
|
|
/// <returns></returns>
|
|
public string GetPathFromElderWay(int intid, string FileName, bool IsHistory = false)
|
|
{
|
|
var client = DbContext.client.Find(intid);
|
|
|
|
if (client == null)
|
|
{
|
|
throw new ServiceException("未找到客户信息");
|
|
}
|
|
if (!IsHistory)
|
|
{
|
|
//如果客户改名了,路径中没有客户名
|
|
var clientPath = $"{client.id}";
|
|
var path = $"/App_Docs/ClientFiles/{clientPath}";
|
|
var fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
path = $"/App_Docs/ClientFiles/SuitablyFiles/{clientPath}";
|
|
fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
|
|
//如果客户没改名,路径中依然存在客户名
|
|
clientPath = $"{client.id}_{client.Name}";
|
|
path = $"/App_Docs/ClientFiles/{clientPath}";
|
|
fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
path = $"/App_Docs/ClientFiles/SuitablyFiles/{clientPath}";
|
|
fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
else
|
|
{
|
|
throw new ServiceException("未找到文件");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//如果客户改名了,路径中没有客户名
|
|
var clientPath = $"{client.id}/history";
|
|
var path = $"/App_Docs/ClientFiles/{clientPath}";
|
|
var fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
path = $"/App_Docs/ClientFiles/SuitablyFiles/{clientPath}";
|
|
fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
|
|
//如果客户没改名,路径中依然存在客户名
|
|
clientPath = $"{client.id}_{client.Name}/history";
|
|
path = $"/App_Docs/ClientFiles/{clientPath}";
|
|
fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
path = $"/App_Docs/ClientFiles/SuitablyFiles/{clientPath}";
|
|
fullName = Path.Combine(path, FileName);
|
|
if (File.Exists(OtcAppContext.MapPath(fullName)))
|
|
{
|
|
return fullName;
|
|
}
|
|
else
|
|
{
|
|
throw new ServiceException("未找到文件");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查关联的交易
|
|
/// </summary>
|
|
/// <param name="protocol"></param>
|
|
/// <returns></returns>
|
|
private bool checkCantactTrades(string protocol)
|
|
{
|
|
var tradeIds = yldb.TradeMeta.Where(o => o.MetaValue == protocol && (o.MetaKey == ConsTradeMetaKey.MainProtocolCode || o.MetaKey == ConsTradeMetaKey.SupProtocolCode)).Select(o => o.TradeId);
|
|
var trades = yldb.trade.Where(o => tradeIds.Contains(o.id) && o.ValidState != "InValid" && ConsTrade.LiveTradeStatusList.Contains(o.TradeStatus));
|
|
return trades.Any();
|
|
}
|
|
|
|
public bool hasDocments(int clientId)
|
|
{
|
|
var clientFiles = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.IsValid);
|
|
return clientFiles.Any();
|
|
}
|
|
|
|
public string checkFiles(int clientId)
|
|
{
|
|
var ret = string.Empty;
|
|
var files = DbContext.client_file.Where(o => o.ApprovalOrder < 1 && o.ClientId == clientId && o.IsValid);
|
|
foreach (var file in files)
|
|
{
|
|
if (!string.IsNullOrEmpty(file.ProtocolNumber))
|
|
{
|
|
if (file.OptState == ConsReport.OptFlagsEnum.A && !file.HasSent)
|
|
{
|
|
ret += "请先删除新增未报送的协议:" + file.ProtocolNumber;
|
|
}
|
|
if (file.OptState != ConsReport.OptFlagsEnum.D && file.HasSent)
|
|
{
|
|
ret += "存在未废弃的协议文件:" + file.ProtocolNumber;
|
|
}
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
public string SaveFile(client_file file, ConsReport.OptFlagsEnum opt, bool isDirectSubApp)
|
|
{
|
|
if (opt == ConsReport.OptFlagsEnum.U)
|
|
{
|
|
return ModifyFile(file, opt, isDirectSubApp);
|
|
}
|
|
return NewFile(file, isDirectSubApp);
|
|
}
|
|
|
|
private string ModifyFile(client_file file, ConsReport.OptFlagsEnum opt, bool isDirectSubApp)
|
|
{
|
|
if (opt == ConsReport.OptFlagsEnum.U)
|
|
{
|
|
return UpdateData(file, isDirectSubApp: isDirectSubApp);
|
|
}
|
|
else
|
|
{
|
|
return AbandonFile(file, isDirectSubApp: isDirectSubApp);
|
|
}
|
|
}
|
|
|
|
private string UpdateData(client_file file, bool isskipApp = false, bool isDirectSubApp = false)
|
|
{
|
|
var protocolTypes = new List<string>() {
|
|
"主协议附件(PDF)", "补充协议附件(PDF)", "履约协议附件(PDF)", "代签产品附件(PDF)" };
|
|
var clientFile = DbContext.client_file.FirstOrDefault(x => x.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && x.ClientId == file.ClientId && x.FileName == file.FileName && x.IsValid);
|
|
if (protocolTypes.Contains(file.FileTypeName))
|
|
{
|
|
if (string.IsNullOrWhiteSpace(file.ProtocolNumber))
|
|
{
|
|
return "协议类型无协议编号,无法新增或修改";
|
|
}
|
|
clientFile = DbContext.client_file.FirstOrDefault(x => x.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && x.ClientId == file.ClientId && x.ProtocolNumber == file.ProtocolNumber && x.IsValid);
|
|
if (clientFile == null)
|
|
{
|
|
return "未知操作:存在同名文件,但协议编号不同";
|
|
}
|
|
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.D)
|
|
{
|
|
return "该协议/文件已废弃,无法修改。";
|
|
}
|
|
if (clientFile.FileTypeName != file.FileTypeName)
|
|
{
|
|
return "该协议/文件文件类型无法修改。";
|
|
}
|
|
|
|
if ((clientFile.ProtocolNumber != file.ProtocolNumber
|
|
|| clientFile.MainProtocolNumber != file.MainProtocolNumber)
|
|
&& (clientFile.OptState != ConsReport.OptFlagsEnum.A || clientFile.HasSent))
|
|
{
|
|
return "该协议/文件只能在新增未报送时修改协议编号或主协议编号。";
|
|
}
|
|
}
|
|
|
|
if (!resetClientProcess(clientFile, ApprovalOrderEnum.文件中转失效))
|
|
{
|
|
//提供恢复文件夹
|
|
var historyPath = "";
|
|
if (!clientFile.FilePath.Contains("history"))
|
|
{
|
|
historyPath = clientFile.FilePath.Replace(clientFile.FileName, @"history");
|
|
}
|
|
string movePath = Path.Combine(historyPath, clientFile.FileName + DateTime.Now.ToString("yyMMddHHmmss"));
|
|
|
|
if (File.Exists(OtcAppContext.MapPath(clientFile.FilePath)))
|
|
{
|
|
File.Move(OtcAppContext.MapPath(clientFile.FilePath), OtcAppContext.MapPath(movePath));
|
|
}
|
|
|
|
clientFile.FilePath = Path.Combine(historyPath, movePath);
|
|
clientFile.IsValid = false;
|
|
//添加新的client_file
|
|
var newfile = file.Clone();
|
|
newfile.createDate = clientFile.createDate;
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.A && !clientFile.HasSent)
|
|
{
|
|
newfile.OptState = ConsReport.OptFlagsEnum.A;
|
|
}
|
|
else
|
|
{
|
|
newfile.OptState = ConsReport.OptFlagsEnum.U;
|
|
}
|
|
newfile.id = 0;
|
|
DbContext.client_file.Add(newfile);
|
|
// 保存审核日志
|
|
var oldFile = DbContext.client_file.FirstOrDefault(a => a.id == file.id);
|
|
oldFile ??= new client_file();
|
|
var ChangesStr = DataChangeHelper.GetDataChanges(oldFile, newfile).ToJson();
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = newfile.ClientId,
|
|
OptType = "修改上传文件",
|
|
Changes = ChangesStr,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
}
|
|
else
|
|
{
|
|
//添加新的client_file
|
|
if (File.Exists(OtcAppContext.MapPath(file.FilePath)))
|
|
{
|
|
var filePath = Path.Combine(file.FilePath.Replace(file.FileName, ""), "Extend");
|
|
var localFilePath = OtcAppContext.MapPath(filePath);
|
|
Directory.CreateDirectory(localFilePath);
|
|
file.FilePath = Path.Combine(filePath, file.FileName);
|
|
}
|
|
var newfile = file.Clone();
|
|
newfile.createDate = clientFile.createDate;
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.A && !clientFile.HasSent)
|
|
{
|
|
newfile.OptState = ConsReport.OptFlagsEnum.A;
|
|
}
|
|
else
|
|
{
|
|
newfile.OptState = ConsReport.OptFlagsEnum.U;
|
|
}
|
|
newfile.id = 0;
|
|
newfile.ApprovalOrder = (int)ApprovalOrderEnum.文件修改;
|
|
newfile.SourceId = clientFile.id;
|
|
DbContext.client_file.Add(newfile);
|
|
if (isDirectSubApp)
|
|
{
|
|
SubmitApproval(clientFile.ClientId);
|
|
}
|
|
}
|
|
|
|
DbContext.SaveChanges();
|
|
return "修改成功";
|
|
}
|
|
|
|
public string UpdateData_ApprovalOrder(client_file file)
|
|
{
|
|
var protocolTypes = new List<string>() {
|
|
"主协议附件(PDF)", "补充协议附件(PDF)", "履约协议附件(PDF)", "代签产品附件(PDF)" };
|
|
var clientFile = DbContext.client_file.FirstOrDefault(x => x.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && x.id == file.SourceId);
|
|
if (protocolTypes.Contains(file.FileTypeName))
|
|
{
|
|
if (string.IsNullOrWhiteSpace(file.ProtocolNumber))
|
|
{
|
|
return "协议类型无协议编号,无法新增或修改";
|
|
}
|
|
clientFile = DbContext.client_file.FirstOrDefault(x => x.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && x.id == file.SourceId && x.ProtocolNumber == file.ProtocolNumber);
|
|
if (clientFile == null)
|
|
{
|
|
return "未知操作:存在同名文件,但协议编号不同";
|
|
}
|
|
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.D)
|
|
{
|
|
return "该协议/文件已废弃,无法修改。";
|
|
}
|
|
if (clientFile.FileTypeName != file.FileTypeName)
|
|
{
|
|
return "该协议/文件文件类型无法修改。";
|
|
}
|
|
|
|
if ((clientFile.ProtocolNumber != file.ProtocolNumber
|
|
|| clientFile.MainProtocolNumber != file.MainProtocolNumber)
|
|
&& (clientFile.OptState != ConsReport.OptFlagsEnum.A || clientFile.HasSent))
|
|
{
|
|
return "该协议/文件只能在新增未报送时修改协议编号或主协议编号。";
|
|
}
|
|
}
|
|
//提供恢复文件夹
|
|
var historyPath = "";
|
|
if (!clientFile.FilePath.Contains("history"))
|
|
{
|
|
historyPath = clientFile.FilePath.Replace(clientFile.FileName, @"history");
|
|
}
|
|
string movePath = Path.Combine(historyPath, clientFile.FileName + DateTime.Now.ToString("yyMMddHHmmss"));
|
|
|
|
if (File.Exists(OtcAppContext.MapPath(clientFile.FilePath)))
|
|
{
|
|
File.Move(OtcAppContext.MapPath(clientFile.FilePath), OtcAppContext.MapPath(movePath));
|
|
}
|
|
|
|
clientFile.FilePath = Path.Combine(historyPath, movePath);
|
|
clientFile.IsValid = false;
|
|
clientFile.ApprovalOrder = (int)ApprovalOrderEnum.无需审批;
|
|
|
|
//添加新的client_file
|
|
var newfile = DbContext.client_file.Find(file.id);
|
|
if (newfile != null)
|
|
{
|
|
newfile.ApprovalOrder = (int)ApprovalOrderEnum.无需审批;
|
|
newfile.createDate = clientFile.createDate;
|
|
if (clientFile.OptState == ConsReport.OptFlagsEnum.A && !clientFile.HasSent)
|
|
{
|
|
newfile.OptState = ConsReport.OptFlagsEnum.A;
|
|
}
|
|
else
|
|
{
|
|
newfile.OptState = ConsReport.OptFlagsEnum.U;
|
|
}
|
|
if (newfile.FilePath.Contains("Extend"))
|
|
{
|
|
var filePath = Path.Combine(newfile.FilePath.Replace("/Extend", ""));
|
|
File.Move(OtcAppContext.MapPath(newfile.FilePath), OtcAppContext.MapPath(filePath));
|
|
newfile.FilePath = filePath;
|
|
}
|
|
// 保存审核日志
|
|
var oldFile = DbContext.client_file.FirstOrDefault(a => a.id == clientFile.id);
|
|
oldFile ??= new client_file();
|
|
var ChangesStr = DataChangeHelper.GetDataChanges(oldFile, newfile).ToJson();
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = file.ClientId,
|
|
OptType = "修改上传文件",
|
|
Changes = ChangesStr,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
}
|
|
DbContext.SaveChanges();
|
|
return "修改成功";
|
|
}
|
|
|
|
public string AbandonFile(client_file file, bool isskipApp = false, bool isDirectSubApp = false)
|
|
{
|
|
if (HasSideprotocols(file.ClientId, file.ProtocolNumber, out var sideProtocolNumbers))
|
|
{
|
|
return "无法废弃,请先废弃相关子协议:" + string.Join(",", sideProtocolNumbers.Select(o => o.ProtocolNumber));
|
|
}
|
|
if (checkCantactTrades(file.ProtocolNumber))
|
|
{
|
|
return "无法废弃,存在关联的交易,请先了结关联交易。";
|
|
}
|
|
if (file.OptState == ConsReport.OptFlagsEnum.A && !file.HasSent)
|
|
{
|
|
return "无法废弃未报送的新增文件,该文件可直接删除";
|
|
}
|
|
|
|
if (!resetClientProcess(file, ApprovalOrderEnum.文件废弃) || isskipApp)
|
|
{
|
|
file.ApprovalOrder = (int)ApprovalOrderEnum.无需审批;
|
|
file.IsValid = false;
|
|
var abandonRecord = setNewRecord(file);
|
|
DbContext.client_file.Add(abandonRecord);
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = file.ClientId,
|
|
OptType = "文件废弃",
|
|
Changes = string.Empty,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
}
|
|
else if (isDirectSubApp)
|
|
{
|
|
SubmitApproval(file.ClientId);
|
|
}
|
|
|
|
DbContext.SaveChanges();
|
|
return "文件已废弃";
|
|
}
|
|
|
|
public string NewFile(client_file file, bool isDirectSubApp)
|
|
{
|
|
file.createDate = DateTime.Now;
|
|
file.OptState = ConsReport.OptFlagsEnum.A;
|
|
DbContext.client_file.Add(file);
|
|
var oldFile = DbContext.client_file.FirstOrDefault(a => a.id == file.id);
|
|
oldFile ??= new client_file();
|
|
if (!resetClientProcess(file, ApprovalOrderEnum.新增))
|
|
{
|
|
file.ApprovalOrder = (int)ApprovalOrderEnum.无需审批;
|
|
var ChangesStr = DataChangeHelper.GetDataChanges(oldFile, file).ToJson();
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = file.ClientId,
|
|
OptType = "上传文件",
|
|
Changes = ChangesStr,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
}
|
|
else if (isDirectSubApp)
|
|
{
|
|
SubmitApproval(file.ClientId);
|
|
}
|
|
DbContext.SaveChanges();
|
|
return string.Empty;
|
|
}
|
|
|
|
public string NewFile_ApprovalOrde(client_file file)
|
|
{
|
|
file.createDate = DateTime.Now;
|
|
file.OptState = ConsReport.OptFlagsEnum.A;
|
|
var oldFile = new client_file();
|
|
file.ApprovalOrder = (int)ApprovalOrderEnum.无需审批;
|
|
var ChangesStr = DataChangeHelper.GetDataChanges(oldFile, file).ToJson();
|
|
DbContext.ClientAuditLog.Add(new ClientAuditLog
|
|
{
|
|
ClientId = file.ClientId,
|
|
OptType = "上传文件",
|
|
Changes = ChangesStr,
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now,
|
|
});
|
|
DbContext.SaveChanges();
|
|
return string.Empty;
|
|
}
|
|
|
|
public bool resetClientProcess(client_file file, ApprovalOrderEnum orderEnum)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(file.ProtocolNumber) && file.ApprovalOrder == (int)ApprovalOrderEnum.无需审批 && !PS.Config.Is浙期)
|
|
{
|
|
return false;
|
|
}
|
|
var clientCount = yldb.approvalprocess.Where(x => x.processType == "ClientProcess").Count();
|
|
var client = DbContext.client.Find(file.ClientId);
|
|
if (clientCount > 0 && (client.ProcessStatus == "已开户" || client.ApprovalStatus == "审批中"))
|
|
{
|
|
file.ApprovalOrder = (int)orderEnum;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void SubmitApproval(int clientid)
|
|
{
|
|
var processlist = yldb.approvalprocess.Where(x => x.processType == "ClientProcess");
|
|
if (processlist.Count() > 0)
|
|
{
|
|
var client = DbContext.client.Find(clientid);
|
|
var groupId = UserBLL.GetApprovalProcessGroup(UserId);
|
|
bool approvalBranch = processlist.Any(x => x.approvalGroupId != 0);//审批流程有分支情况
|
|
client.ProcessOrderBranch = 0;
|
|
client.ApprovalOrderId = ProcessTradeLog.审批中;
|
|
client.ApprovalStatus = "审批中";
|
|
client.ApprovalOptDate = DateTime.Now;
|
|
|
|
if (approvalBranch)//审批流程有分支情况
|
|
{
|
|
//该审批组属于流程的审批组或该审批组不属于流程审批组
|
|
var tradeProessBranchQuery = processlist.FirstOrDefault(x => (x.approvalGroupId == groupId && x.approvalCondition == 1) || (x.approvalGroupId != groupId && x.approvalCondition == 2));
|
|
if (tradeProessBranchQuery == null)//都没有情况
|
|
{
|
|
throw new ServiceException("当前存在分支开户审批流程,检测到申请人不满足任意一个分支进入条件,无法提交审批,请更换申请人或调整审批组");
|
|
}
|
|
client.ApprovalOrderId = tradeProessBranchQuery.order > 1 ? 1 : tradeProessBranchQuery.order + 1;//第一个节点是分支需要跳过审批组那一级
|
|
client.ProcessOrderBranch = tradeProessBranchQuery.node ?? 1;//审批组分支
|
|
}
|
|
|
|
var range = DbContext.client_file_audit.Where(x => x.ClientId == clientid).ToList();
|
|
DbContext.client_file_audit.RemoveRange(range);
|
|
|
|
var clientAuditlog = new ClientAuditLog
|
|
{
|
|
ClientId = client.id,
|
|
OptType = "提交信息变更审批",
|
|
DataType = "00",
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptDate = DateTime.Now
|
|
};
|
|
|
|
///日志记录
|
|
DbContext.ClientAuditLog.Add(clientAuditlog);
|
|
DbContext.SaveChanges();
|
|
client.ApprovalLogId = clientAuditlog.id;
|
|
DbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存客户文件(迁移自:ClientController.UploadFiles)
|
|
/// </summary>
|
|
public string SaveClientFiles(client_file req, string sideProtocolNumber, List<UploadFileModel> uploadFiles, bool isDirectSubApp = false)
|
|
{
|
|
var client = DbContext.client.Where(n => n.id == req.ClientId).Select(n => new { n.id, n.Name }).FirstOrDefault();
|
|
|
|
if (client == null)
|
|
{
|
|
throw new ServiceException("未找到客户信息,请先保存客户后在上传文件");
|
|
}
|
|
|
|
req.ClientId = client.id;
|
|
req.OptId = UserId;
|
|
req.OptName = UserName;
|
|
//req.OptDate是用户手动填写的
|
|
req.HasSent = false;
|
|
req.IsValid = true;
|
|
|
|
req.FileDesc ??= string.Empty;
|
|
|
|
SaveClientFiles_Check(req, uploadFiles, sideProtocolNumber);
|
|
|
|
var result = new List<string>();
|
|
int successCount = 0, failCount = 0;
|
|
var clientPath = client.id.ToString();
|
|
|
|
foreach (var file in uploadFiles)
|
|
{
|
|
var webFolder = req.FileTypeName == "适当性文件" ? $"~/App_Docs/ClientFiles/SuitablyFiles/{clientPath}" : $"~/App_Docs/ClientFiles/{clientPath}";
|
|
var localFolder = OtcAppContext.MapPath(webFolder);
|
|
Directory.CreateDirectory(localFolder);
|
|
|
|
var clientFile = req.Clone();
|
|
|
|
clientFile.FileName = file.FileName;
|
|
clientFile.FilePath = string.Concat(webFolder.TrimStart('~'), "/", file.FileName);
|
|
|
|
// 提供恢复文件夹
|
|
var historyPath = clientFile.FilePath.Replace(clientFile.FileName, @"history");
|
|
if (!Directory.Exists(OtcAppContext.MapPath(historyPath)))
|
|
{
|
|
Directory.CreateDirectory(OtcAppContext.MapPath(historyPath));
|
|
}
|
|
var ret = UploadFile(clientFile, client.Name, isDirectSubApp);
|
|
using (var openStream = file.OpenReadStream())
|
|
{
|
|
var localFilePath = OtcAppContext.MapPath(clientFile.FilePath);
|
|
using var fstream = File.OpenWrite(localFilePath);
|
|
openStream.CopyTo(fstream);
|
|
}
|
|
if (string.IsNullOrWhiteSpace(ret) || ret == "修改成功")
|
|
{
|
|
LogFactory.GetLogger("上传客户文件").Info($"客户{client.Name}(id:{req.ClientId})上传文件({file.FileName}),真实上传时间:{DateTime.Now},客户选择的日期:{req.OptDate}");
|
|
|
|
if (ret == "修改成功")
|
|
{
|
|
result.Add($"{clientFile.ProtocolNumber}文件已修改,文件{clientFile.FileName}已覆盖上传");
|
|
}
|
|
else
|
|
{
|
|
result.Add($"文件{clientFile.FileName}上传成功: {ret}");
|
|
}
|
|
|
|
successCount++;
|
|
}
|
|
else
|
|
{
|
|
failCount++;
|
|
|
|
result.Add($"文件{clientFile.FileName}上传失败: {ret}");
|
|
}
|
|
}
|
|
return failCount > 0 ? string.Join(";", result) : null;
|
|
}
|
|
|
|
private void SaveClientFiles_Check(client_file req, List<UploadFileModel> files, string sideProtocolNumber)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(req.FileTypeName))
|
|
{
|
|
throw new ServiceException("文件类型 必须填写");
|
|
}
|
|
var filetypes = BLL.DictionaryBLL.GetList("文件类型", false);
|
|
if (!filetypes.Exists(g => g.Value == req.FileTypeName))
|
|
{
|
|
throw new ServiceException($"[{req.FileTypeName}]文件类型不正确");
|
|
}
|
|
|
|
if ((req.FileTypeName == "双录视频文件") && (req.BookTime == null || req.BookTime == DateTime.MinValue))
|
|
{
|
|
throw new ServiceException("双录时间 必须填写");
|
|
}
|
|
|
|
if (req.OptDate == null || req.OptDate == DateTime.MinValue)
|
|
{
|
|
throw new ServiceException("操作时间 必须填写");
|
|
}
|
|
|
|
if (req.FileDesc != null && req.FileDesc.Contains('-'))
|
|
{
|
|
throw new ServiceException("文件说明中请不要使用字符‘-’");
|
|
}
|
|
|
|
var list = new List<string>() { "主协议附件(PDF)", "补充协议附件(PDF)", "履约协议附件(PDF)", "代签产品附件(PDF)" };
|
|
|
|
if (list.Contains(req.FileTypeName))
|
|
{
|
|
if (files.Count > 1)
|
|
{
|
|
throw new ServiceException("主协议附件,补充协议附件,履约预付金附件,代签产品附件 只能上传单个文件");
|
|
}
|
|
|
|
if (files[0].Length > SuperviseReportModule.SAC.Model.ReportStatusModel.MaxAnnexLength)
|
|
{
|
|
throw new ServiceException("主协议附件,补充协议附件,履约预付金附件,代签产品附件 单个附件大小不可以超过30M");
|
|
}
|
|
|
|
if (PS.Config.ErpElement.SecuritiesEnvironment && files[0].ContentType != "application/pdf")
|
|
{
|
|
throw new ServiceException("主协议附件,补充协议附件,履约预付金附件,代签产品附件 只能上传pdf文档");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(req.ProtocolNumber))
|
|
{
|
|
if (PS.Config.Company == Configuration.CompanyEnum.华西)
|
|
{
|
|
SaveClientFileNumber(req);
|
|
}
|
|
else
|
|
{
|
|
throw new ServiceException("协议编号未填写");
|
|
}
|
|
}
|
|
|
|
if (req.SignDate == null || req.SignDate.Value == DateTime.MinValue)
|
|
{
|
|
throw new ServiceException("签署日期未填写");
|
|
}
|
|
|
|
switch (req.FileTypeName)
|
|
{
|
|
case "主协议附件(PDF)":
|
|
if (string.IsNullOrWhiteSpace(req.SignType))
|
|
{
|
|
throw new ServiceException("主协议附件签署版本未填写");
|
|
}
|
|
var signTypes = BLL.DictionaryBLL.GetList("权益类签署版本", false);
|
|
if (!signTypes.Exists(g => g.Value == req.SignType))
|
|
{
|
|
throw new ServiceException($"[{req.SignType}]签署版本不正确");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(req.ReportorRole))
|
|
{
|
|
throw new ServiceException("主协议附件填报方角色未填写");
|
|
}
|
|
if (req.ReportorRole != "甲方" && req.ReportorRole != "乙方")
|
|
{
|
|
throw new ServiceException("主协议附件填报方角色填写不正确");
|
|
}
|
|
break;
|
|
case "履约协议附件(PDF)":
|
|
if (string.IsNullOrWhiteSpace(sideProtocolNumber))
|
|
{
|
|
throw new ServiceException("履约协议附件补充协议编号未填写");
|
|
}
|
|
req.MainProtocolNumber = sideProtocolNumber;
|
|
break;
|
|
case "补充协议附件(PDF)":
|
|
case "代签产品附件(PDF)":
|
|
if (string.IsNullOrWhiteSpace(req.MainProtocolNumber))
|
|
{
|
|
throw new ServiceException("主协议编号未填写");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(req.ProtocolNumber))
|
|
{
|
|
if (DbContext.client_file.Any(o => o.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && o.ClientId != req.ClientId && o.IsValid && o.ProtocolNumber == req.ProtocolNumber))
|
|
{
|
|
throw new ServiceException("其他客户存在的文件存在相同的协议编号,请修改协议编号后上传。");
|
|
}
|
|
|
|
if (DbContext.client_file.Any(o => o.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && o.ClientId == req.ClientId && !o.IsValid && o.ProtocolNumber == req.ProtocolNumber && o.OptState == ConsReport.OptFlagsEnum.D))
|
|
{
|
|
throw new ServiceException("该协议编号已存在,且已废弃删除,请修改协议编号后上传。");
|
|
}
|
|
|
|
if (req.FileTypeName == "代签产品附件(PDF)" && DbContext.client_file.Any(o => o.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && o.ClientId == req.ClientId && o.MainProtocolNumber == req.MainProtocolNumber && o.ProtocolNumber != req.ProtocolNumber && o.FileTypeName == "代签产品附件(PDF)" && o.IsValid))
|
|
{
|
|
throw new ServiceException("该主协议在该产品下已存在代签产品附件,请先删除或废弃。");
|
|
}
|
|
}
|
|
|
|
foreach (var file in files)
|
|
{
|
|
var name = file.FileName;
|
|
var oldName = $"{req.FileDesc}-{file.FileName}";
|
|
if (DbContext.client_file.Any(o => o.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && o.ClientId == req.ClientId && (o.FileName == oldName || o.FileName == name) && o.FileTypeName != req.FileTypeName))
|
|
{
|
|
throw new ServiceException("在该客户上传的文件中" + file.FileName + "属于其他文件类型,请确认!");
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(req.ProtocolNumber) && PS.Config.ErpElement.SecuritiesEnvironment)
|
|
{
|
|
var exists = DbContext.client_file
|
|
.Where(o => o.ClientId != req.ClientId && o.ProtocolNumber != null && o.ProtocolNumber != "" && o.IsValid)
|
|
.Where(o => o.FileName == oldName || o.FileName == file.FileName).Any();
|
|
|
|
if (exists)
|
|
{
|
|
throw new ServiceException("其他客户有协议使用的文件与该文件重名,请修改文件名后上传。");
|
|
}
|
|
|
|
if (DbContext.client_file.Any(O => O.ApprovalOrder != (int)ApprovalOrderEnum.审批作废 && (O.FileName == file.FileName || O.FileName == oldName) && O.ProtocolNumber != null && O.ProtocolNumber != "" && O.ProtocolNumber != req.ProtocolNumber && O.IsValid))
|
|
{
|
|
throw new ServiceException("该文件已属于其他协议,请上传未使用的文件。");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SaveClientFileNumber(client_file req)
|
|
{
|
|
const string SACM = "SACM-"; //主协议
|
|
const string SACS = "SACS-"; //补充协议
|
|
const string SACG = "SACG-"; //履保协议
|
|
const string company = "华西证券-";
|
|
var year = req.OptDate == null ? DateTime.Now.ToString("yyyy") : req.OptDate?.ToString("yyyy") + "-";
|
|
|
|
if (req.FileTypeName == ConsGlobal.ClientFileType.MainProtocol)
|
|
{
|
|
if (req.ParentClientFileId != 0)
|
|
{
|
|
var clientFileParent = DbContext.client_file.First(x => x.id == req.ParentClientFileId);
|
|
var existingParentProtocolNumbers = DbContext.client_file.Where(x => x.ApprovalOrder < 1 && x.ProtocolNumber.StartsWith(clientFileParent.ProtocolNumber)).Select(x => x.ProtocolNumber).ToArray();
|
|
var maxProtocolNumber = 0;
|
|
if (existingParentProtocolNumbers.Any())
|
|
{
|
|
maxProtocolNumber = existingParentProtocolNumbers.Max(x => int.TryParse(x.Substring(clientFileParent.ProtocolNumber.Length).Length > 0 ? x.Substring(clientFileParent.ProtocolNumber.Length + 1) : x.Substring(clientFileParent.ProtocolNumber.Length), out var number) ? number : 0);
|
|
}
|
|
req.ProtocolNumber = clientFileParent.ProtocolNumber + "-" + (maxProtocolNumber + 1).ToString();
|
|
}
|
|
else
|
|
{
|
|
var leftPart = SACM + company + year;
|
|
var leftPartSize = leftPart.Length;
|
|
var existingNumbers = DbContext.client_file.Where(x => x.ApprovalOrder < 1 && x.ProtocolNumber.StartsWith(leftPart)).Select(x => x.ProtocolNumber).ToArray();
|
|
var maxNumber = 0;
|
|
if (existingNumbers.Any())
|
|
{
|
|
maxNumber = existingNumbers.Max(x => int.TryParse(x.Substring(leftPartSize), out var number) ? number : 0);
|
|
}
|
|
req.ProtocolNumber = leftPart + (maxNumber + 1).ToString().PadLeft(3, '0');
|
|
}
|
|
}
|
|
|
|
if (req.FileTypeName == ConsGlobal.ClientFileType.EnhanceProtocol)
|
|
{
|
|
var leftPart = SACS + company + year;
|
|
var leftPartSize = leftPart.Length;
|
|
var existingNumbers = DbContext.client_file.Where(x => x.ApprovalOrder < 1 && x.ProtocolNumber.StartsWith(leftPart)).Select(x => x.ProtocolNumber).ToArray();
|
|
var maxNumber = 0;
|
|
if (existingNumbers.Any())
|
|
{
|
|
maxNumber = existingNumbers.Max(x => int.TryParse(x.Substring(leftPartSize), out var number) ? number : 0);
|
|
}
|
|
req.ProtocolNumber = leftPart + (maxNumber + 1).ToString().PadLeft(3, '0');
|
|
}
|
|
|
|
if (req.FileTypeName == ConsGlobal.ClientFileType.DateMargin)
|
|
{
|
|
var leftPart = SACG + company + year;
|
|
var leftPartSize = leftPart.Length;
|
|
var existingNumbers = DbContext.client_file.Where(x => x.ApprovalOrder < 1 && x.ProtocolNumber.StartsWith(leftPart)).Select(x => x.ProtocolNumber).ToArray();
|
|
var maxNumber = 0;
|
|
if (existingNumbers.Any())
|
|
{
|
|
maxNumber = existingNumbers.Max(x => int.TryParse(x.Substring(leftPartSize), out var number) ? number : 0);
|
|
}
|
|
req.ProtocolNumber = leftPart + (maxNumber + 1).ToString().PadLeft(3, '0');
|
|
}
|
|
}
|
|
}
|
|
|
|
public class UploadClientFileProcessManager
|
|
{
|
|
private client_file _file;
|
|
public client_file File
|
|
{
|
|
get { return _file; }
|
|
set
|
|
{
|
|
_file = value;
|
|
getStatus();
|
|
}
|
|
}
|
|
|
|
private void getStatus()
|
|
{
|
|
var statusId = 0;
|
|
if (_file != null)
|
|
{
|
|
switch (_file.OptState)
|
|
{
|
|
case ConsReport.OptFlagsEnum.A:
|
|
statusId = 0;
|
|
break;
|
|
case ConsReport.OptFlagsEnum.U:
|
|
statusId = 2;
|
|
break;
|
|
case ConsReport.OptFlagsEnum.D:
|
|
statusId = 4;
|
|
break;
|
|
}
|
|
if (_file.HasSent)
|
|
{
|
|
statusId += 1;
|
|
}
|
|
}
|
|
NowUnit = Steps.Where(o => o.statusId == statusId).FirstOrDefault();
|
|
}
|
|
|
|
private readonly List<FileProcessUnit> Steps = new();
|
|
|
|
private FileProcessUnit NowUnit;
|
|
|
|
public UploadClientFileProcessManager(List<FileProcessUnit> steps)
|
|
{
|
|
Steps = steps;
|
|
NowUnit = Steps.FirstOrDefault();
|
|
}
|
|
|
|
public UploadClientFileProcessManager(List<FileProcessUnit> steps, string statusName)
|
|
{
|
|
Steps = steps;
|
|
NowUnit = Steps.Where(o => o.statusName == statusName).FirstOrDefault();
|
|
}
|
|
|
|
public string Excute(ConsReport.OptFlagsEnum opt, bool isDirectSubApp)
|
|
{
|
|
var ret = string.Empty;
|
|
if (NowUnit.thisUnitOpreate != null && _file != null)
|
|
{
|
|
ret = NowUnit.thisUnitOpreate(_file, opt, isDirectSubApp);
|
|
}
|
|
//NextTo(Steps.Where(o => o.statusName == "修改未提交").FirstOrDefault());
|
|
getStatus();
|
|
return ret;
|
|
}
|
|
|
|
public string Recover(bool isDirectSubApp)
|
|
{
|
|
var ret = string.Empty;
|
|
if (NowUnit.thisUnitBack != null && _file != null)
|
|
{
|
|
ret = NowUnit.thisUnitBack(_file, isDirectSubApp);
|
|
}
|
|
//BackTo(new FileProcessUnit());
|
|
getStatus();
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
public class FileProcessUnit
|
|
{
|
|
public delegate string ThisUnitOpreateHandle(client_file file, ConsReport.OptFlagsEnum opt, bool isDirectSubApp); // 新增/修改 删除/废弃
|
|
public delegate string ThisUnitBackHandle(client_file file, bool isDirectSubApp); // 回滚
|
|
|
|
public string statusName;
|
|
public int statusId;
|
|
public ThisUnitOpreateHandle thisUnitOpreate;
|
|
public ThisUnitBackHandle thisUnitBack;
|
|
}
|
|
} |