using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using Cksoft.Unity.Log4NetConfig; using DllEapDal; using DllEapEntity; using DllEapEntity.Dtos; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DllEapBll.OFILM { [Route("eap/api/[controller]/[action]")] [Authorize] public class FactoryRegionController : ControllerBase { [HttpGet] public IEnumerable Get(string filter) { using (IDatabase db = DbFactory.Base("eapslave")) { db.BeginTrans(); var dal = new FactoryRegionDal(db); string errorinfo = string.Empty; var roles = dal.GetRegionTree(filter, ref errorinfo); return roles; } } [HttpGet] public FactoryRegion GetSingle(int id) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); return dal.Get(id); } } [HttpPost] public EapResponse Add([FromBody] FactoryRegion programMst) { string usercode = Request.Headers["usercode"]; using (IDatabase db = DbFactory.Base("eap")) { db.BeginTrans(); var dal = new FactoryRegionDal(db); string errorinfo = string.Empty; var response = new EapResponse() { Code = 1, Msg = string.Empty }; int id = -1; if (programMst.Id == 0) { id = dal.Add(programMst, usercode, ref errorinfo); } else { id = dal.Update(programMst, usercode, ref errorinfo); } if (id < 0) { db.Rollback(); response.Code = -1; response.Msg = errorinfo; } else { db.Commit(); if (programMst.Id == 0) { LogHelper.LogFatal("新增FactoryRegion-->" + Json.ToJson(programMst), "用户操作", usercode); } else LogHelper.LogFatal("修改FactoryRegion-->" + Json.ToJson(programMst), "用户操作", usercode); } response.Id = id; return response; } } [HttpPost] public EapResponse Delete([FromBody] int id) { IDatabase db = null; string errormsg = string.Empty; try { db = DbFactory.Base("eap"); var dal = new FactoryRegionDal(db); db.BeginTrans(); var model = dal.getFactoryRegion(id); var modelsec = dal.Get(id); var res = dal.Delete(id, ref errormsg); if (res < 0) { db.Rollback(); return new EapResponse() { Code = -1, Msg = errormsg }; } db.Commit(); LogHelper.LogFatal("删除FactoryRegion-->:" + Json.ToJson(model) + ";" + Json.ToJson(modelsec), "用户操作", Request.Headers["usercode"]); return new EapResponse() { Code = 1, Msg = "" }; } catch (Exception e) { errormsg = e.Message; return new EapResponse { Code = -1, Msg = errormsg }; } finally { if (db != null) db.Close(); } } [HttpGet] public IEnumerable GetRegionTreeSelect(int id) { using (IDatabase db = DbFactory.Base("eapslave")) { db.BeginTrans(); var dal = new FactoryRegionDal(db); string errorinfo = string.Empty; var roles = dal.GetRegionTreeSelect(id, ref errorinfo); return roles; } } [HttpGet] [AllowAnonymous] public IEnumerable> GetSelect() { using (IDatabase db = DbFactory.Base("eapslave")) { db.BeginTrans(); var dal = new FactoryRegionDal(db); string errorinfo = string.Empty; var roles = dal.GetFactorySelect(ref errorinfo); return roles; } } [HttpGet] [AllowAnonymous] public IEnumerable> GetSelectMulti() { using (IDatabase db = DbFactory.Base("eapslave")) { db.BeginTrans(); var dal = new FactoryRegionDal(db); string errorinfo = string.Empty; var roles = dal.GetFactorySelectMulti(ref errorinfo); return roles; } } [HttpGet] [AllowAnonymous] public IEnumerable> GetPlantsSelect(int? factoryId) { if (factoryId == null) return null; string errorinfo = string.Empty; using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); return dal.GetChildrenSelect(factoryId.Value, ref errorinfo); } } [HttpGet] [AllowAnonymous] public IEnumerable> GetFloorsSelect(int? plantId) { if (plantId == null) return null; string errorinfo = string.Empty; using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); return dal.GetChildrenSelect(plantId.Value, ref errorinfo); } } [HttpGet] [AllowAnonymous] public IEnumerable> GetLinesSelect(string ids) { if (string.IsNullOrEmpty(ids)) return null; var floorIds = ids.Split(new char[] { ',' }); if (floorIds == null || floorIds.Count() <= 0) return null; string errorinfo = string.Empty; using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); return dal.GetLinesSelect(floorIds.Select(c => Convert.ToInt32(c)).ToArray()); } } [HttpGet] public IEnumerable GetFactoryRegions(int id) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); var entities = dal.Get(1, 1000, "a.id", "asc", $" and a.ParentId={id}", "").OrderBy(c => c.Id).Select(c => new { Label = c.FName, Value = c.Id }); return entities; } } [HttpGet] public IEnumerable GetFactoryRegionsMulti(string filter) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); var entities = dal.Get(1, 1000, "a.id", "asc", $" {filter}", "").OrderBy(c => c.Id).Select(c => new { Label = c.FName, Value = c.Id }); return entities; } } public IEnumerable GetRegionSelects(string filter, int type = 1) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); return dal.GetRegionSelects(filter, type); } } [HttpGet] public IEnumerable GetFactoryRegionsAll() { //string client = AppConfigurtaionServices.Configuration["DbType"]; using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new FactoryRegionDal(db); var entities = dal.Get(1, 1000, "a.id", "asc", $" and a.ParentId=0 and a.ParentId!=154", "").OrderBy(c => c.Id).Select(c => new { Label = c.FName, Value = c.Id }); return entities; } } } }