using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ant.Service.Common;
using MES.Production.Service.IService;
using Ant.Service.Common.Enums;
using Central.Control.Domain;
using ChangFa.Machinery.WebPage.Controllers;
using Ant.ORM;
using Ant.Data;
using JsonHelper = Ant.Service.Common.JsonHelper;
namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
{
///
/// 数据字典管理
/// add 作者: 季健国 QQ:181589805 by 2016-05-22
///
public class CodeController : BaseController
{
#region 声明容器
///
/// 编码管理
///
ICodeManage CodeManage { get; set; }
#endregion
#region 基本视图
///
/// 加载主页
///
[UserAuthorizeAttribute(ModuleAlias = "Warehouse", OperaAction = "View")]
public ActionResult Index()
{
try
{
#region 处理查询参数
string codetype = Request.QueryString["codetype"];
#endregion
#region 加载列表
var result = BindList(codetype);
ViewBag.Search = base.keywords;
ViewData["codeType"] = ClsDic.DicCodeType;
ViewData["codet"] = codetype;
#endregion
return View(result);
}
catch (Exception e)
{
WriteLog(enumOperator.Select, "数据字典管理加载主页:", e);
throw e.InnerException;
}
}
///
/// 加载列表
///
private PageInfo BindList(string codetype)
{
//var predicate = PredicateBuilder.True();
////关键词
//if (!string.IsNullOrEmpty(keywords))
//{
// predicate = predicate.And(p => p.NAMETEXT.Contains(keywords));
//}
////编码类型
//if (!string.IsNullOrEmpty(codetype))
//{
// predicate = predicate.And(p => p.CODETYPE == codetype);
//}
//var query = this.CodeManage.LoadAll(predicate).OrderByDescending(p => p.CREATEDATE).
// OrderBy(p => p.SHOWORDER).OrderBy(p => p.CODETYPE);//.OrderByDescending(p => p.UPDATEDATE)
//return this.CodeManage.Query(query, base.page, base.pagesize);
#region 加载列表
PageInfo pageInfo = new PageInfo(base.page, base.pagesize, 0, JsonConverter.JsonClass(new List()));
using (AntORM orm = new AntORM())
{
orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
RequestModel res = new RequestModel();
res.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取学校列表" };
var syscodeList = orm.Queryable();
int pageno = (base.page - 1) * base.pagesize;
//if (!nameOrProject.IsEmpty())
//{
// syscodeList = syscodeList.Where(p => (!string.IsNullOrEmpty(p.Name) && p.Name.Contains(nameOrProject)));
//}
//编码类型
if (!string.IsNullOrEmpty(codetype))
{
syscodeList = syscodeList.Where(p => p.CODETYPE == codetype);
}
if(!keywords.IsEmpty())
{
syscodeList = syscodeList.Where(p => (!string.IsNullOrEmpty(p.NAMETEXT) && p.NAMETEXT.Contains(keywords)));
}
var reslut = syscodeList.OrderByDesc(p => p.CODETYPE).ThenBy(p=>p.SHOWORDER).Skip(pageno).Top(pagesize).ToList(res);
if (reslut.IsSuccess)
{
reslut.RecordNum = syscodeList.Count();
pageInfo = new PageInfo(base.page, base.pagesize, reslut.RecordNum, JsonConverter.JsonClass(reslut.ResultModel));
}
}
//ViewData["nameOrProject"] = nameOrProject;
return pageInfo;
#endregion
}
///
/// 加载详情
///
/// 编码ID
///
[UserAuthorizeAttribute(ModuleAlias = "Warehouse", OperaAction = "Detail")]
public ActionResult Detail(int? id)
{
var entity = this.CodeManage.Get(p => p.ID == id) ?? new SYS_CODE();
ViewData["codeType"] = ClsDic.DicCodeType;
return View(entity);
}
///
/// 保存编码
///
[UserAuthorizeAttribute(ModuleAlias = "Warehouse", OperaAction = "Add,Edit")]
public ActionResult Save(SYS_CODE entity)
{
bool isEdit = false;
JsonHelper json = new JsonHelper() { Msg = "保存编码成功", Status = "n" };
try
{
if (entity != null)
{
entity.ISCODE = !string.IsNullOrEmpty(Request.Form["ISCODE"]) ? 1 : 0;
var _entity = new SYS_CODE();
//添加
if (entity.ID <= 0)
{
_entity = entity;
_entity.CREATEDATE = DateTime.Now;
_entity.CREATEUSER = this.CurrentUser.Name;
_entity.UPDATEDATE = DateTime.Now;
_entity.UPDATEUSER = this.CurrentUser.Name;
}
else //修改
{
_entity = this.CodeManage.Get(p => p.ID == entity.ID);
entity.CREATEDATE = _entity.CREATEDATE;
entity.CREATEUSER = _entity.CREATEUSER;
entity.UPDATEDATE = DateTime.Now;
entity.UPDATEUSER = this.CurrentUser.Name;
_entity = entity;
isEdit = true;
}
//判断岗位是否重名
if (!this.CodeManage.IsExist(p => p.NAMETEXT == _entity.NAMETEXT && p.CODETYPE == _entity.CODETYPE && p.ID != _entity.ID))
{
if (CodeManage.SaveOrUpdate(entity, isEdit))
{
json.Status = "y";
}
else
{
json.Msg = "保存失败";
}
}
else
{
json.Msg = "编码" + _entity.NAMETEXT + "已存在,不能重复添加";
}
}
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);
}
///
/// 删除编码
///
/// 编码ID字符串
[UserAuthorizeAttribute(ModuleAlias = "Warehouse", OperaAction = "Remove")]
public ActionResult Delete(string idList)
{
var json = new JsonHelper() { Msg = "删除编码完毕", Status = "n" };
try
{
if (!string.IsNullOrEmpty(idList))
{
var idList1 = idList.Trim(',').Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToList();
this.CodeManage.Delete(p => idList1.Any(e => e == p.ID));
json.Status = "y";
}
else
{
json.Msg = "未找到要删除的记录";
}
WriteLog(enumOperator.Remove, "删除编码,结果:" + json.Msg, enumLog4net.WARN);
}
catch (Exception e) { WriteLog(enumOperator.Remove, "删除编码出现异常:", e); }
return Json(json);
}
#endregion
#region 其他调用
///
/// 根据类别获取上下级关系
///
public ActionResult GetParentCode()
{
var json = new JsonHelper() { Status = "n", Data = "" };
string codetype = Request.Form["type"];
if (!string.IsNullOrEmpty(codetype))
{
var result = this.CodeManage.LoadListAll(p => p.CODETYPE == codetype);
if (result != null && result.Count > 0)
{
json.Data = result;
json.Status = "y";
}
}
return Json(json);
}
#endregion
}
}