TdevBoxService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using AutoMapper;
  2. using LinqKit;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.NodeServices;
  5. using Microsoft.EntityFrameworkCore;
  6. using Newtonsoft.Json;
  7. using Ropin.Core.Common;
  8. using Ropin.Inspection.Common;
  9. using Ropin.Inspection.Common.Accessor.Interface;
  10. using Ropin.Inspection.Common.Helper;
  11. using Ropin.Inspection.Model;
  12. using Ropin.Inspection.Model.Entities;
  13. using Ropin.Inspection.Repository;
  14. using Ropin.Inspection.Repository.Interface;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.ComponentModel.DataAnnotations;
  19. using System.Drawing;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Linq.Expressions;
  23. using System.Reflection;
  24. using System.Security.Cryptography;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. using System.Xml.Linq;
  28. using static System.Net.Mime.MediaTypeNames;
  29. namespace Ropin.Inspection.Service
  30. {
  31. public class TdevBoxService : ITdevBoxService
  32. {
  33. private readonly ITdevBoxRepository _repository;
  34. private readonly IMapper _mapper;
  35. private readonly IClaimsAccessor _claims;
  36. private readonly InspectionDbContext _sqlDBContext;
  37. private readonly ITmtnDevOpsRecordRepository _tmtnDevOpsRecordRepository;
  38. private readonly IUnitOfWork _unitOfWork;
  39. private readonly ITdevBoxDevSpotRepository _devBoxDevSpotRepository;
  40. public TdevBoxService(ITmtnDevOpsRecordRepository tmtnDevOpsRecordRepository,IClaimsAccessor claims, InspectionDbContext sqlDBContext, ITdevBoxRepository repository, IMapper mapper,IUnitOfWork unitOfWork, ITdevBoxDevSpotRepository devBoxDevSpotRepository)
  41. {
  42. _repository = repository;
  43. _mapper = mapper;
  44. _claims = claims;
  45. _sqlDBContext = sqlDBContext;
  46. _tmtnDevOpsRecordRepository = tmtnDevOpsRecordRepository;
  47. _unitOfWork = unitOfWork;
  48. _devBoxDevSpotRepository = devBoxDevSpotRepository;
  49. }
  50. public async Task CreateOneAsync(TdevBoxViewModel viewModel)
  51. {
  52. var datas = await _repository.GetByConditionAsync(t=>t.C_BoxNo==viewModel.C_BoxNo);
  53. if (datas.Count()>0&&datas.FirstOrDefault()!=null)
  54. {
  55. throw new Exception("网关编号已存在!");
  56. }
  57. var id = Guid.NewGuid().ToString();
  58. var path = QRCoderHelper.RenderQrCode(id, "M", _claims.Linsence);
  59. var content = _mapper.Map<TDEV_Box>(viewModel);
  60. content.C_ID = id;
  61. content.C_CreateBy = _claims.ApiUserId;
  62. content.D_CreateOn = DateTime.Now;
  63. content.C_Status = "1";
  64. content.C_QRCode = path;
  65. _repository.Create(content);
  66. var result = await _repository.SaveAsync();
  67. if (!result)
  68. {
  69. throw new Exception("创建失败");
  70. }
  71. }
  72. /// <summary>
  73. /// 业主盒子-拷贝
  74. /// </summary>
  75. /// <param name="oldId"></param>
  76. /// <returns></returns>
  77. /// <exception cref="Exception"></exception>
  78. public async Task CopyCreateAsync(string oldId)
  79. {
  80. TDEV_Box content = await _repository.GetByIdAsync(oldId);
  81. if (content == null)
  82. {
  83. throw new Exception("没有此数据");
  84. }
  85. bool result = false;
  86. _unitOfWork.BeginTransaction();
  87. try
  88. {
  89. string newId = Guid.NewGuid().ToString();
  90. var path = QRCoderHelper.RenderQrCode(newId, "M", _claims.Linsence);
  91. content.C_ID = newId;
  92. content.C_Name = content.C_Name + "-拷贝";
  93. content.C_CreateBy = _claims.ApiUserId;
  94. content.D_CreateOn = DateTime.Now;
  95. content.C_Status = "1";
  96. content.C_QRCode = path;
  97. content.C_LastUpdatedBy = null;
  98. content.D_LastUpdatedOn = null;
  99. result = await _unitOfWork.RegisterNew(content);
  100. if (result)
  101. {
  102. var devBoxDevSpot = await _devBoxDevSpotRepository.GetByConditionAsync(t => t.C_BoxCode == oldId);
  103. List<TDEV_BoxDevSpot> boxDevList = new List<TDEV_BoxDevSpot>();
  104. foreach (var item in devBoxDevSpot)
  105. {
  106. if (item != null)
  107. {
  108. TDEV_BoxDevSpot tDEV_Web = new TDEV_BoxDevSpot();
  109. tDEV_Web = item;
  110. tDEV_Web.C_ID = Guid.NewGuid().ToString();
  111. tDEV_Web.C_BoxCode = content.C_ID;
  112. tDEV_Web.C_LastUpdatedBy = null;
  113. tDEV_Web.D_LastUpdatedOn = null;
  114. tDEV_Web.C_CreateBy = _claims.ApiUserId;
  115. tDEV_Web.D_CreateOn = DateTime.Now;
  116. boxDevList.Add(tDEV_Web);
  117. }
  118. }
  119. if (boxDevList != null && boxDevList.Count > 0)
  120. {
  121. result = await _unitOfWork.RegisterRangeNew(boxDevList.ToList());
  122. }
  123. }
  124. }
  125. catch
  126. {
  127. result = false;
  128. throw;
  129. }
  130. finally
  131. {
  132. if (result)
  133. await _unitOfWork.CommitAsync();
  134. else
  135. _unitOfWork.Rollback();
  136. }
  137. }
  138. public async Task DeleteAsync(string id)
  139. {
  140. var content = await _repository.GetByIdAsync(id);
  141. if (content == null)
  142. {
  143. throw new Exception("数据库中没有此数据");
  144. }
  145. content.C_LastUpdatedBy = _claims.ApiUserId;
  146. content.D_LastUpdatedOn = DateTime.Now;
  147. content.C_Status = "0";
  148. _repository.Update(content);
  149. var result = await _repository.SaveAsync();
  150. if (!result)
  151. {
  152. throw new Exception("删除失败");
  153. }
  154. }
  155. public async Task UpdateAsync(string code, TdevBoxUpdateModel updateModel)
  156. {
  157. var items = await _repository.GetByConditionAsync(C => C.C_ID == code);
  158. var content = items.FirstOrDefault();
  159. if (content == null)
  160. {
  161. throw new Exception("没有此数据");
  162. }
  163. var datas = await _repository.GetByConditionAsync(t => t.C_BoxNo == updateModel.C_BoxNo&&t.C_ID!= code);
  164. if (datas.Count() > 0 && datas.FirstOrDefault() != null)
  165. {
  166. throw new Exception("网关编号已存在!");
  167. }
  168. content.C_LastUpdatedBy = _claims.Linsence == null ? _claims.ApiUserId : Guid.Parse("6e864cbc-5252-11ec-8681-fa163e02b3e4");
  169. content.D_LastUpdatedOn = DateTime.Now;
  170. _mapper.Map(updateModel, content, typeof(TdevBoxUpdateModel), typeof(TDEV_Box));
  171. _repository.Update(content);
  172. var result = await _repository.SaveAsync();
  173. if (!result)
  174. {
  175. throw new Exception("更新失败");
  176. }
  177. }
  178. public async Task<IEnumerable<TdevBoxViewModel>> GetConditionAsync(TdevBoxSearchModel searchModel)
  179. {
  180. return await _repository.GetConditionAsync(searchModel);
  181. //var predicate = PredicateBuilder.New<TDEV_Box>(true);//查询条件,推荐后台使用这种方式灵活筛选
  182. //#region 添加条件查询
  183. //if (!string.IsNullOrEmpty(searchModel.C_Status))
  184. //{
  185. // predicate = predicate.And(i => i.C_Status.Equals(searchModel.C_Status));
  186. //}
  187. //else
  188. //{
  189. // predicate = predicate.And(i => !i.C_Status.Equals("0"));
  190. //}
  191. //if (!string.IsNullOrEmpty(searchModel.C_ID))
  192. //{
  193. // predicate = predicate.And(i => i.C_ID.Equals(searchModel.C_ID));
  194. //}
  195. //if (!string.IsNullOrEmpty(searchModel.C_Name))
  196. //{
  197. // predicate = predicate.And(i => i.C_Name.Contains(searchModel.C_Name));
  198. //}
  199. //if (!string.IsNullOrEmpty(searchModel.C_StoreCode))
  200. //{
  201. // predicate = predicate.And(i => i.C_StoreCode == searchModel.C_StoreCode);
  202. //}
  203. //#endregion
  204. //var list = await _repository.GetPageAsync(predicate, "I_Sort", searchModel.IsPagination, searchModel.PageIndex, searchModel.PageSize);//C_Name,-D_CreateOn
  205. //searchModel.TotalCount = list.Totals;
  206. //var dtoList = _mapper.Map<List<TDEV_Box>, List<TdevBoxViewModel>>(list.Rows);
  207. //return dtoList;
  208. }
  209. public async Task<TdevBoxViewModel> GetConditionByIdAsync(string code)
  210. {
  211. var items = await _repository.GetByConditionAsync(C => C.C_ID == code);
  212. var content = items.FirstOrDefault();
  213. if (content != null)
  214. {
  215. var entity = _mapper.Map<TDEV_Box, TdevBoxViewModel>(content);
  216. return entity;
  217. }
  218. else
  219. {
  220. return null;
  221. }
  222. }
  223. public Task<int> UpdateOneAsync(TdevBoxViewModel viewModel, params string[] fields)
  224. {
  225. throw new NotImplementedException();
  226. }
  227. public Task<TdevBoxViewModel> GetByIdAsync(string id)
  228. {
  229. throw new NotImplementedException();
  230. }
  231. public Task<bool> IsExistAsync(string id)
  232. {
  233. throw new NotImplementedException();
  234. }
  235. }
  236. }