BusEquipmentMaintainNoticeService.cs 11.3 KB
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Hh.Mes.Pojo.System;
using System.Text;
using Hh.Mes.POJO.WebEntity.equipment;
using Hh.Mes.POJO.EnumEntitys;

namespace Hh.Mes.Service.Equipment
{
    public class BusEquipmentMaintainNoticeService : RepositorySqlSugar<daq_equipment_alarm_record>
    {
        public dynamic Load(PageReq pageReq, string projectCode, base_equipment equipment)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var project = Context.Queryable<base_project>().Where(x => x.projectCode == projectCode).Select(x => new base_project
                {
                    keys = x.keys,
                    projectCode = x.projectCode,
                    projectName = x.projectName,
                }).First();
                if (project == null)
                {
                    return result.ResponseError($"项目编号:“{projectCode}”不存在!");
                }

                var query = Context.Queryable<base_equipment>().Where(GetExpression(project.keys, equipment));
                //分页
                if (!equipment.Exel)
                {
                    var total = 0;
                    result.Result = query.Select(x => new
                    {
                        x.id,
                        x.factoryCode,
                        factoryName = SqlFunc.Subqueryable<base_factory>().Where(s => s.factoryCode == x.factoryCode).Select(s => s.factoryName),
                        project.projectName,
                        x.equipmentCode,
                        x.equipmentName,
                        count = SqlFunc.Subqueryable<bus_equipment_maintain_record_head>().Where(s => s.equipmentCode == x.equipmentCode && s.status != (int)MaintainStatus.Maintained).Count(),
                    }).OrderBy(x => x.count, OrderByType.Desc).OrderBy(x => x.equipmentCode).ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                    return result;
                }

                var data = query.ToList();
                result.Result = data;
                result.Count = data.Count;
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic LoadDesc(PageReq pageReq, string equipmentCode, base_equipment_part baseEquipmentPart)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var equipment = Context.Queryable<base_equipment>().Where(x => x.equipmentCode == equipmentCode).First();
                if (equipment == null)
                {
                    return result.ResponseError($"设备{equipmentCode}不存在!");
                }

