using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MES.Production.Service.IService;
using Ant.Service.Common;
using Ant.Service.Common.Enums;
using Central.Control.Domain;
using ChangFa.Machinery.WebPage.Controllers;
namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
{
///
/// 系统管理控制器
/// add 作者: 季健国 QQ:181589805 by 2016-09-09
///
public class SystemController : BaseController
{
#region 声明容器
///
/// 系统管理
///
ISystemManage SystemManage { get; set; }
///
/// 模块管理
///
IModuleManage ModuleManage { get; set; }
#endregion
///
/// 加载主页
///
[UserAuthorizeAttribute(ModuleAlias="SystemSet",OperaAction="View")]
public ActionResult Index()
{
try
{
#region 处理查询参数
//状态
string status = Request.QueryString["status"];
#endregion
#region 加载列表
var result = BindList(status);
ViewData["status"] = status;
#endregion
return View(result);
}
catch (Exception e)
{
WriteLog(enumOperator.Select, "加载系统列表:", e);
throw e.InnerException;
}
}
///
/// 列表
///
private object BindList(string status)
{
var query = this.SystemManage.LoadAll(null);
if (!string.IsNullOrEmpty(status))
{
var islogin = int.Parse(status);
query = query.Where(p => p.IS_LOGIN == islogin);
}
query = query.OrderBy(p => p.ID);
return this.SystemManage.Query(query, base.page, base.pagesize);
}
///
/// 加载详情
///
[UserAuthorizeAttribute(ModuleAlias = "SystemSet", OperaAction = "Detail")]
public ActionResult Detail(string id)
{
var entity = this.SystemManage.Get(p => p.ID == id) ?? new SYS_SYSTEM();
return View(entity);
}
///
/// 保存系统
///
[ValidateInput(false)]
[UserAuthorizeAttribute(ModuleAlias = "SystemSet", OperaAction = "Add,Edit")]
public ActionResult Save(SYS_SYSTEM entity)
{
bool isEdit = false;
JsonHelper json = new JsonHelper() { Msg = "保存系统成功", Status = "n" };
try
{
if (entity != null)
{
var _entity = new SYS_SYSTEM();
if (string.IsNullOrEmpty(entity.ID))
{
_entity = entity;
_entity.ID = Guid.NewGuid().ToString();
_entity.CREATEDATE = DateTime.Now;
if (!string.IsNullOrEmpty(_entity.DOCKPASS))
{
//密码加密
_entity.DOCKPASS = Ant.Service.Common.CryptHelper.DESCrypt.Encrypt(_entity.DOCKPASS);
}
}
else
{
_entity = this.SystemManage.Get(p => p.ID == entity.ID);
entity.CREATEDATE = _entity.CREATEDATE;
if (string.IsNullOrEmpty(entity.DOCKPASS))
{
entity.DOCKPASS = _entity.DOCKPASS;
}
else
{
//密码加密
_entity.DOCKPASS = Ant.Service.Common.CryptHelper.DESCrypt.Encrypt(entity.DOCKPASS);
}
_entity = entity;
isEdit = true;
}
//系统是否存在
if (!this.SystemManage.IsExist(p => p.ID != _entity.ID && p.NAME == _entity.NAME && p.SITEURL == _entity.SITEURL))
{
if (this.SystemManage.SaveOrUpdate(_entity, isEdit))
{
json.Status = "y";
}
else
{
json.Msg = "保存系统失败";
}
}
else
{
json.Msg = _entity.NAME + "系统已存在,不能重复添加";
}
}
else
{
json.Msg = "未找到需要保存的系统";
}
if (isEdit)
{
WriteLog(enumOperator.Edit, "修改系统,结果:" + json.Msg, enumLog4net.INFO);
}
else
{
WriteLog(enumOperator.Add, "添加系统,结果:" + json.Msg, enumLog4net.INFO);
}
}
catch (Exception e)
{
json.Msg = "保存系统发生内部错误!";
WriteLog(enumOperator.None, "保存系统:", e);
}
return Json(json);
}
///
/// 删除系统
///
[UserAuthorizeAttribute(ModuleAlias = "SystemSet", OperaAction = "Remove")]
public ActionResult Delete(string idlist)
{
var json = new JsonHelper() { Msg = "删除成功", Status = "n" };
try
{
idlist = idlist.TrimEnd(',');
if (!string.IsNullOrEmpty(idlist))
{
//验证系统是否为主系统
if (idlist.ToLower().Contains(siteId.ToLower()))
{
json.Msg = "不能删除主系统";
}
else
{
//验证是否是正常使用的系统
if (this.SystemManage.IsExist(p => idlist.Contains(p.ID) && p.IS_LOGIN == 1))
{
json.Msg = "要删除的系统正在使用中,不能删除";
}
else
{
//验证系统是否配置了模块
if (this.ModuleManage.IsExist(p => idlist.Contains(p.FK_BELONGSYSTEM)))
{
json.Msg = "要删除的系统存在使用中的模块,不能删除";
}
else
{
//删除
this.SystemManage.Delete(p => idlist.Contains(p.ID));
json.Status = "y";
}
}
}
}
else
{
json.Msg = "未找到要删除的系统记录";
}
WriteLog(enumOperator.Remove, "删除系统:" + json.Msg, enumLog4net.WARN);
}
catch(Exception e)
{
json.Msg = "删除系统发生内部错误!";
WriteLog(enumOperator.Remove, "删除系统:", e);
}
return Json(json);
}
}
}