ForkWindingDetectionLogService.cs 5.33 KB
using FreeSql;
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Infrastructure.Json;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.Model.ViewEntity;
using NPOI.POIFS.FileSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace HHECS.Application.Service
{
    public class ForkWindingDetectionLogService : BaseService
    {
        public string Load(ForkWindingDetectionLog entity, PageReq pageReq)
        {
            return Execute(() =>
            {
                using var forkWindingDetection_LogRepository = new ForkWindingDetection_LogRepository();
                var query = forkWindingDetection_LogRepository.Where(GetExpression(entity));

                var total = query.Count();
                var data = query.OrderBy(a => a.EquipmentCode)
                                .OrderByDescending(a => a.TestTime)
                                .Page(pageReq.page, pageReq.limit).ToList();

                Response.Result = data;
                Response.Count = total;
                return Response.ToJson();

            });
        }

        public Expression<Func<ForkWindingDetectionLog, bool>> GetExpression(ForkWindingDetectionLog entity)
        {
            Expression<Func<ForkWindingDetectionLog, bool>> filter = t => true;
            if (!string.IsNullOrWhiteSpace(entity.EquipmentName))
            {
                filter = filter.And(t => t.EquipmentName.Contains(entity.EquipmentName));
            }

            if (!string.IsNullOrWhiteSpace(entity.WarehouseCode))
            {
                filter = filter.And(x => x.WarehouseCode == entity.WarehouseCode);
            }
            return filter;
        }

        public string Ins(ForkWindingDetectionLog entity, User user)
        {
            return Execute(() =>
            {
                entity.CreatedBy = user.UserCode;
                entity.Created = DateTime.Now;

                new ForkWindingDetection_LogRepository().Insert(entity);
                return Response.ToJson();
            });
        }


        public string Upd(ForkWindingDetectionLog entity, User user)
        {
            return Execute(() =>
            {
                entity.UpdatedBy = user.UserCode;
                entity.Updated = DateTime.Now;

                var result = InsertOrUpdate(entity);
                if (!result.Success)
                {
                    Response.ResponseError(result.Msg);
                }
                return Response.ToJson();
            });
        }

        public BllResult<ForkWindingDetectionLog> InsertOrUpdate(ForkWindingDetectionLog entity)
        {
            try
            {
                ForkWindingDetection_LogRepository forkWindingDetection_LogRepository = new ForkWindingDetection_LogRepository();


                entity = forkWindingDetection_LogRepository.InsertOrUpdate(entity);
                return BllResultFactory.Success(entity);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<ForkWindingDetectionLog>($"出现异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        public string DelByIds(int[] ids)
        {
            return Execute(() =>
            {
                using ForkWindingDetection_LogRepository forkWindingDetection_LogRepository = new ForkWindingDetection_LogRepository();
                forkWindingDetection_LogRepository.Delete(t => ids.Contains(t.Id));
                return Response.ToJson();
            });
        }

        public string Export(ForkWindingDetectionLog entity)
        {
            return Execute(() =>
            {
                using ForkWindingDetection_LogRepository forkWindingDetection_LogRepository = new ForkWindingDetection_LogRepository();
                var result = forkWindingDetection_LogRepository.Where(GetExpression(entity))
                                                               .OrderByDescending(a => new { a.EquipmentCode, a.TestTime })
                                                               .ToList();

                Response.Result = result;
                Response.Count = result.Count;
                return Response.ToJson();

            });
        }

        /// <summary>
        /// 仓库查询设备
        /// </summary>
        public dynamic QueryEqByWare(string warehouseCode)
        {
            var response = new Response();
            return Execute(() =>
            {
                EquipmentRepository equipmentRepository = new EquipmentRepository();
                //不显示链条机2、滚筒输送机4、液压升降台5、过渡滚筒机8、称重滚筒输送机(双层)9
                var res = equipmentRepository.Where(x => x.WarehouseCode == warehouseCode && x.EquipmentTypeId != 2 && x.EquipmentTypeId != 4 && x.EquipmentTypeId != 5 && x.EquipmentTypeId != 8 && x.EquipmentTypeId != 9).ToList();
                if (res.Count > 0)
                {
                    response.Result = res;
                    response.Status = true;
                }
                if (!response.Status) response.Message = "通过该仓库编码查询不到设备!请检查";
                return response.ToJson();
            });
        }

    }
}