WaitMOMSendService.cs 3.37 KB
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity.log;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Hh.Mes.Service.WebService.Wo
{
    public class WaitMOMSendService : RepositorySqlSugar<MOMSendErrorLog>
    {
        public Response<List<MOMSendErrorLog>> GetMOMSends(PageReq pageReq, MOMSendErrorLog entity)
        {
            var response = new Response<List<MOMSendErrorLog>>();
            int total = 0;
            var data = Context.Queryable<MOMSendErrorLog>().Where(LinqWhere(entity)).OrderBy(x => x.createTime, OrderByType.Desc).ToPageList(pageReq.page, pageReq.limit, ref total);
            response.Count = data.Count;
            response.Result = data;
            return response;
        }

        public Response AddLog(string code, EnumWaitMOMSend sendType, string ramark = "")
        {
            var response = new Response();
            try
            {
                var codeInfo = string.Empty;
                if (sendType == EnumWaitMOMSend.物料)
                {
                    codeInfo = $"物料编码:{code}";
                }
                else if (sendType == EnumWaitMOMSend.工艺路线)
                {
                    codeInfo = $"产品编码:{code}";
                }

                //物料或工艺路线已经存在,再次重复发送不记录日志
                if (ramark.Contains("已存在,请勿重复发送"))
                {
                    return response.ResponseSuccess();
                }
                var oldLog = Context.Queryable<MOMSendErrorLog>().First(x => x.code == code && x.sendType == sendType);
                if (oldLog is null)
                {
                    var entity = new MOMSendErrorLog
                    {
                        code = code,
                        codeInfo = codeInfo,
                        sendType = sendType,
                        remarks = ramark,
                        createTime = DateTime.Now,
                    };
                    Context.Insertable(entity).ExecuteCommand();
                }
                else
                {
                    oldLog.remarks = ramark;
                    oldLog.createTime = DateTime.Now;
                    Context.Updateable(oldLog).ExecuteCommand();
                }
                return response.ResponseSuccess();
            }
            catch (Exception ex)
            {
                return response.ResponseError(ex.Message);
            }
        }

        public Expression<Func<MOMSendErrorLog, bool>> LinqWhere(MOMSendErrorLog model)
        {
            var exp = Expressionable.Create<MOMSendErrorLog>();
            if (!string.IsNullOrWhiteSpace(model.code))
            {
                exp.And(x => x.code.Contains(model.code));
            }
            if (model.startTime != null)
            {
                exp.And(x => x.createTime >= model.startTime);
            }
            if (model.endTime != null)
            {
                exp.And(x => x.createTime <= model.endTime);
            }
            if (model.queryType != 0)
            {
                exp.And(x => x.sendType == (EnumWaitMOMSend)model.queryType);
            }
            return exp.ToExpression();//拼接表达式
        }
    }
}