Пример #1
0
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            bool removeOldPos = true;
            if (chkRemoveOldPos.CheckState == CheckState.Unchecked)
            {
                removeOldPos = false;
            }
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            IMultiuserWorkspaceEdit mwspEdit = (IMultiuserWorkspaceEdit)sdeConn.Workspace;
            ITnFeatureClassName _fcName = new TnFeatureClassName(sdeConn.Workspace);
            ITnTableName _tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace, sdeConn.Environment);
            IQueryFilter qrf = new QueryFilterClass();
            IFeatureClass tgdFeatureClass = null;
            ICurrentConfig config = CurrentConfig.CallMe();
            List<object[,]> pairColValTgd = new List<object[,]>();
            int rowTgdNnHandleUpdate = 0;
            List<object> newId = new List<object>();
            string tgdDraft = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, config.NamApDung);
            _fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            _fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            try
            {
                tgdFeatureClass = fw.OpenFeatureClass(tgdDraft);
            }
            catch (Exception exc)
            {
                //evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgdDraft);
                //onCalculating(evt);
                //onFinished(evt);
                return;
            }
            ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);

            //lay thong tin thua
            for (int i=0;i< _lstThuaFt.Count;i++)
            {
                IFeature thuaFt = _lstThuaFt[i];

                //string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));

                object mathua = _lstMaThua[i];//txtMathua.Text;
                string maduong = txtMaDuong.Text;
                string mahem = txtMaHem.Text;

                if (maduong == "" || maduong ==null)
                {
                    maduong = "0";
                }
                if (mahem == "" || mahem == null)
                {
                    mahem = "0";
                }
                string hesoVitri = txtHsk.Text;
                ICursor tgdFcs1;
                IRow tgdRow = null;
                if (removeOldPos)
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua);
                    tgdFcs1=tblThuaGiaDat.Search(qrf, false);
                    try
                    {
                        while ((tgdRow = tgdFcs1.NextRow()) != null)
                        {
                            #region xoa feature cu
                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            wspEdit.StartEditOperation();
                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                            tgdRow.Delete();
                            wspEdit.StopEditOperation();
                            wspEdit.StopEditing(true);
                            #endregion
                        }
                    }
                    catch { }
                    finally { Marshal.ReleaseComObject(tgdFcs1); }
                }

                if (mahem == "0")
                {
                    qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                    _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                    _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                }
                else if (mahem != "0")
                {
                    qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                    _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_HEM, mahem,
                                    _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                }
                //MessageBox.Show("line 157 FrmSet.., ",+ qrf.WhereClause);
                ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);

                try
                {
                    tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                    if (tgdRow != null)
                    {
                        //MessageBox.Show("co");
                        //kiem tra co cho phep tinh lai vi tri
                        //neu co:xoa feater cu,them feature moi
                        #region xet thua da co vi tri

                        bool isOverWritePos = true;

                        if (!isOverWritePos)
                        {
                            newId.Add(tgdRow.OID);
                            //continue;
                        }
                        else
                        {
                            //[kodoi]
                            //===================
                            #region xoa feature cu
                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            wspEdit.StartEditOperation();
                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                            tgdRow.Delete();
                            wspEdit.StopEditOperation();
                            wspEdit.StopEditing(true);
                            #endregion
                            //===================

                            //[thaydoi] - them gia tri
                            //**********************
                            #region them feature moi
                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            wspEdit.StartEditOperation();
                            object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                            wspEdit.StopEditOperation();
                            wspEdit.StopEditing(true);

                            //them gia tri mathua,maduong,hesovitri
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                            sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                            rowTgdNnHandleUpdate++;
                            pairColValTgd.Clear();
                            newId.Add(copiedId);
                            #endregion
                            //**********************
                        }

                        #endregion

                    }
                    else
                    {
                        //MessageBox.Show("ko co");
                        //MessageBox.Show("line 220 FrmSet.., " + thuaFt.ToString());
                        //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                        //***********************************
                        #region them feature moi
                        mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        wspEdit.StartEditOperation();
                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                        //MessageBox.Show("line 220 FrmSet.., copiedid" + copiedId.ToString());
                        wspEdit.StopEditOperation();
                        wspEdit.StopEditing(true);

                        //them gia tri mathua,maduong,hesovitri
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                        //MessageBox.Show(string.Format("line 248 FrmSet.., mathua={0},maduong={1},hsk={2}, dientich={3}" , mathua,maduong,hesoVitri,dientichpl));
                        //MessageBox.Show(string.Format("line 249 FrmSet..,index mathua={0},maduong={1},hsk={2}, dientich={3}" , _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY)));
                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                        rowTgdNnHandleUpdate++;
                        pairColValTgd.Clear();
                        newId.Add(copiedId);
                        #endregion
                        //***********************************
                    }
                    //MessageBox.Show(newId.Count.ToString());
                }
                catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                finally { Marshal.ReleaseComObject(tgdFcs); }
            }
            #region luu thong tin vao bang gia dat
            //MessageBox.Show("line 250 FrmSet..., "+wspEdit.IsBeingEdited().ToString());
            if (!sdeTblTgdEditor.IsEditing())
            {
                sdeTblTgdEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                //MessageBox.Show("line 250 FrmSet..., " + wspEdit.IsBeingEdited().ToString());
                sdeTblTgdEditor.StartEditOperation();
            }
            try
            {
                sdeTblTgdEditor.SaveEdit();
                sdeTblTgdEditor.StopEditOperation();
                sdeTblTgdEditor.StopEditing(true);
            }
            catch
            {
                sdeTblTgdEditor.AbortEditOperation();
                sdeTblTgdEditor.StopEditing(false);
            }

            #endregion

            #region tinh gia dat cho cac thua vua them vi tri
            CalcLandprice calc = new CalcLandprice();
            calc.Calculate(newId);
            newId.Clear();
            #endregion
            this.Close();
        }
Пример #2
0
        protected override void calculate()
        {
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;
            evt.CurrentIndexCalculator = this._index;
            evt.Log = string.Format("**********  Bắt đầu tính cho thửa nông nghiệp ở vị trí {0} ********", vitri);
            onCalculating(evt);
            #endregion

            //[thaydoi] - them cac khai bao can thiet
            //************************************
            #region khai bao cac bien
            //Lay connection info hien tai
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            this._fcName = new TnFeatureClassName(sdeConn.Workspace);
            this._tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace,sdeConn.Environment);

            #region thua
            string thuaName = string.Format("{0}_{1}", DataNameTemplate.Thua, this._currentConfig.NamApDung);
            _fcName.FC_THUA.NAME = thuaName;
            _fcName.FC_THUA.InitIndex();
            IFeatureClass thuaFeatureClass = fw.OpenFeatureClass(thuaName);
            IFeatureLayer thuaFeatureLayer = new FeatureLayerClass();
            thuaFeatureLayer.FeatureClass = thuaFeatureClass;
            IFeatureSelection thuaFeatureSelection;
            #endregion
            #region xa
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Ranh_Xa_Poly);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureSelection xaFeatureSelection;
            #endregion
            #region duong
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Duong);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            IFeatureSelection duongFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion

            #region thua gia dat
            string tgd = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, this._currentConfig.NamApDung);
            _fcName.FC_THUA_GIADAT_DRAFT.NAME = tgd;
            _fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            //_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgd;
            //_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            IFeatureClass tgdFeatureClass=null;
            try
            {
                tgdFeatureClass = fw.OpenFeatureClass(tgd);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgd);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            IFeatureLayer tgdFeatureLayer = new FeatureLayerClass();
            ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);
            #endregion

            #region gia dat duong
            //string gdd = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Duong, this._currentConfig.NamApDung);
            //_tblName.GIA_DAT_DUONG.NAME = gdd;
            //_tblName.GIA_DAT_DUONG.InitIndex();
            #endregion

            #region ten duong
            ITable tblTenDuong;
            try
            {
                tblTenDuong = fw.OpenTable(DataNameTemplate.Ten_Duong);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Ten_Duong);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region loai dat
            ITable tblLoaiDat;
            try
            {
                tblLoaiDat = fw.OpenTable(DataNameTemplate.Loai_Dat);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Loai_Dat);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region he so vi tri
            ITable tblHesoVitri;
            try
            {
                tblHesoVitri = fw.OpenTable(DataNameTemplate.He_So_K);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.He_So_K);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region khac
            IQueryFilter qrf = new QueryFilterClass();
            bool result = false;
            int sothuatimthay = 0;
            int sothuatinhduoc = 0;
            int sothuaKhongTinhDuoc = 0;
            #endregion

            #endregion
            //************************************

            //*******************************************
            //===========================================
            //===========================================

            #region bat dau tinh

            //[thaydoi] - cac may tinh khac chi can thay dieu kien truy van he so vi tri
            //******************************************************************
            #region lay cac quy tac tim vi tri

            #region log---
            evt.Log = string.Format("\n----Lấy các quy tắc tìm vị trí thửa từ bảng {0}, ứng với hệ số {1} ...", DataNameTemplate.He_So_K, TnHeSoK.DatNongNghiepVt1);
            onCalculating(evt);
            #endregion

            qrf.WhereClause = string.Format("{0}='{1}'", "hesovitri", TnHeSoK.DatNongNghiepVt1);
            ICursor cur = tblHesoVitri.Search(qrf, false);
            string quytac = "";
            string cachtinh = "";
            string cachtinhdongia = "";
            try
            {
                IRow row = cur.NextRow();
                if (row != null)
                {
                    quytac = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.QUY_TAC)).ToString();
                    cachtinh = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH)).ToString();
                    cachtinhdongia = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH_DON_GIA)).ToString();
                }
            }
            catch { }
            finally
            {
                Marshal.ReleaseComObject(cur);
            }
            #endregion

            #region tinh theo xa
            if (this._inputParams.MA_XA != "-1")
            {
                //[kodoi]
                //=======================
                #region chon xa co ma dang xet
                //neu dang tinh cho 1 xa
                if (_inputParams.MA_XA != "*")
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.MA_XA, _inputParams.MA_XA);
                }
                else//neu dang tinh cho toan huyen
                {
                    qrf.WhereClause = "";
                }
                ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                xaFeatureSelection.SelectionSet = xaSelectionSet;
                #endregion
                //=======================

                #region vong lap tung xa

                //[thaydoi] - co the them bien chay
                //*************************
                #region khoi dau
                IEnumIDs xaIds = xaSelectionSet.IDs;
                int xaId = 0;
                IFeature xaFt = null;
                List<object> lstMaDuong = new List<object>();
                #endregion
                //*************************

                while ((xaId = xaIds.Next()) != -1)
                {
                    //[kodoi]
                    //=======================
                    #region lay thong tin xa
                    xaFt = xaFeatureClass.GetFeature(xaId);
                    string maxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_XA)).ToString();
                    string tenxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.TEN_XA)).ToString();
                    string loaixa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_LOAI_XA)).ToString();
                    #endregion

                    #region -----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho xã/phường: {0}  ******", tenxa);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion

                    #region tim duong trong khu vuc do thi bang qua xa
                    //chon xa dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.OID, xaId);
                    xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                    xaFeatureSelection.SelectionSet = xaSelectionSet;
                    //Chon cac duong co do rong theo quy dinh
                    qrf.WhereClause = "";//string.Format("{0}='{1}'", _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";
                    ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                    duongFeatureSelection.SelectionSet = duongSelectionSet;

                    //bat dau truy van khong gian
                    _qrBl.FromLayer = duongFeatureLayer;
                    _qrBl.ByLayer = xaFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultAnd;
                    _qrBl.UseSelectedFeatures = true;

                    duongSelectionSet = _qrBl.Select();

                    #endregion
                    //=======================

                    #region vong lap xet tung duong

                    //[thaydoi] - co the them bien chay
                    //===============================
                    #region khoi dau
                    IEnumIDs eIds = duongSelectionSet.IDs;
                    int duongId;
                    IFeature ftDuong;
                    int iDuong = 0;
                    int progressingTotalCount = 1;
                    evt.Reset();
                    evt.ProgressingTotal = duongSelectionSet.Count;
                    onCalculating(evt);
                    #endregion
                    //================================

                    while ((duongId = eIds.Next()) != -1)
                    {
                        //[kodoi]
                        //======================
                        #region log-----
                        evt.Reset();
                        evt.ProgressingTotalCount = progressingTotalCount;
                        onCalculating(evt);
                        progressingTotalCount++;
                        #endregion
                        //======================

                        //[capnhat] - lay ten duong tu bang ten duong
                        //++++++++++++++++++++++++
                        #region lay thong tin cua duong dang xet

                        ftDuong = duongFeatureClass.GetFeature(duongId);
                        double dorongDuong;
                        result = double.TryParse(ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.DO_RONG)).ToString(), out dorongDuong);
                        if (!result)
                        {
                            dorongDuong = 0;
                        }
                        if (dorongDuong < _currentConfig.DRongDuongVitri1Nn)
                        {
                            continue;
                        }
                        string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                        object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                        lstMaDuong.Add(maduong);
                        string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                        string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                        #endregion
                        //++++++++++++++++++++++++

                        //[kodoi]
                        //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                        //====================
                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                        duongFeatureSelection.SelectionSet = duongSelectionSet;
                        #endregion
                        //====================

                        //tim cac thua theo dieu kien mat tien
                        #region tinh vi tri cho cac thua
                        //doc cach tim thua trong bang he so vi tri
                        //lay cach tim cho dat o
                        //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                        //MessageBox.Show(func);
                        //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                        //[kodoi]
                        //============================
                        #region tim cac thua theo vi tri quy dinh
                        thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                        thuaFeatureSelection.Clear();
                        Evaluation eval = new Evaluation(quytac);
                        eval.ThuaLayer = thuaFeatureLayer;
                        eval.DuongLayer = duongFeatureLayer;
                        eval.EvaluateQuery();
                        //thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                        ISelectionSet thuaSelectionSet = thuaFeatureSelection.SelectionSet;
                        #endregion
                        //============================

                        //MessageBox.Show(thuaSelectionSet.Count.ToString());
                        #endregion

                        //[kodoi]
                        //============================
                        #region ----log
                        if (thuaSelectionSet.Count == 0)
                        {
                            evt.Log = string.Format("\n !!! Không tìm thấy thửa nào tiếp giáp với đường {0} đoạn từ {1} đến {2} và nằm trong vùng buffer 50m", tenduong, batdau, ketthuc);
                            onCalculating(evt);

                            #region report progressing duong
                            if (iDuong < duongSelectionSet.Count)
                            {
                                decimal i = (decimal)iDuong % (decimal)duongSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iDuong++;
                            #endregion

                            continue;
                        }
                        #endregion
                        sothuatimthay += thuaSelectionSet.Count;
                        //============================

                        #region xet tung thua

                        //[thaydoi] - co the them bien chay
                        //********************
                        #region khoi dau
                        IEnumIDs thuaIds = thuaSelectionSet.IDs;
                        int thuaId;
                        IFeature thuaFt;
                        int iThua = 0;
                        List<object[,]> pairColValTgd = new List<object[,]>();
                        int rowTgdNnHandleUpdate = 0;
                        List<object> newId = new List<object>();
                        #endregion
                        //********************

                        while ((thuaId = thuaIds.Next()) != -1)
                        {
                            int hesoVitri = TnHeSoK.DatNongNghiepVt1;

                            //[thaydoi] - co them them dieu kien de xac dinh vi tri cua thua
                            //***********************************
                            #region lay thong tin thua dang xet

                            //[kodoi]
                            //============================
                            #region lay thong tin co ban
                            thuaFt = thuaFeatureClass.GetFeature(thuaId);
                            //neu thua bi khoa tim vi tri,bo qua,xet thua ke
                            string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                            if (lockTimVitri == "1")
                            {
                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;

                            }
                            //neu thua ko thuoc xa dang xet thi ko tinh tiep
                            object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                            object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                            object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));
                            #endregion
                            //============================

                            //[kodoi]
                            //============================
                            #region kiem tra maxa
                            if (maxaThua.ToString() == "" || maxaThua == null)
                            {
                                maxaThua = "0";

                                #region ----log
                                evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                                #endregion

                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }

                            #region danh rieng khi tinh theo xa
                            else if (maxaThua.ToString() != maxa)
                            {
                                #region ----log
                                evt.Reset();
                                evt.Log = string.Format("thửa {0} không thuộc xã {1}", mathua, tenxa);
                                onCalculating(evt);
                                #endregion
                                sothuaKhongTinhDuoc++;
                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion

                            #endregion
                            //============================

                            #region lay loai dat

                            //neu ko co dat phi nong nghiep thi bo qua,xet thua ke
                            //neu co dat nong nghiep thi datnn=true
                            //neu la dat hon hop, neu co dat sxkd thi datsxkd=true,
                            //neu co dat o thi codato=true
                            //cac truong hop he so vi tri:
                            //o:3010, sxkd:4010, o+nn:5010, o+sxkd:6010
                            string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                            //evt.Log = string.Format("\n loai dat cua thua {0} la {1}", thuaId, loaidat);
                            //onCalculating(evt);

                            //[thaydoi] - may tinh dat nong nghiep co the khac
                            //***********************
                            #region kiem tra dat nong nghiep
                            bool codatnn = false;
                            object datnn = "";
                            foreach (string s in TnLoaiDats.NONG_NGHIEP)
                            {
                                if (loaidat == s)
                                {
                                    codatnn = true;
                                    datnn = s;
                                    break;
                                }
                                else
                                {
                                    codatnn = false;
                                }
                            }
                            if (!codatnn)
                            {
                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion
                            //***********************

                            #endregion

                            #endregion
                            //***********************************

                            //[thaydoi] - thay doi he so vi tri phu hop
                            //***********************************
                            #region quyet dinh he so vi tri cho thua
                            if (codatnn)
                            {
                                hesoVitri = TnHeSoK.DatNongNghiepVt1;
                            }
                            else
                            {
                                hesoVitri = TnHeSoK.KhongXacDinh;
                            }
                            #endregion
                            //***********************************

                            //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                            //***********************************
                            #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                            evt.Reset();
                            evt.Log = string.Format("\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới cho thửa {0} ...", mathua);
                            onCalculating(evt);
                            qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                            ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                            IRow tgdRow = null;
                            //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                            try
                            {
                                tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                                if (tgdRow != null)
                                {
                                    //MessageBox.Show("co");
                                    //kiem tra co cho phep tinh lai vi tri
                                    //neu co:xoa feater cu,them feature moi
                                    #region xet thua da co vi tri

                                    bool isOverWritePos = true;

                                    if (!isOverWritePos)
                                    {
                                        newId.Add(tgdRow.OID);
                                        //continue;
                                    }
                                    else
                                    {
                                        //[kodoi]
                                        //===================
                                        #region xoa feature cu
                                        wspEdit.StartEditing(true);
                                        wspEdit.StartEditOperation();
                                        //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                        //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                        tgdRow.Delete();
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                        #endregion
                                        //===================

                                        //[thaydoi] - them gia tri
                                        //**********************
                                        #region them feature moi
                                        wspEdit.StartEditing(true);
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        newId.Add(copiedId);
                                        #endregion
                                        //**********************
                                    }

                                    #endregion

                                }
                                else
                                {
                                    //MessageBox.Show("ko co");
                                    //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                    //***********************************
                                    #region them feature moi
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion
                                    //***********************************
                                }
                                //MessageBox.Show(newId.Count.ToString());
                            }
                            catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                            finally { Marshal.ReleaseComObject(tgdFcs); }
                            #endregion
                            //***********************************

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion
                        }
                        #endregion

                        //[kodoi]
                        //============================
                        #region luu thong tin vao bang gia dat
                        if (!sdeTblTgdEditor.IsEditing())
                        {
                            sdeTblTgdEditor.StartEditing(true);
                            sdeTblTgdEditor.StartEditOperation();
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                        onCalculating(evt);
                        #endregion

                        sdeTblTgdEditor.SaveEdit();
                        sdeTblTgdEditor.StopEditOperation();
                        sdeTblTgdEditor.StopEditing(true);

                        #region ----log
                        evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                        onCalculating(evt);
                        #endregion

                        #endregion

                        #region tinh gia dat cho cac thua vua them vi tri
                        CalcLandprice calc = new CalcLandprice(this);
                        //MessageBox.Show(string.Format("final:{0}", newId.Count));
                        calc.Maduong = maduong;
                        calc.Calculate(newId);
                        newId.Clear();
                        #endregion
                        //============================

                    }
                    #endregion

                }
                #endregion
            }
            #endregion
            //=====================================================================
            //=====================================================================
            #region tinh theo duong,doan duong
            else
            {
                #region timduong co ten dang xet
                //tinh theo doan duong
                #region tinh theo doan duong
                if (_inputParams.MA_DUONG != "-1")
                {
                    qrf.WhereClause = string.Format("{0}={1}", _fcName.FC_DUONG.MA_DUONG, _inputParams.MA_DUONG);// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho đoạn đường có mã: {0}  ******", _inputParams.MA_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                }
                #endregion
                //tinh cho 1 duong
                #region tinh cho 1 duong
                else if (_inputParams.TEN_DUONG != "" && _inputParams.TEN_DUONG != "*")
                {
                    //phai sua lai thiet ke
                    //dung relationshipclass many to many gia duong va ten duong
                    #region tim trong bang ten duong
                    qrf.WhereClause = string.Format("{0}=N'{1}'", _tblName.TEN_DUONG.TEN_DUONG, _inputParams.TEN_DUONG);
                    //MessageBox.Show(string.Format("line 908 CalPosThuaNnVt1, tenduong={0}", _inputParams.TEN_DUONG));
                    ICursor tenduongCur = tblTenDuong.Search(qrf, false);
                    object idDuong;
                    List<object> lstIdDuong=new List<object>();
                    try
                    {
                        IRow tenduongRow = null;
                        while ((tenduongRow = tenduongCur.NextRow()) != null)
                        {
                            idDuong = tenduongRow.get_Value(tenduongRow.Fields.FindField(_tblName.TEN_DUONG.MA_DUONG));
                            lstIdDuong.Add(idDuong);
                        }
                    }
                    catch
                    { }
                    finally{Marshal.ReleaseComObject(tenduongCur);}
                    #endregion

                    #region tim trong bang duong
                    string q = "";
                    for (int i = 0; i < lstIdDuong.Count; i++)
                    {
                        if (i == lstIdDuong.Count - 1)
                        {
                            q +=string.Format("{0}='{1}'",_fcName.FC_DUONG.MA_DUONG,lstIdDuong[i]);
                        }
                        else
                        {
                            q += string.Format("{0}='{1}' or ", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                    }
                        qrf.WhereClause = q;// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho cả đường: {0}  ******", _inputParams.TEN_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                    #endregion
                }
                #endregion
                //tinh cho ca duong
                else
                {
                    qrf.WhereClause = "";
                }
                //MessageBox.Show(string.Format("whereclause:{0}",duong.QueryFilter.WhereClause));
                ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);

                if (duongSelectionSet.Count == 0)
                {
                    return;
                }
                duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                duongFeatureSelection.SelectionSet = duongSelectionSet;

                #endregion

                #region vong lap xet tung duong

                //[thaydoi] - co the them bien chay
                //===============================
                #region khoi dau
                IEnumIDs eIds = duongSelectionSet.IDs;
                int duongId;
                IFeature ftDuong;
                int iDuong = 0;
                int progressingTotalCount = 1;
                evt.Reset();
                evt.ProgressingTotal = duongSelectionSet.Count;
                onCalculating(evt);
                List<object> lstMaDuong = new List<object>();
                #endregion
                //================================

                while ((duongId = eIds.Next()) != -1)
                {
                    //[kodoi]
                    //======================
                    #region log-----
                    evt.Reset();
                    evt.ProgressingTotalCount = progressingTotalCount;
                    onCalculating(evt);
                    progressingTotalCount++;
                    #endregion
                    //======================

                    //[capnhat] - lay ten duong tu bang ten duong
                    //++++++++++++++++++++++++
                    #region lay thong tin cua duong dang xet

                    ftDuong = duongFeatureClass.GetFeature(duongId);
                    double dorongDuong;
                    result = double.TryParse(ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.DO_RONG)).ToString(), out dorongDuong);
                    if (!result)
                    {
                        dorongDuong = 0;
                    }
                    if (dorongDuong < _currentConfig.DRongDuongVitri1Nn)
                    {
                        continue;
                    }
                    string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                    object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                    lstMaDuong.Add(maduong);
                    string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                    string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                    #endregion
                    //++++++++++++++++++++++++

                    //[kodoi]
                    //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                    //====================
                    #region chon duong dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                    duongFeatureSelection.SelectionSet = duongSelectionSet;
                    #endregion
                    //====================

                    //tim cac thua theo dieu kien mat tien
                    #region tinh vi tri cho cac thua
                    //doc cach tim thua trong bang he so vi tri
                    //lay cach tim cho dat o
                    //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                    //MessageBox.Show(func);
                    //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                    //[kodoi]
                    //============================
                    #region tim cac thua theo vi tri quy dinh
                    thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                    thuaFeatureSelection.Clear();
                    Evaluation eval = new Evaluation(quytac);
                    eval.ThuaLayer = thuaFeatureLayer;
                    eval.DuongLayer = duongFeatureLayer;
                    eval.EvaluateQuery();
                    //thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                    ISelectionSet thuaSelectionSet = thuaFeatureSelection.SelectionSet;
                    #endregion
                    //============================

                    //MessageBox.Show(thuaSelectionSet.Count.ToString());
                    #endregion

                    //[kodoi]
                    //============================
                    #region ----log
                    if (thuaSelectionSet.Count == 0)
                    {
                        evt.Log = string.Format("\n !!! Không tìm thấy thửa nào tiếp giáp với đường {0} đoạn từ {1} đến {2} và nằm trong vùng buffer 50m", tenduong, batdau, ketthuc);
                        onCalculating(evt);

                        #region report progressing duong
                        if (iDuong < duongSelectionSet.Count)
                        {
                            decimal i = (decimal)iDuong % (decimal)duongSelectionSet.Count * 100;
                            int i1 = Convert.ToInt32(i);
                            this._bgwCalculating.ReportProgress(i1);
                            //MessageBox.Show("log 009");
                        }
                        else
                        {
                            this._bgwCalculating.ReportProgress(99);
                        }
                        iDuong++;
                        #endregion

                        continue;
                    }
                    #endregion
                    sothuatimthay += thuaSelectionSet.Count;
                    //============================

                    #region xet tung thua

                    //[thaydoi] - co the them bien chay
                    //********************
                    #region khoi dau
                    IEnumIDs thuaIds = thuaSelectionSet.IDs;
                    int thuaId;
                    IFeature thuaFt;
                    int iThua = 0;
                    List<object[,]> pairColValTgd = new List<object[,]>();
                    int rowTgdNnHandleUpdate = 0;
                    List<object> newId = new List<object>();
                    #endregion
                    //********************

                    while ((thuaId = thuaIds.Next()) != -1)
                    {
                        int hesoVitri = TnHeSoK.DatNongNghiepVt1;

                        //[thaydoi] - co them them dieu kien de xac dinh vi tri cua thua
                        //***********************************
                        #region lay thong tin thua dang xet

                        //[kodoi]
                        //============================
                        #region lay thong tin co ban
                        thuaFt = thuaFeatureClass.GetFeature(thuaId);
                        //neu thua bi khoa tim vi tri,bo qua,xet thua ke
                        string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                        if (lockTimVitri == "1")
                        {
                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;

                        }
                        //neu thua ko thuoc xa dang xet thi ko tinh tiep
                        object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                        object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                        object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));
                        #endregion
                        //============================

                        //[kodoi]
                        //============================
                        #region kiem tra maxa
                        if (maxaThua.ToString() == "" || maxaThua==null)
                        {
                            maxaThua = "0";

                            #region ----log
                            evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                            #endregion

                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;
                        }
                        #endregion
                        //============================

                        #region lay loai dat

                        //neu ko co dat phi nong nghiep thi bo qua,xet thua ke
                        //neu co dat nong nghiep thi datnn=true
                        //neu la dat hon hop, neu co dat sxkd thi datsxkd=true,
                        //neu co dat o thi codato=true
                        //cac truong hop he so vi tri:
                        //o:3010, sxkd:4010, o+nn:5010, o+sxkd:6010
                        string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                        //evt.Log = string.Format("\n loai dat cua thua {0} la {1}", thuaId, loaidat);
                        //onCalculating(evt);

                        //[thaydoi] - may tinh dat nong nghiep co the khac
                        //***********************
                        #region kiem tra dat nong nghiep
                        bool codatnn = false;
                        object datnn="";
                        foreach (string s in TnLoaiDats.NONG_NGHIEP)
                        {
                            if (loaidat==s)
                            {
                                codatnn = true;
                                datnn = s;
                                break;
                            }
                            else
                            {
                                codatnn = false;
                            }
                        }
                        if (!codatnn)
                        {
                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;
                        }
                        #endregion
                        //***********************

                        #endregion

                        #endregion
                        //***********************************

                        //[thaydoi] - thay doi he so vi tri phu hop
                        //***********************************
                        #region quyet dinh he so vi tri cho thua
                        if (codatnn)
                        {
                            hesoVitri = TnHeSoK.DatNongNghiepVt1;
                        }
                        else
                        {
                            hesoVitri = TnHeSoK.KhongXacDinh;
                        }
                        #endregion
                        //***********************************

                        //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                        //***********************************
                        #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                        evt.Reset();
                        evt.Log = string.Format("\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới cho thửa {0} ...", mathua);
                        onCalculating(evt);
                        qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                            _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                            _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                            _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                            _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                        ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                        IRow tgdRow = null;
                        //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                        try
                        {
                            tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                            if (tgdRow != null)
                            {
                                //MessageBox.Show("co");
                                //kiem tra co cho phep tinh lai vi tri
                                //neu co:xoa feater cu,them feature moi
                                #region xet thua da co vi tri

                                bool isOverWritePos = true;

                                if (!isOverWritePos)
                                {
                                    newId.Add(tgdRow.OID);
                                    //continue;
                                }
                                else
                                {
                                    //[kodoi]
                                    //===================
                                    #region xoa feature cu
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                    //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                    tgdRow.Delete();
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);
                                    #endregion
                                    //===================

                                    //[thaydoi] - them gia tri
                                    //**********************
                                    #region them feature moi
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion
                                    //**********************
                                }

                                #endregion

                            }
                            else
                            {
                                //MessageBox.Show("ko co");
                                //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                //***********************************
                                #region them feature moi
                                wspEdit.StartEditing(true);
                                wspEdit.StartEditOperation();
                                object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                wspEdit.StopEditOperation();
                                wspEdit.StopEditing(true);

                                //them gia tri mathua,maduong,hesovitri
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                rowTgdNnHandleUpdate++;
                                pairColValTgd.Clear();
                                newId.Add(copiedId);
                                #endregion
                                //***********************************
                            }
                            //MessageBox.Show(newId.Count.ToString());
                        }
                        catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                        finally { Marshal.ReleaseComObject(tgdFcs); }
                        #endregion
                        //***********************************

                        #region report progressing thua
                        if (iThua < thuaSelectionSet.Count)
                        {
                            decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                            int i1 = Convert.ToInt32(i);
                            this._bgwCalculating.ReportProgress(i1);
                            //MessageBox.Show("log 009");
                        }
                        else
                        {
                            this._bgwCalculating.ReportProgress(99);
                        }
                        iThua++;
                        #endregion
                    }
                    #endregion

                    //[kodoi]
                    //============================
                    #region luu thong tin vao bang gia dat
                    if (!sdeTblTgdEditor.IsEditing())
                    {
                        sdeTblTgdEditor.StartEditing(true);
                        sdeTblTgdEditor.StartEditOperation();
                    }

                    #region ----log
                    evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                    onCalculating(evt);
                    #endregion

                    sdeTblTgdEditor.SaveEdit();
                    sdeTblTgdEditor.StopEditOperation();
                    sdeTblTgdEditor.StopEditing(true);

                    #region ----log
                    evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                    onCalculating(evt);
                    #endregion

                    #endregion

                    #region tinh gia dat cho cac thua vua them vi tri
                    CalcLandprice calc = new CalcLandprice(this);
                    //MessageBox.Show(string.Format("final:{0}", newId.Count));
                    calc.Maduong = maduong;
                    calc.Calculate(newId);
                    newId.Clear();
                    #endregion
                    //============================

                }
                #endregion
            }
            #endregion

            #endregion

            //===========================================
            //===========================================
            //*******************************************

            //[kodoi]
            //==============
            #region doan ket
            sothuatinhduoc = sothuatimthay - sothuaKhongTinhDuoc;

            #region ----log
            evt.Log = string.Format("\n\n**************************************\n******* Đã tính xong các thửa phi nông nghiệp tại đô thị vị trí mặt tiền*******\n**********************************");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);

            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);
            #endregion
            //==============
        }
