Пример #1
0
        public ActionResult EditFactoryAuthority(ViewModel_AuFa auth)
        {
            //宣告變數
            string workNumber          = Session["employee_acc"].ToString();
            string employee_workNumber = Request["employee_workNumber"];
            string auth_workNumber     = Request["authority_workNumber"];
            string factoryid           = Request["factoryId"];
            //action為0表示新增,為1表示停用
            int action = Convert.ToInt32(Request["AddorRemove"].ToString());
            //引用Emp類別
            Emp emp = new Emp();

            try
            {
                //action==0時,表示新增權限
                if (action == 0)
                {
                    //用員工編號跟廠別ID查詢
                    var query = (from o in db.Authority
                                 where o.authority_workNumber == workNumber && o.authority_factoryId == factoryid
                                 select o).ToList();
                    //當查無資料時query陣列沒有元素
                    if (query.Count() == 0)
                    {
                        //創新的Authority資料行
                        Authority authfac = new Authority()
                        {
                            authority_workNumber = auth_workNumber,
                            authority_factoryId  = factoryid,
                            authority_IsDisable  = "N",
                            authority_role       = emp.GetRole(employee_workNumber),
                            authority_updateTime = DateTime.Now,
                            authority_updateuser = emp.Name(employee_workNumber)
                        };
                        //把資料行加入Authority資料表,並儲存
                        db.Authority.Add(authfac);
                        db.SaveChanges();
                    }
                    //當查詢員工編號跟廠別編號有值時
                    else if (query.Count() != 0)
                    {
                        //把資料行內的每個元素做變更
                        foreach (var item in query)
                        {
                            item.authority_Id         = item.authority_Id;
                            item.authority_factoryId  = item.authority_factoryId;
                            item.authority_workNumber = item.authority_workNumber;
                            item.authority_IsDisable  = "N";
                            item.authority_updateuser = emp.Name(employee_workNumber);
                            item.authority_updateTime = DateTime.Now;
                        }
                        //寫回資料表
                        db.SaveChanges();
                    }
                }
                //當action為1時,表示停用該權限
                if (action == 1)
                {
                    //查詢員工編號與廠別代碼的資料
                    var query = (from o in db.Authority
                                 where o.authority_workNumber == workNumber && o.authority_factoryId == factoryid
                                 select o);
                    var qtoList = query.ToList();
                    //取出每個資料數值並更新
                    foreach (var item in query)
                    {
                        item.authority_Id         = item.authority_Id;
                        item.authority_factoryId  = item.authority_factoryId;
                        item.authority_workNumber = item.authority_workNumber;
                        item.authority_IsDisable  = "Y";
                        item.authority_updateuser = emp.Name(employee_workNumber);
                        item.authority_updateTime = DateTime.Now;
                    }
                    //寫回資料表
                    db.SaveChanges();
                }
            }
            catch (Exception ex) //寫回錯誤輸出SQL錯誤碼
            {
                return(Content(ex.ToString()));
            }
            //查詢員工編號與權限沒停用的資料
            var q1 = from o in db.Authority
                     where o.authority_workNumber == workNumber && o.authority_IsDisable == "N"
                     select o;
            //查詢廠別資料
            var q2 = from o in db.Factory
                     select o;
            //引用ViewModel_AuFa類別
            ViewModel_AuFa data = new ViewModel_AuFa();

            //把查詢到的廠別資料給ViewModel_AuFa 下 Factoroy
            data.Factory = q2.ToList();
            //把查詢到的權限資料給ViewModel_AuFa 下 AuthView
            data.AuthView = q1.ToList();
            return(View(data));
        }