using Microsoft.AspNetCore.Http;
using QM.KJGH.CGGL.IRepository;
using QM.KJGH.CGGL.Model.QxCghj;
using QM.KJGH.Model.Enums;
using QM.KJGH.Model.KJGH;
using QM.OrmSqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WS;
using WS.AutoMapper;
using WS.IO;
using WS.IO.Upload;
using WS.IO.Zip;
namespace QM.KJGH.CGGL.Repositories
{
///
///
///
public class BYNECghjCgsbrwRepository : SqlSugarRepository, IBYNECghjCgsbrwRepository
{
///
/// 获取成果文件信息
///
///
private CghjQxcg GetCginfo(string path, BYNECghjIo_QX input) {
CghjQxcg qxcg = new CghjQxcg()
{
BSM = input.fileMd5,
SCZT = EnumCghjQxcgsbSC.待审.Description()
};
var txtInfo = Directory.GetFiles(path).ToList().Where(t=>t.EndsWith("基本信息.txt")).FirstOrDefault();
if(txtInfo == null)
throw new UseMassageException($"成果包中必须包含“xxx基本信息.txt”文件,同时注意成果包中应包含“xxx规划成果”的根目录");
List lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("GB2312"))
.Select(t => t.Trim())
.Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
.ToList();
WS.Log.RunLog.Debug($"基本信息:{txtInfo}\r\n{lines.ToJson()}");
var res = lines.Where(t => t.Contains("项目名称")).FirstOrDefault();
if (string.IsNullOrWhiteSpace(res))
{
lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("UTF-8"))
.Select(t => t.Trim())
.Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
.ToList();
}
//反射
Dictionary qxcgDict = typeof(CghjQxcg).GetProperties()
.Where(t => t.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() != null)
.ToDictionary(t => (t.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description, t => t);
var yrars = new List { "基期年", "规划起始年", "近期目标年", "规划目标年", "远景目标年" };
yrars = new List { "基期年", "规划起始年", "近期目标年", "规划目标年" };
foreach (var item in lines)
{
try
{
if (!item.Contains("=")) continue;
var info = item.Trim().Split("=");
if (info.Length != 2) continue;
if (string.IsNullOrWhiteSpace(info[1])) continue;
if (yrars.Contains(info[0])) info[1] = info[1].Replace("年", "");
//时间特殊处理
switch (info[0].Trim())
{
case "编制时间":
qxcg.BZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
break;
case "上报时间":
qxcg.SBSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
break;
case "批准时间":
qxcg.PZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
break;
default:
if (qxcgDict.ContainsKey(info[0].Trim()))
{
var propertyInfo = qxcgDict[info[0].Trim()];
//判断是否为可空泛型
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName.Contains(typeof(Nullable).ToString()))
{
var propType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
var changeType = Convert.ChangeType(info[1], propType);
propertyInfo.SetValue(qxcg, changeType);
}
else
{
propertyInfo.SetValue(qxcg, Convert.ChangeType(info[1], propertyInfo.PropertyType), null);
}
}
break;
}
}
catch (Exception ex)
{
WS.Log.RunLog.Error(ex, $"属性异常:{txtInfo} => {item} => {ex.Message}");
}
}
qxcg.CheckNull();
//名称检查
var cgjc = Db.Queryable().Where(t => t.XMMC == qxcg.XMMC).Count();
if (cgjc > 0)
{
WS.Log.RunLog.Info($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
throw new UseMassageException($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
}
return qxcg;
}
///
/// 获取成果文件信息(村规)
///
///
private CghjQxcg GetCginfoCZGH(string path, BYNECghjIo_QX input)
{
CghjQxcgCZGH qxcg = new CghjQxcgCZGH()
{
BSM = input.fileMd5,
SCZT = EnumCghjQxcgsbSC.待审.Description()
};
var txtInfo = Directory.GetFiles(path).ToList().Where(t => t.EndsWith("基本信息.txt")).FirstOrDefault();
if (txtInfo == null)
throw new UseMassageException($"成果包中必须包含“xxx基本信息.txt”文件,同时注意成果包中应包含“xxx规划成果”的根目录");
List lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("GB2312"))
.Select(t => t.Trim())
.Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
.ToList();
WS.Log.RunLog.Debug($"基本信息:{txtInfo}\r\n{lines.ToJson()}");
var res = lines.Where(t => t.Contains("项目名称")).FirstOrDefault();
if (string.IsNullOrWhiteSpace(res))
{
lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("UTF-8"))
.Select(t => t.Trim())
.Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
.ToList();
}
//反射
Dictionary qxcgDict = typeof(CghjQxcgCZGH).GetProperties()
.Where(t => t.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() != null)
.ToDictionary(t => (t.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description, t => t);
var yrars = new List { "基期年", "规划起始年", "近期目标年", "规划目标年" };
foreach (var item in lines)
{
try
{
if (!item.Contains("=")) continue;
var info = item.Trim().Split("=");
if (info.Length != 2) continue;
if (string.IsNullOrWhiteSpace(info[1])) continue;
if (yrars.Contains(info[0])) info[1] = info[1].Replace("年", "");
//时间特殊处理
switch (info[0].Trim())
{
case "编制时间":
qxcg.BZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
break;
case "上报时间":
qxcg.SBSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
break;
case "批准时间":
qxcg.PZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
break;
default:
if (qxcgDict.ContainsKey(info[0].Trim()))
{
var propertyInfo = qxcgDict[info[0].Trim()];
//判断是否为可空泛型
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName.Contains(typeof(Nullable).ToString()))
{
var propType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
var changeType = Convert.ChangeType(info[1], propType);
propertyInfo.SetValue(qxcg, changeType);
}
else
{
propertyInfo.SetValue(qxcg, Convert.ChangeType(info[1], propertyInfo.PropertyType), null);
}
}
break;
}
}
catch (Exception ex)
{
WS.Log.RunLog.Error(ex, $"属性异常:{txtInfo} => {item} => {ex.Message}");
}
}
qxcg.CheckNull();
//名称检查
var cgjc = Db.Queryable().Where(t => t.XMMC == qxcg.XMMC).Count();
if (cgjc > 0)
{
WS.Log.RunLog.Info($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
throw new UseMassageException($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
}
if (qxcg.CGXS != "简易成果" && qxcg.CGXS != "完整成果")
{
WS.Log.RunLog.Info($"基本信息中所填写的成果形式只能为[简易成果]或[完整成果]而基本信息文件中的成果形式为[{qxcg.CGXS}]");
throw new Exception($"基本信息中所填写的成果形式只能为[简易成果]或[完整成果]而基本信息文件中的成果形式为[{qxcg.CGXS}]");
}
return qxcg.MapTo();
}
///
/// 巴彦淖尔市-提交成果
///
///
///
public async Task BYNEComplete(BYNECghjIo_QX input, string ghcgFile, string zjbgFile)
{
var cglj = Directory.GetDirectories(Path.GetDirectoryName(ghcgFile))[0];
CghjQxcg cghjQxcg = null;
if(input.type == EnumGHLX.村庄规划)
{
cghjQxcg = GetCginfoCZGH(cglj, input);
}
else
{
cghjQxcg = GetCginfo(cglj, input);
}
cghjQxcg.CGB = FileManage.Instance.ToRelativePath(ghcgFile);
cghjQxcg.CGLJ = Path.GetDirectoryName(cglj);
cghjQxcg.BZ = FileManage.Instance.ToRelativePath(zjbgFile);
CghjQxcgsbrw cghjQxcgsbrw = new CghjQxcgsbrw()
{
BSM = cghjQxcg.BSM,
CGLJ = cghjQxcg.CGLJ,
CGWJ = cghjQxcg.CGB,
GHLX = cghjQxcg.CGLJ,
HJSJ = DateTime.Now,
RWZT = EnumCghjQxcgsbrw.Complete,
TIME = DateTime.Now,
XMMC = input.name,
XZQDM = input.xzqdm,
XZQMC = input.xzqmc,
BZ = input.ToJson()
};
try
{
WS.Log.RunLog.Debug("CGHJQXCG:" + cghjQxcg.ToJson());
await Db.Insertable(cghjQxcg).ExecuteCommandIdentityIntoEntityAsync();
}
catch (Exception ex)
{
WS.Log.RunLog.Error(ex, $"提交成果入库异常:{ex.Message} {input.ToJson()} ==> {ghcgFile}");
throw new UseMassageException("提交成果入库异常");
}
try
{
WS.Log.RunLog.Debug("CGHJQXCGSBRW:" + cghjQxcgsbrw.ToJson());
await Db.Insertable(cghjQxcgsbrw).ExecuteCommandIdentityIntoEntityAsync();
}
catch (Exception ex)
{
WS.Log.RunLog.Error(ex, $"提交成果入库异常:{ex.Message} {input.ToJson()} ==> {ghcgFile}");
throw new UseMassageException("提交成果入库异常");
}
return true;
}
///
/// 上报失败
///
///
///
public async Task QXError(string id, string msg)
{
var rwModel = await GetAsync(t => t.BSM == id);
if (rwModel != null)
{
rwModel.RWZT = EnumCghjQxcgsbrw.Error;
rwModel.BZ = msg;
await Db.Updateable(rwModel).ExecuteCommandAsync();
}
var CghjQxcg = await Db.Queryable().Where(t => t.BSM == id).FirstAsync();
if (CghjQxcg != null)
{
CghjQxcg.SCZT = "失败";
CghjQxcg.BZ = msg;
await Db.Updateable(CghjQxcg).ExecuteCommandAsync();
}
return true;
}
}
}