BusEquipmentMaintainRecordDetailService.cs 6.15 KB
using Hh.Mes.Common.Infrastructure;
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 Hh.Mes.Service.SystemAuth;
using SqlSugar;
using System;
using System.Linq;
using System.Linq.Expressions;
using Hh.Mes.POJO.EnumEntitys;

namespace Hh.Mes.Service.Equipment
{
    /// <summary>
    /// 设备保养明细(保养和未保养)-部件维度详情
    /// </summary>
    public class BusEquipmentMaintainRecordDetailService : RepositorySqlSugar<bus_equipment_maintain_record_detail>
    {
        public BusEquipmentMaintainRecordDetailService(IAuth auth) : base(auth) { }

        public dynamic LoadDesc(PageReq pageReq, bus_equipment_maintain_record_head busEquipmentMaintainRecordHead)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var query = Context.Queryable<bus_equipment_maintain_record_detail>().Where(GetDescMaintainedExpression(busEquipmentMaintainRecordHead));
                if (!busEquipmentMaintainRecordHead.Exel)
                {
                    int total = 0;
                    var data = query.Select(x => new
                    {
                        x.id,
                        x.topKeys,
                        x.headKeys,
                        x.projectKeys,
                        x.equipmentCode,
                        equipmentName = SqlFunc.Subqueryable<base_equipment>().Where(s => s.equipmentCode == x.equipmentCode).Select(s => s.equipmentName),
                        x.equipmentTypeCode,

                        x.partCode,
                        x.partName,
                        x.lastMaintainTime,
                        x.nextMaintainTime,

                        x.maintainTime,
                        x.remark,
                        x.maintainLevel,
                        x.maintainPrice,

                        x.maintainer,
                        x.maintainContent,
                        x.nextDurationTime,
                        x.factoryCode,
                        x.pushStatus,
                        status = SqlFunc.Subqueryable<bus_equipment_maintain_record_detail>().Where(s => s.topKeys == x.topKeys && s.partCode == x.partCode && s.status == (int)EnumMaintainStatus.Maintained).Select(s => s.status),
                    }).OrderBy(x => x.maintainTime).ToOffsetPage(pageReq.page, pageReq.limit, ref total);

                    result.Result = data.Select(x => new
                    {
                        x.id,
                        x.topKeys,
                        x.headKeys,
                        x.projectKeys,
                        x.equipmentCode,

                        x.equipmentName,
                        x.equipmentTypeCode,
                        x.partCode,
                        x.partName,

                        x.lastMaintainTime,
                        x.nextMaintainTime,
                        x.remark,
                        x.maintainLevel,

                        x.maintainer,
                        x.maintainContent,
                        x.maintainPrice,
                        x.nextDurationTime,
                        x.factoryCode,
                        x.pushStatus,
                        url = string.Join(',', Context.Queryable<Sys_File>().Where(t => SqlFunc.Subqueryable<base_equipment_part>().Where(s => s.partCode == x.partCode && s.id == t.targetId).Any() && t.targetTableName == nameof(base_equipment_part)).Select(t => t.url).ToList()),
                        x.status,
                        x.maintainTime
                    }).ToList();
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count;
                }
                return result;
            }, catchRetrunValue: "list");
        }
 
 
        public Expression<Func<bus_equipment_maintain_record_detail, bool>> LinqWhere(bus_equipment_maintain_record_detail model)
        {
            try
            {
                var exp = Expressionable.Create<bus_equipment_maintain_record_detail>();
                //数据过滤条件
                //if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
                if (!string.IsNullOrWhiteSpace(model.factoryCode))
                {
                    exp.And(x => x.factoryCode.Contains(model.factoryCode));
                }
                if (!string.IsNullOrWhiteSpace(model.equipmentCode))
                {
                    exp.And(x => x.equipmentCode.Contains(model.equipmentCode));
                }
                if (!string.IsNullOrWhiteSpace(model.equipmentTypeCode))
                {
                    exp.And(x => x.equipmentTypeCode.Contains(model.equipmentTypeCode));
                }
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }

        private Expression<Func<bus_equipment_maintain_record_detail, bool>> GetDescMaintainedExpression(bus_equipment_maintain_record_head busEquipmentMaintainRecordHead)
        {
            var exp = Expressionable.Create<bus_equipment_maintain_record_detail>();
            exp.And(x => x.status == (int)EnumMaintainStatus.Maintained);
            exp.And(x => x.projectKeys == busEquipmentMaintainRecordHead.projectKeys);

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

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

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

            return exp.ToExpression();
        }
    }
}