dengjunjie
2025-03-12 f43b7df8400f4fcffc9f19dca0888d61e2b33d5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_DTO.Inbound;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StockService
{
    public partial class StockInfoDetailService : ServiceBase<Dt_StockInfoDetail, IStockInfoDetailRepository>, IStockInfoDetailService
    {
        private readonly IStockInfoDetailRepository _stockInfoDetailRepository;
        private readonly IStockInfoService _stockInfoService;
        public StockInfoDetailService(IStockInfoDetailRepository BaseDal, IStockInfoDetailRepository stockInfoDetailRepository, IStockInfoService stockInfoService) : base(BaseDal)
        {
            _stockInfoDetailRepository = stockInfoDetailRepository;
            _stockInfoService = stockInfoService;
        }
 
        public IStockInfoDetailRepository Repository => BaseDal;
        public override WebResponseContent DeleteData(object[] keys)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var DelStockInfoDetails = BaseDal.QueryData(x => keys.Contains(x.Id)).ToList();
                if (DelStockInfoDetails == null || DelStockInfoDetails.Count < 1) throw new Exception($"未找到信息");
                if (DelStockInfoDetails.FirstOrDefault(x => x.Status > StockStatusEmun.组盘暂存.ObjToInt()) != null)
                    throw new Exception($"库存明细状态不为组盘暂存暂不能删除");
                var StockIds = DelStockInfoDetails.Select(x => x.StockId).ToList();
                var StockInfoDetails = BaseDal.QueryData(x => StockIds.Contains(x.StockId)).Select(x => x.StockId).GroupBy(x => x).ToList();
                List<Dt_StockInfo> StockInfos = new List<Dt_StockInfo>();
                foreach (var item in StockInfoDetails)
                {
                    if (item.Count() == DelStockInfoDetails.Where(x => x.StockId == item.Key).Count())
                    {
                        var StockInfo = Db.Queryable<Dt_StockInfo>().Where(x => item.Key == x.Id).First();
                        StockInfos.Add(StockInfo);
                    }
                }
                List<Dt_InboundOrder> inboundOrders = new List<Dt_InboundOrder>();
                List<Dt_InboundOrderDetail> inboundOrderDetails = new List<Dt_InboundOrderDetail>();
                foreach (var DelStockInfoDetail in DelStockInfoDetails.GroupBy(x => x.OrderNo))
                {
                    var inboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().Includes(x => x.Details).First(x => x.OrderNo == DelStockInfoDetail.Key);
                    if (inboundOrder != null)
                    {
                        foreach (var item in DelStockInfoDetail)
                        {
                            var inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == item.BatchNo);
                            if (inboundOrderDetail != null)
                            {
                                inboundOrderDetail.ReceiptQuantity -= 1;// item.StockQuantity;
                                if (inboundOrderDetail.ReceiptQuantity == 0) inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt();
                            }
                        }
                        inboundOrderDetails.AddRange(inboundOrder.Details);
                        if (inboundOrder.Details.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) == null)
                        {
                            inboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                            inboundOrders.Add(inboundOrder);
                        }
                    }
                }
                Db.Ado.BeginTran();
                BaseDal.Db.Updateable(inboundOrderDetails).ExecuteCommand();
                BaseDal.Db.Updateable(inboundOrders).ExecuteCommand();
                _stockInfoService.DeleteData(StockInfos);
                base.DeleteData(keys);
                Db.Ado.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
    }
}