private async Task <long> CreateHoSo(CreateOrUpdateHoSo37Input input)
        {
            try
            {
                var insertInput = input.HoSo.MapTo <XHoSo>();
                if (insertInput.LoaiHoSoId.HasValue)
                {
                    var lhs = await _loaiHoSoRepos.GetAsync(insertInput.LoaiHoSoId.Value);

                    insertInput.TenLoaiHoSo = lhs.TenLoaiHoSo;
                }
                insertInput.SoDangKy = _capSoThuTucAppService.SinhSoDangKy(input.HoSo.DoanhNghiepId.Value, (int)CommonENum.THU_TUC_ID.THU_TUC_37);
                long id = await _hoSoRepos.InsertAndGetIdAsync(insertInput);

                #region Sinh Mã Hồ Sơ

                var hoSo = _hoSoRepos.Get(id);
                if (string.IsNullOrEmpty(hoSo.MaHoSo))
                {
                    hoSo.MaHoSo = SinhMaHoSo(hoSo.Id, hoSo.CreationTime, hoSo.ThuTucId.Value);
                    await _hoSoRepos.UpdateAsync(hoSo);
                }

                #endregion Sinh Mã Hồ Sơ

                /// upload file tailieu
                if (input.Teps != null)
                {
                    foreach (var tailieu in input.Teps)
                    {
                        if (tailieu.IsSoCongBo == true)
                        {
                            continue;
                        }
                        var    insertTailieu = tailieu.MapTo <XHoSoTepDinhKem>();
                        string HCC_FILE_PDF  = GetUrlFileDefaut();
                        insertTailieu.ThuTucId = (int)CommonENum.THU_TUC_ID.THU_TUC_37;
                        insertTailieu.HoSoId   = id;
                        if (!String.IsNullOrEmpty(tailieu.DuongDanTep))
                        {
                            insertTailieu.DaTaiLen       = true;
                            insertTailieu.IsActive       = true;
                            insertTailieu.TenTep         = tailieu.TenTep;
                            insertTailieu.DuongDanTep    = tailieu.DuongDanTep;
                            insertTailieu.LoaiTepDinhKem = tailieu.LoaiTepDinhKem;
                        }
                        await _hoSoTepDinhKemRepos.InsertAsync(insertTailieu);
                    }
                }

                // insert pham vi hoat dong
                if (input.PhamViHoatDongIdArr.Count > 0)
                {
                    foreach (var item in input.PhamViHoatDongIdArr)
                    {
                        var hoSoPhamVi = new TT37_HoSoPhamViHD()
                        {
                            HoSoId           = id,
                            PhamViHoatDongId = item,
                        };
                        await _hosoPhamViHDRepos.InsertAsync(hoSoPhamVi);
                    }
                }

                CurrentUnitOfWork.SaveChanges();
                return(id);
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message);
                return(0);
            }
        }
        private async Task <long> UpdateHoSo(CreateOrUpdateHoSo37Input input)
        {
            try
            {
                var updateData = await _hoSoRepos.GetAsync(input.HoSo.Id);

                if (updateData != null)
                {
                    //update lại tài liệu
                    var userId = _session.UserId;
                    var teps   = _hoSoTepDinhKemRepos.GetAll().Where(x => x.HoSoId == updateData.Id && x.IsActive == true);
                    if (teps != null && teps.Count() != 0)
                    {
                        foreach (var tep in teps)
                        {
                            if (updateData.TrangThaiHoSo != (int)TRANG_THAI_HO_SO.DA_HOAN_TAT)
                            {
                                await _hoSoTepDinhKemRepos.DeleteAsync(tep);
                            }
                        }
                    }

                    foreach (var tailieu in input.Teps)
                    {
                        var    insertTailieu = tailieu.MapTo <XHoSoTepDinhKem>();
                        string HCC_FILE_PDF  = GetUrlFileDefaut();
                        insertTailieu.ThuTucId = (int)CommonENum.THU_TUC_ID.THU_TUC_37;
                        insertTailieu.HoSoId   = updateData.Id;
                        if (!String.IsNullOrEmpty(tailieu.DuongDanTep))
                        {
                            insertTailieu.Id             = 0;
                            insertTailieu.DaTaiLen       = true;
                            insertTailieu.IsActive       = true;
                            insertTailieu.TenTep         = tailieu.TenTep;
                            insertTailieu.DuongDanTep    = tailieu.DuongDanTep;
                            insertTailieu.LoaiTepDinhKem = tailieu.LoaiTepDinhKem;
                        }
                        await _hoSoTepDinhKemRepos.InsertAsync(insertTailieu);
                    }

                    if (input.PhamViHoatDongIdArr.Count > 0)
                    {
                        var listPhamViIdSaved = await _hosoPhamViHDRepos.GetAll().Where(x => x.HoSoId == updateData.Id).Select(x => x.PhamViHoatDongId).ToListAsync();

                        foreach (var item in input.PhamViHoatDongIdArr)
                        {
                            if (!listPhamViIdSaved.Contains(item))
                            {
                                var hoSoPhamVi = new TT37_HoSoPhamViHD()
                                {
                                    HoSoId           = updateData.Id,
                                    PhamViHoatDongId = item,
                                };
                                await _hosoPhamViHDRepos.InsertAsync(hoSoPhamVi);
                            }
                        }
                        foreach (var item in listPhamViIdSaved)
                        {
                            if (!input.PhamViHoatDongIdArr.Contains(item.Value))
                            {
                                await _hosoPhamViHDRepos.DeleteAsync(x => x.PhamViHoatDongId == item && x.HoSoId == updateData.Id);
                            }
                        }
                    }

                    //update lai thong tin textbox ho so
                    input.HoSo.MapTo(updateData);
                    updateData.IsCA          = false;
                    updateData.DuongDanTepCA = null;
                    //Loai ho so
                    if (updateData.LoaiHoSoId.HasValue)
                    {
                        var lhs = await _loaiHoSoRepos.GetAsync(updateData.LoaiHoSoId.Value);

                        updateData.TenLoaiHoSo = lhs.TenLoaiHoSo;
                    }
                    #region Sinh Mã Hồ Sơ

                    if (string.IsNullOrEmpty(updateData.MaHoSo))
                    {
                        updateData.MaHoSo = SinhMaHoSo(updateData.Id, updateData.CreationTime, updateData.ThuTucId.Value);
                    }
                    if (string.IsNullOrEmpty(updateData.SoDangKy))
                    {
                        updateData.SoDangKy = _capSoThuTucAppService.SinhSoDangKy(updateData.DoanhNghiepId.Value, (int)CommonENum.THU_TUC_ID.THU_TUC_37);
                    }

                    #endregion Sinh Mã Hồ Sơ

                    if (updateData.TrangThaiHoSo != (int)TRANG_THAI_HO_SO.SUA_DOI_BO_SUNG && updateData.TrangThaiHoSo != (int)TRANG_THAI_HO_SO.MOT_CUA_TRA_LAI)
                    {
                        updateData.TrangThaiHoSo = input.HoSo.TrangThaiHoSo;
                    }
                    await _hoSoRepos.UpdateAsync(updateData);
                }
                CurrentUnitOfWork.SaveChanges();
                return(updateData.Id);
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message);
                return(0);
            }
        }