集成,总结MES功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

375 lines
14 KiB

  1. using BPA.MES.Base.Application.Const;
  2. using BPA.MES.Base.Application.Entitys.Order;
  3. using BPA.MES.Base.Application.Services.OrderService.Dtos;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Yitter.IdGenerator;
  11. namespace BPA.MES.Base.Application.Services.OrderService.Service
  12. {
  13. [ApiDescriptionSettings("订单管理", Order = 2, Name = "Order", Tag = "订单信息", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
  14. public class OrderService : IOrderService, ITransient, IDynamicApiController
  15. {
  16. private readonly ISqlSugarClient _dbContext;
  17. public OrderService(ISqlSugarClient db)
  18. {
  19. _dbContext = db;
  20. }
  21. /// <summary>
  22. /// 分页列表
  23. /// </summary>
  24. /// <returns></returns>
  25. [HttpPost]
  26. public async Task<SqlSugarPagedList<OrderBaseDto>> PagedList(OrderQueryPageInput input)
  27. {
  28. var entity = await _dbContext.Queryable<OrderEntity>()
  29. .WhereIF(!string.IsNullOrEmpty(input.Id), a => a.Id.Contains(input.Id))
  30. .WhereIF(!string.IsNullOrEmpty(input.Status), a => ((int)a.Status).ToString()==input.Status)
  31. .Select((x) => new OrderBaseDto
  32. {
  33. Id = x.Id.SelectAll(),
  34. })
  35. .ToPagedListAsync(input.PageIndex, input.PageSize);
  36. var orderIds= entity.Items.Select(x => x.Id);
  37. var itemList = await _dbContext.Queryable<OrderInfoEntity>()
  38. .Where(x => orderIds.Contains(x.OrderId))
  39. .ToListAsync();
  40. var list = await _dbContext.Queryable<Pztj_WorkInfoEntity>()
  41. .LeftJoin<Pztj_FinalsInfoEntity>((a, b) => a.FinalId == b.Id)
  42. .LeftJoin<ProductLineEntity>((a, b, c) => a.LineId == c.Id)
  43. .LeftJoin<Pztj_WorkInfoStatusEntity>((a, b, c, d) => d.Id == SqlFunc.Subqueryable<Pztj_WorkInfoStatusEntity>().Where(s => s.WorkId == a.Id).OrderByDesc(s => s.CreateTime).Select(s => s.Id))
  44. .LeftJoin<OrderWorkEntity>((a, b, c, d,e) =>a.Id==e.WorkId )
  45. .Where((a, b, c, d, e)=> orderIds.Contains(e.OrderId))
  46. .Select((a, b, c, d,e) => new OrderInfoBaseDto
  47. {
  48. FinalslId=a.FinalId,
  49. FinalslName=b.Name,
  50. Number=string.IsNullOrEmpty(a.Number)?0:Convert.ToInt32(a.Number),
  51. OrderId=e.OrderId,
  52. ProductionLineId=a.LineId,
  53. ProductionLineName=c.Name,
  54. Id = a.Id,
  55. CreateTime=a.CreateTime.ToString(),
  56. Status = d.Status == null ? WorkOrderStatusEnum.Draft : d.Status
  57. }).ToListAsync();
  58. SqlSugarPagedList <OrderBaseDto> output = entity.Adapt<SqlSugarPagedList<OrderBaseDto>>();
  59. foreach (var item in output.Items)
  60. {
  61. item.OrderInfos= list.Where(x=>x.OrderId==item.Id).ToList();
  62. item.ProduceRate= list.Where(x => x.OrderId == item.Id&&x.Status== WorkOrderStatusEnum.Completed).Count();
  63. item.ProduceSum = itemList.Where(x => x.OrderId == item.Id).Sum(x => x.Number);
  64. }
  65. return output;
  66. }
  67. /// <summary>
  68. /// 新增
  69. /// </summary>
  70. /// <param name="inputDto"></param>
  71. /// <returns></returns>
  72. [HttpPost]
  73. public async Task<bool> Add(List<OrderInfoBaseDto> inputDto)
  74. {
  75. try
  76. {
  77. _dbContext.Ado.BeginTran();
  78. OrderEntity order = new OrderEntity();
  79. var res = await _dbContext.Insertable(order)
  80. .IgnoreColumns(ignoreNullColumn: true)
  81. .ExecuteCommandIdentityIntoEntityAsync();
  82. if (res)
  83. {
  84. List<OrderInfoEntity> orderInfo = inputDto.Adapt<List<OrderInfoEntity>>();
  85. orderInfo = orderInfo.GroupBy(x => x.FinalslId).Select(x => new OrderInfoEntity()
  86. {
  87. CreateTime = DateTime.Now,
  88. FinalslId = x.Key,
  89. Number = x.Sum(y => y.Number),
  90. }).ToList();
  91. foreach (var item in orderInfo)
  92. {
  93. item.Id = YitIdHelper.NextId().ToString();
  94. item.OrderId = order.Id;
  95. }
  96. res = await _dbContext.Insertable(orderInfo)
  97. .ExecuteCommandIdentityIntoEntityAsync();
  98. }
  99. _dbContext.Ado.CommitTran();
  100. return res;
  101. }
  102. catch (Exception ex)
  103. {
  104. _dbContext.Ado.RollbackTran();
  105. return false;
  106. }
  107. }
  108. /// <summary>
  109. /// 修改
  110. /// </summary>
  111. /// <param name="inputDto"></param>
  112. /// <returns></returns>
  113. [HttpPost]
  114. public async Task<bool> Update(List<OrderInfoBaseDto> inputDto)
  115. {
  116. try
  117. {
  118. _dbContext.Ado.BeginTran();
  119. var orderId = inputDto.Select(x => x.OrderId).First();
  120. //删除原来的所有
  121. var res = await _dbContext.Deleteable<OrderInfoEntity>(x => x.OrderId == orderId).ExecuteCommandHasChangeAsync();
  122. List<OrderInfoEntity> orderInfo = inputDto.Adapt<List<OrderInfoEntity>>();
  123. orderInfo = orderInfo.GroupBy(x => x.FinalslId).Select(x => new OrderInfoEntity()
  124. {
  125. CreateTime = DateTime.Now,
  126. FinalslId = x.Key,
  127. Number = x.Sum(y => y.Number),
  128. }).ToList();
  129. foreach (var item in orderInfo)
  130. {
  131. item.Id = YitIdHelper.NextId().ToString();
  132. item.OrderId = orderId;
  133. }
  134. res = await _dbContext.Insertable(orderInfo)
  135. .ExecuteCommandIdentityIntoEntityAsync();
  136. _dbContext.Ado.CommitTran();
  137. return res;
  138. }
  139. catch (Exception ex)
  140. {
  141. _dbContext.Ado.RollbackTran();
  142. return false;
  143. }
  144. }
  145. /// <summary>
  146. /// 删除
  147. /// </summary>
  148. /// <param name="input"></param>
  149. /// <returns></returns>
  150. [HttpPost]
  151. public async Task<bool> Del(string Id)
  152. {
  153. try
  154. {
  155. _dbContext.Ado.BeginTran();
  156. var res = await _dbContext.Deleteable<OrderEntity>().Where(x => x.Id == Id).ExecuteCommandHasChangeAsync();
  157. if (res)
  158. {
  159. res = await _dbContext.Deleteable<OrderInfoEntity>().Where(x => x.OrderId == Id).ExecuteCommandHasChangeAsync();
  160. }
  161. _dbContext.Ado.CommitTran();
  162. return res;
  163. }
  164. catch (Exception ex)
  165. {
  166. _dbContext.Ado.RollbackTran();
  167. return false;
  168. }
  169. }
  170. /// <summary>
  171. /// 获取订单的商品
  172. /// </summary>
  173. /// <param name="input"></param>
  174. /// <returns></returns>
  175. [HttpGet]
  176. public async Task<List<OrderInfoBaseDto>> GetOrderMaterial(string OrderId)
  177. {
  178. var res = await _dbContext.Queryable<OrderInfoEntity>().Where(x => x.OrderId == OrderId)
  179. .Select(x => new OrderInfoBaseDto()
  180. {
  181. Id = x.Id,
  182. FinalslId = x.FinalslId,
  183. Number = x.Number,
  184. OrderId = x.OrderId,
  185. })
  186. .ToListAsync();
  187. return res;
  188. }
  189. /// <summary>
  190. /// 添加工单 并绑定订单
  191. /// </summary>
  192. /// <param name="inputDto"></param>
  193. /// <returns></returns>
  194. public async Task<bool> OrderAssociationWork(List<OrderInfoBaseDto> inputDto)
  195. {
  196. var result = false;
  197. var orderId = inputDto.Select(x => x.OrderId).First();
  198. //验证
  199. VerifyOrderProduct(inputDto);
  200. _dbContext.Ado.BeginTran();
  201. try
  202. {
  203. foreach (var item in inputDto)
  204. {
  205. Pztj_WorkInfoEntity entity = new Pztj_WorkInfoEntity()
  206. {
  207. CreateTime = DateTime.Now,
  208. FinalId = item.FinalslId,
  209. LineId = item.ProductionLineId,
  210. Name = "",
  211. Number = item.Number.ToString(),
  212. };
  213. var res= await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
  214. Pztj_WorkInfoStatusEntity pztj_WorkInfoStatusEntity = new()
  215. {
  216. Status = WorkOrderStatusEnum.Draft,
  217. WorkId = entity.Id
  218. };
  219. if (res >0)
  220. {
  221. res= await _dbContext.Insertable(pztj_WorkInfoStatusEntity).IgnoreColumns(true).ExecuteCommandAsync();
  222. if (res>0)
  223. {
  224. res = await _dbContext.Insertable(new OrderWorkEntity()
  225. {
  226. OrderId = orderId,
  227. WorkId=entity.Id
  228. }).IgnoreColumns(true).ExecuteCommandAsync();
  229. if (res>0)
  230. {
  231. var order=_dbContext.Queryable< OrderEntity >().First(x=>x.Id==orderId);
  232. order.Status = OrderStatusEnum.Ongoing;
  233. //修改订单状态
  234. res =await _dbContext.Updateable(order).ExecuteCommandAsync();
  235. if (res<=0)
  236. {
  237. _dbContext.Ado.RollbackTran();
  238. return false;
  239. }
  240. }
  241. else
  242. {
  243. _dbContext.Ado.RollbackTran();
  244. return false;
  245. }
  246. }
  247. else
  248. {
  249. _dbContext.Ado.RollbackTran();
  250. return false;
  251. }
  252. }
  253. else
  254. {
  255. _dbContext.Ado.RollbackTran();
  256. return false;
  257. }
  258. }
  259. }
  260. catch
  261. {
  262. _dbContext.Ado.RollbackTran();
  263. throw Oops.Bah("添加失败!");
  264. }
  265. _dbContext.Ado.CommitTran();
  266. return true;
  267. }
  268. /// <summary>
  269. /// 生成工单时验证成品数量
  270. /// </summary>
  271. /// <param name="inputDto"></param>
  272. private void VerifyOrderProduct(List<OrderInfoBaseDto> inputDto)
  273. {
  274. var orderId = inputDto.Select(x => x.OrderId).First();
  275. var data=_dbContext.Queryable<OrderInfoEntity, Pztj_FinalsInfoEntity>((a,b)=>new JoinQueryInfos(
  276. JoinType.Left,a.FinalslId==b.Id
  277. )).Where((a,b)=>a.OrderId==orderId).
  278. Select((a,b)=>new
  279. {
  280. orderId = a.OrderId,
  281. ProductId = a.FinalslId,
  282. productName = b.Name,
  283. a.Number
  284. }).ToList();
  285. foreach (var item in data)
  286. {
  287. var productCount = inputDto.Where(x => x.FinalslId == item.ProductId).Sum(x => x.Number);
  288. var count= item.Number - productCount;
  289. if (count<0)
  290. {
  291. throw Oops.Bah($"{item.productName}成品生成工单数量大于订单数量,多【{System.Math.Abs(count)}】");
  292. }
  293. if (count>0)
  294. {
  295. throw Oops.Bah($"{item.productName}成品生成工单数量小于订单数量,差【{count}】");
  296. }
  297. }
  298. }
  299. /// <summary>
  300. /// 获取成品剩余制作数量
  301. /// </summary>
  302. /// <param name="id"></param>
  303. /// <returns></returns>
  304. public async Task<List<OrderWorkLimit>> GetFinalslLImit(string OrderId)
  305. {
  306. var result = new List<OrderWorkLimit>();
  307. var orderInfo=_dbContext.Queryable<OrderInfoEntity>().Where(x=>x.OrderId==OrderId).ToList();
  308. var list=_dbContext.Queryable<OrderWorkEntity , Pztj_WorkInfoEntity>((a,b)=>new JoinQueryInfos(
  309. JoinType.Left,a.WorkId==b.Id))
  310. .Where((a, b) => a.OrderId==OrderId)
  311. .Select((a,b)=>new OrderWorkLimit()
  312. {
  313. FinalslId=b.FinalId,
  314. Limit=0
  315. }).ToList();
  316. foreach (var item in orderInfo)
  317. {
  318. var data=list.Where(x=>x.FinalslId==item.FinalslId).ToList();
  319. result.Add(new OrderWorkLimit()
  320. {
  321. FinalslId=item.FinalslId,
  322. Limit=item.Number-data.Sum(x=>x.Limit),
  323. });
  324. }
  325. return result;
  326. }
  327. [HttpGet]
  328. public Task<bool> CodeFirst()
  329. {
  330. var types = Assembly.Load("BPA.MES.Base.Application").GetTypes()
  331. .Where(x => x.GetCustomAttribute<SugarTable>() != null
  332. && x.Namespace == "BPA.MES.Base.Application.Entitys.Order").ToArray();
  333. _dbContext.CodeFirst.InitTables(types);
  334. return Task.FromResult(true);
  335. }
  336. }
  337. }