FBAOrderRepository.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. using Dapper;
  2. using Microsoft.AspNetCore.Http;
  3. using Nest;
  4. using NPOI.SS.Formula.Functions;
  5. using StackExchange.Redis;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using XYY.Common.Standard;
  13. using XYY.Core.Standard.Data.Infrastructure;
  14. using XYY.Model.Standard.Channel;
  15. using XYY.Model.Standard.DingTalk;
  16. using XYY.Model.Standard.Dto.MpsOrder;
  17. using XYY.Model.Standard.Enums;
  18. using XYY.Model.Standard.Finance;
  19. using XYY.Model.Standard.MpsOrder;
  20. using XYY.Model.Standard.MpsOrder.dto;
  21. using XYY.Model.Standard.MpsOrder.Report;
  22. using XYY.Model.Standard.MpsOrder.views;
  23. using XYY.Model.Standard.Order.FBA;
  24. using XYY.Service.Standard.Finance.Charging.Dto;
  25. namespace XYY.Data.Standard.Order
  26. {
  27. public interface IFBAOrderRepository : IBaseRepository<Order_MpsOrder>
  28. {
  29. Task<PageResult<View_Order_ReceivingInfo>> GetReceivingInfo(QueryModel queryModel);
  30. Task<IEnumerable<Order_MpsOrderBox>> GetReceivingBoxs(IEnumerable<int> orderIds);
  31. Task<IEnumerable<Order_MpsOrderBox>> GetReceivingBoxs(IEnumerable<string> systemNos);
  32. Task<bool> UpdateReceivingBoxs(List<Order_MpsOrderBox> boxs);
  33. Task<bool> UpdateReceivingMpsOrders(List<MpsOrderWeight> mps);
  34. Task<IEnumerable<string>> CheackBoxIntegrity(IEnumerable<int> OrderIds);
  35. Task<bool> UpdateMpsOrdersStatus(IEnumerable<int> OrderIds, int status);
  36. Task<bool> UpdateOrderPrintQty(int orderId, bool OrderPrintQty);
  37. Task<View_Order_ReceivingInfo> GetOrderReceivingInfo(int orderId);
  38. Task<bool> UpdateOrderReceiveTime(IEnumerable<int> MpsOrderIds);
  39. Task SaveOrderRceiveingInfo(View_Order_ReceivingInfo order);
  40. Task<PageResult<View_Order_Print>> GetPrintList(QueryModel queryModel);
  41. Task<ReceivingAddress> GetReceivingAddress(int orderId);
  42. Task<IEnumerable<Order_MpsOrderGoods>> GetReceivingGoods(int orderId);
  43. Task<View_Order_Print> GetPrint(int orderId);
  44. Task SavePrintInfos(View_Order_Print order);
  45. Task<bool> UpdatePrintBoxs(List<Order_MpsOrderBox> boxs);
  46. Task<bool> UpdatePrintReviceAddress(ReceivingAddress receivingAddress, int OrderId);
  47. Task<bool> UpdatePrintGoods(List<Order_MpsOrderGoods> orderGoods, int OrderId);
  48. Task<List<FBA_Incidental>> GetFBA_Incidentals(int OrderId, FBAIncidentalCalculateTarget target);
  49. Task<bool> SaveFBA_Incidentals(List<FBA_Incidental> fBA_Incidentals, int OrderId, bool isSettlement = false);
  50. Task<bool> DelFBA_Incidentals(int OrderId, bool isSettlement = false);
  51. Task<decimal> GetUnitPrice(KPUnitPriceRequest dto);
  52. Task<bool> UpdateCustomerOrderConfirm(IEnumerable<CustomerConfirmOrderDto> customerConfirmOrderDtos, int OrderId);
  53. Task<bool> UpdateSettlementOrderConfirm(IEnumerable<SettlementConfirmOrderDto> settlementConfirmOrderDtos, int OrderId);
  54. Task<IEnumerable<ConfirmWeight>> GetBatchConfirmWeight(IEnumerable<int> Ids);
  55. Task<IEnumerable<ConfirmWeight>> GetBatchSettlementWeight(IEnumerable<int> Ids);
  56. Task<bool> UpdateBatchSettlementWeight(List<ConfirmWeight> confirmWeights);
  57. Task<bool> UpdateBatchConfirmWeight(List<ConfirmWeight> confirmWeights);
  58. Task<bool> UpdateBatchConfirmIncidenta(List<ConfirmIncidenta> confirmIncidentas);
  59. Task<bool> UpdateBatchSettlementConfirmIncidenta(List<ConfirmIncidenta> confirmIncidentas);
  60. Task<IEnumerable<FBA_Incidental>> GetBatchIncidenta(IEnumerable<int> Ids, FBAIncidentalCalculateTarget target);
  61. Task<bool> UpdateCustomerConfirmBoxs(IEnumerable<Order_MpsOrderBox> mpsOrderBoxes);
  62. Task<bool> UpdateSettlementConfirmBoxs(IEnumerable<Order_MpsOrderBox> mpsOrderBoxes);
  63. Task<Order_MpsOrder> GetMpsOrder(int OrderId);
  64. Task<IEnumerable<View_Order_CustomerConfirm>> GetCustomerConfirmList(IEnumerable<int> Ids);
  65. Task<IEnumerable<View_Order_SettlementConfirm>> GetSettlementConfirmList(IEnumerable<int> Ids);
  66. Task<bool> UpdateCharge(KPCharges kPCharges);
  67. Task<PageResult<View_Order_CustomerConfirm>> GetCustomerConfirmList(QueryModel queryModel);
  68. Task SaveCustomerConfirm(View_Order_CustomerConfirm order);
  69. Task<View_Order_CustomerConfirm> GetCustomerConfirm(int orderId);
  70. Task<PageResult<View_Order_SettlementConfirm>> GetSettlementConfirmList(QueryModel queryModel);
  71. Task SaveSettlementConfirm(View_Order_SettlementConfirm order);
  72. Task<View_Order_SettlementConfirm> GetSettlementConfirm(int orderId);
  73. Task<bool> SettlementConfirmOperation(int orderId, string NiceName);
  74. Task<bool> UpdateSettlementConfirmForTable(MpsSettlementConfirmTableData dto);
  75. Task<PageResult<GrossProfitByCustomer>> GetGrossProfitByCustomer(QueryModel qm);
  76. Task<PageResult<GrossProfitByOrder>> GetGrossProfitByOrder(QueryModel qm);
  77. Task<Order_MpsOrderBoxPicture> GetBoxPictureOrderInfo(string boxNumber);
  78. Task<bool> AddBoxPicture(Order_MpsOrderBoxPicture model);
  79. Task<bool> RemoveBoxPicture(int[] PicIds);
  80. Task<List<Order_MpsOrderBoxPicture>> GetBoxPictures(string boxNumber);
  81. Task<List<VerifyBoxInfo>> ImportMpsBoxDataVerify(List<string> boxNumbers);
  82. Task<bool> UpdateImportReceivingBoxs(List<Order_MpsOrderBox> boxs);
  83. Task<List<WaitConfirmDto>> GetExportWaitConfirm(IEnumerable<int> Ids);
  84. Task<List<Logistics_Channel>> GetChannels(List<int> channelIds);
  85. Task<List<FBA_Incidental>> GetIncidentalsByOrderIds(List<int> OrderIds, FBAIncidentalCalculateTarget target);
  86. }
  87. public class FBAOrderRepository : BaseRepository<Order_MpsOrder>, IFBAOrderRepository
  88. {
  89. public FBAOrderRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
  90. {
  91. }
  92. public Task<View_Order_CustomerConfirm> GetCustomerConfirm(int orderId)
  93. {
  94. return _unitOfWork.GetAsync<View_Order_CustomerConfirm>(orderId);
  95. }
  96. public Task<IEnumerable<Order_MpsOrderBox>> GetReceivingBoxs(IEnumerable<int> orderIds)
  97. {
  98. return _unitOfWork.QueryBySqlAsync<Order_MpsOrderBox>(@$"select b.* from Order_MpsOrder(nolock) a
  99. left join Order_MpsOrderBox(nolock) b on b.MpsOrderId=a.Id
  100. where a.OrderId in @orderIds ", null, new { orderIds = orderIds });
  101. }
  102. public Task<IEnumerable<Order_MpsOrderBox>> GetReceivingBoxs(IEnumerable<string> systemNos)
  103. {
  104. return _unitOfWork.QueryBySqlAsync<Order_MpsOrderBox>(@$"select b.* from Order_MpsOrder(nolock) a
  105. left join Order_MpsOrderBox(nolock) b on b.MpsOrderId=a.Id
  106. where a.TransferNumber in @TransferNumber ", null, new { TransferNumber = systemNos });
  107. }
  108. public async Task<bool> UpdateReceivingBoxs(List<Order_MpsOrderBox> boxs)
  109. {
  110. bool result = true;
  111. try
  112. {
  113. //比逐条更新稍微快点
  114. string sql = @" update Order_MpsOrderBox set OpInputWeight = @OpInputWeight,OpLength=@OpLength,OpWidth=@OpWidth,OpHeight=@OpHeight,VolumeWeight=@VolumeWeight,VolumeFactor=@VolumeFactor,GoodsNames=@GoodsNames,Remark=@Remark,Identification=@Identification where Id = @Id ";
  115. await _unitOfWork.ExecuteAsync(sql, boxs);
  116. }
  117. catch (Exception ex)
  118. {
  119. result = false;
  120. }
  121. return result;
  122. }
  123. public async Task<bool> UpdateReceivingMpsOrders(List<MpsOrderWeight> mps)
  124. {
  125. bool result = true;
  126. try
  127. {
  128. //比逐条更新稍微快点
  129. string sql = @" update Order_MpsOrder set OPInputWeight=@InputWeight,OPVolumeWeight=@VolumeWeight,OpBillWeight=@OpBillWeight,GoodsNames=@GoodsNames,Remark=@Remark,Identification=@Identification where Id=@MpsOrderId";
  130. await _unitOfWork.ExecuteAsync(sql, mps);
  131. }
  132. catch (Exception ex)
  133. {
  134. throw ex;
  135. result = false;
  136. }
  137. return result;
  138. }
  139. public async Task<IEnumerable<string>> CheackBoxIntegrity(IEnumerable<int> OrderIds)
  140. {
  141. string sql = $@"select a.CustomerOrderNumber from Order_MpsOrder(nolock) a
  142. left join Order_MpsOrderBox(nolock) b on b.MpsOrderId=a.Id
  143. where a.OrderId in @ids and ( 0=ISNULL(b.OpInputWeight,0) or 0=ISNULL(b.VolumeWeight,0) )";
  144. return await _unitOfWork.QueryBySqlAsync<string>(sql, null, new { ids = OrderIds });
  145. }
  146. public async Task<bool> UpdateMpsOrdersStatus(IEnumerable<int> OrderIds, int status)
  147. {
  148. //转入时统一做一次箱子状态更新 和重量的更新
  149. StringBuilder sb = new StringBuilder();
  150. sb.Append($"update Order_MpsOrderBox set BoxOutStatus={(int)BoxOutStatus.Receipt} where MpsOrderId in (select Id from Order_MpsOrder(nolock) where OrderId in @OrderIds);");
  151. //仅转打单才更新
  152. if (status == ((int)MpsOrderStatus.打单))
  153. {
  154. //更新收件时间,订单状态
  155. sb.Append($" update Order_Order set OrderStatus=2 where Id in @OrderIds;");
  156. }
  157. sb.Append($" update Order_MpsOrder set Status={status} where OrderId in @OrderIds ");
  158. await _unitOfWork.ExecuteAsync(sb.ToString(), new { OrderIds = OrderIds });
  159. return true;
  160. }
  161. public async Task<bool> UpdateOrderPrintQty(int orderId, bool OrderPrintQty)
  162. {
  163. string sql = $"update Order_MpsOrder set OrderPrintQty={(OrderPrintQty ? 1 : 0)} where OrderId=@OrderIds";
  164. await _unitOfWork.ExecuteAsync(sql, new { OrderIds = orderId });
  165. return true;
  166. }
  167. public async Task<bool> UpdateOrderReceiveTime(IEnumerable<int> MpsOrderIds)
  168. {
  169. string sql = $"update Order_Order set ReceiveTime=GETDATE() where Id in (select OrderId from Order_MpsOrder(nolock) where Id in @orderIds) and isnull(ReceiveTime,'')='' ";
  170. await _unitOfWork.ExecuteAsync(sql, new { orderIds = MpsOrderIds });
  171. return true;
  172. }
  173. public async Task<ReceivingAddress> GetReceivingAddress(int orderId)
  174. {
  175. string sql = @$"select a.ReceiverName,a.ReceiverPhone,a.ReceiverCountryCode,b.WarehouseCode,a.ReceiverEmail,b.DeclarationMethod
  176. ,a.ReceiverState,a.ReceiverCity,a.ReceiverZipCode,a.House,a.ReceiverStreet,a.ReceiverCompany,b.ReceiverStreet1,b.ReceiverStreet2
  177. from Order_Order(nolock) a
  178. left join Order_MpsOrder(nolock) b on b.Id=a.MpsOrderId
  179. where a.Id={orderId}";
  180. return (await _unitOfWork.QueryBySqlAsync<ReceivingAddress>(sql)).FirstOrDefault();
  181. }
  182. public Task<IEnumerable<Order_MpsOrderGoods>> GetReceivingGoods(int orderId)
  183. {
  184. string sql = @$"select b.* from Order_Order(nolock) a
  185. left join Order_MpsOrderGoods(nolock) b on b.MpsOrderId=a.MpsOrderId
  186. where a.Id={orderId}";
  187. return _unitOfWork.QueryBySqlAsync<Order_MpsOrderGoods>(sql);
  188. }
  189. public async Task<bool> UpdatePrintBoxs(List<Order_MpsOrderBox> boxs)
  190. {
  191. bool result = true;
  192. if (boxs == null || boxs.Count == 0) { return result; }
  193. try
  194. {
  195. //比逐条更新稍微快点
  196. string sql = @" update Order_MpsOrderBox set ChannelWeight = @ChannelWeight,ChannelLength=@ChannelLength,ChannelWidth=@ChannelWidth,ChannelHeight=@ChannelHeight where Id = @Id ";
  197. await _unitOfWork.ExecuteAsync(sql, boxs);
  198. await _unitOfWork.ExecuteAsync($" update Order_MpsOrder set ChannelInputWeight={boxs.Sum(x => (x.ChannelWeight ?? 0))} where Id=" + boxs.FirstOrDefault().MpsOrderId);
  199. }
  200. catch (Exception ex)
  201. {
  202. result = false;
  203. }
  204. return result;
  205. }
  206. public async Task<bool> UpdatePrintReviceAddress(ReceivingAddress receivingAddress, int OrderId)
  207. {
  208. bool result = true;
  209. if (receivingAddress == null) { return result; }
  210. try
  211. {
  212. string sql = @$" update Order_Order set ReceiverName=@ReceiverName,ReceiverPhone=@ReceiverPhone,ReceiverCountryCode=@ReceiverCountryCode,ReceiverEmail=@ReceiverEmail,ReceiverState=@ReceiverState,ReceiverCity=@ReceiverCity,ReceiverZipCode=@ReceiverZipCode,House=@House,ReceiverStreet=@ReceiverStreet1,ReceiverCompany=@ReceiverCompany where Id={OrderId};
  213. update Order_MpsOrder set WarehouseCode=@WarehouseCode,DeclarationMethod=@DeclarationMethod,ReceiverStreet1=@ReceiverStreet1,ReceiverStreet2=@ReceiverStreet2 where OrderId={OrderId} ";
  214. await _unitOfWork.ExecuteAsync(sql, receivingAddress);
  215. }
  216. catch (Exception ex)
  217. {
  218. result = false;
  219. }
  220. return result;
  221. }
  222. public async Task<bool> UpdatePrintGoods(List<Order_MpsOrderGoods> orderGoods, int OrderId)
  223. {
  224. bool result = true;
  225. if (orderGoods == null || orderGoods.Count == 0) { return result; }
  226. try
  227. {
  228. string sql = @$" update Order_MpsOrderGoods set CnName=@CnName,EnName=@EnName,Quantity=@Quantity,GoodsValue=@GoodsValue,Brand=@Brand,Meterial=@Meterial,SaleLink=@SaleLink,HsCode=@HsCode,Magnetic=@Magnetic,Electric=@Electric,Liquid=@Liquid where Id=@Id ";
  229. await _unitOfWork.ExecuteAsync(sql, orderGoods);
  230. string orderSqlSet = string.Empty;
  231. if (orderGoods.Any(x => x.Magnetic))
  232. {
  233. orderSqlSet += "Magnetic=1,";
  234. }
  235. if (orderGoods.Any(x => x.Electric))
  236. {
  237. orderSqlSet += "Electric=1,";
  238. }
  239. if (orderGoods.Any(x => x.Liquid))
  240. {
  241. orderSqlSet += "Liquid=1,";
  242. }
  243. if (!string.IsNullOrEmpty(orderSqlSet))
  244. {
  245. await _unitOfWork.ExecuteAsync("update Order_MpsOrder set " + orderSqlSet.TrimEnd(',') + $" where OrderId={OrderId}");
  246. }
  247. }
  248. catch (Exception ex)
  249. {
  250. result = false;
  251. }
  252. return result;
  253. }
  254. public async Task<PageResult<View_Order_CustomerConfirm>> GetCustomerConfirmList(QueryModel qm)
  255. {
  256. #region 基础sql
  257. string AdditionalSql = string.Empty;
  258. var qmSystemNo = qm?.QueryParamer?.Where(x => x.Filed == "SystemNo")?.FirstOrDefault();
  259. string where = string.Empty;
  260. List<string> numbers = new List<string>();
  261. if (qmSystemNo != null && !string.IsNullOrEmpty(qmSystemNo.Value))
  262. {
  263. AdditionalSql = $" and (SystemNo in @qmSystemNo or CustomerOrderNo in @qmSystemNo)";
  264. numbers.AddRange(qmSystemNo.Value.Split(',', StringSplitOptions.RemoveEmptyEntries));
  265. qm.QueryParamer.Remove(qmSystemNo);
  266. }
  267. if (qm?.QueryParamer != null && qm?.QueryParamer.Count > 0) { where = _unitOfWork.GetWhere(qm.QueryParamer); }
  268. StringBuilder sb = new StringBuilder();
  269. sb.Append($" select * from View_Order_CustomerConfirm(nolock) where 1=1 {AdditionalSql}");
  270. sb.Append(where);
  271. #endregion
  272. #region 分页
  273. StringBuilder pager = new StringBuilder();
  274. string sortString = " Id desc";
  275. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  276. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  277. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  278. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  279. #endregion
  280. #region 取值
  281. string sql = pager.ToString();
  282. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, new { qmSystemNo = numbers }, _unitOfWork.Transaction);
  283. //需要注意读取顺序,依据语句执行顺序进行读取
  284. var pageResult = new PageResult<View_Order_CustomerConfirm>()
  285. {
  286. Result = dr.Read<View_Order_CustomerConfirm>(),
  287. PageIndex = qm.PageIndex,
  288. PageSize = qm.PageIndex,
  289. TotalCount = dr.Read<int>().FirstOrDefault()
  290. };
  291. #endregion
  292. return pageResult;
  293. }
  294. public Task<IEnumerable<View_Order_CustomerConfirm>> GetCustomerConfirmList(IEnumerable<int> Ids)
  295. {
  296. return _unitOfWork.QueryAsync<View_Order_CustomerConfirm>(x => x.Id.In(Ids));
  297. }
  298. public Task<IEnumerable<View_Order_SettlementConfirm>> GetSettlementConfirmList(IEnumerable<int> Ids)
  299. {
  300. return _unitOfWork.QueryAsync<View_Order_SettlementConfirm>(x => x.Id.In(Ids));
  301. }
  302. public async Task<List<FBA_Incidental>> GetFBA_Incidentals(int OrderId, FBAIncidentalCalculateTarget target)
  303. {
  304. string sql = $@"select a.*,b.SMPOtherFeeId from FBA_Incidental(nolock) a
  305. left join FBA_OtherFee(nolock) b on b.Id=a.FBAOtherFeeId
  306. where Target=({(int)target}) and OrderId = {OrderId}";
  307. var data = await _unitOfWork.QueryBySqlAsync<FBA_Incidental>(sql, 600);
  308. return data.ToList();
  309. }
  310. public async Task<List<FBA_Incidental>> GetIncidentalsByOrderIds(List<int> OrderIds, FBAIncidentalCalculateTarget target)
  311. {
  312. string sql = $" select * from FBA_Incidental(nolock) where Target=({(int)target}) and OrderId in @Ids ";
  313. var data=await _unitOfWork.QueryBySqlAsync<FBA_Incidental>(sql, 600, new { Ids = OrderIds });
  314. return data.ToList();
  315. }
  316. public async Task<bool> SaveFBA_Incidentals(List<FBA_Incidental> fBA_Incidentals, int OrderId, bool isSettlement = false)
  317. {
  318. //每次只能先删除后新增
  319. string sql = $"delete FBA_Incidental where OrderId={OrderId}";
  320. if (isSettlement) { sql += " and Target=1 "; }//结算只删除结算的部分,收入将删除所有
  321. await _unitOfWork.ExecuteAsync(sql);
  322. await _unitOfWork.BulkToDBAsync(fBA_Incidentals);
  323. return true;
  324. }
  325. public async Task<bool> DelFBA_Incidentals(int OrderId, bool isSettlement = false)
  326. {
  327. string sql = $"delete FBA_Incidental where OrderId={OrderId}";
  328. if (isSettlement) { sql += " and Target=1 "; }//结算只删除结算的部分,收入将删除所有
  329. await _unitOfWork.ExecuteAsync(sql);
  330. return true;
  331. }
  332. public async Task<bool> UpdateCustomerOrderConfirm(IEnumerable<CustomerConfirmOrderDto> customerConfirmOrderDtos, int OrderId)
  333. {
  334. bool result = true;
  335. try
  336. {
  337. var temp = customerConfirmOrderDtos.FirstOrDefault();
  338. string sql = @$" update Order_MpsOrder set CustomerConfirmBillWeight={temp.CustomerConfirmBillWeight},CustomerConfirmUnitPrice={temp.CustomerConfirmUnitPrice},CustomerConfirmTotalSurcharges={temp.CustomerConfirmTotalSurcharges},CustomerConfirmTotal={temp.CustomerConfirmTotal},ApprovalNumber='{temp.ApprovalNumber}',DeclarationMethod='{temp.DeclarationMethod}',SplitWeightRate={temp.SplitWeightRate} where OrderId={OrderId} ";
  339. await _unitOfWork.ExecuteAsync(sql);
  340. }
  341. catch (Exception ex)
  342. {
  343. result = false;
  344. }
  345. return result;
  346. }
  347. public async Task<bool> UpdateSettlementOrderConfirm(IEnumerable<SettlementConfirmOrderDto> settlementConfirmOrderDtos, int OrderId)
  348. {
  349. bool result = true;
  350. try
  351. {
  352. var temp = settlementConfirmOrderDtos.FirstOrDefault();
  353. string sql = @$" update Order_MpsOrder set SettlementConfirmBillWeight={temp.SettlementConfirmBillWeight},SettlementConfirmUnitPrice={temp.SettlementConfirmUnitPrice},SettlementConfirmTotalSurcharges={temp.SettlementConfirmTotalSurcharges},SettlementConfirmTotal={temp.SettlementConfirmTotal} where OrderId={OrderId} ";
  354. await _unitOfWork.ExecuteAsync(sql);
  355. }
  356. catch (Exception ex)
  357. {
  358. result = false;
  359. }
  360. return result;
  361. }
  362. public async Task<bool> UpdateCustomerConfirmBoxs(IEnumerable<Order_MpsOrderBox> mpsOrderBoxes)
  363. {
  364. bool result = true;
  365. try
  366. {
  367. //比逐条更新稍微快点
  368. string sql = @" update Order_MpsOrderBox set BillWeight = @BillWeight,BillLenght=@BillLenght,BillWidth=@BillWidth,BillHeight=@BillHeight,VolumeWeight=@VolumeWeight,BillVolumeWeight=@BillVolumeWeight,CustomerGoodsNames=@CustomerGoodsNames where Id = @Id ";
  369. await _unitOfWork.ExecuteAsync(sql, mpsOrderBoxes);
  370. }
  371. catch (Exception ex)
  372. {
  373. result = false;
  374. }
  375. return result;
  376. }
  377. public async Task<bool> UpdateSettlementConfirmBoxs(IEnumerable<Order_MpsOrderBox> mpsOrderBoxes)
  378. {
  379. bool result = true;
  380. try
  381. {
  382. //比逐条更新稍微快点
  383. string sql = @" update Order_MpsOrderBox set SettlementWeight = @SettlementWeight,SettlementLenght=@SettlementLenght,SettlementWidth=@SettlementWidth,SettlementHeight=@SettlementHeight,SettlementVolumeWeight=@SettlementVolumeWeight,SettlementVolumeFactor=@SettlementVolumeFactor where Id = @Id ";
  384. await _unitOfWork.ExecuteAsync(sql, mpsOrderBoxes);
  385. }
  386. catch (Exception ex)
  387. {
  388. result = false;
  389. }
  390. return result;
  391. }
  392. public async Task<Order_MpsOrder> GetMpsOrder(int OrderId)
  393. {
  394. return (await _unitOfWork.QueryBySqlAsync<Order_MpsOrder>($"select * from Order_MpsOrder(nolock) where OrderId={OrderId}")).FirstOrDefault();
  395. }
  396. public async Task<IEnumerable<ConfirmWeight>> GetBatchConfirmWeight(IEnumerable<int> Ids)
  397. {
  398. string sql = @"select OrderId,TransferNumber OrderNo,CustomerConfirmBillWeight Weight,CustomerConfirmUnitPrice UnitPrice from Order_MpsOrder
  399. where OrderId in @OrderId";
  400. return await _unitOfWork.QueryBySqlAsync<ConfirmWeight>(sql, null, new { OrderId = Ids });
  401. }
  402. public async Task<IEnumerable<ConfirmWeight>> GetBatchSettlementWeight(IEnumerable<int> Ids)
  403. {
  404. string sql = @"select OrderId,TransferNumber OrderNo,SettlementConfirmBillWeight Weight,SettlementConfirmUnitPrice UnitPrice from Order_MpsOrder
  405. where OrderId in @OrderId";
  406. return await _unitOfWork.QueryBySqlAsync<ConfirmWeight>(sql, null, new { OrderId = Ids });
  407. }
  408. public async Task<bool> UpdateBatchSettlementWeight(List<ConfirmWeight> confirmWeights)
  409. {
  410. bool result = true;
  411. try
  412. {
  413. //比逐条更新稍微快点
  414. string sql = @" update Order_MpsOrder set SettlementConfirmBillWeight=@Weight,SettlementConfirmUnitPrice=@UnitPrice,SettlementConfirmTotal=@Weight*@UnitPrice+isnull(SettlementConfirmTotalSurcharges,0) where OrderId=@OrderId ";
  415. await _unitOfWork.ExecuteAsync(sql, confirmWeights);
  416. }
  417. catch (Exception ex)
  418. {
  419. throw;
  420. }
  421. return result;
  422. }
  423. public async Task<bool> UpdateBatchConfirmWeight(List<ConfirmWeight> confirmWeights)
  424. {
  425. bool result = true;
  426. try
  427. {
  428. //比逐条更新稍微快点
  429. string sql = @" update Order_MpsOrder set CustomerConfirmBillWeight=@Weight,CustomerConfirmUnitPrice=@UnitPrice,CustomerConfirmTotal=@Weight*@UnitPrice+isnull(CustomerConfirmTotalSurcharges,0) where OrderId=@OrderId ";
  430. await _unitOfWork.ExecuteAsync(sql, confirmWeights);
  431. }
  432. catch (Exception ex)
  433. {
  434. throw;
  435. }
  436. return result;
  437. }
  438. public async Task<bool> UpdateBatchConfirmIncidenta(List<ConfirmIncidenta> confirmIncidentas)
  439. {
  440. bool result = true;
  441. try
  442. {
  443. //比逐条更新稍微快点
  444. string sql = @" update Order_MpsOrder set CustomerConfirmTotalSurcharges=@TotalSurcharges,CustomerConfirmTotal=CustomerConfirmBillWeight*CustomerConfirmUnitPrice+@TotalSurcharges where OrderId=@OrderId ";
  445. await _unitOfWork.ExecuteAsync(sql, confirmIncidentas);
  446. }
  447. catch (Exception ex)
  448. {
  449. throw;
  450. }
  451. return result;
  452. }
  453. public async Task<bool> UpdateBatchSettlementConfirmIncidenta(List<ConfirmIncidenta> confirmIncidentas)
  454. {
  455. bool result = true;
  456. try
  457. {
  458. //比逐条更新稍微快点
  459. string sql = @" update Order_MpsOrder set SettlementConfirmTotalSurcharges=@TotalSurcharges,SettlementConfirmTotal=SettlementConfirmBillWeight*SettlementConfirmUnitPrice+@TotalSurcharges where OrderId=@OrderId ";
  460. await _unitOfWork.ExecuteAsync(sql, confirmIncidentas);
  461. }
  462. catch (Exception ex)
  463. {
  464. throw;
  465. }
  466. return result;
  467. }
  468. public async Task<IEnumerable<FBA_Incidental>> GetBatchIncidenta(IEnumerable<int> Ids, FBAIncidentalCalculateTarget target)
  469. {
  470. string sql = $@"select a.[Id]
  471. ,a.[Name]
  472. ,a.[CalculateType]
  473. ,a.[Target]
  474. ,a.[Fee]
  475. ,a.[FBAOtherFeeId]
  476. ,a.[CustomerRemark]
  477. ,a.[JSRemark]
  478. ,a.[SysncJS]
  479. ,a.[CreateUserName]
  480. ,a.[CreateTime]
  481. ,a.[UpdateUserName]
  482. ,a.[UpdateTime]
  483. ,a.[Remark],b.TransferNumber OrderNo,b.OrderId from FBA_Incidental(nolock)a
  484. right join Order_MpsOrder(nolock)b on a.OrderId=b.OrderId
  485. where ISNULL(a.Target,{(int)target})={(int)target} and b.OrderId in @Id ";
  486. return await _unitOfWork.QueryBySqlAsync<FBA_Incidental>(sql, null, new { Id = Ids });
  487. }
  488. public async Task<decimal> GetUnitPrice(KPUnitPriceRequest dto)
  489. {
  490. var fee = (await
  491. _unitOfWork.QueryBySqlAsync<dynamic>(@"select top 1 * from Logistics_ExpressFeeBase(nolock)
  492. Where ExpressId = @ExpressId and CustomerId=@CustomerId and EnableTime<@EnableTime order by EnableTime desc",
  493. null, new
  494. {
  495. ExpressId = dto.PublicExpressId,
  496. CustomerId = dto.CustomerId,
  497. EnableTime = dto.TransactionTime
  498. })).FirstOrDefault();
  499. if (fee == null)
  500. return 0m;
  501. var details = (await _unitOfWork.QueryBySqlAsync<dynamic>($@"
  502. select LEFT(b.ZipCode,c.StartQty) as ZipSrart ,c.StartQty,a.FristWeight,a.FristFee,a.ContinuedWeight,a.ContinuedFee from Logistics_ExpressFeeArea(nolock)c join
  503. Logistics_ExpressFeeGrads (nolock)a on c.ExpressId = a.Expressid and a.BaseId = {fee.Id}
  504. join Logistics_ExpressFeeAreaDetail(nolock)b on a.Countory=b.AreaCode and a.Expressid=b.ExpressId
  505. where a.MaxWeight>{dto.gWeight}
  506. order by a.MaxWeight
  507. ")).ToList();
  508. var d = details.Where(x => dto.ZipCode.Substring(0, x.StartQty) == x.ZipSrart).FirstOrDefault();
  509. if (d == null)
  510. return 0;
  511. return d.ContinuedFee;
  512. }
  513. #region 计费数据处理
  514. public async Task<bool> UpdateCharge(KPCharges kPCharges)
  515. {
  516. try
  517. {
  518. //找出待更新处理的数据
  519. var dic = kPCharges.charges.Select(x => new { x.SystemNo, x.CustomerId, x.CreateUserName }).Distinct().ToList();
  520. //更新旧数据
  521. string updateSql = @$"update Finance_Order_Charges set Offset=1,UpdateTime=GETDATE(),UpdateUserName='{dic.FirstOrDefault().CreateUserName}' where SystemNo=@SystemNo and CustomerId=@CustomerId;
  522. update Finance_ChargesDetail set Offset=1,UpdateTime=GETDATE(),UpdateUserName='{dic.FirstOrDefault().CreateUserName}' where BillNo=@SystemNo and CustomerId=@CustomerId ";
  523. await _unitOfWork.ExecuteAsync(updateSql, dic);
  524. foreach (var item in kPCharges.charges)
  525. {
  526. if (item.ChargeTarget == ChargeTarget.客户)
  527. {
  528. string sql = $@" update order_fee set OPCFreight = {item.ShippingFee},OPCServiceCharge={item.Remark} , AttachFee={item.AttachFee},UpdateTime=GetDate() ,Remark = '{item.Remark}' where OrderId=(select Id from Order_Order(nolock) where systemNo='{item.SystemNo}') ";
  529. await _unitOfWork.ExecuteAsync(sql);
  530. }
  531. await _unitOfWork.InsertAsync(item);
  532. }
  533. await _unitOfWork.BulkToDBAsync(kPCharges.chargesDetails);
  534. return true;
  535. }
  536. catch (Exception ex)
  537. {
  538. throw;
  539. }
  540. }
  541. #endregion
  542. public Task<View_Order_ReceivingInfo> GetOrderReceivingInfo(int orderId)
  543. {
  544. return _unitOfWork.GetAsync<View_Order_ReceivingInfo>(orderId);
  545. }
  546. public Task<View_Order_Print> GetPrint(int orderId)
  547. {
  548. return _unitOfWork.GetAsync<View_Order_Print>(orderId);
  549. }
  550. public async Task<PageResult<View_Order_Print>> GetPrintList(QueryModel qm)
  551. {
  552. #region 基础sql
  553. string AdditionalSql = string.Empty;
  554. var qmSystemNo = qm?.QueryParamer?.Where(x => x.Filed == "SystemNo")?.FirstOrDefault();
  555. string where = string.Empty;
  556. List<string> numbers = new List<string>();
  557. if (qmSystemNo != null && !string.IsNullOrEmpty(qmSystemNo.Value))
  558. {
  559. AdditionalSql = $" and (SystemNo in @qmSystemNo or CustomerOrderNo in @qmSystemNo)";
  560. numbers.AddRange(qmSystemNo.Value.Split(',', StringSplitOptions.RemoveEmptyEntries));
  561. qm.QueryParamer.Remove(qmSystemNo);
  562. }
  563. if (qm?.QueryParamer != null && qm?.QueryParamer.Count > 0) { where = _unitOfWork.GetWhere(qm.QueryParamer); }
  564. StringBuilder sb = new StringBuilder();
  565. sb.Append($" select * from View_Order_Print(nolock) where 1=1 {AdditionalSql}");
  566. sb.Append(where);
  567. #endregion
  568. #region 分页
  569. StringBuilder pager = new StringBuilder();
  570. string sortString = " Id desc";
  571. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  572. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  573. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  574. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  575. #endregion
  576. #region 取值
  577. string sql = pager.ToString();
  578. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, new { qmSystemNo = numbers }, _unitOfWork.Transaction);
  579. //需要注意读取顺序,依据语句执行顺序进行读取
  580. var pageResult = new PageResult<View_Order_Print>()
  581. {
  582. Result = dr.Read<View_Order_Print>(),
  583. PageIndex = qm.PageIndex,
  584. PageSize = qm.PageIndex,
  585. TotalCount = dr.Read<int>().FirstOrDefault()
  586. };
  587. #endregion
  588. return pageResult;
  589. }
  590. public async Task<PageResult<View_Order_ReceivingInfo>> GetReceivingInfo(QueryModel qm)
  591. {
  592. #region 基础sql
  593. string AdditionalSql = string.Empty;
  594. var qmSystemNo = qm?.QueryParamer?.Where(x => x.Filed == "SystemNo")?.FirstOrDefault();
  595. string where = string.Empty;
  596. List<string> numbers= new List<string>();
  597. if (qmSystemNo != null && !string.IsNullOrEmpty(qmSystemNo.Value))
  598. {
  599. AdditionalSql = $" and (SystemNo in @qmSystemNo or CustomerOrderNo in @qmSystemNo)";
  600. numbers.AddRange(qmSystemNo.Value.Split(',', StringSplitOptions.RemoveEmptyEntries));
  601. qm.QueryParamer.Remove(qmSystemNo);
  602. }
  603. if (qm?.QueryParamer != null && qm?.QueryParamer.Count > 0) { where = _unitOfWork.GetWhere(qm.QueryParamer); }
  604. StringBuilder sb = new StringBuilder();
  605. sb.Append($" select * from view_order_receivingInfo(nolock) where 1=1 {AdditionalSql}");
  606. sb.Append(where);
  607. #endregion
  608. #region 分页
  609. StringBuilder pager = new StringBuilder();
  610. string sortString = " Id desc";
  611. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  612. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  613. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  614. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  615. #endregion
  616. #region 取值
  617. string sql = pager.ToString();
  618. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, new { qmSystemNo= numbers }, _unitOfWork.Transaction);
  619. //需要注意读取顺序,依据语句执行顺序进行读取
  620. var pageResult = new PageResult<View_Order_ReceivingInfo>()
  621. {
  622. Result = dr.Read<View_Order_ReceivingInfo>(),
  623. PageIndex = qm.PageIndex,
  624. PageSize = qm.PageIndex,
  625. TotalCount = dr.Read<int>().FirstOrDefault()
  626. };
  627. #endregion
  628. return pageResult;
  629. }
  630. public Task<View_Order_SettlementConfirm> GetSettlementConfirm(int orderId)
  631. {
  632. return _unitOfWork.GetAsync<View_Order_SettlementConfirm>(orderId);
  633. }
  634. public async Task<PageResult<View_Order_SettlementConfirm>> GetSettlementConfirmList(QueryModel qm)
  635. {
  636. #region 基础sql
  637. string AdditionalSql = string.Empty;
  638. var qmSystemNo = qm?.QueryParamer?.Where(x => x.Filed == "SystemNo")?.FirstOrDefault();
  639. string where = string.Empty;
  640. List<string> numbers = new List<string>();
  641. if (qmSystemNo != null && !string.IsNullOrEmpty(qmSystemNo.Value))
  642. {
  643. AdditionalSql = $" and (SystemNo in @qmSystemNo or CustomerOrderNo in @qmSystemNo)";
  644. numbers.AddRange(qmSystemNo.Value.Split(',', StringSplitOptions.RemoveEmptyEntries));
  645. qm.QueryParamer.Remove(qmSystemNo);
  646. }
  647. if (qm?.QueryParamer != null && qm?.QueryParamer.Count > 0) { where = _unitOfWork.GetWhere(qm.QueryParamer); }
  648. StringBuilder sb = new StringBuilder();
  649. sb.Append($" select * from View_Order_SettlementConfirm(nolock) where 1=1 {AdditionalSql}");
  650. sb.Append(where);
  651. #endregion
  652. #region 分页
  653. StringBuilder pager = new StringBuilder();
  654. string sortString = " Id desc";
  655. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  656. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  657. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  658. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  659. #endregion
  660. #region 取值
  661. string sql = pager.ToString();
  662. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, new { qmSystemNo = numbers }, _unitOfWork.Transaction);
  663. //需要注意读取顺序,依据语句执行顺序进行读取
  664. var pageResult = new PageResult<View_Order_SettlementConfirm>()
  665. {
  666. Result = dr.Read<View_Order_SettlementConfirm>(),
  667. PageIndex = qm.PageIndex,
  668. PageSize = qm.PageIndex,
  669. TotalCount = dr.Read<int>().FirstOrDefault()
  670. };
  671. #endregion
  672. return pageResult;
  673. }
  674. public async Task<bool> SettlementConfirmOperation(int orderId, string NiceName)
  675. {
  676. await _unitOfWork.ExecuteAsync($"update Order_MpsOrder set UpdateTime=GETDATE(),IsSettlementConfirm=1,SettlementConfirmUserName='{NiceName}' where OrderId={orderId} ");
  677. return true;
  678. }
  679. public async Task<bool> UpdateSettlementConfirmForTable(MpsSettlementConfirmTableData dto)
  680. {
  681. decimal incidentalsFee = 0;
  682. if (dto.incidentals != null && dto.incidentals.Count() > 0) { incidentalsFee = dto.incidentals.Sum(x => x.Fee); }
  683. string updateSql = $" Update Order_MpsOrder set SettlementSplitWeightRate={dto.SettlementSplitWeightRate},SettlementConfirmBillWeight={dto.SettlementConfirmBillWeight},SettlementConfirmUnitPrice={dto.SettlementConfirmUnitPrice},SettlementConfirmTotalSurcharges={incidentalsFee},SettlementConfirmTotal={dto.SettlementConfirmBillWeight * dto.SettlementConfirmUnitPrice + incidentalsFee} where OrderId={dto.OrderId} ";
  684. await _unitOfWork.ExecuteAsync(updateSql);
  685. return true;
  686. }
  687. public Task SaveCustomerConfirm(View_Order_CustomerConfirm order)
  688. {
  689. throw new NotImplementedException();
  690. }
  691. public Task SaveOrderRceiveingInfo(View_Order_ReceivingInfo order)
  692. {
  693. throw new NotImplementedException();
  694. }
  695. public Task SavePrintInfos(View_Order_Print order)
  696. {
  697. throw new NotImplementedException();
  698. }
  699. public Task SaveSettlementConfirm(View_Order_SettlementConfirm order)
  700. {
  701. throw new NotImplementedException();
  702. }
  703. public async Task<PageResult<GrossProfitByCustomer>> GetGrossProfitByCustomer(QueryModel qm)
  704. {
  705. #region 基础sql
  706. string where = string.Empty;
  707. if (qm.QueryParamer != null && qm.QueryParamer.Count > 0)
  708. {
  709. foreach (var item in qm.QueryParamer)
  710. {
  711. if (item.Filed == "SalesmanUserId") { item.Filed = "c." + item.Filed; }
  712. else
  713. {
  714. item.Filed = "b." + item.Filed;
  715. }
  716. }
  717. where = _unitOfWork.GetWhere(qm.QueryParamer);
  718. }
  719. StringBuilder sb = new StringBuilder();
  720. sb.Append($@" select b.Id,c.CompanyName,d.NickName,COUNT(b.Id) OrderCount,sum(isnull(a.CustomerConfirmBillWeight,0))CustomerConfirmBillWeight,SUM(ISNULL(a.CustomerConfirmTotal,0)) CustomerConfirmTotal,SUM(ISNULL(a.SettlementConfirmBillWeight,0))SettlementConfirmBillWeight,SUM(ISNULL(a.SettlementConfirmTotal,0))SettlementConfirmTotal
  721. ,SUM(ISNULL(a.SettlementConfirmBillWeight,0))-SUM(ISNULL(a.SettlementConfirmTotal,0)) GrossProfit
  722. from Order_MpsOrder(nolock) a
  723. left join Order_Order(nolock) b on b.Id=a.OrderId
  724. left join User_Customer(nolock) c on c.Id=a.CustomerId
  725. left join User_Info(nolock) d on d.Id=c.SalesmanUserId
  726. where b.OrderStatus>0 ");
  727. sb.Append(where);
  728. sb.Append(" group by c.CompanyName,d.NickName ");
  729. #endregion
  730. #region 分页
  731. StringBuilder pager = new StringBuilder();
  732. string sortString = " Id desc";
  733. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  734. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  735. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  736. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  737. #endregion
  738. #region 取值
  739. string sql = pager.ToString();
  740. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, null, _unitOfWork.Transaction);
  741. //需要注意读取顺序,依据语句执行顺序进行读取
  742. var pageResult = new PageResult<GrossProfitByCustomer>()
  743. {
  744. Result = dr.Read<GrossProfitByCustomer>(),
  745. PageIndex = qm.PageIndex,
  746. PageSize = qm.PageIndex,
  747. TotalCount = dr.Read<int>().FirstOrDefault()
  748. };
  749. #endregion
  750. return pageResult;
  751. }
  752. public async Task<PageResult<GrossProfitByOrder>> GetGrossProfitByOrder(QueryModel qm)
  753. {
  754. #region 基础sql
  755. string where = string.Empty;
  756. if (qm.QueryParamer != null && qm.QueryParamer.Count > 0)
  757. {
  758. foreach (var item in qm.QueryParamer)
  759. {
  760. if (item.Filed == "SalesmanUserId") { item.Filed = "c." + item.Filed; }
  761. else if (item.Filed == "NickName") { item.Filed = "d." + item.Filed; }
  762. else
  763. {
  764. item.Filed = "b." + item.Filed;
  765. }
  766. }
  767. where = _unitOfWork.GetWhere(qm.QueryParamer);
  768. }
  769. StringBuilder sb = new StringBuilder();
  770. sb.Append($@" select b.Id,b.ReceiveTime,c.CompanyName,d.NickName,b.CustomerOrderNo,b.SystemNo,b.TransferNumber,e.PublicName,f.BoxCount,a.CustomerConfirmBillWeight,a.CustomerConfirmUnitPrice
  771. ,a.CustomerConfirmTotalSurcharges,a.CustomerConfirmTotal,a.SettlementConfirmBillWeight,a.SettlementConfirmUnitPrice,a.SettlementConfirmTotalSurcharges,
  772. ISNULL(a.CustomerConfirmTotal,0)-ISNULL(a.SettlementConfirmTotal,0) GrossProfit
  773. from Order_MpsOrder(nolock) a
  774. left join Order_Order(nolock) b on b.Id=a.OrderId
  775. left join User_Customer(nolock) c on c.Id=a.CustomerId
  776. left join User_Info(nolock) d on d.Id=c.SalesmanUserId
  777. left join Logistics_Channel(nolock) e on e.Id=b.CreateOrderChannelId
  778. outer apply(select COUNT(Id) BoxCount from Order_MpsOrderBox(nolock) where MpsOrderId=a.Id) f
  779. where b.OrderStatus>0 ");
  780. sb.Append(where);
  781. #endregion
  782. #region 分页
  783. StringBuilder pager = new StringBuilder();
  784. string sortString = " Id desc";
  785. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  786. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  787. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  788. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  789. #endregion
  790. #region 取值
  791. string sql = pager.ToString();
  792. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, null, _unitOfWork.Transaction);
  793. //需要注意读取顺序,依据语句执行顺序进行读取
  794. var pageResult = new PageResult<GrossProfitByOrder>()
  795. {
  796. Result = dr.Read<GrossProfitByOrder>(),
  797. PageIndex = qm.PageIndex,
  798. PageSize = qm.PageIndex,
  799. TotalCount = dr.Read<int>().FirstOrDefault()
  800. };
  801. #endregion
  802. return pageResult;
  803. }
  804. public async Task<PageResult<GrossProfitByOrder>> GetGrossProfitByMonth(QueryModel qm)
  805. {
  806. #region 基础sql
  807. string where = string.Empty;
  808. StringBuilder sb = new StringBuilder();
  809. sb.Append($@" select b.ReceiveTime,c.CompanyName,d.NickName,b.CustomerOrderNo,b.SystemNo,b.TransferNumber,e.PublicName,f.BoxCount,a.CustomerConfirmBillWeight,a.CustomerConfirmUnitPrice
  810. ,a.CustomerConfirmTotalSurcharges,a.CustomerConfirmTotal,a.SettlementConfirmBillWeight,a.SettlementConfirmUnitPrice,a.SettlementConfirmTotalSurcharges,
  811. ISNULL(a.CustomerConfirmTotal,0)-ISNULL(a.SettlementConfirmTotal,0) GrossProfit
  812. from Order_MpsOrder(nolock) a
  813. left join Order_Order(nolock) b on b.Id=a.OrderId
  814. left join User_Customer(nolock) c on c.Id=a.CustomerId
  815. left join User_Info(nolock) d on d.Id=c.SalesmanUserId
  816. left join Logistics_Channel(nolock) e on e.Id=b.ChannelId
  817. outer apply(select COUNT(Id) BoxCount from Order_MpsOrderBox(nolock) where MpsOrderId=a.Id) f
  818. where b.OrderStatus>0 ");
  819. sb.Append(where);
  820. #endregion
  821. #region 分页
  822. StringBuilder pager = new StringBuilder();
  823. string sortString = " Id desc";
  824. pager.Append($@"select * from (select Row_Number()over(order by {sortString})rowId,*
  825. from ({sb.ToString()})a where 1=1 ) T where rowId between {(qm.PageIndex - 1) * qm.PageSize + 1} and {qm.PageSize * qm.PageIndex};");
  826. pager.Append(@$"select count(0)TotalCount from (select Row_Number()over(order by {sortString})rowId,*
  827. from({sb.ToString()})a where 1 = 1 ) T where 1=1");
  828. #endregion
  829. #region 取值
  830. string sql = pager.ToString();
  831. var dr = await _unitOfWork.Connection.QueryMultipleAsync(sql, null, _unitOfWork.Transaction);
  832. //需要注意读取顺序,依据语句执行顺序进行读取
  833. var pageResult = new PageResult<GrossProfitByOrder>()
  834. {
  835. Result = dr.Read<GrossProfitByOrder>(),
  836. PageIndex = qm.PageIndex,
  837. PageSize = qm.PageIndex,
  838. TotalCount = dr.Read<int>().FirstOrDefault()
  839. };
  840. #endregion
  841. return pageResult;
  842. }
  843. public async Task<Order_MpsOrderBoxPicture> GetBoxPictureOrderInfo(string boxNumber)
  844. {
  845. string sql = $@"select a.OrderId,b.MpsOrderId,b.Id MpsOrderBoxId,b.BoxNumber from Order_MpsOrder(nolock) a
  846. left join Order_MpsOrderBox(nolock) b on b.MpsOrderId=a.Id
  847. where b.BoxNumber='{boxNumber}' ";
  848. var query = _unitOfWork.QueryBySqlAsync<Order_MpsOrderBoxPicture>(sql);
  849. return query.Result.FirstOrDefault();
  850. }
  851. public async Task<bool> AddBoxPicture(Order_MpsOrderBoxPicture model)
  852. {
  853. await _unitOfWork.UpdateByPrimaryKeyAsync<Order_MpsOrder>("IsBoxPicture", true, model.MpsOrderId);
  854. await _unitOfWork.UpdateByPrimaryKeyAsync<Order_MpsOrderBox>("IsBoxPicture", true, model.MpsOrderBoxId);
  855. await _unitOfWork.InsertAsync(model);
  856. return true;
  857. }
  858. public async Task<bool> RemoveBoxPicture(int[] PicIds)
  859. {
  860. string boxNumberSql = $" select top 1 * from Order_MpsOrderBoxPicture where Id in @picIds ";
  861. var boxNumber = (await _unitOfWork.QueryBySqlAsync<Order_MpsOrderBoxPicture>(boxNumberSql, null, new { picIds = PicIds })).FirstOrDefault();
  862. string sql = $@"update Order_MpsOrderBoxPicture set IsDelete=1 where Id in @picIds";
  863. await _unitOfWork.ExecuteAsync(sql, new { picIds = PicIds });
  864. if (boxNumber == null) { return true; }
  865. var boxPicInfo = await GetBoxPictures(boxNumber.BoxNumber);
  866. //删完图片,更改状态
  867. if (boxPicInfo == null || boxPicInfo.Count == 0)
  868. {
  869. await _unitOfWork.UpdateByPrimaryKeyAsync<Order_MpsOrder>("IsBoxPicture", false, boxNumber.MpsOrderId);
  870. await _unitOfWork.ExecuteAsync($" update Order_MpsOrderBox set IsBoxPicture=0 where MpsOrderId={boxNumber.MpsOrderId}");
  871. }
  872. else
  873. {
  874. List<int> boxIds = boxPicInfo.Select(x => x.MpsOrderBoxId).Distinct().ToList();
  875. string picSql = $@" update Order_MpsOrderBox set IsBoxPicture=0 where MpsOrderId={boxNumber.MpsOrderId} and Id not in @boxIds ";
  876. await _unitOfWork.ExecuteAsync(picSql, new { boxIds = boxIds });
  877. }
  878. return true;
  879. }
  880. public async Task<List<Order_MpsOrderBoxPicture>> GetBoxPictures(string boxNumber)
  881. {
  882. string sql = $" select * from Order_MpsOrderBoxPicture where IsDelete=0 and BoxNumber='{boxNumber}' ";
  883. return (await _unitOfWork.QueryBySqlAsync<Order_MpsOrderBoxPicture>(sql)).ToList();
  884. }
  885. public async Task<List<VerifyBoxInfo>> ImportMpsBoxDataVerify(List<string> boxNumbers)
  886. {
  887. string sql = @"select c.*,d.CustomerVolumnWeightFactor,a.TrackingNumber from Order_Order(nolock) a
  888. left join Order_MpsOrder(nolock) b on b.OrderId=a.Id
  889. left join Order_MpsOrderBox(nolock) c on c.MpsOrderId=b.Id
  890. left join Logistics_Channel(nolock)d on d.Id=a.CreateOrderChannelId
  891. where b.Status=2 and b.OrderId>0 and (c.BoxNumber in @BoxNumbers or c.BoxServiceNumber in @BoxNumbers) ";
  892. var data = (await _unitOfWork.QueryBySqlAsync<VerifyBoxInfo>(sql, null, new { BoxNumbers = boxNumbers })).ToList();
  893. var repeatDatas = data.GroupBy(x => new { x.BoxNumber }).Where(x => x.Count() > 1);
  894. string error = string.Join(",", repeatDatas.Select(x => x.Key));
  895. if (!string.IsNullOrEmpty(error))
  896. throw new Exception("通过客户箱号查询箱子重复:" + error + ",请改用系统箱号去操作");
  897. if (data.Count == 0)
  898. {
  899. throw new Exception("通过箱号未获取到任何数据,请检查箱号是否正确!");
  900. }
  901. else if (data.Any(x => !x.TrackingNumber.StartsWith("XYY")))
  902. {
  903. throw new Exception("以下箱号所在订单已下单末端,不能再次更新!" + string.Join(',', data.Where(x => !x.TrackingNumber.StartsWith("XYY")).Select(x => x.BoxNumber + "|" + x.BoxServiceNumber).Distinct()
  904. ));
  905. }
  906. return data;
  907. }
  908. public async Task<bool> UpdateImportReceivingBoxs(List<Order_MpsOrderBox> boxs)
  909. {
  910. bool result = true;
  911. try
  912. {
  913. //比逐条更新稍微快点
  914. string sql = @" update Order_MpsOrderBox set OpInputWeight = @OpInputWeight,OpLength=@OpLength,OpWidth=@OpWidth,OpHeight=@OpHeight,VolumeWeight=@VolumeWeight,Remark=@Remark where Id = @Id ";
  915. await _unitOfWork.ExecuteAsync(sql, boxs);
  916. }
  917. catch (Exception ex)
  918. {
  919. result = false;
  920. }
  921. return result;
  922. }
  923. public async Task<List<WaitConfirmDto>> GetExportWaitConfirm(IEnumerable<int> Ids)
  924. {
  925. string sql = $@"select a.Id OrderId,d.CompanyName,a.CustomerOrderNo,c.BoxNumber,c.BoxTrackingNumber,c.BillLenght,c.BillWidth,c.BillHeight,c.BillWeight,c.BillVolumeWeight,b.CustomerConfirmBillWeight,c.GoodsNames,a.ReceiverZipCode,b.GoodsNames OrderGoodsNames,a.TrackingNumber,e.PublicName PublicChannelName
  926. from order_order(nolock) a
  927. left join User_Customer(nolock)d on d.id=a.customerId
  928. left join Order_MpsOrder(nolock) b on b.orderId=a.id
  929. left join Order_MpsOrderBox(nolock) c on c.MpsOrderId=b.Id
  930. left join Logistics_Channel(nolock)e on e.Id=a.CreateOrderChannelId
  931. where a.Id in @Ids ";
  932. var query = await _unitOfWork.QueryBySqlAsync<WaitConfirmDto>(sql, null, new { Ids = Ids });
  933. return query.ToList();
  934. }
  935. public async Task<List<Logistics_Channel>> GetChannels(List<int> channelIds)
  936. {
  937. string sql = "select * from Logistics_Channel(nolock) where Id in @channelIds ";
  938. var channelInfo = await _unitOfWork.QueryBySqlAsync<Logistics_Channel>(sql, null, new { channelIds =channelIds});
  939. return channelInfo.ToList();
  940. }
  941. }
  942. }