public KHUYENMAI InsertKhuyenMai(TienTyLeViewModel.Dto instance) { var dataKhuyenMai = Mapper.Map <TienTyLeViewModel.Dto, KHUYENMAI>(instance); dataKhuyenMai.ID = Guid.NewGuid().ToString(); dataKhuyenMai.LOAI_KHUYENMAI = TypeBuildCode.TIENTYLE.ToString(); dataKhuyenMai.THOIGIAN_TAO = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second; dataKhuyenMai.I_STATE = "C"; var result = AddUnit(dataKhuyenMai); var dataDetails = Mapper.Map <List <TienTyLeViewModel.DtoDetails>, List <KHUYENMAI_CHITIET> >(instance.DataDetails); dataDetails.ForEach(x => { x.ID = Guid.NewGuid().ToString(); x.MA_KHUYENMAI = result.MA_KHUYENMAI; }); result = Insert(result); UnitOfWork.Repository <KHUYENMAI_CHITIET>().InsertRange(dataDetails); return(result); }
public KHUYENMAI UpdateKhuyenMai(TienTyLeViewModel.Dto instance) { var dataKhuyenMai = Mapper.Map <TienTyLeViewModel.Dto, KHUYENMAI>(instance); dataKhuyenMai.I_STATE = "U"; dataKhuyenMai.UNITCODE = GetCurrentUnitCode(); var listProduct = UnitOfWork.Repository <KHUYENMAI_CHITIET>().DbSet.Where(x => x.MA_KHUYENMAI == dataKhuyenMai.MA_KHUYENMAI).ToList(); if (listProduct.Count > 0) { listProduct.ForEach(x => x.ObjectState = ObjectState.Deleted); } var dataDetails = Mapper.Map <List <TienTyLeViewModel.DtoDetails>, List <KHUYENMAI_CHITIET> >(instance.DataDetails); dataDetails.ForEach(x => { x.ID = Guid.NewGuid().ToString(); x.MA_KHUYENMAI = dataKhuyenMai.MA_KHUYENMAI; }); UnitOfWork.Repository <KHUYENMAI_CHITIET>().InsertRange(dataDetails); var result = Update(dataKhuyenMai); return(result); }