372 lines
16 KiB
C#
372 lines
16 KiB
C#
using YLErp.Commons;
|
||
using YLErp.Modules.ClientModule;
|
||
|
||
namespace YLErp.Web.Controllers
|
||
{
|
||
public class Client_RatingController : BaseController
|
||
{
|
||
[MyAuthorize("客户管理-资信等级有效期")]
|
||
public ActionResult List()
|
||
{
|
||
ViewBag.SystemDate = valuedateBLL.ValueDate;
|
||
return View();
|
||
}
|
||
|
||
[HttpPost]
|
||
public JsonResult Query(ClientRatingReq req)
|
||
{
|
||
var bll = new ClientRatingService(CurUser);
|
||
req.DbClientIds = CurUser.GetClientIdsByCustomerManager();
|
||
var sList = bll.SearchList(req);
|
||
return Json(sList);
|
||
}
|
||
|
||
[HttpPost]
|
||
public ActionResult SubmitProcess(int Client_RatingId)
|
||
{
|
||
try
|
||
{
|
||
var Client_Rating = clientDB.Client_Rating.Where(s => !s.IsDeleted).FirstOrDefault(s => s.id == Client_RatingId);
|
||
if (Client_Rating == null)
|
||
{
|
||
return JsonError("请先保存评级基本信息");
|
||
}
|
||
|
||
var processList = yldb.approvalprocess.Where(s => s.processType == "CreditProcess").ToList();//判断有没有设置评级审批流程
|
||
//未设置流程 直接设置成评级状态
|
||
if (processList.Count == 0)
|
||
{
|
||
Client_Rating.ProcessStatus = "已审批";
|
||
Client_Rating.ProcessOrderId = -2;
|
||
Client_Rating.ProcessOptDate = DateTime.Now;
|
||
ClientRating(Client_RatingId, "提交审批", "未设置审批流程,直接通过审批");
|
||
clientDB.SaveChanges();
|
||
return JsonSuccess("已审批");
|
||
}
|
||
if (Client_Rating.ProcessStatus == "已审批")
|
||
{
|
||
return JsonSuccess("已审批,无需再次提交");
|
||
}
|
||
|
||
if (Client_Rating.ProcessStatus == "审批中")
|
||
{
|
||
return JsonSuccess("正在审批中");
|
||
}
|
||
if (Client_Rating.ProcessStatus == "已拒绝")
|
||
{
|
||
Client_Rating.ProcessStatus = "审批中";
|
||
Client_Rating.ProcessOrderId = 1;
|
||
Client_Rating.ProcessOptDate = DateTime.Now;
|
||
ClientRating(Client_RatingId, "提交审批");
|
||
clientDB.SaveChanges();
|
||
return JsonSuccess("已重新提交评级审批");
|
||
}
|
||
|
||
var orderId = Client_Rating.ProcessOrderId;
|
||
Client_Rating.ProcessStatus = "审批中";
|
||
Client_Rating.ProcessOrderId = 1;
|
||
Client_Rating.ProcessOptDate = DateTime.Now;
|
||
clientDB.SaveChanges();
|
||
ClientRating(Client_RatingId, "提交审批");
|
||
return JsonSuccess("已提交,正在审批中");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogFactory.GetLogger<Client_RatingController>().Error("提交评级审批", ex);
|
||
return JsonError("异常");
|
||
}
|
||
}
|
||
|
||
public ActionResult Detail(string enid)
|
||
{
|
||
var intid = DataProtectHelper.DecryptInt(enid);
|
||
var r = clientDB.Client_Rating.Where(s => !s.IsDeleted).FirstOrDefault(s => s.id == intid);
|
||
return View(r);
|
||
}
|
||
|
||
public ActionResult Edit(string enid)
|
||
{
|
||
var r = new ClientRating();
|
||
var isAdd = string.IsNullOrEmpty(enid) || enid == "0";
|
||
ViewBag.IsAdd = isAdd;
|
||
if (isAdd)
|
||
{
|
||
return View(r);
|
||
}
|
||
|
||
var intid = DataProtectHelper.DecryptInt(enid);
|
||
r = clientDB.Client_Rating.Where(s => !s.IsDeleted).FirstOrDefault(s => s.id == intid);
|
||
|
||
ViewBag.CanEditDateRange = !clientDB.Client_Rating.Where(s => !s.IsDeleted).Any(s => s.ClientId == r.ClientId && s.id != intid);
|
||
|
||
return View(r);
|
||
}
|
||
public ActionResult ClientRatingLogList(int id)
|
||
{
|
||
var dates = clientDB.clientrating_log.Where(c => c.RatingId == id)
|
||
.Select(n => new ClientRatingLogDto
|
||
{
|
||
id = n.id,
|
||
OptType = n.OptType,
|
||
Changes = n.Changes ?? "",
|
||
OptName = n.OptName,
|
||
OptDate = n.OptDate
|
||
}).OrderByDescending(d => d.id).ToArray();
|
||
return View(dates);
|
||
}
|
||
[HttpPost]
|
||
public async Task<ActionResult> Edit(ClientRatingReq req)
|
||
{
|
||
var id = 0;
|
||
if (!string.IsNullOrEmpty(req.EncryptId))
|
||
{
|
||
id = DataProtectHelper.DecryptInt(req.EncryptId);
|
||
}
|
||
|
||
var r = await Save(id, req);
|
||
return Redirect("~/Client_Rating/Detail/?id=" + r.Content.EncryptId);
|
||
}
|
||
|
||
|
||
[HttpPost]
|
||
public async Task<JsonResult> EditJson(ClientRatingReq req)
|
||
{
|
||
var id = 0;
|
||
if (!string.IsNullOrEmpty(req.EncryptId))
|
||
{
|
||
id = DataProtectHelper.DecryptInt(req.EncryptId);
|
||
}
|
||
|
||
try
|
||
{
|
||
var collection = Request.Form;
|
||
int.TryParse(collection["ClientId"].ToString() ?? "", out var _cid);
|
||
int.TryParse(collection["id"].ToString() ?? "", out var _crid);
|
||
DateTime.TryParse(collection["RatingStartDate"].ToString() ?? "", out var _ratingStartDate);
|
||
DateTime.TryParse(collection["RatingDeadLine"].ToString() ?? "", out var _ratingDeadLine);
|
||
req.id = _crid;
|
||
req.ClientId = _cid;
|
||
req.Comments = collection["Comments"].ToString() ?? "";
|
||
req.RatingStartDate = _ratingStartDate;
|
||
req.RatingDeadLine = _ratingDeadLine;
|
||
req.RatingAccording = collection["RatingAccording"].ToString() == "" ? null : collection["RatingAccording"].ToString();
|
||
req.RatingPersonnel = collection["RatingPersonnel"].ToString() == "" ? null : collection["RatingPersonnel"].ToString();
|
||
req.CheckPersonnel = collection["CheckPersonnel"].ToString() == "" ? null : collection["CheckPersonnel"].ToString();
|
||
//if (clientDB.Client_Rating.Any(x => !x.IsDeleted && x.ClientId == _cid && x.id!=_crid && (x.ProcessStatus == "审批中" || x.ProcessStatus == "未审批" || x.ProcessStatus == null)))
|
||
//{
|
||
// return JsonError("该客户已存在未审批的记录,请先处理。");
|
||
//}
|
||
|
||
var returnInfo = await Save(id, req);
|
||
if (!returnInfo.Success)
|
||
{
|
||
return JsonError(returnInfo.Msg);
|
||
}
|
||
return JsonSuccess("更新成功", returnInfo.Content);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogFactory.GetLogger<Client_RatingController>().Error("EditJson", ex);
|
||
return JsonError("更新失败");
|
||
}
|
||
}
|
||
|
||
public JsonResult IsUnderDeadLine(ClientRatingReq req)
|
||
{
|
||
var isAddNew = req.id == null || req.id == 0;
|
||
|
||
ClientRating Client_Rating;
|
||
if (isAddNew)
|
||
{
|
||
Client_Rating = new ClientRating
|
||
{
|
||
CreditRatingId = req.CreditRatingId,
|
||
CreditRatingStr = req.CreditRatingStr,
|
||
ClientId = req.ClientId,
|
||
RatingDeadLine = req.RatingDeadLine
|
||
};
|
||
}
|
||
else
|
||
{
|
||
Client_Rating = clientDB.Client_Rating.Where(s => !s.IsDeleted && s.ProcessStatus == "已审批").FirstOrDefault(s => s.id == req.id);
|
||
Client_Rating.CreditRatingId = req.CreditRatingId;
|
||
Client_Rating.CreditRatingStr = req.CreditRatingStr;
|
||
Client_Rating.RatingDeadLine = req.RatingDeadLine;
|
||
}
|
||
//if (Client_RatingBLL.IsClient_RatingOverTheClient_RatingLine(Client_Rating))
|
||
//{
|
||
// return JsonSuccess(null, new { isUnderLimit = false });
|
||
//}
|
||
|
||
return JsonSuccess(null, new { isUnderLimit = true });
|
||
}
|
||
|
||
async Task<ReturnInfo<ClientRating>> Save(int? id, ClientRatingReq req = null)
|
||
{
|
||
//客户检查
|
||
var client = clientDB.client.FirstOrDefault(x => x.id == req.ClientId);
|
||
if (client == null)
|
||
{
|
||
return Return.Fail<ClientRating>($"找不到该客户,id:{req.ClientId}");
|
||
}
|
||
|
||
if (req.RatingDeadLine < req.RatingStartDate)
|
||
{
|
||
return Return.Fail<ClientRating>("评级起始日不能大于评级到期日");
|
||
}
|
||
|
||
ClientRating r;
|
||
var isAddNew = id == null || id == 0;
|
||
var updateComments = false;
|
||
|
||
if (isAddNew)
|
||
{
|
||
r = new ClientRating
|
||
{
|
||
OptId = CurUser.UserId,
|
||
OptName = CurUser.UserName,
|
||
OptDate = DateTime.Now
|
||
};
|
||
clientDB.Client_Rating.Add(r);
|
||
}
|
||
else
|
||
{
|
||
r = clientDB.Client_Rating.Where(s => !s.IsDeleted).FirstOrDefault(s => s.id == req.id);
|
||
updateComments = r.CreditRatingId == req.CreditRatingId
|
||
&& r.RatingDeadLine == req.RatingDeadLine
|
||
&& r.RatingStartDate == req.RatingStartDate
|
||
&& r.RatingAccording == req.RatingAccording
|
||
&& r.RatingPersonnel == req.RatingPersonnel
|
||
&& r.CheckPersonnel == req.CheckPersonnel;
|
||
}
|
||
|
||
if (updateComments)
|
||
{
|
||
r.Comments = req.Comments ?? "";
|
||
clientDB.SaveChanges();
|
||
return Return.Success(r);
|
||
}
|
||
|
||
await TryUpdateModelAsync(r);
|
||
|
||
#region 设置关联表
|
||
|
||
//设置等级名称
|
||
var rating = clientDB.credit_rating.FirstOrDefault(x => x.id == r.CreditRatingId);
|
||
r.CreditRatingStr = rating?.CreditName;
|
||
|
||
//if (clientDB.credit.Any(x => x.ProcessStatus == "已审批" && x.ClientId == r.ClientId && (x.Credit > rating.CreditLine && rating.CreditLine != 0)
|
||
// && x.CreditStartDate >= r.RatingStartDate && x.CreditDeadLine <= r.RatingDeadLine))
|
||
//{
|
||
// return Return.Fail<Client_Rating>($"存在该客户已通过的授信记录额度超过当前选中的资信等级({r.CreditRatingStr}:{rating.CreditLine}),请重新选择!");
|
||
//}
|
||
|
||
//设置客户编号
|
||
if (client != null)
|
||
{
|
||
r.ClientNumber = client.Number;
|
||
r.ClientName = client.Name;
|
||
}
|
||
|
||
var existClient_Ratings = new List<ClientRating>();
|
||
if (isAddNew)
|
||
{
|
||
existClient_Ratings = clientDB.Client_Rating.Where(x => !x.IsDeleted && x.ClientId == req.ClientId).ToList();
|
||
}
|
||
else
|
||
{
|
||
var clientId = clientDB.Client_Rating.Where(s => !s.IsDeleted).FirstOrDefault(s => s.id == req.id).ClientId;
|
||
existClient_Ratings = clientDB.Client_Rating.Where(x => !x.IsDeleted && x.ClientId == clientId && x.id != id).ToList();
|
||
}
|
||
|
||
var preDeadlineDate = existClient_Ratings.OrderByDescending(x => x.RatingDeadLine).ThenByDescending(x => x.ProcessOptDate).FirstOrDefault()?.RatingDeadLine ?? default;
|
||
var nextStartDate = preDeadlineDate.AddDays(1);
|
||
foreach (var Client_Rating in existClient_Ratings)
|
||
{
|
||
if (Client_Rating.RatingStartDate == null && Client_Rating.RatingDeadLine == null)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
////不能有空窗期
|
||
//if (preDeadlineDate != default && r.RatingStartDate != nextStartDate)
|
||
//{
|
||
// return Return.Fail<Client_Rating>($"同一客户评级有效时间不能有空窗期,应该从{nextStartDate.ToString("yyyy-MM-dd")}开始设置下一段有效期");
|
||
//}
|
||
|
||
//if (Client_Rating.RatingDeadLine == null || (Client_Rating.RatingDeadLine >= r.RatingStartDate && Client_Rating.RatingDeadLine <= r.RatingDeadLine)
|
||
// || (Client_Rating.RatingStartDate >= r.RatingStartDate && Client_Rating.RatingStartDate <= r.RatingDeadLine)
|
||
// || (Client_Rating.RatingStartDate <= r.RatingStartDate && Client_Rating.RatingDeadLine >= r.RatingDeadLine))
|
||
//{
|
||
// return Return.Fail<Client_Rating>("同一客户评级有效时间不能重合,请检查该客户已存在的评级记录");
|
||
//}
|
||
|
||
//if (Client_Rating.RatingDeadLine == null
|
||
// || (Client_Rating.RatingDeadLine.Value.Date >= r.RatingDeadLine.Value.Date && Client_Rating.RatingStartDate.Value.Date < r.RatingDeadLine.Value.Date) //尾
|
||
// || (Client_Rating.RatingDeadLine.Value.Date > r.RatingStartDate.Value.Date && Client_Rating.RatingStartDate.Value.Date <= r.RatingStartDate.Value.Date)) //头
|
||
//{
|
||
// return Return.Fail<Client_Rating>($"同一客户评级有效时间不能重合,请检查该客户已存在的冲突的评级记录({Client_Rating.RatingStartDate?.ToString("yyyy-MM-dd") + "~" + Client_Rating.RatingDeadLine?.ToString("yyyy-MM-dd")})");
|
||
//}
|
||
}
|
||
|
||
r.OptDate = DateTime.Now;
|
||
r.ProcessStatus = "未审批";
|
||
r.ProcessOrderId = 0;
|
||
r.ProcessOptDate = DateTime.Now;
|
||
r.Comments = req.Comments ?? "";
|
||
|
||
clientDB.SaveChanges();
|
||
#endregion
|
||
|
||
//Utilities.AddLog(r.id, ClientRating.LogClass, id == 0 ? "创建" : "修改", "");
|
||
if (id == 0 || id == null)
|
||
{
|
||
ClientRating(r.id, "新增资信");
|
||
}
|
||
else
|
||
{
|
||
ClientRating(r.id, "修改资信");
|
||
}
|
||
clientDB.SaveChanges();
|
||
return Return.Success(r);
|
||
}
|
||
|
||
public void ClientRating(int ratingId, string Type, string Changes = null)
|
||
{
|
||
clientDB.clientrating_log.Add(new ClientRatingLog
|
||
{
|
||
RatingId = ratingId,
|
||
OptType = Type,
|
||
OptId = UserId,
|
||
OptName = UserName,
|
||
Changes = Changes,
|
||
OptDate = DateTime.Now
|
||
});
|
||
clientDB.SaveChanges();
|
||
}
|
||
|
||
[HttpPost]
|
||
public JsonResult Delete(string id)
|
||
{
|
||
var intid = DataProtectHelper.DecryptInt(id);
|
||
var r = clientDB.Client_Rating.Where(s => !s.IsDeleted).FirstOrDefault(s => s.id == intid);
|
||
if (r == null)
|
||
{
|
||
return JsonError("找不到评级");
|
||
}
|
||
|
||
////设置等级名称
|
||
//if (clientDB.credit.Any(x => x.ClientId == r.ClientId && (x.ProcessStatus != "已拒绝") && x.CreditStartDate >= r.RatingStartDate && x.CreditDeadLine <= r.RatingDeadLine))
|
||
//{
|
||
// result.success = false;
|
||
// result.obj = $"无法删除!存在该客户已通过或未处理的授信记录 依赖于本评级区间资信等级,请先处理!";
|
||
// return JsonSuccess("", result);
|
||
//}
|
||
//Utilities.AddLog(r.id, ClientRating.LogClass, "删除", r.id + "");
|
||
|
||
r.IsDeleted = true;
|
||
clientDB.SaveChanges();
|
||
return JsonSuccess("删除成功");
|
||
}
|
||
}
|
||
} |