EquipmentOutlineHandleService.cs 7.94 KB
using HHECS.Dal.Repository;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using HHECS.Model.Dtos;
using HHECS.Infrastructure.Json;

namespace HHECS.Application.Service
{
    public class EquipmentOutlineHandleService : BaseService
    {

        public string Load(EquipmentStatusRecord entity, int page, int limit)
        {
            return Execute(() =>
            {
                using var warehouseRepository = new WarehouseRepository();
                using var equipmentDataRecordRepository = new EquipmentDataRecordRepository();
                using var equipmentAlarm = new EquipmentStatusRecordRepository();
                var result = equipmentAlarm.Where(GetExpression(entity));

                var total = result.Count();
                var equipmentStatusList = result.OrderBy(t => t.IsEnd).OrderByDescending(t => t.Created).Page(page, limit).ToList(t => new EquipmentStatusRecordDto());

                var equipmentCodeList = equipmentStatusList.Select(t => t.EquipmentCode).ToList();
                var equipmentList = equipmentDataRecordRepository.Where(x => equipmentCodeList.Contains(x.Code)).ToList();

                var warehouseCodeList = equipmentStatusList.Select(t => t.WarehouseCode).ToList();
                var warehouseList = warehouseRepository.Where(x => warehouseCodeList.Contains(x.Code)).ToList();

                equipmentStatusList.ForEach(t =>
                {
                    t.Duration = string.Format("{0:dd}天 {0:hh}小时 {0:mm}分 {0:ss}秒", t.Time);

                    var online = equipmentList.FirstOrDefault(x => x.Code == t.EquipmentCode);

                    //温度和 震动
                    t.WalkTemperature = online != null ? online.WalkTemperature.ToString() + "℃" : "0℃";
                    t.WalkVibrationVelocity = online != null ? Math.Round(online.WalkVibrationVelocity, 2).ToString() + "(mm/s)" : "0(mm/s)";
                    t.LiftTemperature = online != null ? online.LiftTemperature.ToString() + "℃" : "0℃";
                    t.LiftVibrationVelocity = online != null ? Math.Round(online.LiftVibrationVelocity, 2).ToString() + "(mm/s)" : "0(mm/s)";


                    t.runCurrent = online != null ? Math.Round(online.runCurrent, 2).ToString() + "(A)" : "0(A)";
                    t.liftCurrent = online != null ? Math.Round(online.liftCurrent, 2).ToString() + "(A)" : "0(A)";

                    var warehouse = (t.WarehouseCode == "LM1") ? warehouseList.FirstOrDefault(x => x.Code == "BM2") : warehouseList.FirstOrDefault(x => x.Code == t.WarehouseCode);
                    //var warehouse = warehouseRepository.Where(x => x.Code == t.WarehouseCode).First();
                    t.WarehouseName = warehouse != null ? warehouse.Name : t.WarehouseCode;
                });
                Response.Result = equipmentStatusList;
                Response.Count = total;
                return Response.ToJson();
            });
        }




        public string SumTime(List<EquipmentStatusRecord> equipmentStatusRecords)
        {
            TimeSpan? time = new TimeSpan();
            foreach (var t in equipmentStatusRecords)
            {
                time += t.Time;
            }
            return string.Format("{0:dd}天 {0:hh}小时 {0:mm}分 {0:ss}秒", time);
        }



        private Expression<Func<EquipmentStatusRecord, bool>> GetExpression(EquipmentStatusRecord entity)
        {
            Expression<Func<EquipmentStatusRecord, bool>> filter = t => true;

            filter = filter.And(x => x.EquipmentStatus.Equals("OutLine"));
            if (!string.IsNullOrWhiteSpace(entity.WarehouseCodeExt))
            {
                filter = filter.And(x => entity.WarehouseCodeExt.IndexOf(x.EquipmentCode) >= 0);
            }
            if (entity.Created != null && entity.Updated != null)
            {
                filter = filter.And(t => t.Created >= entity.Created && t.Updated <= entity.Updated);
            }
            if (entity.Created != null && entity.Updated == null)
            {
                filter = filter.And(t => t.Created >= entity.Created);
            }
            if (entity.Updated != null && entity.Created == null)
            {
                filter = filter.And(t => t.Updated <= entity.Updated);
            }
            return filter;
        }

        public Dictionary<string, string> GetEquipmentStatusType()
        {
            var result = new Dictionary<string, string>
            {
                { "全部", "" }
            };
            foreach (var item in EnumHelper.EnumListDicString<EquipmentStatusRecordStatus>())
            {
                result.Add(item.Key, item.Value.ToString());
            }
            return result;
        }

        public string Export(EquipmentStatusRecord entity)
        {
            return Execute(() =>
            {
                using var equipmentStatusRecordRepository = new EquipmentStatusRecordRepository();
                var result = equipmentStatusRecordRepository.Where(GetExpression(entity)).OrderByDescending(a => a.Created).ToList(t => new EquipmentStatusRecordDto());

                result.ForEach(t =>
                {
                    t.Duration = string.Format("{0:dd}天 {0:hh}小时 {0:mm}分 {0:ss}秒", t.Time);
                });
                Response.Result = result;
                Response.Count = result.Count;
                return Response.ToJson(); ;
            });
        }

        public string HanldeOutlineToEquipmentAlarm(EquipmentStatusRecord entity, User user)
        {
            return Execute(() =>
            {
                //查询对应的设备离线状态记录数据
                using EquipmentStatusRecordRepository equipmentStatusRecordRepository = new EquipmentStatusRecordRepository();
                var equipmentStatusRecord = equipmentStatusRecordRepository.Where(x => x.Id == entity.Id).First();
                DateTime? recordStartTime = equipmentStatusRecord.Created;
                DateTime? recordEndTime = equipmentStatusRecord.Updated;

                //判断时间调整是否在范围内
                if (entity.Created > recordEndTime || entity.Created < recordStartTime)//开始时间不能往前调
                {
                    Response.Code = 500;
                    Response.Message = $"确认离线时间的开始时间调整范围应该在{recordStartTime}~{recordEndTime}之间!";
                    return Response.ToJson();
                }
                if (entity.Updated > recordEndTime || entity.Updated < recordStartTime)//结束时间不能往后调
                {
                    Response.Code = 500;
                    Response.Message = $"确认离线时间的结束时间调整范围应该在{recordStartTime}~{recordEndTime}之间!";
                    return Response.ToJson();
                }

                using EquipmentAlarmRecordRepository equipmentAlarmRecordRepository = new EquipmentAlarmRecordRepository();
                EquipmentAlarmRecord equipmentAlarm = new EquipmentAlarmRecord()
                {
                    EquipmentCode = entity.EquipmentCode,
                    EquipmentName = entity.EquipmentName,
                    EquipmentPropCode="F9999",
                    EquipmentPropName="是否异常掉线",
                    Alarm= "人工确认处理异常设备掉线",
                    IsEnd=true,
                    ishandled=false,
                    WarehouseCode=entity.WarehouseCode,
                    Created=entity.Created,
                    CreatedBy=entity.CreatedBy,
                    Updated=entity.Updated,
                    UpdatedBy=user.UserName
                };
                equipmentAlarmRecordRepository.Insert(equipmentAlarm);
                equipmentStatusRecordRepository.Delete(equipmentStatusRecord);
                return Response.ToJson();

            });
        }
    }
}