                var query = Context.Queryable<base_equipment_part>().Where(GetDescExpression(equipment.equipmentTypeCode, baseEquipmentPart));
                if (!baseEquipmentPart.Exel)
                {
                    int total = 0;
                    result.Result = query.Select(x => new
                    {
                        x.id,
                        equipment.equipmentCode,
                        equipment.equipmentName,
                        x.partCode,
                        x.partName,
                        lastMaintainTime = SqlFunc.Subqueryable<bus_equipment_maintain_record_head>().Where(s => s.equipmentCode == equipment.equipmentCode && s.partCode == x.partCode).Max(x => x.lastMaintainTime),
                        nextMaintainTime = SqlFunc.Subqueryable<bus_equipment_maintain_record_head>().Where(s => s.equipmentCode == equipment.equipmentCode && s.partCode == x.partCode && s.status != (int)MaintainStatus.Maintained).Min(x => x.nextMaintainTime),
                        status = SqlFunc.Subqueryable<bus_equipment_maintain_record_head>().Where(s => s.equipmentCode == equipment.equipmentCode && s.partCode == x.partCode && s.status != (int)MaintainStatus.Maintained).OrderBy(x => x.nextMaintainTime).Select(s => s.status),
                        x.createTime,
                    }).OrderBy(x => x.nextMaintainTime).ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count;
                }
                return result;
            }, catchRetrunValue: "list");
        }

        /// <summary>
        /// /左侧列表 设备类型+设备
        /// </summary>
        public dynamic GetTreeList()
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var stringBuilder = new StringBuilder();
                string equipmentTypeCodes = string.Join("', '", SystemVariable.IotNotContainDevice);
                string user = sysWebUser.Account;

                var nodes = new List<dynamic>();

                var rootNode = new
                {
                    id = Guid.NewGuid(),
                    name = "根节点",
                    keys = "r-1",
                    parentId = "0",
                    isok = false,
                    projectKeys = Guid.Empty,
                };

                var data = Context.Queryable<base_project>().Where(GetTreeExpression(user)).Select(x => new
                {
                    id = x.keys,
                    name = x.projectName,
                    keys = x.projectCode,
                    parentId = rootNode.keys,
                    isok = true,
                    projectKeys = x.keys
                }).ToList();

                nodes.Add(rootNode);
                nodes.AddRange(data);
                return nodes;
            });
        }

        private Expression<Func<base_project, bool>> GetTreeExpression(string user)
        {
            var exp = Expressionable.Create<base_project>();
            //非管理员,查询登陆用户绑定客户管理的项目
            if (user != SystemVariable.DefaultCreated)
            {
                var projectRoleKeys = GetProjectRoleKeys(user);
                exp.And(x => SqlFunc.Subqueryable<sys_role_projects_rel>().Where(c => projectRoleKeys.Contains(c.project_roles_key) && c.project_key == x.keys).Any());
            }
            return exp.ToExpression();//拼接表达式
        }

        private Expression<Func<base_equipment, bool>> GetExpression(Guid projectKey, base_equipment equipment)
        {
            var exp = Expressionable.Create<base_equipment>();
            exp.And(x => x.projectKeys == projectKey);
            if (!string.IsNullOrWhiteSpace(equipment.equipmentCode))
            {
                exp.And(x => x.equipmentCode.Contains(equipment.equipmentCode));
            }

            if (!string.IsNullOrWhiteSpace(equipment.equipmentName))
            {
                exp.And(x => x.equipmentName.Contains(equipment.equipmentName));
            }

            return exp.ToExpression();
        }

        private Expression<Func<base_equipment_part, bool>> GetDescExpression(string equipmentTypeCode, base_equipment_part baseEquipmentPart)
        {
            var exp = Expressionable.Create<base_equipment_part>();
            exp.And(x => x.equipmentTypeCode == equipmentTypeCode);

            if (!string.IsNullOrWhiteSpace(baseEquipmentPart.partCode))
            {
                exp.And(x => x.partCode.Contains(baseEquipmentPart.partCode));
            }

            if (!string.IsNullOrWhiteSpace(baseEquipmentPart.partName))
            {
                exp.And(x => x.partName.Contains(baseEquipmentPart.partName));
            }

            return exp.ToExpression();
        }

        public dynamic Ins(daq_equipment_alarm_record entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //entity.createBy = sysWebUser?.Account;
                //entity.createTime = DateTime.Now;
                response.Status = Add(entity);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic Upd(daq_equipment_alarm_record entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //entity.updateBy = sysWebUser?.Account;
                //entity.updateTime = DateTime.Now;
                response.Status = Update(entity);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic DelByIds(Guid[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                Context.Deleteable<daq_equipment_alarm_record>(t => ids.Contains(t.id)).ExecuteCommand();
                return response;
            });
        }

        public Response ExportData(base_equipment entity)
        {
            var response = new Response();
            //if (entity.ProjectCode == "r-1")
            //{
            //    entity.ProjectCode = null;
            //}
            //var sheet1 = Load(null, entity);
            //entity.isHandled = false;
            //var sheet2 = LoadDesc(null, entity);
            ////返回异常数据报错,传空数组给前端
            //response.Result = new
            //{
            //    head = sheet1 is string ? "[]" : sheet1.Result,
            //    body = sheet2 is string ? "[]" : sheet2.Result
            //};
            return response;
        }

        public Dictionary<string, string> GetDicAlarmHandlePeopleList()
        {
            var result = new Dictionary<string, string>();

            //报警处理人 alarmHandlePeople
            var res = Context.Queryable<sys_dict_data>().Where(x => x.dictType == "AlarmHandlePeople").ToList();

            foreach (var item in res)
            {
                result.Add(item.dictValue, item.dictLabel);
            }

            return result;
        }

        public dynamic MaintainHandle(string equipmentCode, string partCode, DateTime maintainTime)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                var data = Context.Queryable<bus_equipment_maintain_record_head>().Where(x => x.equipmentCode == equipmentCode && x.partCode == partCode && x.status != (int)MaintainStatus.Maintained).First();
                if (data == null)
                {
                    return response.ResponseError($"设备编号“{equipmentCode}”对应部件“{partCode}”不存在待保养的记录数据!");
                }

                data.status = (int)MaintainStatus.Maintained;
                data.updateTime = DateTime.Now;

                Context.Updateable(data).UpdateColumns(x => new
                {
                    x.status,
                    x.updateTime,
                }).ExecuteCommand();

                return response;
            });
        }
    }
}