工作中心与产线维护Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace Midea.Web.Areas.MES.Controllers
{
    public class SfcWorkcenterInLineController : BaseController
    {
        //
        // GET: /MES/SfcWorkcenterInLine/
        [Authorize]
        [Permission("23D7BCF06A6002C0E0530A100C355400", "工作中心与产线维护", "工作中心与产线维护")]
        public ActionResult Index()
        {


            //班次
            List<SelectListItem> search_orgcode = new List<SelectListItem>();
            search_orgcode.Add(new SelectListItem() { Text = "请选择产线编码", Value = "" });
            int totalrow = 0;
            INV_OBJECT currenorg = this.CurrentInvObject;
            int invid = currenorg.ORG_ID;
            Expression<Func<WCILORGLINE, bool>> predicate = m => m.INV_ORG_ID == invid;
            predicate = predicate.And(m => m.STATE == "A");
         
            var orgcode = MESDomain.MES.SfcWorkcenterInLine.Getwcilline(predicate, 1, 9999, out totalrow).ToList();
            foreach (var item in orgcode)
            {
                var selectitem = new SelectListItem();
                selectitem.Text = item.ORG_CODE;
                selectitem.Value = item.ORG_CODE;
                search_orgcode.Add(selectitem);
            }
            ViewData.Add("search_orgcode", search_orgcode);



            return View();
        }

        public ActionResult LoadData(string search_centercode, string search_orgcode)
        {
            //获取库存组织ID
            INV_OBJECT currenorg = this.CurrentInvObject;
            decimal invorgid = Convert.ToDecimal(currenorg.ORG_ID);
            //起始数
            int start = Convert.ToInt32(Request.Params["start"]);
            //长度
            int rowsize = Convert.ToInt32(Request.Params["length"]);
            //画的页数
            int draw = Convert.ToInt32(Request.Params["draw"]);

            int pageindex = (start / rowsize) + 1;
            Expression<Func<WorkCenterViewModel, string>> orderbyEx = m => m.ORG_NAME; //m.WORKCENTER_CODE;
          //  Expression<Func<WorkCenterViewModel, decimal?>> orderbyEx = m => m.SEQ;

            Expression<Func<WorkCenterViewModel, bool>> where = m => m.STATE.Equals(StateConverter.Active) && m.INV_ORG_ID == invorgid;
            int totalRecords = 0;
            if (!string.IsNullOrEmpty(search_centercode))
            {
                where = where.And(m => m.WORKCENTER_CODE.Contains(search_centercode) || m.WORKCENTER_NAME.Contains(search_centercode));
            }
            if (!string.IsNullOrEmpty(search_orgcode))
            {
                where = where.And(m => m.ORG_CODE.Contains(search_orgcode));
            }


            var resulte = MESDomain.MES.SfcWorkcenterInLine.Getworkcenterinline(
               where,
               pageindex, rowsize, orderbyEx, true, out totalRecords).ToList().OrderBy(x => x.ORG_CODE).ThenBy(x => x.SEQ);//OrderBy(x=>x.SEQ);
            return Json(new DataTablesResultInfo(resulte, draw, totalRecords, totalRecords), JsonRequestBehavior.AllowGet);
        }


            [Authorize]
            [Permission("0EAB21B0AD474B0583A2C6B5DE8CA401", "工作中心与产线维护--新增", "工作中心与产线维护--新增")]
        public ActionResult Add()
        {
            return View();
        }

            [Authorize]
            [Permission("0EAB21B0AD474B0583A2C6B5DE8CA402", "工作中心与产线维护--编辑", "工作中心与产线维护--编辑")]
        public ActionResult Edit(string id)
        {
            //根据id查找信息
            SFC_WORKCENTER_IN_LINE model = new SFC_WORKCENTER_IN_LINE();
            //
            //根据ID查询,获取数据并赋值
            Expression<Func<SFC_WORKCENTER_IN_LINE, bool>> predicate = m => m.ID == id;
            model = MESDomain.MES.SfcWorkcenterInLine.GetWorkcenterInLine(predicate).FirstOrDefault();
            ViewBag.ID = model.ID;

            int totalrow = 0;
            Expression<Func<VM_SYS_ORG_LINE, bool>> linepredicate = m => true;
            linepredicate = linepredicate.And(m => m.ID == model.LINE_ID);
            var lineModel = MESDomain.MES.CommonOrg.FindOrgLine(linepredicate, 1, 1, out totalrow).FirstOrDefault();
            ViewBag.LINE_ID = model.LINE_ID;
            ViewBag.LINE_NAME = lineModel.ORG_NAME;

            Expression<Func<SFC_WORKCENTER, bool>> where = m => m.STATE == StateConverter.Active && m.ID == model.WORKCENTER_ID;
            var workcenterModel = MESDomain.MES.SelectWorkcenter.GetWorkcenterList(where, 1, 1, out totalrow).FirstOrDefault();
            ViewBag.WORKCENTER_ID = model.WORKCENTER_ID;
            ViewBag.WORKCENTER_NAME = workcenterModel.WORKCENTER_NAME;
            ViewBag.SEQ = model.SEQ.ToString();
            return View();
        }


        
        //虚拟产线选择
        public ActionResult WCILSelectLine(string Controlname, string Controlid, string workshopid = "", string Diolag = "", string IsMuilt = "N", string FuncName = "")
        {
            bool isOK = IsMuilt == "Y" ? true : false;
            ViewBag.control = Controlname;
            ViewBag.controlid = Controlid;
            ViewBag.Diolag = Diolag;
            ViewBag.Workshopid = workshopid;
            ViewBag.IsMuilt = isOK;
            ViewBag.FuncName = FuncName;
            string guid = Guid.NewGuid().ToString("N");
            ViewBag.GuID = guid;
            return View();
        
        }

        /// <summary>
        /// 获取产线数据
        /// </summary>
        /// <param name="LineName"></param>
        /// <returns></returns>
        public ActionResult LoadLineData(string LineName, string workshopid)
        {
            //起始数
            int start = Convert.ToInt32(Request.Params["start"]);

            //长度
            int rowsize = Convert.ToInt32(Request.Params["length"]);

            int draw = Convert.ToInt32(Request.Params["draw"]);//画的页数

            int pageindex = (start / rowsize) + 1;

            string orderby = "";
            Expression<Func<WCILORGLINE, string>> orderbyEx = m => m.ORG_CODE;

            if (Convert.ToInt32(Request.QueryString["order[0][column]"]) == 1)//判断点击的排序列
            {
                orderby = Request.QueryString["order[0][dir]"];//获取升序降序标识

            }


            Expression<Func<WCILORGLINE, bool>> predicate = m => true;

            predicate = predicate.And(m => m.INV_ORG_ID == CurUserDefaultInvOrgID);
            predicate = predicate.And(m => m.STATE == "A");
            if (!string.IsNullOrWhiteSpace(LineName))
            {
                predicate = predicate.And(m => m.ORG_CODE.Contains(LineName) || m.ORG_NAME.Contains(LineName));
            }

            //if (!string.IsNullOrWhiteSpace(workshopid))
            //{
            //    predicate = predicate.And(m => workshopid.Contains(m.PARENT_ORG_ID));
            //}

            int totalrow = 0;
            ICollection<WCILORGLINE> Vmorg = null;

            Vmorg = MESDomain.MES.SfcWorkcenterInLine.Getwcilline(predicate, pageindex, rowsize, out totalrow);

            //var json = from a in Vmorg select new { a.ID, a.ORG_DESC, a.ORG_NAME };

            return Json(new DataTablesResultInfo(Vmorg, draw, totalrow, totalrow), JsonRequestBehavior.AllowGet);
        }

        #region 保存新增
        /// <summary>
        /// 保存新增
        /// </summary>
        /// <param name="postJson"></param>
        /// <returns></returns>
        public ActionResult SaveAdd(string postJson)
        {

            List<string> errMsg = new List<string>();
            Dictionary<string, string> ctlMsg = new Dictionary<string, string>();
            try
            {
                if (!string.IsNullOrWhiteSpace(postJson))
                {
                    SFC_WORKCENTER_IN_LINE model = Newtonsoft.Json.JsonConvert.DeserializeObject<SFC_WORKCENTER_IN_LINE>(postJson);
                    //校验数据
                    if (string.IsNullOrWhiteSpace(model.WORKCENTER_ID))
                    {
                        ctlMsg.Add("WORKCENTER_ID", "工作中心不能为空");
                    }
                    if (string.IsNullOrWhiteSpace(model.LINE_ID))
                    {
                        ctlMsg.Add("LINE_ID", "产线不能为空");
                    }
                    if(string.IsNullOrWhiteSpace(model.SEQ.ToString()))
                    {
                        ctlMsg.Add("SEQ", "顺序不能为空");
                    }
                    if (errMsg.Count == 0 && ctlMsg.Count() == 0)
                    {
                        #region 校验是否存在相同数据
                        Expression<Func<SFC_WORKCENTER_IN_LINE, bool>> predicate = m => m.INV_ORG_ID == CurUserDefaultInvOrgID && m.STATE == "A"
                            && m.WORKCENTER_ID == model.WORKCENTER_ID && m.LINE_ID == model.LINE_ID;


                        SFC_WORKCENTER_IN_LINE tmpModel = null;

                        tmpModel = MESDomain.MES.SfcWorkcenterInLine.GetWorkcenterInLine(predicate).FirstOrDefault();
                        if (tmpModel != null)
                        {
                            errMsg.Add("数据已经存在!不允许重复添加");
                            return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, errMsg));
                        }
                        #endregion

                        model.ID = Convert.ToString(Guid.NewGuid());
                        model.INV_ORG_ID = CurUserDefaultInvOrgID;
                        model.STATE = "A";
                        model.DATETIME_CREATED = DateTime.Now;
                        model.USER_CREATED = CurrentUser.USER_NAME;

                        SFC_WORKCENTER_IN_LINE.Default(ref model, CurrentUser.USER_NAME);
                        if (MESDomain.MES.SfcWorkcenterInLine.AddWorkcenterInLine(model))
                        {
                            return Json(new JsonResultInfo(JsonResultInfo.FLAG_SUCCESS, null, "保存成功"));
                        }
                        else
                        {
                            return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, "保存失败,未知原因!"));
                        }
                    }
                }
                else
                {
                    errMsg.Add("没有提交可用数据!");
                }
            }
            catch (Exception ex)
            {
                errMsg.Add(ex.Message);
            }
            return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, errMsg, ctlMsg));
        }

        #endregion
        #region 保存编辑修改
        /// <summary>
        /// 保存编辑修改
        /// </summary>
        /// <param name="postJson"></param>
        /// <returns></returns>
        public ActionResult SaveEdit(string postJson)
        {
            List<string> errMsg = new List<string>();
            Dictionary<string, string> ctlMsg = new Dictionary<string, string>();
            try
            {
                if (!string.IsNullOrWhiteSpace(postJson))
                {
                    SFC_WORKCENTER_IN_LINE model = Newtonsoft.Json.JsonConvert.DeserializeObject<SFC_WORKCENTER_IN_LINE>(postJson);

                    if(string.IsNullOrWhiteSpace(model.SEQ.ToString()))
                    {
                        ctlMsg.Add("SEQ","工作中心顺序不能为空");
                    }
                    //校验数据
                    if (string.IsNullOrWhiteSpace(model.WORKCENTER_ID))
                    {
                        ctlMsg.Add("WORKCENTER_ID", "工作中心不能为空");
                    }
                    if (string.IsNullOrWhiteSpace(model.LINE_ID))
                    {
                        ctlMsg.Add("LINE_ID", "产线不能为空");
                    }
                    if (errMsg.Count == 0 && ctlMsg.Count() == 0)
                    {
                        #region 校验是否存在相同数据
                        Expression<Func<SFC_WORKCENTER_IN_LINE, bool>> pred = m => m.INV_ORG_ID == CurUserDefaultInvOrgID && m.STATE == "A"
                             && m.WORKCENTER_ID == model.WORKCENTER_ID && m.LINE_ID == model.LINE_ID && m.ID != model.ID;

                        SFC_WORKCENTER_IN_LINE tmp = null;

                        tmp = MESDomain.MES.SfcWorkcenterInLine.GetWorkcenterInLine(pred).FirstOrDefault();
                        if (tmp != null)
                        {
                            errMsg.Add("数据已经存在!不允许重复添加");
                            return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, errMsg));
                        }
                        #endregion

                        //根据ID查询,获取数据并赋值
                        Expression<Func<SFC_WORKCENTER_IN_LINE, bool>> predicate = m => m.ID == model.ID;
                        SFC_WORKCENTER_IN_LINE tmpModel = null;

                        tmpModel = MESDomain.MES.SfcWorkcenterInLine.GetWorkcenterInLine(predicate).FirstOrDefault();
                        if (tmpModel == null)
                        {
                            errMsg.Add("修改数据不存在!");
                            return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, errMsg));
                        }

                        model.DATETIME_CREATED = tmpModel.DATETIME_CREATED;
                        model.DATETIME_MODIFIED = DateTime.Now;
                        model.STATE = tmpModel.STATE;
                        model.INV_ORG_ID = tmpModel.INV_ORG_ID;
                        model.USER_CREATED = tmpModel.USER_CREATED;
                        model.USER_MODIFIED = CurrentUser.USER_NAME;

                        if (MESDomain.MES.SfcWorkcenterInLine.UpdateWorkcenterInLine(model))
                        {
                            return Json(new JsonResultInfo(JsonResultInfo.FLAG_SUCCESS, null, "保存成功"));
                        }
                        else
                        {
                            return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, "保存失败,未知原因!"));
                        }
                    }
                }
                else
                {
                    errMsg.Add("没有提交可用数据!");
                }
            }
            catch (Exception ex)
            {
                errMsg.Add(ex.Message);
                return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, "数据提交失败!"));
            }
            return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, errMsg, ctlMsg));
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult Delete(string ids)
        {
            List<string> errMsg = new List<string>();
            try
            {
                string[] arrId = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                MESDomain.MES.SfcWorkcenterInLine.Delete(arrId);
            }
            catch (Exception ex)
            {
                errMsg.Add(ex.Message);
            }
            if (errMsg.Count == 0)
            {
                return Json(new JsonResultInfo(JsonResultInfo.FLAG_SUCCESS, null, "删除成功"));
            }
            else
            {
                return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null, errMsg));
            }
        }
        #endregion 

    }
}


你可能感兴趣的:(public,中心)