BYNECghjCgsbrwRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using Microsoft.AspNetCore.Http;
  2. using QM.KJGH.CGGL.IRepository;
  3. using QM.KJGH.CGGL.Model.QxCghj;
  4. using QM.KJGH.Model.Enums;
  5. using QM.KJGH.Model.KJGH;
  6. using QM.OrmSqlSugar;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using WS;
  17. using WS.AutoMapper;
  18. using WS.IO;
  19. using WS.IO.Upload;
  20. using WS.IO.Zip;
  21. namespace QM.KJGH.CGGL.Repositories
  22. {
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public class BYNECghjCgsbrwRepository : SqlSugarRepository<CghjQxcgsbrw>, IBYNECghjCgsbrwRepository
  27. {
  28. /// <summary>
  29. /// 获取成果文件信息
  30. /// </summary>
  31. /// <returns></returns>
  32. private CghjQxcg GetCginfo(string path, BYNECghjIo_QX input) {
  33. CghjQxcg qxcg = new CghjQxcg()
  34. {
  35. BSM = input.fileMd5,
  36. SCZT = EnumCghjQxcgsbSC.待审.Description()
  37. };
  38. var txtInfo = Directory.GetFiles(path).ToList().Where(t=>t.EndsWith("基本信息.txt")).FirstOrDefault();
  39. if(txtInfo == null)
  40. throw new UseMassageException($"成果包中必须包含“xxx基本信息.txt”文件,同时注意成果包中应包含“xxx规划成果”的根目录");
  41. List<string> lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("GB2312"))
  42. .Select(t => t.Trim())
  43. .Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
  44. .ToList();
  45. WS.Log.RunLog.Debug($"基本信息:{txtInfo}\r\n{lines.ToJson()}");
  46. var res = lines.Where(t => t.Contains("项目名称")).FirstOrDefault();
  47. if (string.IsNullOrWhiteSpace(res))
  48. {
  49. lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("UTF-8"))
  50. .Select(t => t.Trim())
  51. .Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
  52. .ToList();
  53. }
  54. //反射
  55. Dictionary<string, PropertyInfo> qxcgDict = typeof(CghjQxcg).GetProperties()
  56. .Where(t => t.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() != null)
  57. .ToDictionary(t => (t.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description, t => t);
  58. var yrars = new List<string> { "基期年", "规划起始年", "近期目标年", "规划目标年", "远景目标年" };
  59. yrars = new List<string> { "基期年", "规划起始年", "近期目标年", "规划目标年" };
  60. foreach (var item in lines)
  61. {
  62. try
  63. {
  64. if (!item.Contains("=")) continue;
  65. var info = item.Trim().Split("=");
  66. if (info.Length != 2) continue;
  67. if (string.IsNullOrWhiteSpace(info[1])) continue;
  68. if (yrars.Contains(info[0])) info[1] = info[1].Replace("年", "");
  69. //时间特殊处理
  70. switch (info[0].Trim())
  71. {
  72. case "编制时间":
  73. qxcg.BZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  74. break;
  75. case "上报时间":
  76. qxcg.SBSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  77. break;
  78. case "批准时间":
  79. qxcg.PZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  80. break;
  81. default:
  82. if (qxcgDict.ContainsKey(info[0].Trim()))
  83. {
  84. var propertyInfo = qxcgDict[info[0].Trim()];
  85. //判断是否为可空泛型
  86. if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName.Contains(typeof(Nullable).ToString()))
  87. {
  88. var propType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
  89. var changeType = Convert.ChangeType(info[1], propType);
  90. propertyInfo.SetValue(qxcg, changeType);
  91. }
  92. else
  93. {
  94. propertyInfo.SetValue(qxcg, Convert.ChangeType(info[1], propertyInfo.PropertyType), null);
  95. }
  96. }
  97. break;
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. WS.Log.RunLog.Error(ex, $"属性异常:{txtInfo} => {item} => {ex.Message}");
  103. }
  104. }
  105. qxcg.CheckNull();
  106. //名称检查
  107. var cgjc = Db.Queryable<CghjQxcg>().Where(t => t.XMMC == qxcg.XMMC).Count();
  108. if (cgjc > 0)
  109. {
  110. WS.Log.RunLog.Info($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
  111. throw new UseMassageException($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
  112. }
  113. return qxcg;
  114. }
  115. /// <summary>
  116. /// 获取成果文件信息(村规)
  117. /// </summary>
  118. /// <returns></returns>
  119. private CghjQxcg GetCginfoCZGH(string path, BYNECghjIo_QX input)
  120. {
  121. CghjQxcgCZGH qxcg = new CghjQxcgCZGH()
  122. {
  123. BSM = input.fileMd5,
  124. SCZT = EnumCghjQxcgsbSC.待审.Description()
  125. };
  126. var txtInfo = Directory.GetFiles(path).ToList().Where(t => t.EndsWith("基本信息.txt")).FirstOrDefault();
  127. if (txtInfo == null)
  128. throw new UseMassageException($"成果包中必须包含“xxx基本信息.txt”文件,同时注意成果包中应包含“xxx规划成果”的根目录");
  129. List<string> lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("GB2312"))
  130. .Select(t => t.Trim())
  131. .Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
  132. .ToList();
  133. WS.Log.RunLog.Debug($"基本信息:{txtInfo}\r\n{lines.ToJson()}");
  134. var res = lines.Where(t => t.Contains("项目名称")).FirstOrDefault();
  135. if (string.IsNullOrWhiteSpace(res))
  136. {
  137. lines = File.ReadAllLines(txtInfo, Encoding.GetEncoding("UTF-8"))
  138. .Select(t => t.Trim())
  139. .Where(t => !string.IsNullOrWhiteSpace(t) && !t.StartsWith("["))
  140. .ToList();
  141. }
  142. //反射
  143. Dictionary<string, PropertyInfo> qxcgDict = typeof(CghjQxcgCZGH).GetProperties()
  144. .Where(t => t.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() != null)
  145. .ToDictionary(t => (t.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description, t => t);
  146. var yrars = new List<string> { "基期年", "规划起始年", "近期目标年", "规划目标年" };
  147. foreach (var item in lines)
  148. {
  149. try
  150. {
  151. if (!item.Contains("=")) continue;
  152. var info = item.Trim().Split("=");
  153. if (info.Length != 2) continue;
  154. if (string.IsNullOrWhiteSpace(info[1])) continue;
  155. if (yrars.Contains(info[0])) info[1] = info[1].Replace("年", "");
  156. //时间特殊处理
  157. switch (info[0].Trim())
  158. {
  159. case "编制时间":
  160. qxcg.BZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  161. break;
  162. case "上报时间":
  163. qxcg.SBSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  164. break;
  165. case "批准时间":
  166. qxcg.PZSJ = DateTime.ParseExact(info[1], "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
  167. break;
  168. default:
  169. if (qxcgDict.ContainsKey(info[0].Trim()))
  170. {
  171. var propertyInfo = qxcgDict[info[0].Trim()];
  172. //判断是否为可空泛型
  173. if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName.Contains(typeof(Nullable).ToString()))
  174. {
  175. var propType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
  176. var changeType = Convert.ChangeType(info[1], propType);
  177. propertyInfo.SetValue(qxcg, changeType);
  178. }
  179. else
  180. {
  181. propertyInfo.SetValue(qxcg, Convert.ChangeType(info[1], propertyInfo.PropertyType), null);
  182. }
  183. }
  184. break;
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. WS.Log.RunLog.Error(ex, $"属性异常:{txtInfo} => {item} => {ex.Message}");
  190. }
  191. }
  192. qxcg.CheckNull();
  193. //名称检查
  194. var cgjc = Db.Queryable<CghjQxcg>().Where(t => t.XMMC == qxcg.XMMC).Count();
  195. if (cgjc > 0)
  196. {
  197. WS.Log.RunLog.Info($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
  198. throw new UseMassageException($"基本信息中所填写的项目名称[{qxcg.XMMC}]已经存在");
  199. }
  200. if (qxcg.CGXS != "简易成果" && qxcg.CGXS != "完整成果")
  201. {
  202. WS.Log.RunLog.Info($"基本信息中所填写的成果形式只能为[简易成果]或[完整成果]而基本信息文件中的成果形式为[{qxcg.CGXS}]");
  203. throw new Exception($"基本信息中所填写的成果形式只能为[简易成果]或[完整成果]而基本信息文件中的成果形式为[{qxcg.CGXS}]");
  204. }
  205. return qxcg.MapTo<CghjQxcg>();
  206. }
  207. /// <summary>
  208. /// 巴彦淖尔市-提交成果
  209. /// </summary>
  210. /// <param name="input"></param>
  211. /// <returns></returns>
  212. public async Task<bool> BYNEComplete(BYNECghjIo_QX input, string ghcgFile, string zjbgFile)
  213. {
  214. var cglj = Directory.GetDirectories(Path.GetDirectoryName(ghcgFile))[0];
  215. CghjQxcg cghjQxcg = null;
  216. if(input.type == EnumGHLX.村庄规划)
  217. {
  218. cghjQxcg = GetCginfoCZGH(cglj, input);
  219. }
  220. else
  221. {
  222. cghjQxcg = GetCginfo(cglj, input);
  223. }
  224. cghjQxcg.CGB = FileManage.Instance.ToRelativePath(ghcgFile);
  225. cghjQxcg.CGLJ = Path.GetDirectoryName(cglj);
  226. cghjQxcg.BZ = FileManage.Instance.ToRelativePath(zjbgFile);
  227. CghjQxcgsbrw cghjQxcgsbrw = new CghjQxcgsbrw()
  228. {
  229. BSM = cghjQxcg.BSM,
  230. CGLJ = cghjQxcg.CGLJ,
  231. CGWJ = cghjQxcg.CGB,
  232. GHLX = cghjQxcg.CGLJ,
  233. HJSJ = DateTime.Now,
  234. RWZT = EnumCghjQxcgsbrw.Complete,
  235. TIME = DateTime.Now,
  236. XMMC = input.name,
  237. XZQDM = input.xzqdm,
  238. XZQMC = input.xzqmc,
  239. BZ = input.ToJson()
  240. };
  241. try
  242. {
  243. WS.Log.RunLog.Debug("CGHJQXCG:" + cghjQxcg.ToJson());
  244. await Db.Insertable(cghjQxcg).ExecuteCommandIdentityIntoEntityAsync();
  245. }
  246. catch (Exception ex)
  247. {
  248. WS.Log.RunLog.Error(ex, $"提交成果入库异常:{ex.Message} {input.ToJson()} ==> {ghcgFile}");
  249. throw new UseMassageException("提交成果入库异常");
  250. }
  251. try
  252. {
  253. WS.Log.RunLog.Debug("CGHJQXCGSBRW:" + cghjQxcgsbrw.ToJson());
  254. await Db.Insertable(cghjQxcgsbrw).ExecuteCommandIdentityIntoEntityAsync();
  255. }
  256. catch (Exception ex)
  257. {
  258. WS.Log.RunLog.Error(ex, $"提交成果入库异常:{ex.Message} {input.ToJson()} ==> {ghcgFile}");
  259. throw new UseMassageException("提交成果入库异常");
  260. }
  261. return true;
  262. }
  263. /// <summary>
  264. /// 上报失败
  265. /// </summary>
  266. /// <param name="input"></param>
  267. /// <returns></returns>
  268. public async Task<bool> QXError(string id, string msg)
  269. {
  270. var rwModel = await GetAsync(t => t.BSM == id);
  271. if (rwModel != null)
  272. {
  273. rwModel.RWZT = EnumCghjQxcgsbrw.Error;
  274. rwModel.BZ = msg;
  275. await Db.Updateable(rwModel).ExecuteCommandAsync();
  276. }
  277. var CghjQxcg = await Db.Queryable<CghjQxcg>().Where(t => t.BSM == id).FirstAsync();
  278. if (CghjQxcg != null)
  279. {
  280. CghjQxcg.SCZT = "失败";
  281. CghjQxcg.BZ = msg;
  282. await Db.Updateable(CghjQxcg).ExecuteCommandAsync();
  283. }
  284. return true;
  285. }
  286. }
  287. }