Example #1
0
        private void biInput_Click(object sender, EventArgs e)
        {
            //WinInfoHelper.ShowInfoWindow(this, "建设中,敬请期待!");
            CtrlWaiting waiting = new CtrlWaiting(() =>
            {
                try
                {
                    bool iscancel;
                    List <Maticsoft.Model.SMT_ORG_INFO> orgs = new List <Maticsoft.Model.SMT_ORG_INFO>();
                    ImportHelper2.ImportEx(out iscancel, 2, 1, 4, new ImportDataHandle((o, ise, row, error) =>
                    {
                        if (ise)
                        {
                            WinInfoHelper.ShowInfoWindow(this, "导入中发生行值错误,行号:" + row + " 错误:" + error);
                            return;
                        }
                        Maticsoft.Model.SMT_ORG_INFO org = new Maticsoft.Model.SMT_ORG_INFO();
                        org.ORG_CODE = o[0];
                        int ord      = 100;
                        int.TryParse(o[3], out ord);
                        org.ORDER_VALUE  = ord;
                        org.ORG_NAME     = o[2];
                        org.PAR_ORG_CODE = o[1];
                        org.PAR_ID       = -1;
                        orgs.Add(org);
                    }));
                    if (!iscancel)
                    {
                        if (orgs.Count == 0)
                        {
                            WinInfoHelper.ShowInfoWindow(this, "导入部门个数为0!");
                            return;
                        }
                        foreach (var item in orgs)
                        {
                            if (string.IsNullOrWhiteSpace(item.ORG_CODE) || string.IsNullOrWhiteSpace(item.ORG_NAME))
                            {
                                WinInfoHelper.ShowInfoWindow(this, "导入失败,存在部门编码或者名称为空!编码:" + item.ORG_CODE + ",名称:" + item.ORG_NAME + ",行号:" + orgs.IndexOf(item));
                                return;
                            }
                        }

                        foreach (var item in orgs)
                        {
                            var dept = DeptDataHelper.GetDeptByCode(item.ORG_CODE);
                            if (dept == null)
                            {
                                item.ID = DeptDataHelper.AddDept(item);//添加
                            }
                            else
                            {
                                item.ID     = dept.ID;
                                item.PAR_ID = dept.PAR_ID;
                                DeptDataHelper.UpdateDept(item);//更新
                            }
                        }
                        foreach (var item in orgs)
                        {
                            if (string.IsNullOrWhiteSpace(item.PAR_ORG_CODE))
                            {
                                continue;
                            }
                            var f = orgs.Find(m => m.ORG_CODE == item.PAR_ORG_CODE);
                            if (f == null)
                            {
                                f = DeptDataHelper.GetDeptByCode(item.PAR_ORG_CODE);
                            }
                            if (f == null)
                            {
                                if (item.PAR_ID == -1)
                                {
                                    continue;
                                }
                                item.PAR_ID = -1;
                            }
                            else
                            {
                                if (item.PAR_ID == f.ID)
                                {
                                    continue;
                                }
                                item.PAR_ID = f.ID;
                            }
                            DeptDataHelper.UpdateDept(item);
                        }
                        WinInfoHelper.ShowInfoWindow(this, "导入结束!");
                        this.Invoke(new Action(() =>
                        {
                            biRefreshDept_Click(sender, e);
                        }));
                    }
                }
                catch (Exception ex)
                {
                    log.Error("导入异常:", ex);
                    WinInfoHelper.ShowInfoWindow(this, "导入异常:" + ex.Message);
                }
            });

            waiting.Show(this);
        }