using System.Collections; using Aspose.Cells; using Microsoft.AspNetCore.Mvc; using Estsh.Core.Services.IServices; using Estsh.Core.Model.Result; using Estsh.Core.Models; using System.Text.Json; using Estsh.Core.Controllers; using Estsh.Core.Model.ExcelModel; using Estsh.Core.Util; /*************************************************************************************************** * * 更新人:sitong.dong * 描述:BOM管理 * 修改时间:2022.06.22 * 修改日志:系统迭代升级 * **************************************************************************************************/ namespace Estsh.Core.Web.Controllers { public class BOMDefineController : BaseController { private IBOMDefineService service; private ICommonService commonService; private IWebHostEnvironment hostingEnvironment; public BOMDefineController(IBOMDefineService _service, ICommonService _commonService, IWebHostEnvironment _hostingEnvironment) { service = _service; commonService = _commonService; hostingEnvironment = _hostingEnvironment; } // // GET: /BOMDefineController/ public ActionResult Index() { return View(); } /// /// 获取车型列表 /// /// public ActionResult getSelectModelType() { Hashtable result = new Hashtable(); List modelTypeList = commonService.getModelTypeInfo(); result.Add("list", modelTypeList); return Json(result); } /// /// 获取配置列表 /// /// public ActionResult getSelectModel(String modelType) { Hashtable result = new Hashtable(); List modelList = service.getSelectModel(modelType); result.Add("list", modelList); return Json(result); } /// /// 获取BOM类型列表 /// /// public ActionResult getSelectBOMType() { Hashtable result = new Hashtable(); List bomTypeList = commonService.GetSysEnum("sys_bom_type"); result.Add("list", bomTypeList); return Json(result); } /// /// 获取零件名称列表 /// /// public ActionResult getSelectPartNo(String bomType, String model) { Hashtable result = new Hashtable(); List partList = this.service.getSelectPartNo(bomType, model); result.Add("list", partList); return Json(result); } /// /// 获取获取工站数据列表 /// /// public ActionResult getProcessData() { Hashtable result = new Hashtable(); List partList = this.service.getProcessData(CurrentEmp.FactoryId); result.Add("list", partList); return Json(result); } /// /// 获取BOM数据列表 /// /// /// /// /// /// public ActionResult getBOMList(String bomType, String partId,string pdline, String sort, String direction, string enabled = "Y") { Hashtable result = new Hashtable(); if (string.IsNullOrEmpty(partId) || partId=="null") { return Json(""); } List bomDataList = this.service.getBOMList(bomType, partId, sort, direction, pdline, enabled); result.Add("rows", bomDataList); result.Add("sort", sort); result.Add("direction", direction); return Json(result); } /// /// 校验零件号是否存在 /// /// /// public ActionResult validatePart(String validateValue) { Hashtable result = new Hashtable(); ValidateResult vResult = new ValidateResult(); int _id = service.GetPartID(validateValue); if (_id == 0) { vResult.valid = false; vResult.message = "零件号不存在!"; result.Add("validateResult", vResult); } else { vResult.valid = true; vResult.message = "OK"; result.Add("validateResult", vResult); } return Json(result); } /// /// 编辑BOM明细的子零件信息 /// /// /// public ActionResult editBOMD(String editType, String partNo, String bomType) { return View("AddBOM"); } /// /// 编辑BOM明细的子零件信息 /// /// /// public ActionResult editBOMDetail(String editType, SysBom bom) { String partNo= bom.PartNo; ViewData.Add("editType", editType); if (editType != null) { int BomID = service.GetBOMID(partNo, bom.BomType); if (BomID == 0) { return Json("零件号不存在!"); } if (editType.Equals("new")) { ViewData.Add("partNo", partNo); ViewData.Add("itemCount", 0); ViewData.Add("bomId", BomID); } else { ViewData.Add("bomId", BomID); ViewData.Add("partNo", bom.PartNo); ViewData.Add("stepType", bom.PartNo); ViewData.Add("itemPartNo", bom.ItemPartNo); ViewData.Add("itemCount", bom.ItemCount); ViewData.Add("itemGroup", bom.ItemGroup); ViewData.Add("location", bom.Location); ViewData.Add("processId", bom.ProcessId); ViewData.Add("version", bom.Version); ViewData.Add("vitualPart", bom.VitualPart); ViewData.Add("guid", bom.Guid); } return View("EditBOMDefine"); } else { return Json("编辑类型为空!"); } } /// /// 保存BOM明细的子零件信息 /// /// public ActionResult saveBOMDetail() { String editType = Request.Form["editType"].ToString(); String bomId = Request.Form["bomId"].ToString(); String partNo = Request.Form["partNo"].ToString(); int partId = 0; if (!string.IsNullOrEmpty(partNo)) { partId = Convert.ToInt32(service.GetBOMPartId(partNo)); } String stepType = Request.Form["stepType"].ToString(); String itemPartNo = Request.Form["itemPartNo"].ToString(); String itemCount = Request.Form["itemCount"].ToString() == null ? "" : Request.Form["itemCount"].ToString(); String itemGroup = Request.Form["itemGroup"].ToString() == "" ? "" : Request.Form["itemGroup"].ToString(); String location = Request.Form["location"].ToString() == "" ? "" : Request.Form["location"].ToString(); String processId = Request.Form["processName"].ToString() == "" ? "" : Request.Form["processName"].ToString(); String item_version = Request.Form["itemVersion"].ToString() == "" ? "" : Request.Form["itemVersion"].ToString(); string vitualPart = Request.Form["vitualPart"].ToString() == "" ? "" : Request.Form["vitualPart"].ToString(); String message = ""; String flag = "OK"; Hashtable result = new Hashtable(); string BOMType = service.GetBOMType(Convert.ToInt32(bomId)); int _id = service.GetPartID(itemPartNo); if (BOMType == "绑定" || BOMType == "特征") { if (!service.ExistsPartSNRule(_id)) { message = "请先定义条码特征!"; flag = "fail"; result.Add("message", message); result.Add("flag", flag); return Json(result); } } SysBomDetail model = new SysBomDetail(); model.BomId =Convert.ToInt32(bomId); model.PartId = partId; model.PartNo = partNo; model.StepType = Convert.ToInt32(stepType); model.ItemPartId = _id; model.ItemGroup = itemGroup; model.ItemQty = Convert.ToDecimal(itemCount); model.ProcessId = Convert.ToInt32(processId); model.Version = item_version; model.Location = location; model.VitualPart = vitualPart; SysBomDetail detail = new SysBomDetail(); detail.BomId = Convert.ToInt32(bomId); bool resultFlag = false; if (editType != null && editType.Trim().Equals("new")) { try { model.CreateUserId = CurrentEmp.EmpId; resultFlag = this.service.addBomItemPart("sys_bom_detail", model); } catch (Exception e) { message = "添加失败!"; flag = "fail"; } } else { try { model.UpdateUserId = CurrentEmp.EmpId; String guid = Request.Form["guid"].ToString() == "" ? "" : Request.Form["guid"].ToString(); resultFlag = this.service.UpdateBOMDetail(model, guid); } catch (Exception e) { message = "修改失败!"; flag = "fail"; } } if (resultFlag) { if (BOMType.Equals("物料")) { this.service.BuildBOMMatch(Convert.ToInt32(bomId)); } else if (BOMType.Equals("特征")) { this.service.BuildBOMMatchForTransBind(Convert.ToInt32(bomId)); } message = "保存成功!"; flag = "OK"; } else { message = "保存失败!"; flag = "fail"; } result.Add("message", message); result.Add("flag", flag); return Json(result); } /// /// 删除BOMdetail数据 /// /// /// public ActionResult deleteBOMDetail(String guid) { int delCount = 0; try { delCount = this.service.deleteBOMDetail(guid); } catch (Exception e) { delCount = -1; } Hashtable result = new Hashtable(); result.Add("status", delCount); return Json(result); } /// /// 保存BOM /// /// public ActionResult saveBOM() { String modelType = Request.Form["modelType"].ToString(); String model = Request.Form["model"].ToString(); int bom_Type = int.Parse(Request.Form["bomType"].ToString()); String partNo = Request.Form["part"].ToString(); String message = ""; String flag = "OK"; Hashtable result = new Hashtable(); int bomID = service.GetBOMID(partNo, bom_Type); if (bomID != 0) { message = "BOM 已经存在!"; flag = "fail"; result.Add("message", message); result.Add("flag", flag); return Json(result); } int partId = service.GetPartID(partNo); Hashtable values = new Hashtable(); values.Add("partId", partId); values.Add("bom_type", bom_Type); values.Add("createUserid", CurrentEmp.EmpId); values.Add("enabled", "Y"); SysBom bom = new SysBom(); bom.PartId = partId; bom.BomType = bom_Type; bom.CreateUserId = CurrentEmp.EmpId; bom.Enabled = "Y"; if (service.InsertBOM(bom)) { message = "新增成功"; flag = "OK"; } else { message = "新增失败!"; flag = "fail"; } result.Add("message", message); result.Add("flag", flag); return Json(result); } /// /// 删除BOM /// /// /// public ActionResult deleteBOM(String partNo, int bomType) { String message = ""; int flag = 0; try { int bomId = service.GetBOMID(partNo, bomType); if (bomId == 0) { message = "请选择 BOM 和 BOM 类型"; flag = 0; } if (service.DeleteBOM(bomId)) { message = "BOM 删除成功!"; flag = 1; } else { message = "BOM 删除失败!"; flag = 0; } } catch (Exception e) { message = "删除BOM出错!"; flag = 0; } Hashtable result = new Hashtable(); result.Add("message", message); result.Add("flag", flag); return Json(result); } /// /// 启用 /// /// /// public ActionResult onEnable(String ids) { String message = ""; int flag = 0; int delCount = 0; try { delCount = this.service.EnableData(ids); } catch (Exception e) { delCount = -1; } Hashtable result = new Hashtable(); result.Add("status", delCount); return Json(result); } /// /// 禁用 /// /// /// public ActionResult onDisable(String ids) { int delCount = 0; try { delCount = this.service.DisableData(ids); } catch (Exception e) { delCount = -1; } Hashtable result = new Hashtable(); result.Add("status", delCount); return Json(result); } /// /// 导入Bom Excel文件 /// /// public ActionResult importBOMInfo() { Hashtable result = new Hashtable(); IFormFile file = Request.Form.Files[0]; List data = ExcelHelper.GetList(file, 0); result = service.ImportExcel(data, CurrentEmp.EmpId); return Json(result); } /// /// 导出Bom 数据 /// /// public ActionResult exportBOMInfo(String bomType, String partId) { List dataHt = this.service.exportBOMInfo(bomType, partId); var memoryStream = ExcelHelper.ToExcel(dataHt); return File(memoryStream.ToArray(), "application/ms-excel", "BOM管理.xls"); } public ActionResult exportALL(String bomType) { List dataHt = this.service.exportALL(bomType); var memoryStream = ExcelHelper.ToExcel(dataHt); return File(memoryStream.ToArray(), "application/ms-excel", "BOM管理.xls"); } } }