UpstreamSendMaterial.cs 5.38 KB
using Hh.Mes.Common.log;
using System;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Response;
using SqlSugar;
using Hh.Mes.POJO.Request;


namespace Hh.Mes.Service.ApiService
{
    public partial class UpstreamService
    {
        public dynamic SendMaterial(MaterialEntity entity)
        {
            var response = new ResponseUpstream<string>(entity.plmeid);
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                if (!typeValidation(entity.type, response).Status) return response;
              
                int resultCount = 0;
                var material = new base_material
                {
                    keys = Guid.NewGuid(),
                    plmeId = entity.plmeid,
                    factoryCode = entity.factory_code,
                    materialCode = entity.mater_code,
                    materialName = entity.mater_name,
                    unitCode = entity.unit_code,
                    mtClassify = entity.mt_classify,
                  
                    mtTypeCode= "material",//默认物料

                    specifications = entity.specifications,
                    diameter=entity.diameter,
                    thickness=entity.thickness,
                    types= entity.types,
                    isDelete =int.Parse(entity.activeFlag),
                    materialType=entity.materialType
                };
                var dictType = "mtClassify";
                var materialSel = base.Context.Queryable<base_material>().Where(x => x.materialCode == material.materialCode && x.isDelete == AddOrUpdateFlag).First();


                if (materialSel == null)
                {
                    entity.type = EnumAction.I.ToString();

                    if (base.Context.Queryable<base_factory>().Any(x => x.otherCode != material.factoryCode))
                        return response.ResponseError($"【上位系统】物料信息工厂编码[{nameof(material.factoryCode)}]“{material.factoryCode}”不存在产线系统,请核实后再发送!");

                    if (base.Context.Queryable<base_material>().Any(x => x.materialCode == material.materialCode && x.isDelete == AddOrUpdateFlag))
                        return response.ResponseError($"【上位系统】物料信息[{nameof(material.materialCode)}]“{material.materialCode}”已存在,请勿重复发送!");
                   
                    var isMtClassify = Context.Queryable<sys_dict_data>().With(SqlWith.NoLock).Any(x => x.dictType == dictType && x.dictValue == material.mtClassify);
                    if (!isMtClassify)
                    {
                        var dicData = new sys_dict_data
                        {
                            headerId = 2199,
                            dictValue = material.mtClassify,
                            dictLabel= entity.mt_classify_name,
                            dictType = dictType,
                            createTime = DateTime.Now,
                            createBy = SystemVariable.DefaultCreatedApi
                        };
                        base.Context.Insertable(dicData).AddQueue();
                    }

                    material.createTime = DateTime.Now;
                    material.createBy = SystemVariable.DefaultCreated;

                    var rate = new base_material_rate
                    {
                        materialCode = material.materialCode,
                        materialName = material.materialName,
                        mtClassify = entity.mt_classify,
                        unitCode = material.unitCode,
                        specifications = material.specifications,
                        materialKeys = material.keys,
                        quantityRate = 1,

                        createTime = DateTime.Now,
                        createBy = SystemVariable.DefaultCreated
                    };
                    base.Context.Insertable(material).AddQueue();
                    base.Context.Insertable(rate).AddQueue();
                    resultCount = base.Context.SaveQueues();
                }
                else
                {

                    entity.type = EnumAction.U.ToString();

                    if (base.Context.Queryable<base_factory>().Any(x => x.otherCode != material.factoryCode))
                        return response.ResponseError($"【上位系统】物料信息工厂编码[{nameof(material.factoryCode)}]“{material.factoryCode}”不存在产线系统,请核实后再发送!");

                    material.updateTime = DateTime.Now;
                    material.updateBy = SystemVariable.DefaultCreated;
                    resultCount = base.Context.Updateable(material)
                                     .IgnoreColumns(it => new { it.keys, it.typesPlcCode, it.createBy, it.createTime })
                                     .Where(x => x.materialCode == material.materialCode && x.isDelete == AddOrUpdateFlag)
                                     .ExecuteCommand();
                }

                //同步物料数据给WCS
                var url = GetDictionaryDictValue("WCSSendMaterial", "GetUrl");
                HttpManVoid(url, entity, EnumLog.同步物料数据给WCS.ToString(), method: "post");

                return resultCount > 0 ? response.ResponseSuccess() : response.ResponseError();
            });
        }
    }
}