mh_sms/APT.MicroApi/APT.SC.WebApi/Controllers/Api/LRController/6LawController.cs

321 lines
13 KiB
C#
Raw Normal View History

2024-01-22 09:17:01 +08:00
using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Entities.FO;
using APT.MS.Domain.Entities.LR;
using APT.MS.Domain.Entities.SC.SC;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using static NPOI.HSSF.Util.HSSFColor;
namespace APT.LR.WebApi.Controllers.Api
{
[Route("api/LR/LRLaw")]
public class LawController : AuthorizeApiController<T_LR_LAW>
{
/// <summary>
/// 保存
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost, Route("FullUpdate")]
public JsonActionResult<bool> FullUpdate([FromBody] T_LR_LAW entity)
{
return SafeExecute<bool>(() =>
{
List<T_LR_LAW_VERSION_FILE> lawVersionFiles = new List<T_LR_LAW_VERSION_FILE>();
var lawSCSystem = entity.Nav_LawSCSystem;
var lawVersion = entity.Nav_LawVersion;
if (entity.USER_ID == Guid.Empty)
{
entity.USER_ID = (Guid)APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
}
entity.Nav_LawSCSystem = null;
entity.Nav_LawVersion = null;
if (lawVersion != null)
{
int noVersionCount = lawVersion.Count(t => t.VERSION_STATUS == 0);
if (lawVersion.Count() == noVersionCount)//都是废除状态
{
entity.LAW_STATUS = 0;
entity.VERSION_NUM = lawVersion.Where(t => t.UPDATE_TIME == lawVersion.Max(m => m.UPDATE_TIME)).FirstOrDefault().VERSION_NUM;//现行版本号
}
else //现行法律
{
int versionCount = lawVersion.Count(t => t.VERSION_STATUS == BSLawStatusEnum. && t.IS_DELETED == false);
if (versionCount > 1)
{
throw new Exception("该法律为现行法律,必须只有一个版本现行!");
}
entity.LAW_STATUS = BSLawStatusEnum.;
entity.VERSION_NUM = lawVersion.Where(t => t.VERSION_STATUS == BSLawStatusEnum.).FirstOrDefault().VERSION_NUM;//现行版本号
entity.UPDATE_TIME = lawVersion.Where(t => t.VERSION_STATUS == BSLawStatusEnum.).FirstOrDefault().UPDATE_TIME;//现行版本制修订日期
}
lawVersion.ForEach(t =>
{
if (t.Nav_Files != null && t.Nav_Files.Count() > 0)
{
t.Nav_Files.ForEach(nf =>
{
nf.VERSION_ID = t.ID;
});
lawVersionFiles.AddRange(t.Nav_Files);
t.Nav_Files = null;
}
else if (entity.LAW_STATUS == BSLawStatusEnum.)
{
throw new Exception("状态为现行,请上传法律法规附件!");
}
});
}
#region
bool isUndateBSMain = false;
string oldLAWName = string.Empty;
ICollection<Guid> listMainID = null;
//var checkLaw = GetEntity<T_LR_LAW>(entity.ID);
var checkLaw = GetEntity<T_SC_STANDARD_CREATE>(entity.ID);
if (checkLaw != null && checkLaw.NAME != entity.NAME)
{
BaseFilter filterCheck = new BaseFilter();
filterCheck.SelectField = new List<string> { "CHECK_MAIN_ID" };
var listMain = GetEntities<T_BS_CHECK_MAIN_LAW>(e => e.STANDARD_ID == entity.ID, filterCheck, null);
if (listMain != null && listMain.Any())
{
oldLAWName = checkLaw.NAME;
listMainID = listMain.Select(e => e.CHECK_MAIN_ID).Distinct().ToList();
isUndateBSMain = true;
}
}
#endregion
UnifiedCommit(() =>
{
UpdateEntityNoCommit(entity);
if (lawSCSystem != null)
BantchSaveEntityNoCommit(lawSCSystem);
if (lawVersion != null)
BantchSaveEntityNoCommit(lawVersion);
if (lawVersionFiles.Any())
BantchSaveEntityNoCommit(lawVersionFiles);
});
#region
if (isUndateBSMain)
{
try
{
var listCheckMain = GetEntities<T_BS_CHECK_MAIN>(e => listMainID.Contains(e.ID), null, null);
List<string> listRoot = null;
foreach (var item in listCheckMain)
{
listRoot = item.CHECKPROOF.Split('', StringSplitOptions.RemoveEmptyEntries).ToList();
for (int i = 0; i < listRoot.Count; i++)
{
if (listRoot[i] == oldLAWName)
{
listRoot[i] = entity.NAME;
}
}
item.CHECKPROOF = String.Join('', listRoot);
}
if (listCheckMain != null && listCheckMain.Any())
{
UnifiedCommit(() =>
{
BantchSaveEntityNoCommit(listCheckMain);
});
}
}
catch
{
}
}
#endregion
return true;
});
}
/// <summary>
/// 排序分页查询数据
/// </summary>
/// <param name="pageFilter">分页过滤实体</param>
/// <returns></returns>
[HttpPost, Route("OrderPaged")]
public PagedActionResult<T_LR_LAW> OrderPaged([FromBody] KeywordPageFilter pageFilter)
{
Expression<Func<T_LR_LAW, bool>> uExpress = t => t.IS_DELETED == false;
if (pageFilter.Keyword != "")
{
List<Guid> ids = new List<Guid>();
var tempids = pageFilter.Keyword.Split(",");
foreach (var id in tempids)
{
ids.Add(Guid.Parse(id));
}
uExpress = uExpress.And(t => ids.Contains(t.TYPE_ID));
}
return WitOrderPaged(uExpress, pageFilter);
}
/// <summary>
/// 获取导入数据
/// 参考 [Route("api/PF/Import")]
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetImportData")]
public JsonActionResult<ImportDataModel> GetImportData()
{
return SafeExecute<ImportDataModel>(() =>
{
var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
var httpRequest = this.HttpContext.Request;
string orgIdStr = httpRequest.Form["OrgId"];// 获取 组织
ImportDataModel result = new ImportDataModel();
var dic = Path.Combine(System.AppContext.BaseDirectory, "tempImportFiles");
if (!Directory.Exists(dic))
Directory.CreateDirectory(dic);
foreach (var key in httpRequest.Form.Files) // 文件键
{
var postedFile = key; // 获取文件键对应的文件对象
string filePath = Path.Combine(dic, DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + "_" + postedFile.FileName);
Byte[] fileData = new Byte[postedFile.Length];
Stream sr = postedFile.OpenReadStream();//创建数据流对象
sr.Read(fileData, 0, (int)postedFile.Length);
using (FileStream fs = new FileStream(filePath, FileMode.CreateNew))
{
fs.Write(fileData, 0, fileData.Length);
fs.Flush();
fs.Close();
}
//获取数据
Dictionary<int, int> startRowIndexs = new Dictionary<int, int>();
startRowIndexs.Add(0, 1);//根据Excel格式数据赋值
var dataTables = FileUtils.ReadExcelByOledb(filePath, startRowIndexs);
string Msg = string.Empty;
bool isOK = InsertModel(dataTables.Tables[0], orgId, ref Msg);
try
{
System.IO.File.Delete(filePath);
}
catch { }
result.Data = Msg;
result.MessageList = new List<string> { Msg };
}
return result;
});
}
/// <summary>
/// 数据插入
/// </summary>
/// <param name="dtSource"></param>
/// <param name="OrgID"></param>
/// <param name="Msg"></param>
/// <param name="rowIndex"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public bool InsertModel(DataTable dtSource, Guid? orgId, ref string Msg, int rowIndex = 1)
{
var userId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
if (dtSource == null || dtSource.Rows.Count < rowIndex)
{
Msg = "未获取到导入数据";
throw new Exception(Msg);
}
int rowAll = dtSource.Rows.Count;
if (!string.IsNullOrEmpty(Msg))
{
throw new Exception(Msg);
}
List<T_LR_LAW> laws = new List<T_LR_LAW>();
List<T_LR_LAW_VERSION> lawVersions = new List<T_LR_LAW_VERSION>();
var lawTypess = GetEntities<T_LR_LAW_TYPE>(t => t.IS_DELETED == false, new BaseFilter(orgId));
DateTime dtNow = DateTime.Now;
for (int i = 0; i < rowAll; i++)
{
T_LR_LAW law = new T_LR_LAW();
law.ID = Guid.NewGuid();
law.VERSION_NUM = dtSource.Rows[i][2].ToString().Trim();
law.USER_ID = (Guid)userId;
if (!string.IsNullOrEmpty(dtSource.Rows[i][0].ToString().Trim()))
{
law.NAME = dtSource.Rows[i][0].ToString().Trim();
}
else
{
throw new Exception("名称不能为空!");
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][3].ToString().Trim()))
{
law.UPDATE_TIME = DateTime.Parse(dtSource.Rows[i][3].ToString().Trim());
}
if (lawTypess.FirstOrDefault(t => t.NAME == dtSource.Rows[i][1].ToString().Trim()) != null)
{
law.TYPE_ID = lawTypess.FirstOrDefault(t => t.NAME == dtSource.Rows[i][1].ToString().Trim()).ID;
}
else
{
throw new Exception("需求类别:" + dtSource.Rows[i][1].ToString().Trim() + "未找到");
}
law.LAW_STATUS = BSLawStatusEnum.;
law.ORG_ID = orgId;
T_LR_LAW_VERSION lawVersion = new T_LR_LAW_VERSION
{
ID = Guid.NewGuid(),
LAW_ID = law.ID,
VERSION_NUM = dtSource.Rows[i][2].ToString().Trim(),
ENTER_TIME = dtNow,
VERSION_STATUS = BSLawStatusEnum.,
ORG_ID = orgId,
};
if (!string.IsNullOrEmpty(dtSource.Rows[i][3].ToString().Trim()))
{
lawVersion.UPDATE_TIME = DateTime.Parse(dtSource.Rows[i][3].ToString().Trim());
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][4].ToString().Trim()))
{
lawVersion.EXECUTE_TIME = DateTime.Parse(dtSource.Rows[i][4].ToString().Trim());
}
laws.Add(law);
lawVersions.Add(lawVersion);
}
UnifiedCommit(() =>
{
if (laws != null && laws.Any())
BantchAddEntityNoCommit(laws);
if (lawVersions != null && lawVersions.Any())
BantchAddEntityNoCommit(lawVersions);
});
Msg = "导入成功!";
return true;
}
}
}