Пример #3
0
        protected override void calculate()
        {
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;
            evt.CurrentIndexCalculator = this._index;
            evt.Log = string.Format("**********  Bắt đầu tính giá cho hẻm {0} ********", vitri);
            onCalculating(evt);
            #endregion

            //[thaydoi] - them cac khai bao can thiet
            //************************************
            #region khai bao cac bien
            //Lay connection info hien tai
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            IMultiuserWorkspaceEdit mwspEdit = (IMultiuserWorkspaceEdit)sdeConn.Workspace;
            this._fcName = new TnFeatureClassName(sdeConn.Workspace);
            this._tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace, sdeConn.Environment);

            #region thua

            //string thuaName = string.Format("{0}_{1}", DataNameTemplate.Thua, this._currentConfig.NamApDung);
            //_fcName.FC_THUA.NAME = thuaName;
            ////MessageBox.Show(string.Format("line 55 CalcMattien {0}", thuaName));
            //_fcName.FC_THUA.InitIndex();
            //IFeatureClass thuaFeatureClass = fw.OpenFeatureClass(thuaName);
            //IFeatureLayer thuaFeatureLayer = new FeatureLayerClass();
            //thuaFeatureLayer.FeatureClass = thuaFeatureClass;
            //IFeatureSelection thuaFeatureSelection;
            #endregion
            #region xa
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Ranh_Xa_Poly);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureSelection xaFeatureSelection;
            #endregion
            #region duong
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Duong);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            IFeatureSelection duongFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion

            #region hem
            IFeatureClass hemFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Hem);
            IFeatureLayer hemChinhFeatureLayer = new FeatureLayerClass();
            hemChinhFeatureLayer.FeatureClass = hemFeatureClass;
            IFeatureSelection hemChinhFeatureSelection;

            //IFeatureClass hemPhuFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Hem);
            IFeatureLayer hemPhuFeatureLayer = new FeatureLayerClass();
            hemPhuFeatureLayer.FeatureClass = hemFeatureClass;
            IFeatureSelection hemPhuFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion
            #region gia dat hem
            string gdh = DataNameTemplate.Gia_Hem + "_" + _currentConfig.NamApDung.ToString();
            _fcName.FC_GIA_DAT_HEM.NAME = gdh;
            _fcName.FC_GIA_DAT_HEM.InitIndex();
            IFeatureClass gdhChinhFeatureClass = fw.OpenFeatureClass(gdh);
            IFeatureLayer gdhChinhFeatureLayer = new FeatureLayerClass();
            gdhChinhFeatureLayer.FeatureClass = gdhChinhFeatureClass;
            IFeatureSelection gdhChinhFeatureSelection;
            ITable tblGdhChinh = (ITable)gdhChinhFeatureClass;
            ISDETableEditor sdeTblGdhEditor = new SDETable(tblGdhChinh, sdeConn.Workspace);
            #endregion

            #region thua gia dat
            //string tgdDraft = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, this._currentConfig.NamApDung);
            //_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            //_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            ////_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            ////_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            //IFeatureClass tgdFeatureClass=null;
            //try
            //{
            //    tgdFeatureClass = fw.OpenFeatureClass(tgdDraft);
            //}
            //catch (Exception exc)
            //{
            //    evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgdDraft);
            //    onCalculating(evt);
            //    onFinished(evt);
            //    return;
            //}
            //ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            //IFeatureLayer tgdFeatureLayer = new FeatureLayerClass();
            //ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);
            #endregion

            #region gia dat duong
            //string gdd = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Duong, this._currentConfig.NamApDung);
            //_tblName.GIA_DAT_DUONG.NAME = gdd;
            //_tblName.GIA_DAT_DUONG.InitIndex();
            #endregion

            #region ten duong
            ITable tblTenDuong;
            try
            {
                tblTenDuong = fw.OpenTable(DataNameTemplate.Ten_Duong);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Ten_Duong);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region loai dat
            //ITable tblLoaiDat;
            //try
            //{
            //    tblLoaiDat = fw.OpenTable(DataNameTemplate.Loai_Dat);
            //}
            //catch (Exception exc)
            //{
            //    evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Loai_Dat);
            //    onCalculating(evt);
            //    onFinished(evt);
            //    return;
            //}
            #endregion

            #region he so vi tri
            //ITable tblHesoVitri;
            //try
            //{
            //    tblHesoVitri = fw.OpenTable(DataNameTemplate.He_So_K);
            //}
            //catch (Exception exc)
            //{
            //    evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.He_So_K);
            //    onCalculating(evt);
            //    onFinished(evt);
            //    return;
            //}
            #endregion

            #region khac
            IQueryFilter qrf = new QueryFilterClass();
            IDataManager _dataManager;
            ISdeVersionInfo _version;
            IBuffer _bufferTool;
            //IClip _clipTool;
            IErase _eraseTool;
            ITnSystemTempPath _sysTempPath;
            _sysTempPath = new TnSystemTempPath();
            bool result = false;
            int sothuatimthay = 0;
            int sothuatinhduoc = 0;
            int sothuaKhongTinhDuoc = 0;
            _dataManager = new DataManager(sdeConn.Workspace, sdeConn.Environment);
            _version = SdeVersionsTool.CallMe();
            //_clipTool = new GExtractTool(sdeConn.Environment);
            _eraseTool = new GExtractTool(sdeConn.Environment);
            string duongBuffer200mNoSde = FilterSdeLayerName.GetActualName(DataNameTemplate.Duong_Buffer_);
            duongBuffer200mNoSde += "200";
            string hemErase200mNoSde = FilterSdeLayerName.GetActualName(DataNameTemplate.Hem_Erase_);
            hemErase200mNoSde += "200";
            //string duongBuffer50mCoSde = string.Format("{0}{1}", DataNameTemplate.Duong_Buffer_, 50);
            string hemErase200mCoSde = string.Format("{0}{1}", DataNameTemplate.Hem_Erase_, 200);
            #endregion

            #endregion
            //************************************

            //*******************************************
            //===========================================
            //===========================================

            #region bat dau tinh
            _bufferTool = new GProximityTool(sdeConn.Environment);

            if (!_dataManager.LayerExist(duongBuffer200mNoSde))
            {

                _bufferTool.BufferInsideSde(duongFeatureClass.AliasName, duongBuffer200mNoSde, 200);
            }
            IFeatureClass duongBuff200FeatureClass = fw.OpenFeatureClass(duongBuffer200mNoSde);
            IFeatureLayer duongBuff200FeatureLayer = new FeatureLayerClass();
            ISelectionSet duongBuff200Sls;
            IFeatureSelection duongBuff200Fsls;
            duongBuff200FeatureLayer.FeatureClass = duongBuff200FeatureClass;
            #region test
            //string ex = string.Format("VongLap(ChonDuong('dorong=10'),VongLap(ChonThua('dientich<1.5'),test([doituonglap]),[doituonglap]))");
            ////ex=string.Format("VongLap(DuongDangChon(),VongLap()
            //Evaluation evalu = new Evaluation(ex);
            //evalu.DuongLayer = duongFeatureLayer;
            //evalu.ThuaLayer = thuaFeatureLayer;
            //evalu.EvaluateCalculating();
            //return;

            #endregion

            //[thaydoi] - cac may tinh khac chi can thay dieu kien truy van he so vi tri
            //******************************************************************
            #region lay cac quy tac tim vi tri

            //#region log---
            //evt.Log = string.Format("\n----Lấy các quy tắc tìm vị trí thửa từ bảng {0}, ứng với hệ số {1} ...", DataNameTemplate.He_So_K, TnHeSoK.DatOMtTtDt);
            //onCalculating(evt);
            //#endregion
            ////MessageBox.Show(String.Format("line 189 CalcThuaMattien, hsk={0}",TnHeSoK.DatOMtTtDt));
            //qrf.WhereClause = string.Format("{0}='{1}'", "hesovitri", TnHeSoK.DatOMtTtDt);
            //ICursor cur = tblHesoVitri.Search(qrf, false);
            //string quytac = "";
            //string cachtinh = "";
            //string cachtinhdongia = "";
            //try
            //{
            //    IRow row = cur.NextRow();
            //    if (row != null)
            //    {

            //        quytac = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.QUY_TAC)).ToString();
            //        cachtinh = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH)).ToString();
            //        cachtinhdongia = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH_DON_GIA)).ToString();
            //        //MessageBox.Show(string.Format("line 198 CalcMatTien, quytac={0}", quytac));
            //    }
            //}
            //catch { }
            //finally
            //{
            //    Marshal.ReleaseComObject(cur);
            //}
            #endregion

            #region tinh theo xa
            if (this._inputParams.MA_XA != "-1")
            {
                //[kodoi]
                //=======================
                #region chon xa co ma dang xet
                //neu dang tinh cho 1 xa
                if (_inputParams.MA_XA != "*")
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.MA_XA, _inputParams.MA_XA);
                }
                else//neu dang tinh cho toan huyen
                {
                    qrf.WhereClause = "";
                }
                ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                xaFeatureSelection.SelectionSet = xaSelectionSet;
                #endregion
                //=======================

                #region vong lap tung xa

                //[thaydoi] - co the them bien chay
                //*************************
                #region khoi dau
                IEnumIDs xaIds = xaSelectionSet.IDs;
                int xaId = 0;
                IFeature xaFt = null;
                List<object> lstMaDuong = new List<object>();
                #endregion
                //*************************

                while ((xaId = xaIds.Next()) != -1)
                {
                    //[kodoi]
                    //=======================
                    #region lay thong tin xa
                    xaFt = xaFeatureClass.GetFeature(xaId);
                    string maxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_XA)).ToString();
                    string tenxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.TEN_XA)).ToString();
                    string loaixa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_LOAI_XA)).ToString();
                    #endregion

                    #region -----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho xã/phường: {0}  ******", tenxa);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion

                    #region tim duong trong khu vuc do thi bang qua xa

                    //chon xa dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.OID, xaId);
                    xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                    xaFeatureSelection.SelectionSet = xaSelectionSet;
                    //Chon cac duong co do rong theo quy dinh
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";
                    ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                    duongFeatureSelection.SelectionSet = duongSelectionSet;

                    //bat dau truy van khong gian
                    _qrBl.FromLayer = duongFeatureLayer;
                    _qrBl.ByLayer = xaFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultAnd;
                    _qrBl.UseSelectedFeatures = true;

                    duongSelectionSet = _qrBl.Select();

                    #endregion
                    //=======================

                    #region vong lap xet tung duong

                    //[thaydoi] - co the them bien chay
                    //===============================
                    #region khoi dau
                    IEnumIDs eIds = duongSelectionSet.IDs;
                    int duongId;
                    IFeature ftDuong;
                    int iDuong = 0;
                    int progressingTotalCount = 1;
                    evt.Reset();
                    evt.ProgressingTotal = duongSelectionSet.Count;
                    onCalculating(evt);
                    //List<object> lstMaDuong = new List<object>();
                    #endregion
                    //================================

                    while ((duongId = eIds.Next()) != -1)
                    {
                        //[kodoi]
                        //======================
                        #region log-----
                        evt.Reset();
                        evt.ProgressingTotalCount = progressingTotalCount;
                        onCalculating(evt);
                        progressingTotalCount++;
                        #endregion
                        //======================

                        //[capnhat] - lay ten duong tu bang ten duong
                        //++++++++++++++++++++++++
                        #region lay thong tin cua duong dang xet

                        ftDuong = duongFeatureClass.GetFeature(duongId);
                        string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                        object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                        lstMaDuong.Add(maduong);
                        string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                        string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                        #endregion
                        //++++++++++++++++++++++++

                        //[kodoi]
                        //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                        //====================
                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                        duongFeatureSelection.SelectionSet = duongSelectionSet;
                        #endregion
                        //====================

                        //tim cac thua theo dieu kien mat tien

                        //doc cach tim thua trong bang he so vi tri
                        //lay cach tim cho dat o
                        //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                        //MessageBox.Show(func);
                        //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                        //[kodoi]
                        //============================

                        #region chon hem chinh cua duong
                        qrf.WhereClause = string.Format("{0}='{1}' and ({2}='{3}' or {4} is null)", _fcName.FC_HEM.MA_DUONG, maduong, _fcName.FC_HEM.HEM_CHINH, 0, _fcName.FC_HEM.HEM_CHINH);
                        ISelectionSet hemChinhSelectionSet = hemFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                        hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                        hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;

                        #endregion

                        #region clip cac hem vua tim duoc

                        #region delete bang hem_sau100m_clip
                        IFeatureClass hemClipFc = null;
                        #region log---
                        evt.Log = string.Format("\n----Kiểm tra, xóa bảng {0} ...", hemErase200mCoSde);
                        onCalculating(evt);
                        #endregion

                        //if (_dataManager.LayerExist(hemErase200mNoSde))
                        //{
                        //thuaClipFc = fw.OpenFeatureClass("sde.thua_sau50m_clip");
                        //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        //wspEdit.StartEditing(false);
                        //wspEdit.StartEditOperation();
                        try
                        {
                            ((IFeatureClassManager)_dataManager).DeleteFcInSde(hemErase200mCoSde);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("line 924 CalGiaHemTren200,ex=" + ex.ToString());
                        }
                        //((IDataset)thuaClipFc).Delete();
                        //wspEdit.StopEditOperation();
                        //wspEdit.StopEditing(true);
                        //}
                        #endregion

                        #region chon duong buffer 100 dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongBuff200Sls = duongBuff200FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongBuff200Fsls = (IFeatureSelection)duongBuff200FeatureLayer;
                        duongBuff200Fsls.SelectionSet = duongBuff200Sls;
                        #endregion

                        #region chon hem nam trong vung buffer 200m nhưng khong nam trong vùng buffer 100m
                        _qrBl.FromLayer = hemChinhFeatureLayer;
                        _qrBl.ByLayer = duongFeatureLayer;
                        _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectContainedBy;
                        _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultSubtract;
                        _qrBl.UseSelectedFeatures = true;
                        _qrBl.BufferDistance = 200;
                        try
                        {
                            hemChinhSelectionSet = _qrBl.Select();
                            hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                            hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;
                        }
                        catch
                        { continue; }

                        if (!(hemChinhSelectionSet.Count > 0))
                        {
                            continue;
                        }
                        #endregion

                        #region log---
                        evt.Log = string.Format("\n----Kiểm tra, clip hẻm theo đường buffer {0}m, lưu vào bảng {1} ...", _currentConfig.DKhoangCach50mMatTien, hemErase200mCoSde);
                        onCalculating(evt);
                        #endregion

                        string hemlyr = string.Format("{0}/{1}", _sysTempPath.TempPath, hemErase200mNoSde);
                        string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, duongBuffer200mNoSde);
                        _eraseTool.EraseByLayerFileInsideSde(hemChinhFeatureLayer, hemlyr, duongBuff200FeatureLayer, duonglyr, hemErase200mNoSde);
                        try
                        {
                            hemClipFc = fw.OpenFeatureClass(hemErase200mCoSde);
                        }
                        catch (Exception ex) { continue; }
                        _version.RegisterDataset((IDataset)hemClipFc, true, true);
                        #endregion

                        #region xet tung hem da clip

                        //[thaydoi] - co the them bien chay
                        //********************
                        #region khoi dau
                        //IFeatureCursor hemErCur = hemClipFc.Search(null, false);

                        IEnumIDs hemChinhIds = hemChinhSelectionSet.IDs;
                        int hemChinhId;
                        IFeature hemChinhFt;
                        int iHem = 0;
                        List<object[,]> pairColValTgd = new List<object[,]>();
                        int rowTgdNnHandleUpdate = 0;
                        List<object> newId = new List<object>();
                        #endregion
                        try
                        {
                            while ((hemChinhId = hemChinhIds.Next()) != -1)
                            {
                                //he so vi tri hem ung voi doan sau <100m
                                int hesoVitri = 3;
                                #region lay thong tin hem dang xet

                                //[kodoi]
                                //============================
                                #region lay thong tin co ban
                                hemChinhFt = hemFeatureClass.GetFeature(hemChinhId);
                                //hemErId = hemErFt.OID;
                                object maHem = hemChinhFt.get_Value(hemChinhFt.Fields.FindField(_fcName.FC_HEM.MA_HEM));

                                #endregion

                                #endregion

                                #region kiem tra trong bang giadat_hem, voi dieu kien:mahem,hesovitri,khoagia=0
                                evt.Reset();
                                evt.Log = "\n[!]--- Kiểm tra các hẻm đã có giá, cập nhật giá mới ...";
                                onCalculating(evt);
                                qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}'",
                                    _fcName.FC_GIA_DAT_HEM.LOCKED, 0, _fcName.FC_GIA_DAT_HEM.LOCKED,
                                    _fcName.FC_GIA_DAT_HEM.MA_HEM, maHem,
                                    _fcName.FC_GIA_DAT_HEM.HE_SO, hesoVitri);
                                IFeatureCursor gdhChinhFcs = gdhChinhFeatureClass.Search(qrf, false);
                                IFeature gdhChinhFt = null;

                                try
                                {
                                    gdhChinhFt = gdhChinhFcs.NextFeature();//dam bao la chi co 1 hang ket qua
                                    if (gdhChinhFt != null)
                                    {
                                        //MessageBox.Show("co");
                                        //kiem tra co cho phep tinh lai vi tri
                                        //neu co:xoa feater cu,them feature moi
                                        #region xet thua da co vi tri

                                        bool isOverWritePos = true;

                                        if (!isOverWritePos)
                                        {
                                            newId.Add(gdhChinhFt.OID);
                                            //continue;
                                        }
                                        else
                                        {
                                            //[kodoi]
                                            //===================
                                            #region xoa feature cu
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                            gdhChinhFt.Delete();
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                            #endregion
                                            //===================
                                            #region chon trong bang thua clip
                                            qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                            IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                            IFeature hemErFt = null;
                                            try
                                            {
                                                hemErFt = hemClipFcur.NextFeature();
                                                if (hemErFt == null)
                                                {
                                                    continue;
                                                }
                                            }
                                            catch (Exception ex) { return; }
                                            finally { Marshal.ReleaseComObject(hemClipFcur); }

                                            #region lay thong tin thua clip
                                            //IArea area = (IArea)hemClipFt.Shape;
                                            #endregion

                                            #endregion
                                            //[thaydoi] - them gia tri
                                            //**********************
                                            #region them feature moi
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                            try
                                            {
                                                wspEdit.StopEditOperation();
                                                wspEdit.StopEditing(true);
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show("line 1292 CalcGiaHemChinh ex=" + ex.ToString());
                                                wspEdit.StopEditOperation();
                                                wspEdit.StopEditing(false);
                                            }

                                            //them gia tri mathua,maduong,hesovitri
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                            pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                            //MessageBox.Show(string.Format("line 1062 CalcGiaHemChinh mahem={0},maduong={1}", maHem, maduong));
                                            sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                            rowTgdNnHandleUpdate++;
                                            pairColValTgd.Clear();
                                            //MessageBox.Show("line 1066 CalcGiaHemChinh copyid=" + copiedId);
                                            newId.Add(copiedId);
                                            #endregion
                                            //**********************
                                        }

                                        #endregion

                                    }
                                    else
                                    {
                                        //MessageBox.Show("ko co");
                                        //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                        //***********************************
                                        #region chon trong bang thua clip
                                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                        //MessageBox.Show("line 1322 CalcGiaHemChinh, mahem=" + maHem);
                                        IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                        IFeature hemErFt = null;
                                        try
                                        {
                                            hemErFt = hemClipFcur.NextFeature();
                                            if (hemErFt == null)
                                            {
                                                continue;
                                            }
                                        }
                                        catch (Exception ex) { return; }
                                        finally { Marshal.ReleaseComObject(hemClipFcur); }

                                        #region lay thong tin thua clip
                                        //IArea area = (IArea)hemClipFt.Shape;
                                        #endregion

                                        #endregion
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                        try
                                        {
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("line 1355 CalcGiaHemChinh ex=" + ex.ToString());
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(false);
                                        }

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                        //MessageBox.Show(string.Format("line 1125 CalcGiaHemChinh mahem={0},maduong={1}",maHem,maduong));
                                        sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        //MessageBox.Show("line 1129 CalcGiaHemChinh copyid=" + copiedId);
                                        newId.Add(copiedId);

                                        #endregion
                                        //***********************************
                                    }
                                    //MessageBox.Show(newId.Count.ToString());
                                }
                                catch (Exception e1)
                                {
                                    MessageBox.Show(string.Format("CalcGiaHemChinh, line 1448-\n{0}", e1));
                                }
                                finally { Marshal.ReleaseComObject(gdhChinhFcs); }

                                #endregion
                            }
                        }
                        catch (Exception ex) { }
                        //finally { Marshal.ReleaseComObject(hemErCur); }

                        #endregion

                        //[kodoi]
                        //============================
                        #region luu thong tin vao bang gia dat
                        if (!sdeTblGdhEditor.IsEditing())
                        {
                            sdeTblGdhEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            sdeTblGdhEditor.StartEditOperation();
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                        onCalculating(evt);
                        #endregion
                        try
                        {
                            sdeTblGdhEditor.SaveEdit();
                            sdeTblGdhEditor.StopEditOperation();
                            sdeTblGdhEditor.StopEditing(true);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("line 1405 CalcGiaHemChinh ex=" + ex.ToString());
                            sdeTblGdhEditor.StopEditOperation();
                            sdeTblGdhEditor.StopEditing(false);
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                        onCalculating(evt);
                        #endregion

                        #endregion

                        #region tinh gia dat cho cac hem vua them vi tri
                        CalcLandprice calc = new CalcLandprice(this);
                        //MessageBox.Show(string.Format("final:{0}", newId.Count));

                        calc.CalcGiaHem(newId);
                        newId.Clear();
                        #endregion
                        //============================

                    }
                    #endregion

                }
                #endregion
            }
            #endregion
            //=====================================================================
            //=====================================================================
            #region tinh theo duong,doan duong

            else
            {
                #region timduong co ten dang xet
                //tinh theo doan duong
                #region tinh theo doan duong
                if (_inputParams.MA_DUONG != "-1")
                {
                    qrf.WhereClause = string.Format("{0}={1} and {2}={3}", _fcName.FC_DUONG.MA_DUONG, _inputParams.MA_DUONG, _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho đoạn đường có mã: {0}  ******", _inputParams.MA_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                }
                #endregion
                //tinh cho 1 duong
                #region tinh cho 1 duong
                else if (_inputParams.TEN_DUONG != "" && _inputParams.TEN_DUONG != "*")
                {
                    //phai sua lai thiet ke
                    //dung relationshipclass many to many gia duong va ten duong
                    #region tim trong bang ten duong
                    qrf.WhereClause = string.Format("{0}=N'{1}'", _tblName.TEN_DUONG.TEN_DUONG, _inputParams.TEN_DUONG);
                    ICursor tenduongCur = tblTenDuong.Search(qrf, false);
                    object idDuong;
                    List<object> lstIdDuong = new List<object>();
                    try
                    {
                        IRow tenduongRow = null;
                        while ((tenduongRow = tenduongCur.NextRow()) != null)
                        {
                            idDuong = tenduongRow.get_Value(tenduongRow.Fields.FindField(_tblName.TEN_DUONG.MA_DUONG));
                            lstIdDuong.Add(idDuong);
                        }
                    }
                    catch
                    { }
                    finally { Marshal.ReleaseComObject(tenduongCur); }
                    #endregion

                    #region tim trong bang duong
                    string q = "";
                    for (int i = 0; i < lstIdDuong.Count; i++)
                    {
                        if (i == lstIdDuong.Count - 1)
                        {
                            q += string.Format("{0}='{1}'", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                        else
                        {
                            q += string.Format("{0}='{1}' or ", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                    }
                    qrf.WhereClause = q;// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho cả đường: {0}  ******", _inputParams.TEN_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                    #endregion
                }
                #endregion
                //tinh cho ca duong
                else
                {
                    qrf.WhereClause = "";
                }
                //MessageBox.Show(string.Format("whereclause:{0}",duong.QueryFilter.WhereClause));
                ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);

                if (duongSelectionSet.Count == 0)
                {
                    return;
                }
                duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                duongFeatureSelection.SelectionSet = duongSelectionSet;

                #endregion

                #region vong lap xet tung duong

                //[thaydoi] - co the them bien chay
                //===============================
                #region khoi dau
                IEnumIDs eIds = duongSelectionSet.IDs;
                int duongId;
                IFeature ftDuong;
                int iDuong = 0;
                int progressingTotalCount = 1;
                evt.Reset();
                evt.ProgressingTotal = duongSelectionSet.Count;
                onCalculating(evt);
                List<object> lstMaDuong = new List<object>();
                #endregion
                //================================

                while ((duongId = eIds.Next()) != -1)
                {
                    //[kodoi]
                    //======================
                    #region log-----
                    evt.Reset();
                    evt.ProgressingTotalCount = progressingTotalCount;
                    onCalculating(evt);
                    progressingTotalCount++;
                    #endregion
                    //======================

                    //[capnhat] - lay ten duong tu bang ten duong
                    //++++++++++++++++++++++++
                    #region lay thong tin cua duong dang xet

                    ftDuong = duongFeatureClass.GetFeature(duongId);
                    string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                    object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                    lstMaDuong.Add(maduong);
                    string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                    string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                    #endregion
                    //++++++++++++++++++++++++

                    //[kodoi]
                    //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                    //====================
                    #region chon duong dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                    duongFeatureSelection.SelectionSet = duongSelectionSet;
                    #endregion
                    //====================

                    //tim cac thua theo dieu kien mat tien

                    //doc cach tim thua trong bang he so vi tri
                    //lay cach tim cho dat o
                    //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                    //MessageBox.Show(func);
                    //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                    //[kodoi]
                    //============================

                    #region chon hem chinh cua duong
                    qrf.WhereClause = string.Format("{0}='{1}' and ({2}='{3}' or {4} is null)", _fcName.FC_HEM.MA_DUONG, maduong, _fcName.FC_HEM.HEM_CHINH, 0, _fcName.FC_HEM.HEM_CHINH);
                    ISelectionSet hemChinhSelectionSet = hemFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                    hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;

                    #endregion

                    #region clip cac hem vua tim duoc

                    #region delete bang hem_sau100m_clip
                    IFeatureClass hemClipFc = null;
                    #region log---
                    evt.Log = string.Format("\n----Kiểm tra, xóa bảng {0} ...", hemErase200mCoSde);
                    onCalculating(evt);
                    #endregion

                    //if (_dataManager.LayerExist(hemErase200mNoSde))
                    //{
                        //thuaClipFc = fw.OpenFeatureClass("sde.thua_sau50m_clip");
                        //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        //wspEdit.StartEditing(false);
                        //wspEdit.StartEditOperation();
                        try
                        {
                            ((IFeatureClassManager)_dataManager).DeleteFcInSde(hemErase200mCoSde);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("line 924 CalGiaHemTren200,ex=" + ex.ToString());
                        }
                        //((IDataset)thuaClipFc).Delete();
                        //wspEdit.StopEditOperation();
                        //wspEdit.StopEditing(true);
                    //}
                    #endregion

                    #region chon duong buffer 100 dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongBuff200Sls = duongBuff200FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongBuff200Fsls = (IFeatureSelection)duongBuff200FeatureLayer;
                    duongBuff200Fsls.SelectionSet = duongBuff200Sls;
                    #endregion

                    #region chon hem nam trong vung buffer 200m nhưng khong nam trong vùng buffer 100m
                    _qrBl.FromLayer = hemChinhFeatureLayer;
                    _qrBl.ByLayer = duongFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectContainedBy;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultSubtract;
                    _qrBl.UseSelectedFeatures = true;
                    _qrBl.BufferDistance = 200;
                    try
                    {
                        hemChinhSelectionSet = _qrBl.Select();
                        hemChinhFeatureSelection = (IFeatureSelection)hemChinhFeatureLayer;
                        hemChinhFeatureSelection.SelectionSet = hemChinhSelectionSet;
                    }
                    catch
                    { continue; }

                    if (!(hemChinhSelectionSet.Count > 0))
                    {
                        continue;
                    }
                    #endregion

                    #region log---
                    evt.Log = string.Format("\n----Kiểm tra, clip hẻm theo đường buffer {0}m, lưu vào bảng {1} ...", _currentConfig.DKhoangCach50mMatTien, hemErase200mCoSde);
                    onCalculating(evt);
                    #endregion

                    string hemlyr = string.Format("{0}/{1}", _sysTempPath.TempPath, hemErase200mNoSde);
                    string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, duongBuffer200mNoSde);
                    _eraseTool.EraseByLayerFileInsideSde(hemChinhFeatureLayer, hemlyr, duongBuff200FeatureLayer, duonglyr, hemErase200mNoSde);
                    try
                    {
                        hemClipFc = fw.OpenFeatureClass(hemErase200mCoSde);
                    }
                    catch (Exception ex) { continue; }
                    _version.RegisterDataset((IDataset)hemClipFc, true, true);
                    #endregion

                    #region xet tung hem da clip

                    //[thaydoi] - co the them bien chay
                    //********************
                    #region khoi dau
                    //IFeatureCursor hemErCur = hemClipFc.Search(null, false);

                    IEnumIDs hemChinhIds = hemChinhSelectionSet.IDs;
                    int hemChinhId;
                    IFeature hemChinhFt;
                    int iHem = 0;
                    List<object[,]> pairColValTgd = new List<object[,]>();
                    int rowTgdNnHandleUpdate = 0;
                    List<object> newId = new List<object>();
                    #endregion
                    try
                    {
                        while ((hemChinhId = hemChinhIds.Next()) != -1)
                        {
                            //he so vi tri hem ung voi doan sau <100m
                            int hesoVitri = 3;
                            #region lay thong tin hem dang xet

                            //[kodoi]
                            //============================
                            #region lay thong tin co ban
                            hemChinhFt = hemFeatureClass.GetFeature(hemChinhId);
                            //hemErId = hemErFt.OID;
                            object maHem = hemChinhFt.get_Value(hemChinhFt.Fields.FindField(_fcName.FC_HEM.MA_HEM));

                            #endregion

                            #endregion

                            #region kiem tra trong bang giadat_hem, voi dieu kien:mahem,hesovitri,khoagia=0
                            evt.Reset();
                            evt.Log = "\n[!]--- Kiểm tra các hẻm đã có giá, cập nhật giá mới ...";
                            onCalculating(evt);
                            qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}'",
                                _fcName.FC_GIA_DAT_HEM.LOCKED, 0, _fcName.FC_GIA_DAT_HEM.LOCKED,
                                _fcName.FC_GIA_DAT_HEM.MA_HEM, maHem,
                                _fcName.FC_GIA_DAT_HEM.HE_SO, hesoVitri);
                            IFeatureCursor gdhChinhFcs = gdhChinhFeatureClass.Search(qrf, false);
                            IFeature gdhChinhFt = null;

                            try
                            {
                                gdhChinhFt = gdhChinhFcs.NextFeature();//dam bao la chi co 1 hang ket qua
                                if (gdhChinhFt != null)
                                {
                                    //MessageBox.Show("co");
                                    //kiem tra co cho phep tinh lai vi tri
                                    //neu co:xoa feater cu,them feature moi
                                    #region xet thua da co vi tri

                                    bool isOverWritePos = true;

                                    if (!isOverWritePos)
                                    {
                                        newId.Add(gdhChinhFt.OID);
                                        //continue;
                                    }
                                    else
                                    {
                                        //[kodoi]
                                        //===================
                                        #region xoa feature cu
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                        //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                        gdhChinhFt.Delete();
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                        #endregion
                                        //===================
                                        #region chon trong bang thua clip
                                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                        IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                        IFeature hemErFt = null;
                                        try
                                        {
                                            hemErFt = hemClipFcur.NextFeature();
                                            //MessageBox.Show("line 1058 CalcGiaHemTren200, id="+hemErFt.get_Value(0));
                                            if (hemErFt == null)
                                            {
                                                continue;
                                            }
                                        }
                                        catch (Exception ex) { continue; }
                                        finally { Marshal.ReleaseComObject(hemClipFcur); }

                                        #region lay thong tin thua clip
                                        //IArea area = (IArea)hemClipFt.Shape;
                                        #endregion

                                        #endregion
                                        //[thaydoi] - them gia tri
                                        //**********************
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                        try
                                        {
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("line 1292 CalcGiaHemChinh ex=" + ex.ToString());
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(false);
                                        }

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                        pairColValTgd.Add(new object[,] { { gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                        //MessageBox.Show(string.Format("line 1062 CalcGiaHemChinh mahem={0},maduong={1}", maHem, maduong));
                                        sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        //MessageBox.Show("line 1066 CalcGiaHemChinh copyid=" + copiedId);
                                        newId.Add(copiedId);
                                        #endregion
                                        //**********************
                                    }

                                    #endregion

                                }
                                else
                                {
                                    //MessageBox.Show("ko co");
                                    //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                    //***********************************
                                    #region chon trong bang thua clip
                                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, maHem);
                                    //MessageBox.Show("line 1322 CalcGiaHemChinh, mahem=" + maHem);
                                    IFeatureCursor hemClipFcur = hemClipFc.Search(qrf, false);
                                    IFeature hemErFt = null;
                                    try
                                    {
                                        hemErFt = hemClipFcur.NextFeature();
                                        if (hemErFt == null)
                                        {
                                            continue;
                                        }
                                    }
                                    catch (Exception ex) { return; }
                                    finally { Marshal.ReleaseComObject(hemClipFcur); }

                                    #region lay thong tin thua clip
                                    //IArea area = (IArea)hemClipFt.Shape;
                                    #endregion

                                    #endregion
                                    #region them feature moi
                                    if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                    {
                                        mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                    }
                                    else
                                    {
                                        wspEdit.StartEditing(true);
                                    }
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(hemErFt, gdhChinhFeatureClass);
                                    try
                                    {
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("line 1355 CalcGiaHemChinh ex=" + ex.ToString());
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(false);
                                    }

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_HEM), maHem } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.HE_SO), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_GIA_DAT_HEM.GetIndex(_fcName.FC_GIA_DAT_HEM.LOCKED), 0 } });
                                    //MessageBox.Show(string.Format("line 1125 CalcGiaHemChinh mahem={0},maduong={1}",maHem,maduong));
                                    sdeTblGdhEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    //MessageBox.Show("line 1129 CalcGiaHemChinh copyid=" + copiedId);
                                    newId.Add(copiedId);

                                    #endregion
                                    //***********************************
                                }
                                //MessageBox.Show(newId.Count.ToString());
                            }
                            catch (Exception e1)
                            {
                                MessageBox.Show(string.Format("CalcGiaHemChinh, line 1448-\n{0}", e1));
                            }
                            finally { Marshal.ReleaseComObject(gdhChinhFcs); }

                            #endregion
                        }
                    }
                    catch (Exception ex) { }
                    //finally { Marshal.ReleaseComObject(hemErCur); }

                    #endregion

                    //[kodoi]
                    //============================
                    #region luu thong tin vao bang gia dat
                    if (!sdeTblGdhEditor.IsEditing())
                    {
                        sdeTblGdhEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        sdeTblGdhEditor.StartEditOperation();
                    }

                    #region ----log
                    evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                    onCalculating(evt);
                    #endregion
                    try
                    {
                        sdeTblGdhEditor.SaveEdit();
                        sdeTblGdhEditor.StopEditOperation();
                        sdeTblGdhEditor.StopEditing(true);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("line 1405 CalcGiaHemChinh ex=" + ex.ToString());
                        sdeTblGdhEditor.StopEditOperation();
                        sdeTblGdhEditor.StopEditing(false);
                    }

                    #region ----log
                    evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", gdhChinhFeatureClass.AliasName);
                    onCalculating(evt);
                    #endregion

                    #endregion

                    #region tinh gia dat cho cac hem vua them vi tri
                    CalcLandprice calc = new CalcLandprice(this);
                    //MessageBox.Show(string.Format("final:{0}", newId.Count));

                    calc.CalcGiaHem(newId);
                    newId.Clear();
                    #endregion
                    //============================

                }
                #endregion
            }
            #endregion

            #endregion

            //===========================================
            //===========================================
            //*******************************************

            //[kodoi]
            //==============
            #region doan ket
            sothuatinhduoc = sothuatimthay - sothuaKhongTinhDuoc;

            #region ----log
            evt.Log = string.Format("\n\n**************************************\n******* Đã tính xong các thửa phi nông nghiệp tại đô thị vị trí mặt tiền*******\n**********************************");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);
            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);
            #endregion
            //==============
        }
