WmsSnService.cs 5.31 KB
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity.bus;
using Hh.Mes.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace Hh.Mes.Service
{
    public class WmsSnService : RepositorySqlSugar<bus_sn_project_info_wms_head>
    {
        /// <summary>
        /// Json 接受实体
        /// </summary>
        private WmsSnInfoEntity RequestData { get; set; }


        /// <summary>
        /// 新增标识 true 新增 反之删除
        /// </summary>
        public bool IsInsertType { get; set; }

        /// <summary>
        /// WMS-SN信息推送IOT
        /// </summary>
        public dynamic SendIoTDataBySN(WmsSnInfoEntity entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                RequestData = entity;

                IsInsertType = entity.Types == EnumAction.I.ToString();
                var response = new ResponseWms();
                // 类型验证
                if (!typeValidation(entity.Types, response).status) return response;

                int resultCount = 0;
                var validation = ExecuteQueuesBefore(entity, response);
                if (!validation.status) return validation;

                var listHead = new List<bus_sn_project_info_wms_head>();
                var listDetail = new List<bus_sn_project_info_wms_detail>();

                #region  赋值      
                foreach (var item in entity.Result)
                {
                    var head = CreateHead(item);
                    listHead.Add(head);
                    foreach (var components in item.Components)
                    {
                        var detail = CreateDetail(head, item, components);
                        listDetail.Add(detail);
                    }
                }
                #endregion

                // 执行插入或删除操作
                if (IsInsertType)
                {
                    Context.Insertable(listHead).AddQueue();
                    Context.Insertable(listDetail).AddQueue();
                    resultCount = ExecuteQueues(Context);
                    return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
                }
                return DeleteHandleData(response);
            });
        }

        public ResponseWms ExecuteQueuesBefore(WmsSnInfoEntity entity, ResponseWms response)
        {
            var existingSnList = Context.Queryable<bus_sn_project_info_wms_head>().Select(x => x.sn).ToList();
            foreach (var item in entity.Result)
            {
                if (string.IsNullOrEmpty(item.Sn)) return response.ResponseError($"【WMS系统】序列号为空【Sn】,请核对数据!");
                if (IsInsertType && existingSnList.Contains(item.Sn))
                    return response.ResponseError($"WMS系统】序列号已存在IOT系统【{item.Sn}】,请不要重复发送数据!");
            }
            return response;
        }

        /// <summary>
        /// type is I or D value
        /// </summary>
        public ResponseWms typeValidation(string type, ResponseWms response)
        {
            if ("ID".IndexOf(type) == -1) return response.ResponseError($"【WMS系统】【type 只能是I 或者D标识】,请核实确认!");
            return response;
        }

        /// <summary>
        /// 删除
        /// </summary>
        public ResponseWms DeleteHandleData(ResponseWms response)
        {
            foreach (var item in RequestData.Result)
            {
                var head = Context.Queryable<bus_sn_project_info_wms_head>().First(t => t.sn == item.Sn);
                if (head == null) return response.ResponseError($"WMS系统】删除失败,序列号不已存在IOT系统【{item.Sn}】请核实确认!");
                Context.Deleteable<bus_sn_project_info_wms_head>().Where(x => x.keys == head.keys).AddQueue();
                Context.Deleteable<bus_sn_project_info_wms_detail>().Where(x => x.headKey == head.keys).AddQueue();
            }

            int resultCount = ExecuteQueues(Context);
            return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
        }


        private bus_sn_project_info_wms_head CreateHead(Result item)
        {
            return new bus_sn_project_info_wms_head
            {
                keys = Guid.NewGuid(),
                sn = item.Sn,
                correlatedCode = item.CorrelatedCode,
                equipmentType = item.EquipmentType,
                userBy = item.User,

                createBy = SystemVariable.DefaultCreated,
                createTime = DateTime.Now
            };
        }

        private bus_sn_project_info_wms_detail CreateDetail(bus_sn_project_info_wms_head head, Result item, Components component)
        {
            return new bus_sn_project_info_wms_detail
            {
                headKey = head.keys,
                sn = item.Sn,
                correlatedCode = item.CorrelatedCode,
                userBy = item.User,
                partCode = component.PartCode,

                createBy = SystemVariable.DefaultCreated,
                createTime = DateTime.Now
            };
        }
    }
}