ResItemInfoRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. return null;
  118. }
  119. /// <summary>
  120. /// 创建供应商信息
  121. /// </summary>
  122. /// <param name="_dto"></param>
  123. /// <returns></returns>
  124. public async Task<bool> addVendorInfo(Edit_ResItemVendorDto _dto)
  125. {
  126. Dictionary<string, object> dic = new Dictionary<string, object>();
  127. dic.Add("FullName", _dto.VendorFullName);
  128. dic.Add("ShortName", string.IsNullOrEmpty(_dto.VendorShortName) ? "" : _dto.VendorShortName);
  129. dic.Add("Address", string.IsNullOrEmpty(_dto.VendorAddress) ? "" : _dto.VendorAddress);
  130. dic.Add("Linker", _dto.VendorLinker);
  131. dic.Add("Job", string.IsNullOrEmpty(_dto.VendorJob) ? "" : _dto.VendorJob);
  132. dic.Add("Mobile", _dto.VendorMobile);
  133. dic.Add("CreateUserId", _dto.SysUserId);
  134. dic.Add("CreateTime", DateTime.Now);
  135. dic.Add("DeleteUserId", 0);
  136. dic.Add("DeleteTime", "Null");
  137. dic.Add("Remark", _dto.Remark);
  138. dic.Add("IsDel", 0);
  139. dic.Add("BusRange", string.IsNullOrEmpty(_dto.BusRange) ? "," : _dto.BusRange);
  140. return await InsertDataByDictionary(dic, "Res_ItemVendor");
  141. }
  142. /// <summary>
  143. /// 修改供应商信息
  144. /// </summary>
  145. /// <param name="_dto"></param>
  146. /// <returns></returns>
  147. public async Task<bool> updVendorInfo(Res_ItemVendor _entity)
  148. {
  149. var result = await _sqlSugar.Updateable<Res_ItemVendor>()
  150. .SetColumns(it => it.FullName == _entity.FullName)
  151. .SetColumns(it => it.ShortName == _entity.ShortName)
  152. .SetColumns(it => it.Address == _entity.Address)
  153. .SetColumns(it => it.Linker == _entity.Linker)
  154. .SetColumns(it => it.Mobile == _entity.Mobile)
  155. .SetColumns(it => it.BusRange == _entity.BusRange)
  156. .SetColumns(it => it.Job == _entity.Job)
  157. .SetColumns(it => it.Remark == _entity.Remark)
  158. .Where(s => s.Id == _entity.Id)
  159. .ExecuteCommandAsync();
  160. return result > 0;
  161. }
  162. /// <summary>
  163. /// 删除供应商信息
  164. /// </summary>
  165. /// <param name="_entity"></param>
  166. /// <returns></returns>
  167. public async Task<bool> delVendorInfo(Res_ItemVendor _entity)
  168. {
  169. _entity.IsDel = 1;
  170. _entity.DeleteUserId = _entity.CreateUserId;
  171. _entity.DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  172. var result = await _sqlSugar.Updateable<Res_ItemVendor>()
  173. .SetColumns(it => it.IsDel == _entity.IsDel)
  174. .SetColumns(it => it.DeleteTime == _entity.DeleteTime)
  175. .SetColumns(it => it.DeleteUserId == _entity.DeleteUserId)
  176. .Where(s => s.Id == _entity.Id)
  177. .ExecuteCommandAsync();
  178. return result > 0;
  179. }
  180. #endregion
  181. #region 物料
  182. /// <summary>
  183. /// 物料信息列表查询
  184. /// </summary>
  185. /// <param name="_dto"></param>
  186. /// <returns></returns>
  187. public Res_ItemInfoListView GetItemList(Search_ItemInfoDto _dto)
  188. {
  189. string sqlWhere = string.Format(" Where d.IsDel=0 ");
  190. #region SqlWhere
  191. if (!string.IsNullOrEmpty(_dto.ItemName))//物料名称
  192. {
  193. sqlWhere += string.Format(" And d.ItemName Like '%{0}%' ", _dto.ItemName);
  194. }
  195. if (_dto.VendorId > 0)//供应商Id
  196. {
  197. sqlWhere += string.Format(" And d.VendorId = {0} ", _dto.VendorId);
  198. }
  199. if (_dto.ItemTypeId > 0)//类型Id
  200. {
  201. sqlWhere += string.Format(" And d.ItemTypeId = {0} ", _dto.ItemTypeId);
  202. }
  203. if (_dto.MinRate > 0)//价格区间(最低)
  204. {
  205. sqlWhere += string.Format(" And d.MinRate >= {0} ", _dto.MinRate);
  206. }
  207. if (_dto.MaxRate > 0)//价格区间(最高)
  208. {
  209. sqlWhere += string.Format(" And d.MaxRate <= {0} ", _dto.MaxRate);
  210. }
  211. //if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  212. //{
  213. // Regex r = new Regex("And");
  214. // sqlWhere = r.Replace(sqlWhere, "Where", 1);
  215. //}
  216. #endregion
  217. int currPIndex = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) * _dto.PageSize) + 1;
  218. int currPSize = (((_dto.PageIndex > 0) ? (_dto.PageIndex - 1) : 0) + 1) * _dto.PageSize;
  219. string sql = string.Format(@" SELECT * FROM(
  220. Select ROW_NUMBER() Over(order By d.Id desc) as RowNumber, d.ItemName,d.Id as ItemId,d.VendorId,v.FullName as VendorFullName,
  221. v.ShortName as VendorShortName,t.Id as ItemTypeId,t.TypeName as ItemTypeName,
  222. d.MinRate,d.MaxRate,d.CurrRate,d.Remark
  223. From Res_ItemDetail as d With(Nolock)
  224. Inner Join Res_ItemVendor as v With(Nolock) On d.VendorId=v.Id
  225. Inner Join Res_ItemType as t With(Nolock) On d.ItemTypeId=t.Id
  226. {2}
  227. ) as tb Where tb.RowNumber Between {0} And {1} ", currPIndex, currPSize, sqlWhere);
  228. Res_ItemInfoListView rst = new Res_ItemInfoListView();
  229. rst.CurrPageIndex = currPIndex;
  230. rst.CurrPageSize = currPSize;
  231. rst.DataList = _sqlSugar.SqlQueryable<Res_ItemInfoView>(sql).ToList();
  232. if (rst.DataList.Count > 0)
  233. {
  234. string sqlCount = string.Format(@" Select Id From Res_ItemDetail as d With(Nolock) {0} ", sqlWhere);
  235. int dataCount = _sqlSugar.SqlQueryable<Res_ItemInfoView>(sqlCount).Count();
  236. rst.DataCount = dataCount;
  237. }
  238. return rst;
  239. }
  240. /// <summary>
  241. /// 物料详细信息查询
  242. /// </summary>
  243. /// <param name="_dto"></param>
  244. /// <returns></returns>
  245. public Res_ItemInfoView getItemInfo(Search_ItemInfoDto _dto)
  246. {
  247. string sqlWhere = string.Format(" Where IsDel=0 ");
  248. if (_dto.ItemId > 0)
  249. {
  250. 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,
  251. d.MinRate,d.MaxRate,d.CurrRate,d.Remark
  252. From Res_ItemDetail as d With(Nolock)
  253. Inner Join Res_ItemType as t With(Nolock) On d.ItemTypeId=t.Id
  254. Inner Join Res_ItemVendor as v With(Nolock) On d.VendorId=v.Id
  255. Where d.Id={0} ", _dto.ItemId);
  256. Res_ItemInfoView _view = _sqlSugar.SqlQueryable<Res_ItemInfoView>(sql).First();
  257. return _view;
  258. }
  259. else
  260. {
  261. return null;
  262. }
  263. return null;
  264. }
  265. /// <summary>
  266. /// 修改物料信息
  267. /// </summary>
  268. /// <param name="_entity"></param>
  269. /// <returns></returns>
  270. public async Task<bool> updItemInfo(Res_ItemDetailInfo _entity)
  271. {
  272. var source = await _sqlSugar.Queryable<Res_ItemDetailInfo>().Where(s => s.Id == _entity.Id).FirstAsync();
  273. if (source != null)
  274. {
  275. DateTime dtNow = DateTime.Now;
  276. if (_entity.CurrRate < source.MinRate)
  277. {
  278. _entity.MinRate = _entity.CurrRate;
  279. _entity.MinDt = dtNow;
  280. }
  281. else
  282. {
  283. _entity.MinRate = source.MinRate;
  284. _entity.MinDt = source.MinDt;
  285. }
  286. if (_entity.CurrRate > source.MaxRate)
  287. {
  288. _entity.MaxRate = _entity.CurrRate;
  289. _entity.MaxDt = dtNow;
  290. }
  291. else
  292. {
  293. _entity.MaxRate = source.MaxRate;
  294. _entity.MaxDt = source.MaxDt;
  295. }
  296. _entity.CurrDt = dtNow;
  297. Log.Warning("物料信息修改,原数据:【" + JsonSerializer.Serialize(source) + "】,修改参数:【" + JsonSerializer.Serialize(_entity) + "】");
  298. var result = await _sqlSugar.Updateable<Res_ItemDetailInfo>()
  299. .SetColumns(it => it.CurrDt == _entity.CurrDt)
  300. .SetColumns(it => it.CurrRate == _entity.CurrRate)
  301. .SetColumns(it => it.ItemName == _entity.ItemName)
  302. .SetColumns(it => it.MaxDt == _entity.MaxDt)
  303. .SetColumns(it => it.MaxRate == _entity.MaxRate)
  304. .SetColumns(it => it.MinDt == _entity.MinDt)
  305. .SetColumns(it => it.MinRate == _entity.MinRate)
  306. .SetColumns(it => it.Remark == _entity.Remark)
  307. .SetColumns(it => it.ItemTypeId == _entity.ItemTypeId)
  308. .SetColumns(it => it.VendorId == _entity.VendorId)
  309. .Where(s => s.Id == _entity.Id)
  310. .ExecuteCommandAsync();
  311. return result > 0;
  312. }
  313. return false;
  314. }
  315. /// <summary>
  316. /// 删除物料信息
  317. /// </summary>
  318. /// <param name="_entity"></param>
  319. /// <returns></returns>
  320. public async Task<bool> delItemInfo(Res_ItemDetailInfo _entity)
  321. {
  322. _entity.IsDel = 1;
  323. _entity.DeleteUserId = _entity.CreateUserId;
  324. _entity.DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  325. var result = await _sqlSugar.Updateable<Res_ItemDetailInfo>()
  326. .SetColumns(it => it.IsDel == _entity.IsDel)
  327. .SetColumns(it => it.DeleteTime == _entity.DeleteTime)
  328. .SetColumns(it => it.DeleteUserId == _entity.DeleteUserId)
  329. .Where(s => s.Id == _entity.Id)
  330. .ExecuteCommandAsync();
  331. return result > 0;
  332. }
  333. #endregion
  334. }
  335. }