ResItemInfoRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using OASystem.Domain.Dtos.Resource;
  2. using OASystem.Domain.Entities.Resource;
  3. using OASystem.Domain.ViewModels.Resource;
  4. using Serilog;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace OASystem.Infrastructure.Repositories.Resource
  11. {
  12. public class ResItemInfoRepository : BaseRepository<Res_ItemDetailInfo, Res_ItemInfoView>
  13. {
  14. public ResItemInfoRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
  15. {
  16. }
  17. #region 供应商
  18. /// <summary>
  19. /// 供应商列表查询
  20. /// </summary>
  21. /// <param name="_dto"></param>
  22. /// <returns></returns>
  23. public Res_ItemVendorListView GetVendorList(Search_ResItemVendorDto _dto)
  24. {
  25. string sqlWhere = string.Format(" Where IsDel=0 ");
  26. #region SqlWhere
  27. if (!string.IsNullOrEmpty(_dto.VendorAddress))//供应商联系地址
  28. {
  29. sqlWhere += string.Format(" And [Address] Like '%{0}%' ", _dto.VendorAddress);
  30. }
  31. if (!string.IsNullOrEmpty(_dto.VendorLinker))//供应商联系人
  32. {
  33. sqlWhere += string.Format(" And Linker Like '%{0}%' ", _dto.VendorLinker);
  34. }
  35. if (!string.IsNullOrEmpty(_dto.VendorMobile))//供应商联系电话
  36. {
  37. sqlWhere += string.Format(" And Mobile Like '%{0}%' ", _dto.VendorMobile);
  38. }
  39. if (!string.IsNullOrEmpty(_dto.VendorName))//供应商公司名称/简称
  40. {
  41. sqlWhere += string.Format(" And ( FullName Like '%{0}%' Or ShortName Like '%{0}%' ) ", _dto.VendorName);
  42. }
  43. if (!string.IsNullOrEmpty(_dto.BusRange))//供应商经营范围
  44. {
  45. //List<int> list = _dto.BusRange.Split(',').ToList().ConvertAll(t => Convert.ToInt32(t));
  46. //单数值匹配
  47. sqlWhere += string.Format(" And BusRange Like '%,{0},%' ", _dto.BusRange);
  48. }
  49. //if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  50. //{
  51. // Regex r = new Regex("And");
  52. // sqlWhere = r.Replace(sqlWhere, "Where", 1);
  53. //}
  54. #endregion
  55. int currPIndex = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) * _dto.PageSize) + 1;
  56. int currPSize = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) + 1) * _dto.PageSize;
  57. string sql = string.Format(@" Select * From(Select ROW_NUMBER() Over(order By Id desc) as RowNumber, Id,FullName,ShortName,Address,Linker,
  58. Job,Mobile,Remark From Res_ItemVendor With(Nolock) {2}
  59. ) as tb Where tb.RowNumber Between {0} And {1} ", currPIndex, currPSize, sqlWhere);
  60. Res_ItemVendorListView rst = new Res_ItemVendorListView();
  61. rst.CurrPageIndex = currPIndex;
  62. rst.CurrPageSize = currPSize;
  63. List<Res_ItemVendorView> dataSource = _sqlSugar.SqlQueryable<Res_ItemVendorView>(sql).ToList();
  64. foreach (var item in dataSource)
  65. {
  66. string rangeStr = "";
  67. List<int> rangeList = item.BusRange.TrimStart(',').Split(',').ToList().ConvertAll(t => Convert.ToInt32(t));
  68. foreach (var item2 in rangeList)
  69. {
  70. rangeStr += GetText_BusRanger(item2) + ",";
  71. }
  72. item.BusRange_Text = rangeStr;
  73. }
  74. rst.DataList = new List<Res_ItemVendorView>(dataSource);
  75. if (rst.DataList.Count > 0)
  76. {
  77. string sqlCount = string.Format(@" Select Id From Res_ItemVendor With(Nolock) {0} ", sqlWhere);
  78. int dataCount = _sqlSugar.SqlQueryable<Res_ItemVendorView>(sqlCount).Count();
  79. rst.DataCount = dataCount;
  80. }
  81. return rst;
  82. }
  83. private string GetText_BusRanger(int sdid)
  84. {
  85. List<Sys_SetData> dataList = _sqlSugar.Queryable<Sys_SetData>().Where(s => s.STid == 21).ToList();
  86. string result = "未知类型" + sdid;
  87. if (dataList.FirstOrDefault(s => s.Id == sdid) != null)
  88. {
  89. result = dataList.FirstOrDefault(s => s.Id == sdid).Name;
  90. }
  91. return result;
  92. }
  93. /// <summary>
  94. /// 供应商信息查询
  95. /// </summary>
  96. /// <param name="_dto"></param>
  97. /// <returns></returns>
  98. public Res_ItemVendorView getVendorInfo(Search_ResItemVendorDto _dto)
  99. {
  100. string sqlWhere = string.Format(" Where IsDel=0 ");
  101. if (_dto.VendorId > 0)
  102. {
  103. Res_ItemVendorView _view = _sqlSugar.Queryable<Res_ItemVendorView>().Where(s => s.Id == _dto.VendorId).First();
  104. string rangeStr = "";
  105. List<int> rangeList = _view.BusRange.TrimStart(',').Split(',').ToList().ConvertAll(t => Convert.ToInt32(t));
  106. foreach (var item2 in rangeList)
  107. {
  108. rangeStr += GetText_BusRanger(item2) + ",";
  109. }
  110. _view.BusRange_Text = rangeStr;
  111. return _view;
  112. }
  113. else
  114. {
  115. return null;
  116. }
  117. }
  118. /// <summary>
  119. /// 创建供应商信息
  120. /// </summary>
  121. /// <param name="_dto"></param>
  122. /// <returns></returns>
  123. public async Task<bool> addVendorInfo(Edit_ResItemVendorDto _dto)
  124. {
  125. Dictionary<string, object> dic = new Dictionary<string, object>();
  126. dic.Add("FullName", _dto.VendorFullName);
  127. dic.Add("ShortName", string.IsNullOrEmpty(_dto.VendorShortName) ? "" : _dto.VendorShortName);
  128. dic.Add("Address", string.IsNullOrEmpty(_dto.VendorAddress) ? "" : _dto.VendorAddress);
  129. dic.Add("Linker", _dto.VendorLinker);
  130. dic.Add("Job", string.IsNullOrEmpty(_dto.VendorJob) ? "" : _dto.VendorJob);
  131. dic.Add("Mobile", _dto.VendorMobile);
  132. dic.Add("CreateUserId", _dto.SysUserId);
  133. dic.Add("CreateTime", DateTime.Now);
  134. dic.Add("DeleteUserId", 0);
  135. dic.Add("DeleteTime", "Null");
  136. dic.Add("Remark", _dto.Remark);
  137. dic.Add("IsDel", 0);
  138. dic.Add("BusRange", string.IsNullOrEmpty(_dto.BusRange) ? "," : _dto.BusRange);
  139. return await InsertDataByDictionary(dic, "Res_ItemVendor");
  140. }
  141. /// <summary>
  142. /// 修改供应商信息
  143. /// </summary>
  144. /// <param name="_dto"></param>
  145. /// <returns></returns>
  146. public async Task<bool> updVendorInfo(Res_ItemVendor _entity)
  147. {
  148. var result = await _sqlSugar.Updateable<Res_ItemVendor>()
  149. .SetColumns(it => it.FullName == _entity.FullName)
  150. .SetColumns(it => it.ShortName == _entity.ShortName)
  151. .SetColumns(it => it.Address == _entity.Address)
  152. .SetColumns(it => it.Linker == _entity.Linker)
  153. .SetColumns(it => it.Mobile == _entity.Mobile)
  154. .SetColumns(it => it.BusRange == _entity.BusRange)
  155. .SetColumns(it => it.Job == _entity.Job)
  156. .SetColumns(it => it.Remark == _entity.Remark)
  157. .Where(s => s.Id == _entity.Id)
  158. .ExecuteCommandAsync();
  159. return result > 0;
  160. }
  161. /// <summary>
  162. /// 删除供应商信息
  163. /// </summary>
  164. /// <param name="_entity"></param>
  165. /// <returns></returns>
  166. public async Task<bool> delVendorInfo(Res_ItemVendor _entity)
  167. {
  168. _entity.IsDel = 1;
  169. _entity.DeleteUserId = _entity.CreateUserId;
  170. _entity.DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  171. var result = await _sqlSugar.Updateable<Res_ItemVendor>()
  172. .SetColumns(it => it.IsDel == _entity.IsDel)
  173. .SetColumns(it => it.DeleteTime == _entity.DeleteTime)
  174. .SetColumns(it => it.DeleteUserId == _entity.DeleteUserId)
  175. .Where(s => s.Id == _entity.Id)
  176. .ExecuteCommandAsync();
  177. return result > 0;
  178. }
  179. #endregion
  180. #region 物料
  181. /// <summary>
  182. /// 物料信息列表查询
  183. /// </summary>
  184. /// <param name="_dto"></param>
  185. /// <returns></returns>
  186. public Res_ItemInfoListView GetItemList(Search_ItemInfoDto _dto)
  187. {
  188. string sqlWhere = string.Format(" Where d.IsDel=0 ");
  189. #region SqlWhere
  190. if (!string.IsNullOrEmpty(_dto.ItemName))//物料名称
  191. {
  192. sqlWhere += string.Format(" And d.ItemName Like '%{0}%' ", _dto.ItemName);
  193. }
  194. if (_dto.VendorId > 0)//供应商Id
  195. {
  196. sqlWhere += string.Format(" And d.VendorId = {0} ", _dto.VendorId);
  197. }
  198. if (_dto.ItemTypeId > 0)//类型Id
  199. {
  200. sqlWhere += string.Format(" And d.ItemTypeId = {0} ", _dto.ItemTypeId);
  201. }
  202. if (_dto.MinRate > 0)//价格区间(最低)
  203. {
  204. sqlWhere += string.Format(" And d.MinRate >= {0} ", _dto.MinRate);
  205. }
  206. if (_dto.MaxRate > 0)//价格区间(最高)
  207. {
  208. sqlWhere += string.Format(" And d.MaxRate <= {0} ", _dto.MaxRate);
  209. }
  210. //if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  211. //{
  212. // Regex r = new Regex("And");
  213. // sqlWhere = r.Replace(sqlWhere, "Where", 1);
  214. //}
  215. #endregion
  216. int currPIndex = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) * _dto.PageSize) + 1;
  217. int currPSize = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) + 1) * _dto.PageSize;
  218. string sql = string.Format(@" SELECT * FROM(
  219. Select ROW_NUMBER() Over(order By d.Id desc) as RowNumber, d.ItemName,d.Id as ItemId,d.VendorId,v.FullName as VendorFullName,
  220. v.ShortName as VendorShortName,t.Id as ItemTypeId,t.TypeName as ItemTypeName,
  221. d.MinRate,d.MaxRate,d.CurrRate,d.Remark
  222. From Res_ItemDetail as d With(Nolock)
  223. Inner Join Res_ItemVendor as v With(Nolock) On d.VendorId=v.Id
  224. Inner Join Res_ItemType as t With(Nolock) On d.ItemTypeId=t.Id
  225. {2}
  226. ) as tb Where tb.RowNumber Between {0} And {1} ", currPIndex, currPSize, sqlWhere);
  227. Res_ItemInfoListView rst = new Res_ItemInfoListView();
  228. rst.CurrPageIndex = currPIndex;
  229. rst.CurrPageSize = currPSize;
  230. rst.DataList = _sqlSugar.SqlQueryable<Res_ItemInfoView>(sql).ToList();
  231. if (rst.DataList.Count > 0)
  232. {
  233. string sqlCount = string.Format(@" Select Id From Res_ItemDetail as d With(Nolock) {0} ", sqlWhere);
  234. int dataCount = _sqlSugar.SqlQueryable<Res_ItemInfoView>(sqlCount).Count();
  235. rst.DataCount = dataCount;
  236. }
  237. return rst;
  238. }
  239. /// <summary>
  240. /// 物料详细信息查询
  241. /// </summary>
  242. /// <param name="_dto"></param>
  243. /// <returns></returns>
  244. public Res_ItemInfoView getItemInfo(Search_ItemInfoDto _dto)
  245. {
  246. string sqlWhere = string.Format(" Where IsDel=0 ");
  247. if (_dto.ItemId > 0)
  248. {
  249. string sql = string.Format(@" Select d.ItemName,d.Id as ItemId,d.VendorId,v.FullName as VendorFullName,v.ShortName as VendorShortName,t.Id as ItemTypeId,t.TypeName as ItemTypeName,
  250. d.MinRate,d.MaxRate,d.CurrRate,d.Remark
  251. From Res_ItemDetail as d With(Nolock)
  252. Inner Join Res_ItemType as t With(Nolock) On d.ItemTypeId=t.Id
  253. Inner Join Res_ItemVendor as v With(Nolock) On d.VendorId=v.Id
  254. Where d.Id={0} ", _dto.ItemId);
  255. Res_ItemInfoView _view = _sqlSugar.SqlQueryable<Res_ItemInfoView>(sql).First();
  256. return _view;
  257. }
  258. else
  259. {
  260. return null;
  261. }
  262. //return null;
  263. }
  264. /// <summary>
  265. /// 修改物料信息
  266. /// </summary>
  267. /// <param name="_entity"></param>
  268. /// <returns></returns>
  269. public async Task<bool> updItemInfo(Res_ItemDetailInfo _entity)
  270. {
  271. var source = await _sqlSugar.Queryable<Res_ItemDetailInfo>().Where(s => s.Id == _entity.Id).FirstAsync();
  272. if (source != null)
  273. {
  274. DateTime dtNow = DateTime.Now;
  275. if (_entity.CurrRate < source.MinRate)
  276. {
  277. _entity.MinRate = _entity.CurrRate;
  278. _entity.MinDt = dtNow;
  279. }
  280. else
  281. {
  282. _entity.MinRate = source.MinRate;
  283. _entity.MinDt = source.MinDt;
  284. }
  285. if (_entity.CurrRate > source.MaxRate)
  286. {
  287. _entity.MaxRate = _entity.CurrRate;
  288. _entity.MaxDt = dtNow;
  289. }
  290. else
  291. {
  292. _entity.MaxRate = source.MaxRate;
  293. _entity.MaxDt = source.MaxDt;
  294. }
  295. _entity.CurrDt = dtNow;
  296. Log.Warning("物料信息修改,原数据:【" + JsonSerializer.Serialize(source) + "】,修改参数:【" + JsonSerializer.Serialize(_entity) + "】");
  297. var result = await _sqlSugar.Updateable<Res_ItemDetailInfo>()
  298. .SetColumns(it => it.CurrDt == _entity.CurrDt)
  299. .SetColumns(it => it.CurrRate == _entity.CurrRate)
  300. .SetColumns(it => it.ItemName == _entity.ItemName)
  301. .SetColumns(it => it.MaxDt == _entity.MaxDt)
  302. .SetColumns(it => it.MaxRate == _entity.MaxRate)
  303. .SetColumns(it => it.MinDt == _entity.MinDt)
  304. .SetColumns(it => it.MinRate == _entity.MinRate)
  305. .SetColumns(it => it.Remark == _entity.Remark)
  306. .SetColumns(it => it.ItemTypeId == _entity.ItemTypeId)
  307. .SetColumns(it => it.VendorId == _entity.VendorId)
  308. .Where(s => s.Id == _entity.Id)
  309. .ExecuteCommandAsync();
  310. return result > 0;
  311. }
  312. return false;
  313. }
  314. /// <summary>
  315. /// 删除物料信息
  316. /// </summary>
  317. /// <param name="_entity"></param>
  318. /// <returns></returns>
  319. public async Task<bool> delItemInfo(Res_ItemDetailInfo _entity)
  320. {
  321. _entity.IsDel = 1;
  322. _entity.DeleteUserId = _entity.CreateUserId;
  323. _entity.DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  324. var result = await _sqlSugar.Updateable<Res_ItemDetailInfo>()
  325. .SetColumns(it => it.IsDel == _entity.IsDel)
  326. .SetColumns(it => it.DeleteTime == _entity.DeleteTime)
  327. .SetColumns(it => it.DeleteUserId == _entity.DeleteUserId)
  328. .Where(s => s.Id == _entity.Id)
  329. .ExecuteCommandAsync();
  330. return result > 0;
  331. }
  332. #endregion
  333. }
  334. }