Thứ Năm, 13 tháng 2, 2020

Đa luồng công nợ


private object objLock = new object();
        private bool isStop = false;
        private int _taskIndex;

        private List<BackgroundWorker> Threads = new List<BackgroundWorker>();

        private List<CongNoItem>
            _noDauKy,
            _phatSinh,
            _daThu,
            _khauTru,
            _thuTruocA,
            _thuTruocB,
            _conNo;


        private System.Collections.Generic.List<Library.mbMatBang> _lMatBang;
        private System.Collections.Generic.List<Library.tnKhachHang> _lKhachHang;

        public frmReceivables()
        {
            InitializeComponent();

            for (int i = 0; i <= 8; i++)
            {
                var thread = new BackgroundWorker();
                thread.DoWork += thread_DoWork;
                thread.RunWorkerCompleted += thread_RunWorkerCompleted;
                Threads.Add(thread);
            }
        }

        private void thread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (isStop)
            {
                if (!Threads.Any(_ => _.IsBusy))
                    GetData();
            }
            else ((BackgroundWorker) sender).RunWorkerAsync();
        }

        private void thread_DoWork(object sender, DoWorkEventArgs e)
        {
            GetTask();
        }

        public class CongNoItem
        {
            public int? MaKH { get; set; }
            public decimal? SoTien { get; set; }
        }

        private void GetData()
        {
            var wai = DialogBox.WaitingForm();
            wai.Show();

            gcHoaDon.DataSource = (from kh in _lKhachHang
                join mb in _lMatBang on kh.MaKH equals mb.MaKHF1

                join ndk in _noDauKy on kh.MaKH equals ndk.MaKH into noDauKy
                from ndk in noDauKy.DefaultIfEmpty()
                join ps in _phatSinh on kh.MaKH equals ps.MaKH into phatSinh
                from ps in phatSinh.DefaultIfEmpty()
                join dt in _daThu on kh.MaKH equals dt.MaKH into daThu
                from dt in daThu.DefaultIfEmpty()
                join kt in _khauTru on kh.MaKH equals kt.MaKH into khauTru
                from kt in khauTru.DefaultIfEmpty()
                join ttA in _thuTruocA on kh.MaKH equals ttA.MaKH into thuTruocA
                from ttA in thuTruocA.DefaultIfEmpty()
                join ttB in _thuTruocB on kh.MaKH equals ttB.MaKH into thuTruocB
                from ttB in thuTruocA.DefaultIfEmpty()
                join cn in _conNo on kh.MaKH equals cn.MaKH into conNo
                from cn in conNo.DefaultIfEmpty()

                select new
                {
                    kh.MaKH, kh.KyHieu, kh.MaPhu, TenKH = kh.IsCaNhan == true ? kh.HoKH + " " + kh.TenKH : kh.CtyTen,
                    DienThoai = kh.DienThoaiKH, kh.EmailKH, DiaChi = kh.CtyDiaChi, LoaiMB = mb.mbLoaiMatBang.TenLMB,
                    mb.MaMB, NoDauKy = ndk != null ? ndk.SoTien : 0,
                    PhatSinh = ps != null ? ps.SoTien : 0, DaThu = dt != null ? dt.SoTien : 0,
                    KhauTru = kt != null ? kt.SoTien : 0, ConNo = cn != null ? cn.SoTien : 0,
                    ThuTruoc = (ttA != null ? ttA.SoTien : 0) - (ttB != null ? ttB.SoTien : 0)
                }).ToList();
            itemNap.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            wai.Dispose();
        }

        private void GetTask()
        {
            Library.MasterDataContext db = new Library.MasterDataContext();
            var maTn = (byte) itemToaNha.EditValue;
            var thang = System.Convert.ToInt32(itemThang.EditValue);
            var nam = System.Convert.ToInt32(itemNam.EditValue);
            var tuNgay = new System.DateTime(nam, thang, 1);
            var denNgay = Library.Common.GetLastDayOfMonth(thang, nam);
            var ngayHienTai = System.DateTime.UtcNow.AddHours(7);

            var index = 0;
            lock (objLock) index = _taskIndex++;

            switch (index)
            {
                case 1:
                    _noDauKy = (from p in db.dvHoaDons
                        where SqlMethods.DateDiffDay(p.NgayTT, tuNgay) > 0 & p.IsDuyet == true & p.MaTN == maTn
                        group new {p} by new {p.MaKH}
                        into g
                        select new CongNoItem
                        {
                            MaKH = g.Key.MaKH,
                            SoTien = g.Sum(_ => _.p.PhaiThu -
                                (from ct in db.ptChiTietPhieuThus
                                    where ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &
                                          SqlMethods.DateDiffDay(ct.ptPhieuThu.NgayThu,
                                              maTn == 27 ? ngayHienTai : tuNgay) >
                                          0
                                    select ct.SoTien).Sum().GetValueOrDefault() - (from ct in db.ktttChiTiets
                                    where ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &
                                          SqlMethods.DateDiffDay(ct.ktttKhauTruThuTruoc.NgayCT,
                                              maTn == 27 ? ngayHienTai : tuNgay) > 0
                                    select ct.SoTien).Sum().GetValueOrDefault()).GetValueOrDefault()
                        }).ToList();
                    break;
                case 2:
                    _phatSinh = (from p in db.dvHoaDons
                        where SqlMethods.DateDiffMonth(p.NgayTT, tuNgay) == 0 & p.IsDuyet == true & p.MaTN == maTn
                        group new {p} by new {p.MaKH}
                        into g
                        select new CongNoItem
                        {
                            MaKH = g.Key.MaKH, SoTien = g.Sum(_ => _.p.PhaiThu).GetValueOrDefault()
                        }).ToList();
                    break;
                case 3:
                    _daThu = (from p in db.ptChiTietPhieuThus
                        join hd in db.dvHoaDons on new {p.TableName, p.LinkID} equals
                            new {TableName = "dvHoaDon", LinkID = (long?) hd.ID}
                        where SqlMethods.DateDiffMonth(p.ptPhieuThu.NgayThu, tuNgay) == 0 & hd.MaTN == maTn
                        group new {hd, p} by new {hd.MaKH}
                        into g
                        select new CongNoItem
                        {
                            MaKH = g.Key.MaKH, SoTien = g.Sum(_ => _.p.SoTien).GetValueOrDefault()
                        }).ToList();
                    break;
                case 4:
                    _khauTru = (from p in db.ktttChiTiets
                        join hd in db.dvHoaDons on new {p.TableName, p.LinkID} equals
                            new {TableName = "dvHoaDon", LinkID = (long?) hd.ID}
                        where SqlMethods.DateDiffMonth(p.ktttKhauTruThuTruoc.NgayCT, tuNgay) == 0 & hd.MaTN == maTn
                        group new {hd, p} by new {hd.MaKH}
                        into g
                        select new CongNoItem
                        {
                            MaKH = g.Key.MaKH, SoTien = g.Sum(_ => _.p.SoTien).GetValueOrDefault()
                        }).ToList();
                    break;
                case 5:
                    _thuTruocA = (from p in db.ptPhieuThus
                        where p.MaPL == 2 & SqlMethods.DateDiffDay(p.NgayThu, denNgay) >= 0 & p.MaTN == maTn
                        group new {p} by new {p.MaKH}
                        into g
                        select new CongNoItem
                        {
                            MaKH = g.Key.MaKH,
                            SoTien = g.Sum(_ => _.p.SoTien).GetValueOrDefault()
                        }).ToList();
                    break;
                case 6:
                    _thuTruocB = (from p in db.ktttKhauTruThuTruocs
                        where SqlMethods.DateDiffDay(p.NgayCT, denNgay) >= 0
                        group new {p} by new {p.MaKH}
                        into g
                        select new CongNoItem
                        {
                            MaKH = g.Key.MaKH,
                            SoTien = g.Sum(_ => _.p.SoTien).GetValueOrDefault()
                        }).ToList();
                    break;
                case 7: _lMatBang = db.mbMatBangs.Where(_ => _.MaTN == maTn).ToList();
                    break;
                case 8:
                    _lKhachHang = db.tnKhachHangs.Where(_ => _.MaTN == maTn).ToList();
                    break;
                case 9:
                    _conNo = (from p in db.dvHoaDons
                        where SqlMethods.DateDiffDay(p.NgayTT, denNgay) >= 0 & p.IsDuyet == true & p.MaTN == maTn
                        group new {p} by new {p.MaKH}
                        into g
                        select new CongNoItem
                        {
                            MaKH = g.Key.MaKH,
                            SoTien = g.Sum(_ =>
                                _.p.PhaiThu - (from ct in db.ptChiTietPhieuThus
                                                where ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &
                                                      SqlMethods.DateDiffDay(ct.ptPhieuThu.NgayThu, denNgay) >= 0
                                                select ct.SoTien).Sum().GetValueOrDefault()
                                            - (from ct in db.ktttChiTiets
                                                where ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &
                                                      SqlMethods.DateDiffDay(ct.ktttKhauTruThuTruoc.NgayCT, denNgay) >=
                                                      0
                                                select ct.SoTien).Sum().GetValueOrDefault()).GetValueOrDefault()
                        }).ToList();
                    break;
                default:
                    isStop = true;
                    break;
            }
        }

        private void LoadData()
        {
            _taskIndex = 1;
            isStop = false;

            gcHoaDon.DataSource = null;
            itemNap.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            foreach (var thread in Threads)
                thread.RunWorkerAsync();
        }

Không có nhận xét nào:

Đăng nhận xét

Cám ơn bạn đã quan tâm đến bài viết của mình nhé!