Пример #4
0
        protected override void calculate()
        {
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;
            evt.CurrentIndexCalculator = this._index;
            evt.Log = string.Format("**********  Bắt đầu tính cho thửa phi nông nghiệp ở vị trí {0} tại đô thị ********", vitri);
            onCalculating(evt);
            #endregion

            //[thaydoi] - them cac khai bao can thiet
            //************************************
            #region khai bao cac bien
            //Lay connection info hien tai
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            IMultiuserWorkspaceEdit mwspEdit = (IMultiuserWorkspaceEdit)sdeConn.Workspace;
            this._fcName = new TnFeatureClassName(sdeConn.Workspace);
            this._tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager(sdeConn.Workspace, sdeConn.Environment);

            #region thua

            string thuaName = string.Format("{0}_{1}", DataNameTemplate.Thua, this._currentConfig.NamApDung);
            _fcName.FC_THUA.NAME = thuaName;
            //MessageBox.Show(string.Format("line 55 CalcMattien {0}", thuaName));
            _fcName.FC_THUA.InitIndex();
            IFeatureClass thuaFeatureClass = fw.OpenFeatureClass(thuaName);
            IFeatureLayer thuaFeatureLayer = new FeatureLayerClass();
            thuaFeatureLayer.FeatureClass = thuaFeatureClass;
            IFeatureSelection thuaFeatureSelection;
            #endregion
            #region xa
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Ranh_Xa_Poly);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureSelection xaFeatureSelection;
            #endregion
            #region duong
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Duong);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            IFeatureSelection duongFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion

            #region hem
            IFeatureClass hemFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Hem);
            IFeatureLayer hemChinhFeatureLayer = new FeatureLayerClass();
            hemChinhFeatureLayer.FeatureClass = hemFeatureClass;
            IFeatureSelection hemChinhFeatureSelection;
            #endregion

            #region thua gia dat
            string tgdDraft = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, this._currentConfig.NamApDung);
            _fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            _fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            //_fcName.FC_THUA_GIADAT_DRAFT.NAME = tgdDraft;
            //_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            IFeatureClass tgdFeatureClass = null;
            try
            {
                tgdFeatureClass = fw.OpenFeatureClass(tgdDraft);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgdDraft);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            IFeatureLayer tgdFeatureLayer = new FeatureLayerClass();
            ISDETableEditor sdeTblTgdEditor = new SDETable(tblThuaGiaDat, sdeConn.Workspace);
            #endregion

            #region gia dat hem
            string gdh = DataNameTemplate.Gia_Hem_Phu + "_" + _currentConfig.NamApDung.ToString();
            _fcName.FC_GIA_DAT_HEM_PHU.NAME = gdh;
            _fcName.FC_GIA_DAT_HEM_PHU.InitIndex();
            IFeatureClass gdhChinhFeatureClass = fw.OpenFeatureClass(gdh);
            IFeatureLayer gdhChinhFeatureLayer = new FeatureLayerClass();
            gdhChinhFeatureLayer.FeatureClass = gdhChinhFeatureClass;
            IFeatureSelection gdhChinhFeatureSelection;

            #endregion

            #region gia dat duong
            string gdd = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Duong, this._currentConfig.NamApDung);
            _tblName.GIA_DAT_DUONG.NAME = gdd;
            _tblName.GIA_DAT_DUONG.InitIndex();
            #endregion

            #region ten duong
            ITable tblTenDuong;
            try
            {
                tblTenDuong = fw.OpenTable(DataNameTemplate.Ten_Duong);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Ten_Duong);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region loai dat
            ITable tblLoaiDat;
            try
            {
                tblLoaiDat = fw.OpenTable(DataNameTemplate.Loai_Dat);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Loai_Dat);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region he so vi tri
            ITable tblHesoVitri;
            try
            {
                tblHesoVitri = fw.OpenTable(DataNameTemplate.He_So_K);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.He_So_K);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region khac
            IQueryFilter qrf = new QueryFilterClass();
            bool result = false;
            int sothuatimthay = 0;
            int sothuatinhduoc = 0;
            int sothuaKhongTinhDuoc = 0;
            #endregion

            #endregion
            //************************************

            //*******************************************
            //===========================================
            //===========================================

            #region bat dau tinh

            #region test
            //string ex = string.Format("VongLap(ChonDuong('dorong=10'),VongLap(ChonThua('dientich<1.5'),test([doituonglap]),[doituonglap]))");
            ////ex=string.Format("VongLap(DuongDangChon(),VongLap()
            //Evaluation evalu = new Evaluation(ex);
            //evalu.DuongLayer = duongFeatureLayer;
            //evalu.ThuaLayer = thuaFeatureLayer;
            //evalu.EvaluateCalculating();
            //return;

            #endregion

            //[thaydoi] - cac may tinh khac chi can thay dieu kien truy van he so vi tri
            //******************************************************************
            #region lay cac quy tac tim vi tri

            #region log---
            evt.Log = string.Format("\n----Lấy các quy tắc tìm vị trí thửa từ bảng {0}, ứng với hệ số {1} ...", DataNameTemplate.He_So_K, TnHeSoK.DatOMtTtDt);
            onCalculating(evt);
            #endregion
            //MessageBox.Show(String.Format("line 189 CalcThuaMattien, hsk={0}",TnHeSoK.DatOMtTtDt));
            qrf.WhereClause = string.Format("{0}='{1}'", "hesovitri", TnHeSoK.DatOMtTtDt);
            ICursor cur = tblHesoVitri.Search(qrf, false);
            string quytac = "";
            string cachtinh = "";
            string cachtinhdongia = "";
            try
            {
                IRow row = cur.NextRow();
                if (row != null)
                {

                    quytac = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.QUY_TAC)).ToString();
                    cachtinh = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH)).ToString();
                    cachtinhdongia = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH_DON_GIA)).ToString();
                    //MessageBox.Show(string.Format("line 198 CalcMatTien, quytac={0}", quytac));
                }
            }
            catch { }
            finally
            {
                Marshal.ReleaseComObject(cur);
            }
            #endregion

            #region tinh theo xa
            if (this._inputParams.MA_XA != "-1")
            {
                //[kodoi]
                //=======================
                #region chon xa co ma dang xet
                //neu dang tinh cho 1 xa
                if (_inputParams.MA_XA != "*")
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.MA_XA, _inputParams.MA_XA);
                }
                else//neu dang tinh cho toan huyen
                {
                    qrf.WhereClause = "";
                }
                ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                xaFeatureSelection.SelectionSet = xaSelectionSet;
                #endregion
                //=======================

                #region vong lap tung xa

                //[thaydoi] - co the them bien chay
                //*************************
                #region khoi dau
                IEnumIDs xaIds = xaSelectionSet.IDs;
                int xaId = 0;
                IFeature xaFt = null;
                List<object> lstMaDuong = new List<object>();
                #endregion
                //*************************

                while ((xaId = xaIds.Next()) != -1)
                {
                    //[kodoi]
                    //=======================
                    #region lay thong tin xa
                    xaFt = xaFeatureClass.GetFeature(xaId);
                    string maxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_XA)).ToString();
                    string tenxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.TEN_XA)).ToString();
                    string loaixa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_LOAI_XA)).ToString();
                    int loaidothi = 0;
                    result = int.TryParse(xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.LOAI_DO_THI)).ToString(), out loaidothi);
                    #endregion

                    if (loaidothi == 0)
                    {
                        continue;
                    }

                    #region -----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho xã/phường: {0}  ******", tenxa);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion

                    #region tim duong trong khu vuc do thi bang qua xa
                    //chon xa dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.OID, xaId);
                    xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                    xaFeatureSelection.SelectionSet = xaSelectionSet;
                    //Chon cac duong co do rong theo quy dinh
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";
                    ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                    duongFeatureSelection.SelectionSet = duongSelectionSet;

                    //bat dau truy van khong gian
                    _qrBl.FromLayer = duongFeatureLayer;
                    _qrBl.ByLayer = xaFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultAnd;
                    _qrBl.UseSelectedFeatures = true;

                    duongSelectionSet = _qrBl.Select();

                    #endregion
                    //=======================

                    #region vong lap xet tung duong

                    //[thaydoi] - co the them bien chay
                    //===============================
                    #region khoi dau
                    IEnumIDs eIds = duongSelectionSet.IDs;
                    int duongId;
                    IFeature ftDuong;
                    int iDuong = 0;
                    int progressingTotalCount = 1;
                    evt.Reset();
                    evt.ProgressingTotal = duongSelectionSet.Count;
                    onCalculating(evt);
                    //List<object> lstMaDuong = new List<object>();
                    #endregion
                    //================================

                    while ((duongId = eIds.Next()) != -1)
                    {
                        //[kodoi]
                        //======================
                        #region log-----
                        evt.Reset();
                        evt.ProgressingTotalCount = progressingTotalCount;
                        onCalculating(evt);
                        progressingTotalCount++;
                        #endregion
                        //======================

                        //[capnhat] - lay ten duong tu bang ten duong
                        //++++++++++++++++++++++++
                        #region lay thong tin cua duong dang xet

                        ftDuong = duongFeatureClass.GetFeature(duongId);
                        string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                        object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                        lstMaDuong.Add(maduong);
                        string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                        string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                        #endregion
                        //++++++++++++++++++++++++

                        //[kodoi]
                        //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                        //====================
                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                        duongFeatureSelection.SelectionSet = duongSelectionSet;
                        #endregion
                        //====================

                        #region chon hem gia dat cua duong
                        qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}'",
                                        _fcName.FC_GIA_DAT_HEM_PHU.LOCKED, 0, _fcName.FC_GIA_DAT_HEM_PHU.LOCKED,
                                        _fcName.FC_GIA_DAT_HEM_PHU.MA_DUONG, maduong);
                        ISelectionSet gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                        gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                        gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;

                        #endregion

                        #region xet tung hem co gia
                        IEnumIDs gdhChinhIds = gdhChinhSelectionSet.IDs;
                        //IFeatureCursor hemClipCur = hemClipFc.Search(null, false);
                        int gdhChinhId;
                        IFeature gdhChinhFt;
                        int iHem = 0;
                        //List<object[,]> pairColValTgd = new List<object[,]>();
                        //int rowTgdNnHandleUpdate = 0;
                        //List<object> newId = new List<object>();
                        #endregion
                        while ((gdhChinhId = gdhChinhIds.Next()) != -1)
                        {
                            gdhChinhFt = gdhChinhFeatureClass.GetFeature(gdhChinhId);
                            int hesoHem = 3;
                            result = int.TryParse(gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.HE_SO)).ToString(), out hesoHem);
                            if (!result)
                            {
                                hesoHem = 3;
                            }
                            object giahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.GIA_DAT));
                            #region lay thong tin hem
                            object mahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.MA_HEM));
                            qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, mahem);
                            IFeatureCursor hemCur = hemFeatureClass.Search(qrf, false);
                            IFeature hemFt = null;
                            double dorong = 0;
                            try
                            {
                                if ((hemFt = hemCur.NextFeature()) != null)
                                {
                                    result = double.TryParse(hemFt.get_Value(hemFt.Fields.FindField(_fcName.FC_HEM.DO_RONG)).ToString(), out dorong);
                                    if (!result)
                                    {
                                        dorong = 0;
                                    }
                                }
                            }
                            catch { }
                            finally { Marshal.ReleaseComObject(hemCur); }
                            #endregion

                            #region chon hem dang xet
                            qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_GIA_DAT_HEM_PHU.OID, gdhChinhId);
                            gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                            gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                            gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;
                            #endregion
                            #region chon cac thua tiep giap voi hem dang xet
                            //thuaFeatureSelection.Clear();
                            //IQueryByLayer _qrBl = new QueryByLayerClass();
                            _qrBl.FromLayer = thuaFeatureLayer;
                            _qrBl.ByLayer = gdhChinhFeatureLayer;
                            _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                            _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultNew;
                            _qrBl.UseSelectedFeatures = true;
                            _qrBl.BufferDistance = 1;
                            ISelectionSet thuaSelectionSet;
                            try
                            {
                                thuaSelectionSet = _qrBl.Select();
                                thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                                thuaFeatureSelection.SelectionSet = thuaSelectionSet;
                            }
                            catch
                            { continue; }
                            #endregion
                            #region xet tung thua
                            if (thuaSelectionSet.Count == 0 || thuaSelectionSet == null)
                            {
                                continue;
                            }
                            #region khoi dau
                            IEnumIDs thuaIds = thuaSelectionSet.IDs;
                            int thuaId;
                            IFeature thuaFt;
                            int iThua = 0;
                            List<object[,]> pairColValTgd = new List<object[,]>();
                            int rowTgdNnHandleUpdate = 0;
                            List<object> newId = new List<object>();
                            #endregion
                            while ((thuaId = thuaIds.Next()) != -1)
                            {
                                thuaFt = thuaFeatureClass.GetFeature(thuaId);
                                object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                                object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                                //[kodoi]
                                //============================
                                #region kiem tra maxa
                                if (maxaThua.ToString() == "" || maxaThua == null)
                                {
                                    maxaThua = "0";

                                    #region ----log
                                    evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                                    #endregion

                                    sothuaKhongTinhDuoc++;

                                    #region report progressing thua
                                    if (iThua < thuaSelectionSet.Count)
                                    {
                                        decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                        int i1 = Convert.ToInt32(i);
                                        this._bgwCalculating.ReportProgress(i1);
                                        //MessageBox.Show("log 009");
                                    }
                                    else
                                    {
                                        this._bgwCalculating.ReportProgress(99);
                                    }
                                    iThua++;
                                    #endregion

                                    continue;
                                }
                                #region danh rieng khi tinh theo xa
                                else if (maxaThua.ToString() != maxa)
                                {
                                    #region ----log
                                    evt.Reset();
                                    evt.Log = string.Format("thửa {0} không thuộc xã {1} (maxaThua={2},maxa={3})", mathua, tenxa, maxaThua, maxa);
                                    onCalculating(evt);
                                    #endregion
                                    sothuaKhongTinhDuoc++;
                                    #region report progressing thua
                                    if (iThua < thuaSelectionSet.Count)
                                    {
                                        decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                        int i1 = Convert.ToInt32(i);
                                        this._bgwCalculating.ReportProgress(i1);
                                        //MessageBox.Show("log 009");
                                    }
                                    else
                                    {
                                        this._bgwCalculating.ReportProgress(99);
                                    }
                                    iThua++;
                                    #endregion

                                    continue;
                                }
                                #endregion
                                #endregion
                                object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));

                                string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                                int hesoVitri = CalcTool.GetHeSoViTriThuaHem(dorong, hesoHem, loaidat, true);
                                if (hesoVitri == -1)
                                {
                                    continue;
                                }
                                //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                                //***********************************
                                #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                                evt.Reset();
                                evt.Log = "\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới ...";
                                onCalculating(evt);
                                qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                    _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                    _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                    _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                                ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                                IRow tgdRow = null;
                                //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                                try
                                {
                                    tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                                    if (tgdRow != null)
                                    {
                                        //MessageBox.Show("co");
                                        //kiem tra co cho phep tinh lai vi tri
                                        //neu co:xoa feater cu,them feature moi
                                        #region xet thua da co vi tri

                                        bool isOverWritePos = true;

                                        if (!isOverWritePos)
                                        {
                                            newId.Add(tgdRow.OID);
                                            //continue;
                                        }
                                        else
                                        {
                                            //[kodoi]
                                            //===================
                                            #region xoa feature cu
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                            //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                            tgdRow.Delete();
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                            #endregion
                                            //===================

                                            //[thaydoi] - them gia tri
                                            //**********************
                                            #region them feature moi
                                            if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                            {
                                                mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            }
                                            else
                                            {
                                                wspEdit.StartEditing(true);
                                            }
                                            wspEdit.StartEditOperation();
                                            object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);

                                            //them gia tri mathua,maduong,hesovitri
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                            pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                            //pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                            pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                            sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                            rowTgdNnHandleUpdate++;
                                            pairColValTgd.Clear();
                                            newId.Add(copiedId);
                                            #endregion
                                            //**********************
                                        }

                                        #endregion

                                    }
                                    else
                                    {
                                        //MessageBox.Show("ko co");
                                        //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                        //***********************************
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                        //pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        newId.Add(copiedId);
                                        #endregion
                                        //***********************************
                                    }
                                    //MessageBox.Show(newId.Count.ToString());
                                }
                                catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                                finally { Marshal.ReleaseComObject(tgdFcs); }
                                #endregion
                                //***********************************

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion
                            }
                            #endregion

                            //[kodoi]
                            //============================
                            #region luu thong tin vao bang gia dat
                            if (!sdeTblTgdEditor.IsEditing())
                            {
                                sdeTblTgdEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                sdeTblTgdEditor.StartEditOperation();
                            }

                            #region ----log
                            evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                            onCalculating(evt);
                            #endregion

                            sdeTblTgdEditor.SaveEdit();
                            sdeTblTgdEditor.StopEditOperation();
                            sdeTblTgdEditor.StopEditing(true);

                            #region ----log
                            evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                            onCalculating(evt);
                            #endregion

                            #endregion

                            #region tinh gia dat cho cac thua vua them vi tri
                            CalcLandprice calc = new CalcLandprice(this);
                            //MessageBox.Show(string.Format("final:{0}", newId.Count));
                            calc.Calculate(newId);
                            newId.Clear();
                            #endregion
                            //============================

                        }
                    }
                    #endregion
                }
                #endregion
            }
            #endregion
            //=====================================================================
            //=====================================================================
            #region tinh theo duong,doan duong
            else
            {
                #region timduong co ten dang xet
                //tinh theo doan duong
                #region tinh theo doan duong
                if (_inputParams.MA_DUONG != "-1")
                {
                    qrf.WhereClause = string.Format("{0}={1} and {2}={3}", _fcName.FC_DUONG.MA_DUONG, _inputParams.MA_DUONG, _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho đoạn đường có mã: {0}  ******", _inputParams.MA_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                }
                #endregion
                //tinh cho 1 duong
                #region tinh cho 1 duong
                else if (_inputParams.TEN_DUONG != "" && _inputParams.TEN_DUONG != "*")
                {
                    //phai sua lai thiet ke
                    //dung relationshipclass many to many gia duong va ten duong
                    #region tim trong bang ten duong
                    qrf.WhereClause = string.Format("{0}=N'{1}'", _tblName.TEN_DUONG.TEN_DUONG, _inputParams.TEN_DUONG);
                    ICursor tenduongCur = tblTenDuong.Search(qrf, false);
                    object idDuong;
                    List<object> lstIdDuong = new List<object>();
                    try
                    {
                        IRow tenduongRow = null;
                        while ((tenduongRow = tenduongCur.NextRow()) != null)
                        {
                            idDuong = tenduongRow.get_Value(tenduongRow.Fields.FindField(_tblName.TEN_DUONG.MA_DUONG));
                            lstIdDuong.Add(idDuong);
                        }
                    }
                    catch
                    { }
                    finally { Marshal.ReleaseComObject(tenduongCur); }
                    #endregion

                    #region tim trong bang duong
                    string q = "";
                    for (int i = 0; i < lstIdDuong.Count; i++)
                    {
                        if (i == lstIdDuong.Count - 1)
                        {
                            q += string.Format("{0}='{1}'", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                        else
                        {
                            q += string.Format("{0}='{1}' or ", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                    }
                    qrf.WhereClause = q;// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho cả đường: {0}  ******", _inputParams.TEN_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                    #endregion
                }
                #endregion
                //tinh cho ca duong
                else
                {
                    qrf.WhereClause = "";
                }
                //MessageBox.Show(string.Format("whereclause:{0}",duong.QueryFilter.WhereClause));
                ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);

                if (duongSelectionSet.Count == 0)
                {
                    return;
                }
                duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                duongFeatureSelection.SelectionSet = duongSelectionSet;

                #endregion

                #region vong lap xet tung duong

                //[thaydoi] - co the them bien chay
                //===============================
                #region khoi dau
                IEnumIDs eIds = duongSelectionSet.IDs;
                int duongId;
                IFeature ftDuong;
                int iDuong = 0;
                int progressingTotalCount = 1;
                evt.Reset();
                evt.ProgressingTotal = duongSelectionSet.Count;
                onCalculating(evt);
                List<object> lstMaDuong = new List<object>();
                #endregion
                //================================

                while ((duongId = eIds.Next()) != -1)
                {
                    //[kodoi]
                    //======================
                    #region log-----
                    evt.Reset();
                    evt.ProgressingTotalCount = progressingTotalCount;
                    onCalculating(evt);
                    progressingTotalCount++;
                    #endregion
                    //======================

                    //[capnhat] - lay ten duong tu bang ten duong
                    //++++++++++++++++++++++++
                    #region lay thong tin cua duong dang xet

                    ftDuong = duongFeatureClass.GetFeature(duongId);
                    string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                    object maduong = ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                    lstMaDuong.Add(maduong);
                    string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                    string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                    #endregion
                    //++++++++++++++++++++++++

                    //[kodoi]
                    //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                    //====================
                    #region chon duong dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                    duongFeatureSelection.SelectionSet = duongSelectionSet;
                    #endregion
                    //====================

                    #region chon hem gia dat cua duong
                    qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}'",
                                    _fcName.FC_GIA_DAT_HEM_PHU.LOCKED, 0, _fcName.FC_GIA_DAT_HEM_PHU.LOCKED,
                                    _fcName.FC_GIA_DAT_HEM_PHU.MA_DUONG, maduong);
                    ISelectionSet gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                    gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;

                    #endregion

                    #region xet tung hem co gia
                    IEnumIDs gdhChinhIds = gdhChinhSelectionSet.IDs;
                    //IFeatureCursor hemClipCur = hemClipFc.Search(null, false);
                    int gdhChinhId;
                    IFeature gdhChinhFt;
                    int iHem = 0;
                    //List<object[,]> pairColValTgd = new List<object[,]>();
                    //int rowTgdNnHandleUpdate = 0;
                    //List<object> newId = new List<object>();
                    #endregion
                    while ((gdhChinhId = gdhChinhIds.Next()) != -1)
                    {
                        gdhChinhFt = gdhChinhFeatureClass.GetFeature(gdhChinhId);
                        int hesoHem = 3;
                        result = int.TryParse(gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.HE_SO)).ToString(), out hesoHem);
                        if (!result)
                        {
                            hesoHem = 3;
                        }
                        object giahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.GIA_DAT));
                        #region lay thong tin hem
                        object mahem = gdhChinhFt.get_Value(gdhChinhFt.Fields.FindField(_fcName.FC_GIA_DAT_HEM_PHU.MA_HEM));
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_HEM.MA_HEM, mahem);
                        IFeatureCursor hemCur = hemFeatureClass.Search(qrf, false);
                        IFeature hemFt = null;
                        double dorong = 0;
                        try
                        {
                            if ((hemFt = hemCur.NextFeature()) != null)
                            {
                                result = double.TryParse(hemFt.get_Value(hemFt.Fields.FindField(_fcName.FC_HEM.DO_RONG)).ToString(), out dorong);
                                if (!result)
                                {
                                    dorong = 0;
                                }
                            }
                        }
                        catch { }
                        finally { Marshal.ReleaseComObject(hemCur); }
                        #endregion

                        #region chon hem dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_GIA_DAT_HEM_PHU.OID, gdhChinhId);
                        gdhChinhSelectionSet = gdhChinhFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                        gdhChinhFeatureSelection = (IFeatureSelection)gdhChinhFeatureLayer;
                        gdhChinhFeatureSelection.SelectionSet = gdhChinhSelectionSet;
                        #endregion
                        #region chon cac thua tiep giap voi hem dang xet
                        //thuaFeatureSelection.Clear();
                        //IQueryByLayer _qrBl = new QueryByLayerClass();
                        _qrBl.FromLayer = thuaFeatureLayer;
                        _qrBl.ByLayer = gdhChinhFeatureLayer;
                        _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                        _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultNew;
                        _qrBl.UseSelectedFeatures = true;
                        _qrBl.BufferDistance = 1;
                        ISelectionSet thuaSelectionSet;
                        try
                        {
                            thuaSelectionSet = _qrBl.Select();
                            thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                            thuaFeatureSelection.SelectionSet = thuaSelectionSet;
                        }
                        catch
                        { continue; }
                        #endregion
                        #region xet tung thua
                        if (thuaSelectionSet.Count == 0 || thuaSelectionSet == null)
                        {
                            continue;
                        }
                        #region khoi dau
                        IEnumIDs thuaIds = thuaSelectionSet.IDs;
                        int thuaId;
                        IFeature thuaFt;
                        int iThua = 0;
                        List<object[,]> pairColValTgd = new List<object[,]>();
                        int rowTgdNnHandleUpdate = 0;
                        List<object> newId = new List<object>();
                        #endregion
                        while ((thuaId = thuaIds.Next()) != -1)
                        {
                            thuaFt = thuaFeatureClass.GetFeature(thuaId);
                            object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                            object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                            //[kodoi]
                            //============================
                            #region kiem tra maxa
                            if (maxaThua.ToString() == "" || maxaThua == null)
                            {
                                maxaThua = "0";

                                #region ----log
                                evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                                #endregion

                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion
                            object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));

                            string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                            int hesoVitri = CalcTool.GetHeSoViTriThuaHem(dorong, hesoHem, loaidat,true);
                            if (hesoVitri == -1)
                            {
                                continue;
                            }
                            //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                            //***********************************
                            #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                            evt.Reset();
                            evt.Log = "\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới ...";
                            onCalculating(evt);
                            qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                            ICursor tgdFcs = tblThuaGiaDat.Search(qrf, false);
                            IRow tgdRow = null;
                            //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                            try
                            {
                                tgdRow = tgdFcs.NextRow();//dam bao la chi co 1 hang ket qua
                                if (tgdRow != null)
                                {
                                    //MessageBox.Show("co");
                                    //kiem tra co cho phep tinh lai vi tri
                                    //neu co:xoa feater cu,them feature moi
                                    #region xet thua da co vi tri

                                    bool isOverWritePos = true;

                                    if (!isOverWritePos)
                                    {
                                        newId.Add(tgdRow.OID);
                                        //continue;
                                    }
                                    else
                                    {
                                        //[kodoi]
                                        //===================
                                        #region xoa feature cu
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                        //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                        tgdRow.Delete();
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                        #endregion
                                        //===================

                                        //[thaydoi] - them gia tri
                                        //**********************
                                        #region them feature moi
                                        if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                        {
                                            mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        }
                                        else
                                        {
                                            wspEdit.StartEditing(true);
                                        }
                                        wspEdit.StartEditOperation();
                                        object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);

                                        //them gia tri mathua,maduong,hesovitri
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                        //pairColValTgd.Add(new object[,] { { tgdRow.Fields.FindField(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        newId.Add(copiedId);
                                        #endregion
                                        //**********************
                                    }

                                    #endregion

                                }
                                else
                                {
                                    //MessageBox.Show("ko co");
                                    //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                    //***********************************
                                    #region them feature moi
                                    if (mwspEdit.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMVersioned))
                                    {
                                        mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                    }
                                    else
                                    {
                                        wspEdit.StartEditing(true);
                                    }
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.Copy(thuaFt, tgdFeatureClass);
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);

                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_HEM), mahem } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    //pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DON_GIA), giahem } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), dientichpl } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion
                                    //***********************************
                                }
                                //MessageBox.Show(newId.Count.ToString());
                            }
                            catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaMattien, line 1448-\n{0}", e1)); }
                            finally { Marshal.ReleaseComObject(tgdFcs); }
                            #endregion
                            //***********************************

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion
                        }
                        #endregion

                        //[kodoi]
                        //============================
                        #region luu thong tin vao bang gia dat
                        if (!sdeTblTgdEditor.IsEditing())
                        {
                            sdeTblTgdEditor.StartEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            sdeTblTgdEditor.StartEditOperation();
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                        onCalculating(evt);
                        #endregion

                        sdeTblTgdEditor.SaveEdit();
                        sdeTblTgdEditor.StopEditOperation();
                        sdeTblTgdEditor.StopEditing(true);

                        #region ----log
                        evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgdDraft);
                        onCalculating(evt);
                        #endregion

                        #endregion

                        #region tinh gia dat cho cac thua vua them vi tri
                        CalcLandprice calc = new CalcLandprice(this);
                        //MessageBox.Show(string.Format("final:{0}", newId.Count));
                        calc.Calculate(newId);
                        newId.Clear();
                        #endregion
                        //============================

                    }
                }
                #endregion
            }
            #endregion

            #endregion

            //===========================================
            //===========================================
            //*******************************************

            //[kodoi]
            //==============
            #region doan ket
            sothuatinhduoc = sothuatimthay - sothuaKhongTinhDuoc;

            #region ----log
            evt.Log = string.Format("\n\n**************************************\n******* Đã tính xong các thửa phi nông nghiệp tại đô thị vị trí mặt tiền*******\n**********************************");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);
            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);
            #endregion
            //==============
        }
