using Microsoft.AspNetCore.Mvc; using System.Net; using System.Net.Http.Headers; using Microsoft.Extensions.Hosting.Internal; using Microsoft.AspNetCore.Http.Extensions; using IStation.Untity; using Furion.DynamicApiController; using System.ComponentModel.DataAnnotations; using Mapster; using Microsoft.AspNetCore.Http; namespace IStation.Application { /// /// InspectContent /// [Route("Inspect/Record/Mobile")] [ApiDescriptionSettings("Inspect", Name = "巡检记录(手机)", Order = 999)] public class InspectRecord_MobileController : IDynamicApiController { private readonly IHttpContextAccessor _httpContextAccessor; /// /// /// public InspectRecord_MobileController(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } #region 巡检中 /// /// 获取某产品的巡检项目(正在巡检时) /// /// /// /// [Route("GetContentByProductID4Input@V1.0")] [HttpGet] public IStation.Dto.InspectRecordContent4Ing GetContentByProductID4Input( long CorpID, long ProductID, long EmployeeID) { IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); IStation.Service.InspectionContentBundle service_Content = new Service.InspectionContentBundle(); var contents = service_Content.GetByProductID(CorpID, ProductID); if (contents == null || contents.Count() == 0) { throw new Exception("未配置巡检项"); } var content_item_ids = contents.Select(x => x.ItemID); var model_record = service_Record.GetOrCreateRecord(CorpID, ProductID, DateTime.Now, EmployeeID, content_item_ids.ToList()); List vm_list = new List(); foreach (var ds_detail in model_record.Details) { var content = contents.Find(x => x.ItemID == ds_detail.ContentID); if (content == null) continue; Dto.InspectRecordDetail detail_vm = new Dto.InspectRecordDetail(); detail_vm.ContentID = content.ItemID; detail_vm.Name = content.ItemName; detail_vm.ValueType = content.ValueType; detail_vm.ValueNullAble = content.ValueNullAble; detail_vm.ValueRangeMin = content.ValueRangeMin; detail_vm.ValueRangeMax = content.ValueRangeMax; detail_vm.TipInfo = content.TipInfo; if (detail_vm.TipInfo == null) detail_vm.TipInfo = ""; detail_vm.MonitorPointID = content.MonitorPointID; detail_vm.GroupName = content.GroupName; if (detail_vm.GroupName == null) detail_vm.GroupName = ""; detail_vm.DetailID = ds_detail.ID; detail_vm.Value = ds_detail.Value; if (detail_vm.Value == null) detail_vm.Value = ""; detail_vm.ValueStatus = ds_detail.Status; detail_vm.ValueList = content.ValueList; vm_list.Add(detail_vm); } IStation.Dto.InspectRecordContent4Ing vm_record = new IStation.Dto.InspectRecordContent4Ing(); vm_record.Details = vm_list; vm_record.RecordID = model_record.Record.ID; vm_record.Note = model_record.Record.Note; return vm_record; } /// /// 获取产品列表(正在巡检时) /// /// 公司ID /// 登录的员工ID /// 经度 /// 纬度 /// [Route("GetProductScheduleByToDay4Input")] [HttpGet] public List GetProductScheduleByToDay4Input( long CorpID, long EmployeeID, double PosiX, double PosiY) { var products = GetProductByCorpID(CorpID); if (products == null || products.Count() == 0) throw new Exception("未配置巡检项"); IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); var records = service_Record.GetByRecordDay(CorpID, DateTime.Today); if (records == null) records = new List(); var allStations = new IStation.Service.Station().GetByCorpID(CorpID); //暂时不按距离排序 List items = new List(); foreach (var product in products) { IStation.Dto.ProductInspectStatus vm = new IStation.Dto.ProductInspectStatus(); vm.ProductID = product.ID; vm.ProductCode = product.Code; vm.Catalog = product.Catalog; vm.BelongID = product.BelongID; vm.BelongType = product.BelongType; if (vm.BelongType == IStation.ObjectType.Station) {//带上泵站名字 vm.ProductName = string.Format("{0}({1})", vm.ProductName, (from x in allStations where x.ID == product.BelongID select x.Name).FirstOrDefault()); } else { vm.ProductName = product.Name; } var record = records.Find(x => x.ProductID == product.ID); if (record != null) { vm.RecordID = record.ID; vm.WorryCount = record.WorryCount; vm.ProgressInfo = record.ProgressInfo; vm.CompleteStatus = (int)record.CompleteStatus; } items.Add(vm); } //暂时不按经纬度排序 //if (PosiX > 0.001 && PosiY > 0.001) //{ //} return items; } /// /// 根据公司,获取所有巡检配置的产品列表 /// /// /// private List GetProductByCorpID(long CorpID) { IStation.Service.InspectionContentBundle service_Content = new Service.InspectionContentBundle(); var product_ids = service_Content.GetProductIDList(CorpID); if (product_ids == null || product_ids.Count() == 0) { return null; } IStation.Service.Product service_product = new Service.Product(); var all_products = service_product.GetByCorpID(CorpID); List products = (from x in all_products where product_ids.Contains(x.ID) select x).ToList(); return products; } /// /// 保存输入数据 /// /// /// [Route("SaveRecordDetails")] [HttpPost] public IStation.Model.InspectRecord SaveRecordDetails(IStation.Dto.SaveRecordDetails_Request request) { if (request == null) throw new Exception("内容为空"); if (string.IsNullOrEmpty(request.DetailContent)) throw new Exception("内容为空"); var sss = request.DetailContent.Split(new string[] { "$$$" }, StringSplitOptions.RemoveEmptyEntries); List details = new List(); foreach (var s in sss) { var vvv = s.Split(new string[] { "###" }, StringSplitOptions.None); var detailID = Convert.ToInt64(vvv[0]); var status = Convert.ToInt32(vvv[1]); var detailValue = vvv[2]; if (string.IsNullOrEmpty(detailValue) || string.IsNullOrWhiteSpace(detailValue) || detailValue == "null") detailValue = null; details.Add(new Model.InspectRecordDetailBase() { ID = detailID, Value = detailValue, Status = status }); } var blank_count = (from x in details where string.IsNullOrEmpty(x.Value) select x).Count(); IStation.Model.InspectRecord Record = new IStation.Model.InspectRecord(); Record.ID = request.RecordID; Record.ProductID = request.ProductID; Record.WorryCount = (from x in details where x.Status == 2 select x).Count(); Record.ProgressInfo = string.Format("{0}/{1}", details.Count() - blank_count, details.Count()); Record.Note = request.Note; if (blank_count == 0) { Record.CompleteStatus = IStation.Model.InspectRecord.eCompleteStatus.巡检完成; } else if (details.Count() - blank_count > 0) { Record.CompleteStatus = IStation.Model.InspectRecord.eCompleteStatus.巡检中; } else { Record.CompleteStatus = IStation.Model.InspectRecord.eCompleteStatus.未巡检; } Record.RecordDay = DateTime.Today.ToString("yyyy-MM-dd"); IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); bool ret = service_Record.UpdateRecord(Record, details); return Record; } #endregion #region 根根据月份,统计 , 某日的巡检人数 /// /// 根据月份,统计 , 某日的巡检人数 ,为0表示没人巡检 /// /// 公司ID /// 年 /// 月 /// [Route("GetDayEmployeeCountByMonth")] [HttpGet] public List GetDayEmployeeCountByMonth(long CorpID, int Year, int Month) { IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); var start_day = new DateTime(Year, Month, 1); var end_day = start_day.AddMonths(1).AddDays(-1); var ds_records = service_Record.GetDayEmployeeCountByDay(CorpID, start_day, end_day); List records = new List(); if (ds_records == null || ds_records.Count() == 0) { return new List(); } for (int d = 1; d <= DateTime.DaysInMonth(Year, Month); d++) { records.Add(new DayAndEmployeeCount() { Day = d, Count = (from x in ds_records where x.RecordDay.Day == d select x.Count).FirstOrDefault() }); } return records; } /// /// /// public class DayAndEmployeeCount { /// /// /// public int Day { get; set; } /// /// /// public int Count { get; set; } } #endregion #region 根据日期 查询巡检记录 /// /// 根据日期 查询巡检记录 /// /// 公司ID /// 日期 /// [Route("GetRecordListByDay")] [HttpGet] public List GetRecordListByDay(long CorpID, string Day) { IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); DateTime t = DateTime.Today.AddDays(-1); DateTime.TryParse(Day, out t); var allEmployees = new IStation.Service.Employee().GetByCorpID(CorpID); var allProducts = new IStation.Service.Product().GetByCorpID(CorpID); var allStations = new IStation.Service.Station().GetByCorpID(CorpID); var ds_records = service_Record.GetByRecordDay(CorpID, t); List vs = new List(); if (ds_records != null) { foreach (var record in ds_records) { IStation.Dto.InspectRecordItem vm = new IStation.Dto.InspectRecordItem(record); vm.EmployeeName = (from x in allEmployees where x.ID == record.EmployeeID select x.Name).FirstOrDefault(); if (vm.EmployeeName == null) vm.EmployeeName = ""; var product = (from x in allProducts where x.ID == record.ProductID select x).FirstOrDefault(); if (product != null) { vm.ProductName = product.Name; // vm.ProductNO = product.Code; vm.ProductCode = product.Code; } if (product.BelongType == IStation.ObjectType.Station) { vm.ProductName = string.Format("{0}({1})", vm.ProductName, (from x in allStations where x.ID == product.BelongID select x.Name).FirstOrDefault()); } vs.Add(vm); } } return vs; } #endregion #region 根据产品获取巡检记录 /// /// 获取所有 巡检配置的产品 /// /// 公司ID /// [Route("GetAllInspectProduct")] [HttpGet] public List GetAllInspectProduct(long CorpID) { var products = GetProductByCorpID(CorpID); if (products == null || products.Count() == 0) throw new Exception("未配置巡检项"); var allEmployee = new Service.Employee().GetByCorpID(CorpID); var allStations = new IStation.Service.Station().GetByCorpID(CorpID); IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); var records = service_Record.GetProductLastRecordByCorpID(CorpID); //GetProductLastRecordByProductID((from x in products select x.ID).ToList());//最后一条巡检记录 if (records == null) records = new List(); var worrys = service_Record.GetWorryCountStaticByProduct(CorpID, DateTime.Now.AddMonths(-1), DateTime.Now); if (worrys == null) worrys = new List(); List items = new List(); foreach (var product in products) { Dto.ProductInspectLastRecord vm = new Dto.ProductInspectLastRecord(); vm.ProductID = product.ID; vm.ProductName = product.Name; vm.ProductCode = product.Code; vm.Catalog = product.Catalog; vm.BelongID = product.BelongID; vm.BelongType = product.BelongType; if (product.BelongType == IStation.ObjectType.Station) { vm.ProductName = string.Format("{0}({1})", vm.ProductName, (from x in allStations where x.ID == product.BelongID select x.Name).FirstOrDefault()); } var record = records.Find(x => x.ProductID == product.ID); if (record != null) { vm.RecordID = record.ID; vm.LastRecordDay = record.RecordDay; vm.LastInpectEmployee = (from x in allEmployee where x.ID == record.EmployeeID select x.Name).FirstOrDefault(); vm.WorryCount = record.WorryCount; vm.ProgressInfo = record.ProgressInfo; vm.CompleteStatus = (int)record.CompleteStatus; } var worry = worrys.Find(x => x.ProductID == product.ID); if (worry != null) { vm.WorryCount = worry.WorryCount; } items.Add(vm); } return items; } /// /// 根据产品 查询巡检记录(获取分页数据) /// /// 公司ID /// /// 页码序号(从0开始) /// 每一页的条数 /// [Route("GetPageListByProduct")] [HttpGet] public List GetPageListByProduct(long CorpID, long ProductID, int PageIndex, int PageSize) { IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); var allEmployees = new Service.Employee().GetByCorpID(CorpID); var allProducts = new IStation.Service.Product().GetByCorpID(CorpID); int total = 0; var ds_records = service_Record.GetPageListByProductID(PageIndex, PageSize, CorpID, ProductID, out total); List vs = new List(); if (ds_records != null) { foreach (var record in ds_records) { Dto.InspectRecordItem v = new Dto.InspectRecordItem(record); v.EmployeeName = (from x in allEmployees where x.ID == record.EmployeeID select x.Name).FirstOrDefault(); if (v.EmployeeName == null) v.EmployeeName = ""; //var product = (from x in allProducts where x.ID == record.ProductID select x).FirstOrDefault(); //if (product != null) //{ // v.ProductName = product.Name; // v.ProductNO = product.NO; // v.ProductCode = product.Code; //} vs.Add(v); } } return vs; } #endregion #region 根据员工获取巡检记录 /// /// 获取所有巡检员 /// /// /// [Route("GetAllInspectEmployee")] [HttpGet] public List GetAllInspectEmployee(long CorpID) { IStation.Service.Employee service_employee = new Service.Employee(); var allEmployee = service_employee.GetByFlag(CorpID,IStation.LogicFlags.巡检员); if (allEmployee == null || allEmployee.Count() == 0) { return null; } IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); var records = service_Record.GetEmployeeLastRecord(CorpID);//最后一条巡检记录 if (records == null) records = new List(); List vm_list = new List(); foreach (var employee in allEmployee) { IStation.Dto.InspectorBase vm_model = new IStation.Dto.InspectorBase(); vm_model.EmployeeID = employee.ID; vm_model.RealName = employee.Name; vm_model.LastInsepectDay = "未巡检过"; var record = (from x in records where x.EmployeeID == employee.ID select x).FirstOrDefault(); if (record != null) { vm_model.LastInsepectDay = record.RecordDay; } vm_list.Add(vm_model); } return new List(vm_list); } /// /// 根据产品 查询巡检记录(获取分页数据) /// /// 公司ID /// /// 页码序号(从0开始) /// 每一页的条数 /// [Route("GetPageListByEmployee")] [HttpGet] public List GetPageListByEmployee(long CorpID, long EmployeeID, int PageIndex, int PageSize) { IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); var ds_records = service_Record.GetPageListByEmployeeID(PageIndex, PageSize, EmployeeID); return ds_records; } #endregion #region 获取某产品某日的巡检项目明细 /// /// 获取某产品某日的巡检项目明细 /// /// /// /// /// [Route("GetRecordDetail")] [HttpGet] public IStation.Dto.InspectRecordContent4Ing GetRecordDetail(long CorpID, long ProductID, long EmployeeID, string Day) { IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); IStation.Service.InspectionContentBundle service_Content = new Service.InspectionContentBundle(); var contents = service_Content.GetByProductID(CorpID, ProductID); if (contents == null || contents.Count() == 0) { throw new Exception("未配置巡检项"); } var model_record = service_Record.GetByProductID(ProductID, DateTime.Parse(Day)); if (model_record == null || model_record.Details == null || model_record.Details.Count() == 0) { throw new Exception("未找到巡检记录"); } List vm_list = new List(); foreach (var content in contents) { Dto.InspectRecordDetail detail_vm = new Dto.InspectRecordDetail(); detail_vm.ContentID = content.ItemID; detail_vm.Name = content.ItemName; detail_vm.ValueType = content.ValueType; detail_vm.ValueNullAble = content.ValueNullAble; detail_vm.ValueRangeMin = content.ValueRangeMin; detail_vm.ValueRangeMax = content.ValueRangeMax; detail_vm.TipInfo = content.TipInfo; detail_vm.MonitorPointID = content.MonitorPointID; detail_vm.GroupName = content.GroupName; detail_vm.ValueList = content.ValueList; var ds_detail = model_record.Details.Find(x => x.ContentID == content.ItemID); if (ds_detail == null) { detail_vm.DetailID = 0; detail_vm.Value = "未填写"; } else { detail_vm.DetailID = ds_detail.ID; detail_vm.Value = ds_detail.Value; detail_vm.ValueStatus = ds_detail.Status; } vm_list.Add(detail_vm); } var files = new IStation.Service.InspectRecordFile().GetByRecordID(model_record.Record.ID); IStation.Dto.InspectRecordContent4Ing vm_record = new IStation.Dto.InspectRecordContent4Ing(); vm_record.Details = vm_list; vm_record.RecordID = model_record.Record.ID; vm_record.Note = model_record.Record.Note; if (files != null && files.Count > 0) { vm_record.Files = (from x in files select new IStation.Dto.InspectFile() { ID = x.ID, Path = string.Format("http://api.beng35.com/Repair/InspectRecordFile/{0}/{1}", model_record.Record.ID, x.DataSetFile), ThumbPath = string.Format("http://api.beng35.com/Repair/InspectRecordFile/{0}/{1}", model_record.Record.ID, x.DataSetFile.Replace(".", "_th.")) }).ToList(); } return vm_record; } #endregion #region 获取某巡检点的历史记录 /// /// 获取某巡检点的历史记录 /// /// /// /// /// /// [Route("GetContentHistoryValueList")] [HttpGet] public List GetContentHistoryValueList(long CorpID, long ContentID, string StartDay, string EndDay) { if (CorpID <= 0) { throw new Exception("CorpID未设置"); } if (ContentID <= 0) { throw new Exception("ContentID未设置"); } IStation.Service.InspectRecord service_Record = new Service.InspectRecord(); int Year = DateTime.Today.Year; DateTime startTimeT = DateTime.Now.AddDays(-30); DateTime endTimeT = DateTime.Now; if (!string.IsNullOrEmpty(StartDay)) { startTimeT = DateTime.Parse(StartDay); Year = DateTime.Parse(StartDay).Year; } if (!string.IsNullOrEmpty(EndDay)) endTimeT = DateTime.Parse(EndDay); var details = service_Record.GetDetailByContentID(ContentID, startTimeT, endTimeT); if (details == null || details.Count() == 0) { throw new Exception("未配置巡检記錄"); } var records = service_Record.GetRecordByContentID(ContentID, startTimeT, endTimeT); if (records == null || records.Count() == 0) { throw new Exception("未配置巡检記錄"); } var allEmployees = new Service.Employee().GetByCorpID(CorpID); List vm_list = new List(); foreach (var detail in details) { //if (detail.Time < startTimeT) // continue; //if (detail.Time > endTimeT) // continue; ContentDetailHistoryValueItem detail_vm = new ContentDetailHistoryValueItem(); detail_vm.DetailID = detail.ID; detail_vm.Value = detail.Value; detail_vm.Status = detail.Status; detail_vm.Time = detail.Time.ToString("yyyy-MM-dd HH:mm:ss"); var record = records.Find(x => x.ID == detail.RecordID); if (record != null) { detail_vm.EmployeeName = (from x in allEmployees where x.ID == record.EmployeeID select x.Name).FirstOrDefault(); } vm_list.Add(detail_vm); } return vm_list; } #endregion /// /// /// public class ContentDetailHistoryValueItem { #region Model /// /// 标识 /// public long DetailID { get; set; } /// /// 填写的值 /// public string Value { get; set; } = ""; /// /// 状态 2 隐患 3 已报修 /// public int Status { get; set; } /// /// 填写时间 /// public string Time { get; set; } = ""; /// /// 巡检人 /// public string EmployeeName { get; set; } = ""; #endregion } } }