| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- 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
- {
- /// <summary>
- ///
- /// </summary>
- public class BYNECghjCgsbrwRepository : SqlSugarRepository<CghjQxcgsbrw>, IBYNECghjCgsbrwRepository
- {
- /// <summary>
- /// 获取成果文件信息
- /// </summary>
- /// <returns></returns>
- 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<string> 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<string, PropertyInfo> 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<string> { "基期年", "规划起始年", "近期目标年", "规划目标年", "远景目标年" };
- yrars = new List<string> { "基期年", "规划起始年", "近期目标年", "规划目标年" };
- 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<CghjQxcg>().Where(t => t.XMMC == qxcg.XMMC).Count();
- if (cgjc > 0)
- {
- WS.Log.RunLog.Info($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
- throw new UseMassageException($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
- }
- return qxcg;
- }
- /// <summary>
- /// 获取成果文件信息(村规)
- /// </summary>
- /// <returns></returns>
- 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<string> 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<string, PropertyInfo> 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<string> { "基期年", "规划起始年", "近期目标年", "规划目标年" };
-
- 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<CghjQxcg>().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<CghjQxcg>();
- }
- /// <summary>
- /// 巴彦淖尔市-提交成果
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<bool> 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;
- }
- /// <summary>
- /// 上报失败
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<bool> 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<CghjQxcg>().Where(t => t.BSM == id).FirstAsync();
- if (CghjQxcg != null)
- {
- CghjQxcg.SCZT = "失败";
- CghjQxcg.BZ = msg;
- await Db.Updateable(CghjQxcg).ExecuteCommandAsync();
- }
- return true;
- }
- }
- }
|