Пример #5
0
        protected override void calculate()
        {
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            evt.CurrentIndexCalculator = this._index;
            List<object> newId = new List<object>();
            newId.Add(_inputParams.OID_THUA_RIENG_LE);
            if (this._inputParams.TINH_THUA_RIENG_LE == 1)
            {
                evt.Log = string.Format("**********  Bắt đầu tính giá cho thửa có id= {0} ********", _inputParams.OID_THUA_RIENG_LE);
                onCalculating(evt);
                CalcLandprice calc = new CalcLandprice(this);
                calc.Calculate(newId);
            }
            #region ----log
            evt.Log = string.Format("\n\n**************************************\n******* Đã tính xong*******\n**********************************");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);

            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);

            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;

            #endregion
        }
Пример #6
0
        protected override void calculate()
        {
            //base.calculate();
            #region khoi dau
            base.calculate();
            CalculationEventArg evt = new CalculationEventArg();
            //evt.Type = EnumTypeOfLoopCalculation.InListCalculators;
            evt.CurrentIndexCalculator = this._index;
            evt.Log = string.Format("**********  Bắt đầu tính cho thửa phi nông nghiệp ở vị trí {0} tại đô thị ********", vitri);
            onCalculating(evt);
            #endregion

            //[thaydoi] - them cac khai bao can thiet
            //************************************
            #region khai bao cac bien
            //Lay connection info hien tai
            SdeConnection conn = new SdeConnection();
            ISdeConnectionInfo sdeConn = conn as ISdeConnectionInfo;
            IWorkspaceEdit wspEdit = (IWorkspaceEdit)sdeConn.Workspace;
            IMultiuserWorkspaceEdit mwspEdit=(IMultiuserWorkspaceEdit)sdeConn.Workspace;
            this._fcName = new TnFeatureClassName(sdeConn.Workspace);
            this._tblName = new TnTableName(sdeConn.Workspace);
            IFeatureWorkspace fw = (IFeatureWorkspace)sdeConn.Workspace;
            ICopyFeatures copyTool = new DataManager();

            #region thua
            string thuaName = string.Format("{0}_{1}", DataNameTemplate.Thua, this._currentConfig.NamApDung);
            _fcName.FC_THUA.NAME = thuaName;
            _fcName.FC_THUA.InitIndex();
            IFeatureClass thuaFeatureClass = fw.OpenFeatureClass(thuaName);
            IFeatureLayer thuaFeatureLayer = new FeatureLayerClass();
            thuaFeatureLayer.FeatureClass = thuaFeatureClass;
            IFeatureSelection thuaFeatureSelection;
            #endregion
            #region xa
            IFeatureClass xaFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Ranh_Xa_Poly);
            IFeatureLayer xaFeatureLayer = new FeatureLayerClass();
            xaFeatureLayer.FeatureClass = xaFeatureClass;
            IFeatureSelection xaFeatureSelection;
            #endregion
            #region duong
            IFeatureClass duongFeatureClass = fw.OpenFeatureClass(DataNameTemplate.Duong);
            IFeatureLayer duongFeatureLayer = new FeatureLayerClass();
            duongFeatureLayer.FeatureClass = duongFeatureClass;
            IFeatureSelection duongFeatureSelection;
            //_fcName.FC_DUONG.InitIndex();
            #endregion

            #region thua gia dat
            string tgd = string.Format("{0}_{1}", DataNameTemplate.Thua_Gia_Dat_Draft, this._currentConfig.NamApDung);
            _fcName.FC_THUA_GIADAT_DRAFT.NAME = tgd;
            _fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            //_fcName.FC_THUA_GIADAT_DRAFT_.NAME = tgd;
            //_fcName.FC_THUA_GIADAT_DRAFT.InitIndex();
            IFeatureClass tgdFeatureClass = null;
            try
            {
                tgdFeatureClass = fw.OpenFeatureClass(tgd);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", tgd);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            //ITable tblThuaGiaDat = (ITable)tgdFeatureClass;
            IFeatureLayer tgdFeatureLayer = new FeatureLayerClass();
            ISDETableEditor sdeTblTgdEditor = new SDETable((ITable)tgdFeatureClass, sdeConn.Workspace);
            #endregion

            #region gia dat duong
            string gdd = string.Format("{0}_{1}", DataNameTemplate.Gia_Dat_Duong, this._currentConfig.NamApDung);
            _tblName.GIA_DAT_DUONG.NAME = gdd;
            _tblName.GIA_DAT_DUONG.InitIndex();
            #endregion

            #region ten duong
            ITable tblTenDuong;
            try
            {
                tblTenDuong = fw.OpenTable(DataNameTemplate.Ten_Duong);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Ten_Duong);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region loai dat
            ITable tblLoaiDat;
            try
            {
                tblLoaiDat = fw.OpenTable(DataNameTemplate.Loai_Dat);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.Loai_Dat);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region he so vi tri
            ITable tblHesoVitri;
            try
            {
                tblHesoVitri = fw.OpenTable(DataNameTemplate.He_So_K);
            }
            catch (Exception exc)
            {
                evt.Log = string.Format("Không tìm thấy lớp dữ liệu: {0}", DataNameTemplate.He_So_K);
                onCalculating(evt);
                onFinished(evt);
                return;
            }
            #endregion

            #region khac
            IQueryFilter qrf = new QueryFilterClass();
            bool result = false;
            int sothuatimthay = 0;
            int sothuatinhduoc = 0;
            int sothuaKhongTinhDuoc = 0;
            _dataManager = new DataManager(sdeConn.Workspace, sdeConn.Environment);
            _version = SdeVersionsTool.CallMe();
            _eraseTool = new GExtractTool(sdeConn.Environment);
            string duongBuffer50mNoSde = FilterSdeLayerName.GetActualName(DataNameTemplate.Duong_Buffer_);
            duongBuffer50mNoSde += _currentConfig.DKhoangCach50mMatTien;
            string thuaClip50mNoSde=FilterSdeLayerName.GetActualName(DataNameTemplate.Thua_Erase_);
            thuaClip50mNoSde += _currentConfig.DKhoangCach50mMatTien;
            string duongBuffer50mCoSde = string.Format("{0}{1}", DataNameTemplate.Duong_Buffer_, _currentConfig.DKhoangCach50mMatTien);
            string thuaClip50mCoSde = string.Format("{0}{1}", DataNameTemplate.Thua_Erase_, _currentConfig.DKhoangCach50mMatTien);
            #endregion

            #endregion
            //************************************

            //*******************************************
            //===========================================
            //===========================================

            #region bat dau tinh
            _bufferTool = new GProximityTool(sdeConn.Environment);

            if (!_dataManager.LayerExist(duongBuffer50mNoSde))
            {

                _bufferTool.BufferInsideSde(duongFeatureClass.AliasName, duongBuffer50mNoSde, _currentConfig.DKhoangCach50mMatTien);
            }
            IFeatureClass duongBuff50FeatureClass = fw.OpenFeatureClass(duongBuffer50mNoSde);
            IFeatureLayer duongBuff50FeatureLayer = new FeatureLayerClass();
            ISelectionSet duongBuff50Sls;
            IFeatureSelection duongBuff50Fsls;
            duongBuff50FeatureLayer.FeatureClass = duongBuff50FeatureClass;

            #region test
            //string ex = string.Format("VongLap(ChonDuong('dorong=10'),VongLap(ChonThua('dientich<1.5'),test([doituonglap]),[doituonglap]))");
            ////ex=string.Format("VongLap(DuongDangChon(),VongLap()
            //Evaluation evalu = new Evaluation(ex);
            //evalu.DuongLayer = duongFeatureLayer;
            //evalu.ThuaLayer = thuaFeatureLayer;
            //evalu.EvaluateCalculating();
            //return;

            #endregion

            //[thaydoi] - cac may tinh khac chi can thay dieu kien truy van he so vi tri
            //******************************************************************
            #region lay cac quy tac tim vi tri

            #region log---
            evt.Log = string.Format("\n----Lấy các quy tắc tìm vị trí thửa từ bảng {0}, ứng với hệ số {1} ...", DataNameTemplate.He_So_K, TnHeSoK.DatOSauMatTien50mDt);
            onCalculating(evt);
            #endregion

            qrf.WhereClause = string.Format("{0}='{1}'", "hesovitri", TnHeSoK.DatOSauMatTien50mDt);
            ICursor cur = tblHesoVitri.Search(qrf, false);
            string quytac = "";
            string cachtinh = "";
            string cachtinhdongia = "";
            try
            {
                IRow row = cur.NextRow();
                if (row != null)
                {
                    quytac = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.QUY_TAC)).ToString();
                    cachtinh = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH)).ToString();
                    cachtinhdongia = row.get_Value(_tblName.HESO_VITRI.GetIndex(_tblName.HESO_VITRI.CACH_TINH_DON_GIA)).ToString();
                }
            }
            catch { }
            finally
            {
                Marshal.ReleaseComObject(cur);
            }
            #endregion

            #region tinh theo xa
            if (this._inputParams.MA_XA != "-1")
            {
                //[kodoi]
                //=======================
                #region chon xa co ma dang xet
                //neu dang tinh cho 1 xa
                if (_inputParams.MA_XA != "*")
                {
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.MA_XA, _inputParams.MA_XA);
                }
                else//neu dang tinh cho toan huyen
                {
                    qrf.WhereClause = "";
                }
                ISelectionSet xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                xaFeatureSelection.SelectionSet = xaSelectionSet;
                #endregion
                //=======================

                #region vong lap tung xa

                //[thaydoi] - co the them bien chay
                //*************************
                #region khoi dau
                IEnumIDs xaIds = xaSelectionSet.IDs;
                int xaId = 0;
                IFeature xaFt = null;
                List<object> lstMaDuong = new List<object>();
                #endregion
                //*************************

                while ((xaId = xaIds.Next()) != -1)
                {
                    //[kodoi]
                    //=======================
                    #region lay thong tin xa
                    xaFt = xaFeatureClass.GetFeature(xaId);
                    string maxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_XA)).ToString();
                    string tenxa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.TEN_XA)).ToString();
                    string loaixa = xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.MA_LOAI_XA)).ToString();
                    int loaidothi = 0;
                    result = int.TryParse(xaFt.get_Value(xaFt.Fields.FindField(_fcName.FC_RANH_XA_POLY.LOAI_DO_THI)).ToString(), out loaidothi);
                    #endregion

                    if (loaidothi == 0)
                    {
                        continue;
                    }

                    #region -----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho xã/phường: {0}  ******", tenxa);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion

                    #region tim duong trong khu vuc do thi bang qua xa
                    //chon xa dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_RANH_XA_POLY.OID, xaId);
                    xaSelectionSet = xaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    xaFeatureSelection = (IFeatureSelection)xaFeatureLayer;
                    xaFeatureSelection.SelectionSet = xaSelectionSet;
                    //Chon cac duong co do rong theo quy dinh
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";
                    ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                    duongFeatureSelection.SelectionSet = duongSelectionSet;

                    //bat dau truy van khong gian
                    _qrBl.FromLayer = duongFeatureLayer;
                    _qrBl.ByLayer = xaFeatureLayer;
                    _qrBl.LayerSelectionMethod = esriLayerSelectionMethod.esriLayerSelectIntersect;
                    _qrBl.ResultType = esriSelectionResultEnum.esriSelectionResultAnd;
                    _qrBl.UseSelectedFeatures = true;

                    duongSelectionSet = _qrBl.Select();

                    #endregion
                    //=======================

                    #region vong lap xet tung duong

                    //[thaydoi] - co the them bien chay
                    //===============================
                    #region khoi dau
                    IEnumIDs eIds = duongSelectionSet.IDs;
                    int duongId;
                    IFeature duongFt;
                    int iDuong = 0;
                    int progressingTotalCount = 1;
                    evt.Reset();
                    evt.ProgressingTotal = duongSelectionSet.Count;
                    onCalculating(evt);
                    //List<object> lstMaDuong = new List<object>();
                    #endregion
                    //================================

                    while ((duongId = eIds.Next()) != -1)
                    {
                        //[kodoi]
                        //======================
                        #region log-----
                        evt.Reset();
                        evt.ProgressingTotalCount = progressingTotalCount;
                        onCalculating(evt);
                        progressingTotalCount++;
                        #endregion
                        //======================

                        //[capnhat] - lay ten duong tu bang ten duong
                        //++++++++++++++++++++++++
                        #region lay thong tin cua duong dang xet

                        duongFt = duongFeatureClass.GetFeature(duongId);
                        string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                        object maduong = duongFt.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                        //lstMaDuong.Add(maduong);
                        string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                        string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                        #endregion
                        //++++++++++++++++++++++++

                        //[kodoi]
                        //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                        //====================
                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                        duongFeatureSelection.SelectionSet = duongSelectionSet;
                        #endregion
                        //====================

                        //tim cac thua theo dieu kien mat tien
                        #region tinh vi tri cho cac thua
                        //doc cach tim thua trong bang he so vi tri
                        //lay cach tim cho dat o
                        //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                        //MessageBox.Show(func);
                        //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                        //[kodoi]
                        //============================
                        #region tim cac thua theo vi tri quy dinh
                        thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                        thuaFeatureSelection.Clear();
                        Evaluation eval = new Evaluation(quytac);
                        eval.ThuaLayer = thuaFeatureLayer;
                        eval.DuongLayer = duongFeatureLayer;
                        eval.EvaluateQuery();
                        //thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                        ISelectionSet thuaSelectionSet = thuaFeatureSelection.SelectionSet;
                        #endregion
                        //============================

                        //MessageBox.Show(thuaSelectionSet.Count.ToString());
                        #endregion

                        #region clip cac thua vua tim duoc

                        #region delete bang thua_sau50m_clip
                        IFeatureClass thuaClipFc = null;

                        if (_dataManager.LayerExist(thuaClip50mNoSde))
                        {
                            #region log---
                            evt.Log = string.Format("\n----Kiểm tra, xóa bảng {0} ...", thuaClip50mCoSde);
                            onCalculating(evt);
                            #endregion
                            //thuaClipFc = fw.OpenFeatureClass("sde.thua_sau50m_clip");
                            //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                            //wspEdit.StartEditing(false);
                            //wspEdit.StartEditOperation();
                            ((IFeatureClassManager)_dataManager).DeleteFcInSde(thuaClip50mCoSde);
                            //((IDataset)thuaClipFc).Delete();
                            //wspEdit.StopEditOperation();
                            //wspEdit.StopEditing(true);
                        }
                        #endregion

                        #region chon duong buffer 50 dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                        duongBuff50Sls = duongBuff50FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        duongBuff50Fsls = (IFeatureSelection)duongBuff50FeatureLayer;
                        duongBuff50Fsls.SelectionSet = duongBuff50Sls;
                        #endregion

                        #region log---
                        evt.Log = string.Format("\n----Kiểm tra, clip thửa theo đường buffer {0}m, lưu vào bảng {1} ...", _currentConfig.DKhoangCach50mMatTien, thuaClip50mCoSde);
                        onCalculating(evt);
                        #endregion

                        string thualyr = string.Format("{0}/{1}", _sysTempPath.TempPath, thuaClip50mNoSde);
                        string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, duongBuffer50mNoSde);
                        _eraseTool.EraseByLayerFileInsideSde(thuaFeatureLayer, thualyr, duongBuff50FeatureLayer, duonglyr, thuaClip50mNoSde);
                        try
                        {
                            thuaClipFc = fw.OpenFeatureClass(thuaClip50mCoSde);
                        }
                        catch (Exception ex) { continue; }
                        _version.RegisterDataset((IDataset)thuaClipFc, true, true);
                        #endregion

                        //[kodoi]
                        //============================
                        #region ----log
                        if (thuaSelectionSet.Count == 0)
                        {
                            evt.Log = string.Format("\n !!! Không tìm thấy thửa nào tiếp giáp với đường {0} đoạn từ {1} đến {2} và nằm trong vùng buffer 50m", tenduong, batdau, ketthuc);
                            onCalculating(evt);

                            #region report progressing duong
                            if (iDuong < duongSelectionSet.Count)
                            {
                                decimal i = (decimal)iDuong % (decimal)duongSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iDuong++;
                            #endregion

                            continue;
                        }
                        #endregion
                        sothuatimthay += thuaSelectionSet.Count;
                        //============================

                        #region xet tung thua

                        //[thaydoi] - co the them bien chay
                        //********************
                        #region khoi dau
                        IEnumIDs thuaIds = thuaSelectionSet.IDs;
                        int thuaId;
                        IFeature thuaFt;
                        int iThua = 0;
                        List<object[,]> pairColValTgd = new List<object[,]>();
                        int rowTgdNnHandleUpdate = 0;
                        List<object> newId = new List<object>();
                        #endregion
                        //********************

                        while ((thuaId = thuaIds.Next()) != -1)
                        {
                            int hesoVitri = TnHeSoK.DatOSauMatTien50mDt;

                            #region chon duong dang xet
                            qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA.OID, thuaId);
                            thuaSelectionSet = thuaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                            thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                            thuaFeatureSelection.SelectionSet = thuaSelectionSet;
                            #endregion

                            //[thaydoi] - co them them dieu kien de xac dinh vi tri cua thua
                            //***********************************
                            #region lay thong tin thua dang xet

                            //[kodoi]
                            //============================
                            #region lay thong tin co ban
                            thuaFt = thuaFeatureClass.GetFeature(thuaId);
                            //neu thua bi khoa tim vi tri,bo qua,xet thua ke
                            string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                            if (lockTimVitri == "1")
                            {
                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;

                            }
                            //neu thua ko thuoc xa dang xet thi ko tinh tiep
                            object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                            object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                            object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));
                            #endregion
                            //============================

                            //[kodoi]
                            //============================
                            #region kiem tra maxa
                            if (maxaThua.ToString() == "" || maxaThua == null)
                            {
                                maxaThua = "0";

                                #region ----log
                                evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                                #endregion

                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #region danh rieng khi tinh theo xa
                            else if (maxaThua.ToString() != maxa)
                            {
                                #region ----log
                                evt.Reset();
                                evt.Log = string.Format("thửa {0} không thuộc xã {1}", mathua, tenxa);
                                onCalculating(evt);
                                #endregion
                                sothuaKhongTinhDuoc++;
                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion
                            #endregion

                            #region lay thong tin loai do thi
                            //neu ko la do thi thi bo qua,xet thua ke
                            qrf.WhereClause = string.Format("{0}={1}", _fcName.FC_RANH_XA_POLY.MA_XA, maxaThua);
                            IFeatureCursor fcrXa = xaFeatureClass.Search(qrf, false);

                            IFeature ftXa = fcrXa.NextFeature();
                            string loaiDoThi = "";
                            if (ftXa != null)
                            {
                                loaiDoThi = ftXa.get_Value(ftXa.Fields.FindField(_fcName.FC_RANH_XA_POLY.LOAI_DO_THI)).ToString();
                            }
                            else
                            {
                                loaiDoThi = "0";
                            }
                            if (loaiDoThi == "0")
                            {
                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion
                            //============================

                            #region lay loai dat

                            //neu ko co dat phi nong nghiep thi bo qua,xet thua ke
                            //neu co dat nong nghiep thi datnn=true
                            //neu la dat hon hop, neu co dat sxkd thi datsxkd=true,
                            //neu co dat o thi codato=true
                            //cac truong hop he so vi tri:
                            //o:3010, sxkd:4010, o+nn:5010, o+sxkd:6010
                            string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                            //evt.Log = string.Format("\n loai dat cua thua {0} la {1}", thuaId, loaidat);
                            //onCalculating(evt);

                            //[thaydoi] - may tinh dat nong nghiep co the khac
                            //***********************
                            #region kiem tra dat phi nong nghiep
                            bool datpnn = false;
                            foreach (string s in TnLoaiDats.PHI_NONG_NGHIEP_DOTHI)
                            {
                                if (loaidat.Contains(s))
                                {
                                    datpnn = true;
                                    break;
                                }
                                else
                                {
                                    datpnn = false;
                                }
                            }
                            if (!datpnn)
                            {
                                sothuaKhongTinhDuoc++;

                                #region report progressing thua
                                if (iThua < thuaSelectionSet.Count)
                                {
                                    decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                    int i1 = Convert.ToInt32(i);
                                    this._bgwCalculating.ReportProgress(i1);
                                    //MessageBox.Show("log 009");
                                }
                                else
                                {
                                    this._bgwCalculating.ReportProgress(99);
                                }
                                iThua++;
                                #endregion

                                continue;
                            }
                            #endregion
                            //***********************

                            //[thaydoi] - may tinh dat nong nghiep co the khac
                            //***********************
                            #region kiem tra dat honhop
                            bool codatsxkd = false;
                            bool dathonhop = false;
                            bool codato = false;
                            bool chicodato = false;
                            bool chicodatsxkd = false;
                            bool codatnn = false;
                            string loaidatNn = "";

                            //evt.Log = string.Format("line 983 mattien, loaidat:{0}", loaidat);
                            //onCalculating(evt);
                            if (loaidat.Contains("+"))
                            {
                                //evt.Log = string.Format("line 987 mattien, loaidat:{0}, co +", loaidat);
                                //onCalculating(evt);
                                dathonhop = true;
                                #region kiem tra co dat o
                                foreach (string dato in TnLoaiDats.PNN_O_DT)
                                {
                                    if (loaidat.Contains(dato))
                                    {
                                        //evt.Log = string.Format("line 992 mattien, loaidat:{0}, co odt", loaidat);
                                        //onCalculating(evt);

                                        codato = true;
                                        break;
                                    }

                                }
                                #endregion

                                #region kiem tra co dat nong nghiep

                                foreach (string s in TnLoaiDats.NONG_NGHIEP)
                                {
                                    if (loaidat.Contains(s))
                                    {
                                        codatnn = true;
                                        loaidatNn = s;

                                        break;
                                    }
                                    else
                                    {
                                        codatnn = false;
                                    }
                                }
                                #endregion

                                #region kiem tra chi co dat sxkd
                                foreach (string s in TnLoaiDats.PNN_SX_KD)
                                {
                                    if (loaidat == s)
                                    {
                                        chicodatsxkd = true;

                                        break;
                                    }

                                    else
                                    {
                                        chicodatsxkd = false;
                                    }
                                }
                                #endregion
                            }
                            else
                            {
                                #region kiem tra chi co dat o
                                foreach (string dato in TnLoaiDats.PNN_O_DT)
                                {
                                    if (loaidat.Contains(dato))
                                    {
                                        //evt.Log = string.Format("line 992 mattien, loaidat:{0}, co odt", loaidat);
                                        //onCalculating(evt);
                                        chicodato = true;
                                        break;
                                    }
                                    else
                                    {
                                        chicodato = false;
                                    }
                                }
                                #endregion

                                #region kiem tra chi co dat sxkd
                                foreach (string s in TnLoaiDats.PNN_SX_KD)
                                {
                                    if (loaidat == s)
                                    {
                                        chicodatsxkd = true;

                                        break;
                                    }

                                    else
                                    {
                                        chicodatsxkd = false;
                                    }
                                }
                                #endregion

                            }

                            #endregion
                            //***********************

                            #endregion

                            #endregion
                            //***********************************

                            //[thaydoi] - thay doi he so vi tri phu hop
                            //***********************************
                            #region quyet dinh he so vi tri cho thua
                            if (chicodato)
                            {
                                hesoVitri = TnHeSoK.DatOSauMatTien50mDt;
                            }
                            else if (chicodatsxkd)
                            {
                                hesoVitri = TnHeSoK.DatSxkdSauMatTien50mDt;
                            }
                            else if (dathonhop && codato && codatnn)
                            {
                                hesoVitri = TnHeSoK.DatONnSauMatTien50m;
                            }
                            //else if (dathonhop && codato && codatsxkd)
                            //{
                            //    hesoVitri = TnHeSoK.DatOSxkdSauMatTien50m;
                            //}
                            #endregion
                            //***********************************

                            //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                            //***********************************
                            #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                            evt.Reset();
                            evt.Log = "\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới ...";
                            onCalculating(evt);
                            qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                                _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                                _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                                _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                            IFeatureCursor tgdFcs = tgdFeatureClass.Search(qrf, false);
                            IFeature tgdRow = null;
                            //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                            try
                            {
                                tgdRow = tgdFcs.NextFeature();//dam bao la chi co 1 hang ket qua
                                Marshal.ReleaseComObject(tgdFcs);
                                if (tgdRow != null)
                                {
                                    //MessageBox.Show("co");
                                    //kiem tra co cho phep tinh lai vi tri
                                    //neu co:xoa feater cu,them feature moi
                                    #region xet thua da co vi tri

                                    bool isOverWritePos = true;

                                    if (!isOverWritePos)
                                    {
                                        newId.Add(tgdRow.OID);
                                        //continue;
                                    }
                                    else
                                    {
                                        //[kodoi]
                                        //===================
                                        #region xoa feature cu
                                        wspEdit.StartEditing(true);
                                        wspEdit.StartEditOperation();
                                        //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                        //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                        tgdRow.Delete();
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                        #endregion
                                        //===================

                                        #region chon trong bang thua clip
                                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA.MA_THUA, mathua);
                                        IFeatureCursor thuaClipFcur = thuaClipFc.Search(qrf, false);
                                        IFeature thuaClipFt = null;
                                        try
                                        {
                                            thuaClipFt = thuaClipFcur.NextFeature();
                                        }
                                        catch (Exception ex) { return; }
                                        finally { Marshal.ReleaseComObject(thuaClipFcur); }

                                        #region lay thong tin thua clip
                                        IArea area = (IArea)thuaClipFt.Shape;
                                        #endregion

                                        #endregion

                                        #region them feature moi

                                        object copiedId = null;
                                        try
                                        {
                                            //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                            wspEdit.StartEditing(true);
                                            wspEdit.StartEditOperation();
                                            copiedId = copyTool.Copy(thuaClipFt, tgdFeatureClass);
                                            wspEdit.StopEditOperation();
                                            wspEdit.StopEditing(true);
                                        }
                                        catch (Exception ex)
                                        {
                                            wspEdit.AbortEditOperation();
                                            wspEdit.StopEditing(false);
                                        }

                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                        pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), Math.Round(area.Area, 2) } });
                                        sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                        //MessageBox.Show(string.Format("line 1604 CalcPosThuaSau50m copiedId={0}", copiedId));
                                        rowTgdNnHandleUpdate++;
                                        pairColValTgd.Clear();
                                        newId.Add(copiedId);
                                        #endregion
                                    }

                                    #endregion

                                }
                                else
                                {
                                    #region chon trong bang thua clip
                                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA.MA_THUA, mathua);
                                    IFeatureCursor thuaClipFcur = thuaClipFc.Search(qrf, false);
                                    IFeature thuaClipFt = null;
                                    try
                                    {
                                        thuaClipFt = thuaClipFcur.NextFeature();
                                    }
                                    catch (Exception ex) { return; }
                                    finally { Marshal.ReleaseComObject(thuaClipFcur); }

                                    #region lay thong tin thua clip
                                    IArea area = (IArea)thuaClipFt.Shape;
                                    #endregion

                                    #endregion

                                    #region them feature moi

                                    object copiedId = null;
                                    try
                                    {
                                        //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        wspEdit.StartEditing(true);
                                        wspEdit.StartEditOperation();
                                        copiedId = copyTool.Copy(thuaClipFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                    }
                                    catch (Exception ex)
                                    {
                                        wspEdit.AbortEditOperation();
                                        wspEdit.StopEditing(true);
                                    }

                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), Math.Round(area.Area, 2) } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    //MessageBox.Show(string.Format("line 1604 CalcPosThuaSau50m copiedId={0}", copiedId));
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion
                                    //MessageBox.Show("ko co");
                                    //***********************************
                                }
                                //MessageBox.Show(newId.Count.ToString());
                            }
                            catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaSau50m, line 1448-\n{0}", e1)); }
                            finally { Marshal.ReleaseComObject(tgdFcs); }
                            #endregion
                            //***********************************

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion
                        }
                        #endregion

                        //[kodoi]
                        //============================
                        #region luu thong tin vao bang gia dat
                        if (!sdeTblTgdEditor.IsEditing())
                        {
                            sdeTblTgdEditor.StartEditing(true);
                            sdeTblTgdEditor.StartEditOperation();
                        }

                        #region ----log
                        evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                        onCalculating(evt);
                        #endregion

                        sdeTblTgdEditor.SaveEdit();
                        sdeTblTgdEditor.StopEditOperation();
                        sdeTblTgdEditor.StopEditing(true);

                        #region ----log
                        evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                        onCalculating(evt);
                        #endregion

                        #endregion

                        #region tinh gia dat cho cac thua vua them vi tri
                        CalcLandprice calc = new CalcLandprice(this);
                        //MessageBox.Show(string.Format("final:{0}", newId.Count));
                        calc.Maduong = maduong;
                        calc.Calculate(newId);
                        newId.Clear();
                        #endregion
                        //============================

                    }
                    #endregion

                }
                #endregion
            }
            #endregion
            //=====================================================================
            //=====================================================================
            #region tinh theo duong,doan duong
            else
            {
                #region timduong co ten dang xet
                //tinh theo doan duong
                #region tinh theo doan duong
                if (_inputParams.MA_DUONG != "-1")
                {
                    qrf.WhereClause = string.Format("{0}={1} and {2}={3}", _fcName.FC_DUONG.MA_DUONG, _inputParams.MA_DUONG, _fcName.FC_DUONG.PHAN_LOAI, _isDothi);// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho đoạn đường có mã: {0}  ******", _inputParams.MA_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                }
                #endregion
                //tinh cho 1 duong
                #region tinh cho 1 duong
                else if (_inputParams.TEN_DUONG != "" && _inputParams.TEN_DUONG != "*")
                {
                    //phai sua lai thiet ke
                    //dung relationshipclass many to many gia duong va ten duong
                    #region tim trong bang ten duong
                    qrf.WhereClause = string.Format("{0}=N'{1}'", _tblName.TEN_DUONG.TEN_DUONG, _inputParams.TEN_DUONG);
                    ICursor tenduongCur = tblTenDuong.Search(qrf, false);
                    object idDuong;
                    List<object> lstIdDuong = new List<object>();
                    try
                    {
                        IRow tenduongRow = null;
                        while ((tenduongRow = tenduongCur.NextRow()) != null)
                        {
                            idDuong = tenduongRow.get_Value(tenduongRow.Fields.FindField(_tblName.TEN_DUONG.MA_DUONG));
                            lstIdDuong.Add(idDuong);
                        }
                    }
                    catch
                    { }
                    finally { Marshal.ReleaseComObject(tenduongCur); }
                    #endregion

                    #region tim trong bang duong
                    string q = "";
                    for (int i = 0; i < lstIdDuong.Count; i++)
                    {
                        if (i == lstIdDuong.Count - 1)
                        {
                            q += string.Format("{0}='{1}'", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                        else
                        {
                            q += string.Format("{0}='{1}' or ", _fcName.FC_DUONG.MA_DUONG, lstIdDuong[i]);
                        }
                    }
                    qrf.WhereClause = q;// "maduong='2'";

                    #region ----log
                    evt.Log = string.Format("\n************************************************");
                    evt.Log += string.Format("\n******   Đang tính cho cả đường: {0}  ******", _inputParams.TEN_DUONG);
                    evt.Log += string.Format("\n************************************************");
                    onCalculating(evt);
                    #endregion
                    #endregion
                }
                #endregion
                //tinh cho ca duong
                else
                {
                    qrf.WhereClause = "";
                }
                //MessageBox.Show(string.Format("whereclause:{0}",duong.QueryFilter.WhereClause));
                ISelectionSet duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionNormal, sdeConn.Workspace);

                if (duongSelectionSet.Count == 0)
                {
                    return;
                }
                duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;
                duongFeatureSelection.SelectionSet = duongSelectionSet;

                #endregion

                #region vong lap xet tung duong

                //[thaydoi] - co the them bien chay
                //===============================
                #region khoi dau
                IEnumIDs eIds = duongSelectionSet.IDs;
                int duongId;
                IFeature duongFt;
                int iDuong = 0;
                int progressingTotalCount = 1;
                evt.Reset();
                evt.ProgressingTotal = duongSelectionSet.Count;
                onCalculating(evt);
                //List<object> lstMaDuong = new List<object>();
                #endregion
                //================================

                while ((duongId = eIds.Next()) != -1)
                {
                    //[kodoi]
                    //======================
                    #region log-----
                    evt.Reset();
                    evt.ProgressingTotalCount = progressingTotalCount;
                    onCalculating(evt);
                    progressingTotalCount++;
                    #endregion
                    //======================

                    //[capnhat] - lay ten duong tu bang ten duong
                    //++++++++++++++++++++++++
                    #region lay thong tin cua duong dang xet

                    duongFt = duongFeatureClass.GetFeature(duongId);
                    string tenduong = "";//ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.TEN_DUONG)).ToString();
                    object maduong = duongFt.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.MA_DUONG));
                    //lstMaDuong.Add(maduong);
                    string batdau = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.BAT_DAU)).ToString();
                    string ketthuc = "";// ftDuong.get_Value(_fcName.FC_DUONG.GetIndex(_fcName.FC_DUONG.KET_THUC)).ToString();

                    #endregion
                    //++++++++++++++++++++++++

                    //[kodoi]
                    //Chon duong dang xet, buoc này de ra layer duong dung de truy van khong gian cac thua dat
                    //====================
                    #region chon duong dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongSelectionSet = duongFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongFeatureSelection = (IFeatureSelection)duongFeatureLayer;

                    duongFeatureSelection.SelectionSet = duongSelectionSet;
                    #endregion
                    //====================

                    //tim cac thua theo dieu kien mat tien
                    #region tinh vi tri cho cac thua
                    //doc cach tim thua trong bang he so vi tri
                    //lay cach tim cho dat o
                    //truyen thong so cach tinh,thualayer,duonglayer,khoangcach

                    //MessageBox.Show(func);
                    //func = "ChongLop([INTERSECT],[NEW_SELECTION],1) Then ChongLop([CONTAINED_BY],[AND_SELECTION],50)";

                    //[kodoi]
                    //============================
                    #region tim cac thua theo vi tri quy dinh
                    thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                    thuaFeatureSelection.Clear();
                    Evaluation eval = new Evaluation(quytac);
                    eval.ThuaLayer = thuaFeatureLayer;
                    eval.DuongLayer = duongFeatureLayer;
                    eval.EvaluateQuery();
                    //thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                    ISelectionSet thuaSelectionSet = thuaFeatureSelection.SelectionSet;
                    #endregion
                    //============================

                    //MessageBox.Show(thuaSelectionSet.Count.ToString());
                    #endregion

                    #region clip cac thua vua tim duoc

                    #region delete bang thua_sau50m_clip
                    IFeatureClass thuaClipFc=null;
                    #region log---
                    evt.Log = string.Format("\n----Kiểm tra, xóa bảng {0} ...", thuaClip50mCoSde);
                    onCalculating(evt);
                    #endregion

                    if (_dataManager.LayerExist(thuaClip50mNoSde))
                    {
                        //thuaClipFc = fw.OpenFeatureClass("sde.thua_sau50m_clip");
                        //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                        //wspEdit.StartEditing(false);
                        //wspEdit.StartEditOperation();
                        ((IFeatureClassManager)_dataManager).DeleteFcInSde(thuaClip50mCoSde);
                        //((IDataset)thuaClipFc).Delete();
                        //wspEdit.StopEditOperation();
                        //wspEdit.StopEditing(true);
                    }
                    #endregion

                    #region chon duong buffer 50 dang xet
                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                    duongBuff50Sls = duongBuff50FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                    duongBuff50Fsls = (IFeatureSelection)duongBuff50FeatureLayer;
                    duongBuff50Fsls.SelectionSet = duongBuff50Sls;
                    #endregion

                    #region log---
                    evt.Log = string.Format("\n----Kiểm tra, clip thửa theo đường buffer {0}m, lưu vào bảng {1} ...", _currentConfig.DKhoangCach50mMatTien, thuaClip50mCoSde);
                    onCalculating(evt);
                    #endregion

                    string thualyr = string.Format("{0}/{1}", _sysTempPath.TempPath, thuaClip50mNoSde);
                    string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, duongBuffer50mNoSde);
                    _eraseTool.EraseByLayerFileInsideSde(thuaFeatureLayer, thualyr, duongBuff50FeatureLayer, duonglyr, thuaClip50mNoSde);
                    try
                    {
                        thuaClipFc = fw.OpenFeatureClass(thuaClip50mCoSde);
                    }
                    catch (Exception ex) { continue; }
                    _version.RegisterDataset((IDataset)thuaClipFc, true, true);
                    #endregion

                    //[kodoi]
                    //============================
                    #region ----log
                    if (thuaSelectionSet.Count == 0)
                    {
                        evt.Log = string.Format("\n !!! Không tìm thấy thửa nào tiếp giáp với đường {0} đoạn từ {1} đến {2} và nằm trong vùng buffer 50m", tenduong, batdau, ketthuc);
                        onCalculating(evt);

                        #region report progressing duong
                        if (iDuong < duongSelectionSet.Count)
                        {
                            decimal i = (decimal)iDuong % (decimal)duongSelectionSet.Count * 100;
                            int i1 = Convert.ToInt32(i);
                            this._bgwCalculating.ReportProgress(i1);
                            //MessageBox.Show("log 009");
                        }
                        else
                        {
                            this._bgwCalculating.ReportProgress(99);
                        }
                        iDuong++;
                        #endregion

                        continue;
                    }
                    #endregion
                    sothuatimthay += thuaSelectionSet.Count;
                    //============================

                    #region xet tung thua

                    //[thaydoi] - co the them bien chay
                    //********************
                    #region khoi dau
                    IEnumIDs thuaIds = thuaSelectionSet.IDs;
                    int thuaId;
                    IFeature thuaFt;
                    int iThua = 0;
                    List<object[,]> pairColValTgd = new List<object[,]>();
                    int rowTgdNnHandleUpdate = 0;
                    List<object> newId = new List<object>();
                    #endregion
                    //********************

                    while ((thuaId = thuaIds.Next()) != -1)
                    {
                        int hesoVitri = TnHeSoK.DatOSauMatTien50mDt;

                        #region chon duong dang xet
                        qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA.OID, thuaId);
                        thuaSelectionSet = thuaFeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                        thuaFeatureSelection = (IFeatureSelection)thuaFeatureLayer;
                        thuaFeatureSelection.SelectionSet = thuaSelectionSet;
                        #endregion

                        //[thaydoi] - co them them dieu kien de xac dinh vi tri cua thua
                        //***********************************
                        #region lay thong tin thua dang xet

                        //[kodoi]
                        //============================
                        #region lay thong tin co ban
                        thuaFt = thuaFeatureClass.GetFeature(thuaId);
                        //neu thua bi khoa tim vi tri,bo qua,xet thua ke
                        string lockTimVitri = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOCKED)).ToString();
                        if (lockTimVitri == "1")
                        {
                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;

                        }
                        //neu thua ko thuoc xa dang xet thi ko tinh tiep
                        object mathua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_THUA));
                        object maxaThua = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.MA_XA));
                        object dientichpl = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.DIEN_TICH));
                        #endregion
                        //============================

                        //[kodoi]
                        //============================
                        #region kiem tra maxa
                        if (maxaThua.ToString() == "" || maxaThua == null)
                        {
                            maxaThua = "0";

                            #region ----log
                            evt.Log = string.Format("Chưa xác định mã xã cho thửa {0}", mathua);
                            #endregion

                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;
                        }
                        #endregion

                        #region lay thong tin loai do thi
                        //neu ko la do thi thi bo qua,xet thua ke
                        qrf.WhereClause = string.Format("{0}={1}", _fcName.FC_RANH_XA_POLY.MA_XA, maxaThua);
                        IFeatureCursor fcrXa = xaFeatureClass.Search(qrf, false);

                        IFeature ftXa = fcrXa.NextFeature();
                        string loaiDoThi = "";
                        if (ftXa != null)
                        {
                            loaiDoThi = ftXa.get_Value(ftXa.Fields.FindField(_fcName.FC_RANH_XA_POLY.LOAI_DO_THI)).ToString();
                        }
                        else
                        {
                            loaiDoThi = "0";
                        }
                        if (loaiDoThi == "0")
                        {
                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;
                        }
                        #endregion
                        //============================

                        #region lay loai dat

                        //neu ko co dat phi nong nghiep thi bo qua,xet thua ke
                        //neu co dat nong nghiep thi datnn=true
                        //neu la dat hon hop, neu co dat sxkd thi datsxkd=true,
                        //neu co dat o thi codato=true
                        //cac truong hop he so vi tri:
                        //o:3010, sxkd:4010, o+nn:5010, o+sxkd:6010
                        string loaidat = thuaFt.get_Value(thuaFt.Fields.FindField(_fcName.FC_THUA.LOAI_DAT)).ToString();
                        //evt.Log = string.Format("\n loai dat cua thua {0} la {1}", thuaId, loaidat);
                        //onCalculating(evt);

                        //[thaydoi] - may tinh dat nong nghiep co the khac
                        //***********************
                        #region kiem tra dat phi nong nghiep
                        bool datpnn = false;
                        foreach (string s in TnLoaiDats.PHI_NONG_NGHIEP_DOTHI)
                        {
                            if (loaidat.Contains(s))
                            {
                                datpnn = true;
                                break;
                            }
                            else
                            {
                                datpnn = false;
                            }
                        }
                        if (!datpnn)
                        {
                            sothuaKhongTinhDuoc++;

                            #region report progressing thua
                            if (iThua < thuaSelectionSet.Count)
                            {
                                decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                                int i1 = Convert.ToInt32(i);
                                this._bgwCalculating.ReportProgress(i1);
                                //MessageBox.Show("log 009");
                            }
                            else
                            {
                                this._bgwCalculating.ReportProgress(99);
                            }
                            iThua++;
                            #endregion

                            continue;
                        }
                        #endregion
                        //***********************

                        //[thaydoi] - may tinh dat nong nghiep co the khac
                        //***********************
                        #region kiem tra dat honhop
                        bool codatsxkd = false;
                        bool dathonhop = false;
                        bool codato = false;
                        bool chicodato = false;
                        bool chicodatsxkd = false;
                        bool codatnn = false;
                        string loaidatNn = "";

                        //evt.Log = string.Format("line 983 mattien, loaidat:{0}", loaidat);
                        //onCalculating(evt);
                        if (loaidat.Contains("+"))
                        {
                            //evt.Log = string.Format("line 987 mattien, loaidat:{0}, co +", loaidat);
                            //onCalculating(evt);
                            dathonhop = true;
                            #region kiem tra co dat o
                            foreach (string dato in TnLoaiDats.PNN_O_DT)
                            {
                                if (loaidat.Contains(dato))
                                {
                                    //evt.Log = string.Format("line 992 mattien, loaidat:{0}, co odt", loaidat);
                                    //onCalculating(evt);

                                    codato = true;
                                    break;
                                }
                            }
                            #endregion

                            #region kiem tra co dat nong nghiep

                            foreach (string s in TnLoaiDats.NONG_NGHIEP)
                            {
                                if (loaidat.Contains(s))
                                {
                                    codatnn = true;
                                    loaidatNn = s;

                                    break;
                                }
                                else
                                {
                                    codatnn = false;
                                }
                            }
                            #endregion

                            #region kiem tra chi co dat sxkd
                            foreach (string s in TnLoaiDats.PNN_SX_KD)
                            {
                                if (loaidat == s)
                                {
                                    chicodatsxkd = true;

                                    break;
                                }

                                else
                                {
                                    chicodatsxkd = false;
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region kiem tra chi co dat o
                            foreach (string dato in TnLoaiDats.PNN_O_DT)
                            {
                                if (loaidat.Contains(dato))
                                {
                                    //evt.Log = string.Format("line 992 mattien, loaidat:{0}, co odt", loaidat);
                                    //onCalculating(evt);
                                    chicodato = true;
                                    break;
                                }
                                else
                                {
                                    chicodato = false;
                                }
                            }
                            #endregion

                            #region kiem tra chi co dat sxkd
                            foreach (string s in TnLoaiDats.PNN_SX_KD)
                            {
                                if (loaidat == s)
                                {
                                    chicodatsxkd = true;

                                    break;
                                }

                                else
                                {
                                    chicodatsxkd = false;
                                }
                            }
                            #endregion

                        }

                        #endregion
                        //***********************

                        #endregion

                        #endregion
                        //***********************************

                        //[thaydoi] - thay doi he so vi tri phu hop
                        //***********************************
                        #region quyet dinh he so vi tri cho thua
                        if (chicodato)
                        {
                            hesoVitri = TnHeSoK.DatOSauMatTien50mDt;
                        }
                        else if (chicodatsxkd)
                        {
                            hesoVitri = TnHeSoK.DatSxkdSauMatTien50mDt;
                        }
                        else if (dathonhop && codato && codatnn)
                        {
                            hesoVitri = TnHeSoK.DatONnSauMatTien50m;
                        }
                        //else if (dathonhop && codato && codatsxkd)
                        //{
                        //    hesoVitri = TnHeSoK.DatOSxkdSauMatTien50m;
                        //}
                        #endregion
                        //***********************************

                        //[thaydoi] - thay doi dieu kien truy van ung voi tung may tinh khac nhau
                        //***********************************
                        #region kiem tra trong bang thua_giadat, voi dieu kien:mathua,maduong,hesovitri,khoagia=0
                        evt.Reset();
                        evt.Log = "\n[!]--- Kiểm tra các vị trí, cập nhật vị trí mới ...";
                        onCalculating(evt);
                        qrf.WhereClause = string.Format("({0}='{1}' or {2} is null) and {3}='{4}' and {5}='{6}' and {7}='{8}'",
                            _fcName.FC_THUA_GIADAT_DRAFT.LOCKED, 0, _fcName.FC_THUA_GIADAT_DRAFT.LOCKED,
                            _fcName.FC_THUA_GIADAT_DRAFT.MA_THUA, mathua,
                            _fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG, maduong,
                            _fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K, hesoVitri);
                        IFeatureCursor tgdFcs = tgdFeatureClass.Search(qrf, false);
                        IFeature tgdRow = null;
                        //MessageBox.Show(string.Format("line 1401 CalcPosThuaMattien - bat dau try, query:\n{0}", qrf.WhereClause));
                        try
                        {
                            tgdRow = tgdFcs.NextFeature();//dam bao la chi co 1 hang ket qua
                            Marshal.ReleaseComObject(tgdFcs);
                            if (tgdRow != null)
                            {
                                //MessageBox.Show("co");
                                //kiem tra co cho phep tinh lai vi tri
                                //neu co:xoa feater cu,them feature moi
                                #region xet thua da co vi tri

                                bool isOverWritePos = true;

                                if (!isOverWritePos)
                                {
                                    newId.Add(tgdRow.OID);
                                    //continue;
                                }
                                else
                                {
                                    //[kodoi]
                                    //===================
                                    #region xoa feature cu
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    //qrf.WhereClause = string.Format("{0}='{1}'", "OBJECTID", tgdRow.OID);
                                    //tblThuaGiaDat.DeleteSearchedRows(qrf);
                                    tgdRow.Delete();
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);
                                    #endregion
                                    //===================

                                    #region chon trong bang thua clip
                                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA.MA_THUA, mathua);
                                    IFeatureCursor thuaClipFcur = thuaClipFc.Search(qrf, false);
                                    IFeature thuaClipFt = null;
                                    try
                                    {
                                        thuaClipFt = thuaClipFcur.NextFeature();
                                    }
                                    catch (Exception ex) { return; }
                                    finally { Marshal.ReleaseComObject(thuaClipFcur); }

                                    #region lay thong tin thua clip
                                    IArea area = (IArea)thuaClipFt.Shape;
                                    #endregion

                                    #endregion

                                    #region them feature moi

                                    object copiedId = null;
                                    try
                                    {
                                        //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                        wspEdit.StartEditing(true);
                                        wspEdit.StartEditOperation();
                                        copiedId = copyTool.Copy(thuaClipFt, tgdFeatureClass);
                                        wspEdit.StopEditOperation();
                                        wspEdit.StopEditing(true);
                                    }
                                    catch (Exception ex)
                                    {
                                        wspEdit.AbortEditOperation();
                                        wspEdit.StopEditing(false);
                                    }

                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), Math.Round(area.Area,2) } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    //MessageBox.Show(string.Format("line 1604 CalcPosThuaSau50m copiedId={0}", copiedId));
                                    //evt.Log = string.Format("\n----Lưu thông tin cho vùng giá {0}---- ", copiedId);
                                    //onCalculating(evt);
                                    //evt.Log = string.Format("\n----Cột mathua:{0},cột maduong:{1}, cot hesovitri:{2}, cot dientichpl:{3}---- ",
                                    //    _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA),
                                    //    _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG),
                                    //    _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K),
                                    //    _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY));
                                    //onCalculating(evt);
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                    #endregion

                                    //[thaydoi] - them gia tri
                                    //**********************
                                    #region clip thua dang xet
                                    /*
                                    #region chon duong buffer 50 dang xet
                                    qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                                    duongBuff50Sls = duongBuff50FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                                    duongBuff50Fsls = (IFeatureSelection)duongBuff50FeatureLayer;
                                    duongBuff50Fsls.SelectionSet = duongBuff50Sls;
                                    #endregion
                                    //_clipTool = new TnClip(sdeConn.Environment);
                                    //string thualyr = string.Format("{0}/{1}", _sysTempPath.TempPath, "thua_sau50m_clip.lyr");
                                    //string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, "duong_sau50m_clip.lyr");
                                    //_clipTool.ClipByLayerFileInsideSde(thuaFeatureLayer, thualyr, duongBuff50FeatureLayer, duonglyr, "thua_sau50m_clip");
                                    //_dataManager.SaveToLayerFile((ILayer)thuaFeatureLayer, thualyr);
                                    //_dataManager.SaveToLayerFile((ILayer)duongBuff50FeatureLayer, duonglyr);
                                    //_clipTool.Clip(thualyr, duonglyr, "thua_sau50m_clip");
                                    //MessageBox.Show(string.Format("line 1459 CalPosThuaSau50m {0}", _sysTempPath.TempFullPathNoEnd));
                                    IFeatureClass fcThuaCliped = _dataManager.TnOpenFeatureClassFromFileMdb(_sysTempPath.TempFullPathNoEnd, "thua_sau50m_clip");
                                    string clipPath = string.Format("{0}{1}", _sysTempPath.TempFullPath, "thua_sau50m_clip");
                                    IFeatureLayer flThuaCliped = new FeatureLayerClass();
                                    flThuaCliped.FeatureClass = fcThuaCliped;
                                    IFeatureCursor fcur = fcThuaCliped.Search(null, false);
                                    IFeature clipFt = null;
                                    try
                                    {
                                        clipFt = fcur.NextFeature();
                                    }
                                    catch { continue; }
                                    finally { Marshal.ReleaseComObject(fcur); }
                                    if (clipFt == null)
                                    {
                                        continue;
                                    }
                                    #endregion

                                    #region lay thong tin thua clip
                                    IArea area = (IArea)clipFt.Shape;
                                    #endregion

                                    #region them feature moi
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    object copiedId = copyTool.CopyFromMdb(clipFt, tgdFeatureClass);
                                    //copyTool.CopyUseGeoprocessing(clipPath, tgdFeatureClass.AliasName);
                                    //IFeatureCursor fcurClip = tgdFeatureClass.Search(null, false);
                                    //object copiedId = null;
                                    //IFeature ftClip = null;
                                    //try
                                    //{
                                    //    while ((ftClip = fcurClip.NextFeature()) != null)
                                    //    {
                                    //        copiedId = ftClip.get_Value(0);
                                    //    }
                                    //}
                                    //catch {  }
                                    //finally { Marshal.ReleaseComObject(fcurClip); }
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);
                                    //them gia tri mathua,maduong,hesovitri
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                    pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), area.Area } });
                                    sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                    MessageBox.Show(string.Format("line 1533 CalcPosThuaSau50m copiedId={0}", copiedId));
                                    rowTgdNnHandleUpdate++;
                                    pairColValTgd.Clear();
                                    newId.Add(copiedId);
                                     * */
                                    #endregion
                                    //**********************
                                }

                                #endregion

                            }
                            else
                            {
                                #region chon trong bang thua clip
                                qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_THUA.MA_THUA, mathua);
                                IFeatureCursor thuaClipFcur = thuaClipFc.Search(qrf, false);
                                IFeature thuaClipFt = null;
                                try
                                {
                                    thuaClipFt = thuaClipFcur.NextFeature();
                                }
                                catch (Exception ex) { return; }
                                finally { Marshal.ReleaseComObject(thuaClipFcur); }

                                #region lay thong tin thua clip
                                IArea area = (IArea)thuaClipFt.Shape;
                                #endregion

                                #endregion

                                #region them feature moi

                                object copiedId = null;
                                try
                                {
                                    //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                    wspEdit.StartEditing(true);
                                    wspEdit.StartEditOperation();
                                    copiedId = copyTool.Copy(thuaClipFt, tgdFeatureClass);
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);
                                }
                                catch (Exception ex)
                                {
                                    wspEdit.AbortEditOperation();
                                    wspEdit.StopEditing(true);
                                }

                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), Math.Round(area.Area, 2) } });
                                sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                //MessageBox.Show(string.Format("line 1604 CalcPosThuaSau50m copiedId={0}", copiedId));
                                rowTgdNnHandleUpdate++;
                                pairColValTgd.Clear();
                                newId.Add(copiedId);
                                #endregion
                                //MessageBox.Show("ko co");
                                #region clip thua dang xet
                                /*
                                #region chon duong buffer 50 dang xet
                                qrf.WhereClause = string.Format("{0}='{1}'", _fcName.FC_DUONG.OID, duongId);
                                duongBuff50Sls = duongBuff50FeatureClass.Select(qrf, esriSelectionType.esriSelectionTypeHybrid, esriSelectionOption.esriSelectionOptionOnlyOne, sdeConn.Workspace);
                                duongBuff50Fsls = (IFeatureSelection)duongBuff50FeatureLayer;
                                duongBuff50Fsls.SelectionSet = duongBuff50Sls;
                                #endregion

                                //_clipTool = new TnClip(sdeConn.Environment);
                                //string thualyr = string.Format("{0}/{1}", _sysTempPath.TempPath, "thua_sau50m_clip.lyr");
                                //string duonglyr = string.Format("{0}/{1}", _sysTempPath.TempPath, "duong_sau50m_clip.lyr");
                                //_dataManager.SaveToLayerFile((ILayer)thuaFeatureLayer, thualyr);
                                //_dataManager.SaveToLayerFile((ILayer)duongBuff50FeatureLayer, duonglyr);

                                //((IFeatureClassManager)_dataManager).DeleteFcInSde("sde.thua_sau50m_clip");
                                //mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                //wspEdit.StartEditOperation();
                                IFeatureClass fcThuaCliped;
                                //try
                                //{
                                //    fcThuaCliped = fw.OpenFeatureClass("thua_sau50m_clip");
                                //    IDataset ft = (IDataset)fcThuaCliped;
                                //    ft.Delete();

                                //    wspEdit.StopEditOperation();
                                //    wspEdit.StopEditing(true);
                                //}
                                //catch (Exception ex)
                                //{
                                //    MessageBox.Show(string.Format("line 1577 CalcPosThuaSau50m {0}", ex));
                                //    wspEdit.AbortEditOperation();
                                //    wspEdit.StopEditing(false);
                                //}

                                _clipTool.ClipInsideSde(thualyr, duonglyr, "thua_sau50m_clip");

                                //MessageBox.Show(string.Format("line 1517 CalPosThuaSau50m {0}", _sysTempPath.TempFullPathNoEnd));
                                fcThuaCliped = fw.OpenFeatureClass("sde.thua_sau50m_clip");//_dataManager.TnOpenFeatureClassFromFileMdb(_sysTempPath.TempFullPathNoEnd, "thua_sau50m_clip");
                                _version.RegisterDataset((IDataset)fcThuaCliped, true, true);

                                string clipPath = string.Format("{0}{1}", _sysTempPath.TempFullPath, "thua_sau50m_clip");
                                IFeatureLayer flThuaCliped = new FeatureLayerClass();
                                flThuaCliped.FeatureClass = fcThuaCliped;
                                IFeatureCursor fcur = fcThuaCliped.Search(null, false);
                                IFeature clipFt = null;
                                try
                                {
                                    clipFt = fcur.NextFeature();
                                }
                                catch { continue; }
                                finally { Marshal.ReleaseComObject(fcur); }
                                if (clipFt == null)
                                {
                                    continue;
                                }
                                #endregion

                                #region lay thong tin thua clip
                                IArea area = (IArea)clipFt.Shape;
                                #endregion
                                //[thaydoi] - them cac gia tri thich hop vao thua_giadat
                                //***********************************
                                #region them feature moi

                                object copiedId = null;
                                try
                                {
                                    //MessageBox.Show()
                                    mwspEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned);
                                    wspEdit.StartEditOperation();
                                    copiedId = copyTool.Copy(clipFt, tgdFeatureClass);
                                    //copyTool.CopyUseGeoprocessing(clipPath, tgdFeatureClass.AliasName,sdeConn.Environment);
                                    //IAppend app = (IAppend)copyTool;
                                    //app.Append(fcThuaCliped, thuaFeatureClass);
                                    #region
                                    //IGeometry g = new PolygonClass();

                                    //MessageBox.Show(string.Format("line 1597 CalcPosThuaSau50m {0}", g.GeometryType.ToString()));
                                    //IFeature feature = tgdFeatureClass.CreateFeature();
                                    //feature.Shape = g;

                                    //// Apply the appropriate subtype to the feature.
                                    //ISubtypes subtypes = (ISubtypes)tgdFeatureClass;
                                    //IRowSubtypes rowSubtypes = (IRowSubtypes)feature;
                                    //if (subtypes.HasSubtype)
                                    //{
                                    //    // In this example, the value of 3 represents the PVC subtype.
                                    //    rowSubtypes.SubtypeCode = 3;
                                    //}

                                    //// Initialize any default values the feature has.
                                    //rowSubtypes.InitDefaultValues();

                                    //// Update the value on a string field that indicates who installed the feature.
                                    ////int contractorFieldIndex = tgdFeatureClass.FindField("CONTRACTOR");
                                    ////feature.set_Value(contractorFieldIndex, "K Johnston");

                                    //// Commit the new feature to the geodatabase.
                                    //feature.Store();
                                    wspEdit.StopEditOperation();
                                    wspEdit.StopEditing(true);
                                    #endregion
                                }
                                catch (Exception e1)
                                {
                                    wspEdit.AbortEditOperation();
                                    wspEdit.StopEditing(false);
                                    MessageBox.Show(string.Format("line 1593 CalcPosThuaSau50m {0} \n{1}", copiedId, e1));
                                }
                                MessageBox.Show(string.Format("line 1593 CalcPosThuaSau50m {0}",copiedId));
                                //copyTool.CopyUseGeoprocessing(clipPath, tgdFeatureClass.AliasName);
                                //IFeatureCursor fcurClip = tgdFeatureClass.Search(null, false);
                                //object copiedId=null;
                                //IFeature ftClip=null;
                                //try
                                //{
                                //    while ((ftClip = fcurClip.NextFeature()) != null)
                                //    {
                                //        copiedId = ftClip.get_Value(0);
                                //    }
                                //}
                                //catch { }
                                //finally { Marshal.ReleaseComObject(fcurClip); }

                                //them gia tri mathua,maduong,hesovitri
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_THUA), mathua } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.MA_DUONG), maduong } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.HE_SO_K), hesoVitri } });
                                pairColValTgd.Add(new object[,] { { _fcName.FC_THUA_GIADAT_DRAFT.GetIndex(_fcName.FC_THUA_GIADAT_DRAFT.DIEN_TICH_PHAP_LY), area.Area } });
                                sdeTblTgdEditor.CacheData(copiedId, rowTgdNnHandleUpdate, pairColValTgd, EnumTypeOfEdit.UPDATE);
                                MessageBox.Show(string.Format("line 1604 CalcPosThuaSau50m copiedId={0}", copiedId));
                                rowTgdNnHandleUpdate++;
                                pairColValTgd.Clear();
                                newId.Add(copiedId);
                                 * */
                                #endregion
                                //***********************************
                            }
                            //MessageBox.Show(newId.Count.ToString());
                        }
                        catch (Exception e1) { MessageBox.Show(string.Format("CalcPosThuaSau50m, line 1448-\n{0}", e1)); }
                        finally { Marshal.ReleaseComObject(tgdFcs); }
                        #endregion
                        //***********************************

                        #region report progressing thua
                        if (iThua < thuaSelectionSet.Count)
                        {
                            decimal i = (decimal)iThua / (decimal)thuaSelectionSet.Count * 100;
                            int i1 = Convert.ToInt32(i);
                            this._bgwCalculating.ReportProgress(i1);
                            //MessageBox.Show("log 009");
                        }
                        else
                        {
                            this._bgwCalculating.ReportProgress(99);
                        }
                        iThua++;
                        #endregion
                    }
                    #endregion

                    //[kodoi]
                    //============================
                    #region luu thong tin vao bang gia dat
                    if (!sdeTblTgdEditor.IsEditing())
                    {
                        sdeTblTgdEditor.StartEditing(true);
                        sdeTblTgdEditor.StartEditOperation();
                    }

                    #region ----log
                    evt.Log = string.Format("\n----||| Đang lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                    onCalculating(evt);
                    #endregion

                    sdeTblTgdEditor.SaveEdit();
                    sdeTblTgdEditor.StopEditOperation();
                    sdeTblTgdEditor.StopEditing(true);

                    #region ----log
                    evt.Log = string.Format("\n----||| Đã lưu vị trí các thửa vào bảng {0} |||---- ", tgd);
                    onCalculating(evt);
                    #endregion

                    #endregion

                    #region tinh gia dat cho cac thua vua them vi tri
                    CalcLandprice calc = new CalcLandprice(this);
                    //MessageBox.Show(string.Format("final:{0}", newId.Count));
                    calc.Maduong = maduong;
                    calc.Calculate(newId);
                    newId.Clear();
                    #endregion
                    //============================

                }
                #endregion
            }
            #endregion

            #endregion

            //===========================================
            //===========================================
            //*******************************************

            //[kodoi]
            //==============
            #region doan ket
            sothuatinhduoc = sothuatimthay - sothuaKhongTinhDuoc;

            #region ----log
            evt.Log = string.Format("\n\n=====================================\n******* Đã tính xong*******\n=====================================");
            //evt.Log += string.Format("\n Số thửa tìm thấy: {0}", sothuatimthay);
            //evt.Log += string.Format("\n Số thửa được tính: {0}", sothuatinhduoc);
            onCalculating(evt);
            #endregion
            evt.Reset();
            evt.ProgressingTotalCount = ".";
            evt.ProgressingTotal = ".";
            onCalculating(evt);
            evt.CurrentIndexCalculator = this._index;
            onFinished(evt);
            #endregion
            //==============
        }