EquipmentProcessTemplateService.cs 4.64 KB
using Hh.Mes.Common.Infrastructure;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.WebEntity.bus;
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;

namespace Hh.Mes.Service.Wo
{
    public class EquipmentProcessTemplateService : RepositorySqlSugar<bus_equipment_process_template>
    {
        public dynamic Load(PageReq pageReq, bus_equipment_process_template entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response<List<bus_equipment_process_template>>();
                var expression = LinqWhere(entity);
                //先组合查询表达式
                var query = Context.Queryable<bus_equipment_process_template>().Where(expression);
                //Exel为ture就不分页,因为导出的话是全部导出
                if (pageReq != null)
                {
                    int total = 0;
                    result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count();
                }
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic Ins(bus_equipment_process_template entity, string user)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                entity.device_code = entity.device_information;
                entity.keys = Guid.NewGuid();
                entity.createBy = user;
                entity.createTime = DateTime.Now.ToLocalTime();
                Context.Insertable(entity).ExecuteCommand();
                return response;
            });
        }

        public dynamic Upd(bus_equipment_process_template entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                var result = Context.Updateable(entity).ExecuteCommand();
                if (result <= 0)
                {
                    return response.ResponseError(SystemVariable.dataActionError);
                }
                return response;
            });
        }

        public dynamic DelByIds(int[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                var result = Context.Deleteable<bus_equipment_process_template>(t => ids.Contains(t.id)).ExecuteCommand();
                if (result <= 0)
                {
                    return response.ResponseError(SystemVariable.dataActionError);
                }
                return response;
            });
        }

        public Response ExportData(bus_equipment_process_template entity)
        {
            return Load(null, entity);
        }

        public Expression<Func<bus_equipment_process_template, bool>> LinqWhere(bus_equipment_process_template model)
        {
            try
            {
                var exp = Expressionable.Create<bus_equipment_process_template>();
                //数据过滤条件
                //if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
                if (!string.IsNullOrWhiteSpace(model.device_information))
                {
                    exp.And(x => x.device_information.Contains(model.device_information));
                }
                if (!string.IsNullOrWhiteSpace(model.processing_steps))
                {
                    exp.And(x => x.processing_steps.Contains(model.processing_steps));
                }
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }

        public Dictionary<string, string> LoadEquipmentItems()
        {
            var result = new Dictionary<string, string>();
            try
            {
                result = Context.Queryable<base_equipment>().Where(x => !x.name.Contains("心跳")).Select(x => new { x.code, x.name }).Distinct().ToList().ToDictionary(k => k.code, v => v.name);
            }
            catch (Exception ex)
            {
                Log4NetHelper.Instance.Error($"【{nameof(EquipmentProcessTemplateService)}_LoadEquipmentItems】" + ex.Message);
            }
            return result;
        }
    }
}