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 _lKhachHangjoin mb in _lMatBang on kh.MaKH equals mb.MaKHF1join ndk in _noDauKy on kh.MaKH equals ndk.MaKH into noDauKyfrom ndk in noDauKy.DefaultIfEmpty()join ps in _phatSinh on kh.MaKH equals ps.MaKH into phatSinhfrom ps in phatSinh.DefaultIfEmpty()join dt in _daThu on kh.MaKH equals dt.MaKH into daThufrom dt in daThu.DefaultIfEmpty()join kt in _khauTru on kh.MaKH equals kt.MaKH into khauTrufrom kt in khauTru.DefaultIfEmpty()join ttA in _thuTruocA on kh.MaKH equals ttA.MaKH into thuTruocAfrom ttA in thuTruocA.DefaultIfEmpty()join ttB in _thuTruocB on kh.MaKH equals ttB.MaKH into thuTruocBfrom ttB in thuTruocA.DefaultIfEmpty()join cn in _conNo on kh.MaKH equals cn.MaKH into conNofrom 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.dvHoaDonswhere SqlMethods.DateDiffDay(p.NgayTT, tuNgay) > 0 & p.IsDuyet == true & p.MaTN == maTngroup new {p} by new {p.MaKH}into gselect new CongNoItem{MaKH = g.Key.MaKH,SoTien = g.Sum(_ => _.p.PhaiThu -(from ct in db.ptChiTietPhieuThuswhere ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &SqlMethods.DateDiffDay(ct.ptPhieuThu.NgayThu,maTn == 27 ? ngayHienTai : tuNgay) >0select ct.SoTien).Sum().GetValueOrDefault() - (from ct in db.ktttChiTietswhere ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &SqlMethods.DateDiffDay(ct.ktttKhauTruThuTruoc.NgayCT,maTn == 27 ? ngayHienTai : tuNgay) > 0select ct.SoTien).Sum().GetValueOrDefault()).GetValueOrDefault()}).ToList();break;case 2:_phatSinh = (from p in db.dvHoaDonswhere SqlMethods.DateDiffMonth(p.NgayTT, tuNgay) == 0 & p.IsDuyet == true & p.MaTN == maTngroup new {p} by new {p.MaKH}into gselect new CongNoItem{MaKH = g.Key.MaKH, SoTien = g.Sum(_ => _.p.PhaiThu).GetValueOrDefault()}).ToList();break;case 3:_daThu = (from p in db.ptChiTietPhieuThusjoin hd in db.dvHoaDons on new {p.TableName, p.LinkID} equalsnew {TableName = "dvHoaDon", LinkID = (long?) hd.ID}where SqlMethods.DateDiffMonth(p.ptPhieuThu.NgayThu, tuNgay) == 0 & hd.MaTN == maTngroup new {hd, p} by new {hd.MaKH}into gselect new CongNoItem{MaKH = g.Key.MaKH, SoTien = g.Sum(_ => _.p.SoTien).GetValueOrDefault()}).ToList();break;case 4:_khauTru = (from p in db.ktttChiTietsjoin hd in db.dvHoaDons on new {p.TableName, p.LinkID} equalsnew {TableName = "dvHoaDon", LinkID = (long?) hd.ID}where SqlMethods.DateDiffMonth(p.ktttKhauTruThuTruoc.NgayCT, tuNgay) == 0 & hd.MaTN == maTngroup new {hd, p} by new {hd.MaKH}into gselect new CongNoItem{MaKH = g.Key.MaKH, SoTien = g.Sum(_ => _.p.SoTien).GetValueOrDefault()}).ToList();break;case 5:_thuTruocA = (from p in db.ptPhieuThuswhere p.MaPL == 2 & SqlMethods.DateDiffDay(p.NgayThu, denNgay) >= 0 & p.MaTN == maTngroup new {p} by new {p.MaKH}into gselect new CongNoItem{MaKH = g.Key.MaKH,SoTien = g.Sum(_ => _.p.SoTien).GetValueOrDefault()}).ToList();break;case 6:_thuTruocB = (from p in db.ktttKhauTruThuTruocswhere SqlMethods.DateDiffDay(p.NgayCT, denNgay) >= 0group new {p} by new {p.MaKH}into gselect 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.dvHoaDonswhere SqlMethods.DateDiffDay(p.NgayTT, denNgay) >= 0 & p.IsDuyet == true & p.MaTN == maTngroup new {p} by new {p.MaKH}into gselect new CongNoItem{MaKH = g.Key.MaKH,SoTien = g.Sum(_ =>_.p.PhaiThu - (from ct in db.ptChiTietPhieuThuswhere ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &SqlMethods.DateDiffDay(ct.ptPhieuThu.NgayThu, denNgay) >= 0select ct.SoTien).Sum().GetValueOrDefault()- (from ct in db.ktttChiTietswhere ct.TableName == "dvHoaDon" & ct.LinkID == _.p.ID &SqlMethods.DateDiffDay(ct.ktttKhauTruThuTruoc.NgayCT, denNgay) >=0select 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();}
Thứ Năm, 13 tháng 2, 2020
Đa luồng công nợ
Đăng ký:
Đăng Nhận xét (Atom)
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é!