MaterialUpCict.cs 8.31 KB
using Hh.Mes.Common.config;
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 Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Newtonsoft.Json;
using NPOI.POIFS.FileSystem;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Hh.Mes.Service.ApiService
{
    public partial class UpstreamService
    {
        /// <summary>
        /// 设备状态查询
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public dynamic GetEquipmentType(string code)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT<Response>(() =>
            {
                if (string.IsNullOrWhiteSpace(code))
                {
                    return response.ResponseError($"设备编码为空!");
                }

                var equipment = base.Context.Queryable<base_equipment>().Where(x => x.code == code).First();
                if (equipment == null)
                {
                    return response.ResponseError($"没有此设备!");
                }

                var equipmentinformationList = base.Context.Queryable<EquipmentinformationView>().Where(x => x.equipmentId == equipment.id).ToList();

                response.Result = equipmentinformationList;
                return response;
            });
        }

        /// <summary>
        /// 信科物料基础信息 同步
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public dynamic MaterialUpCict()
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT<Response>(() =>
            {
                var urlDic = "UrlMaterialCict" + ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend1;//AppCustomExtend1:"_onLine/_off"
                var url = GetDictionaryDictValue(urlDic, "GetUrl");

                response = HttpMan(url, null, EnumLog.Cict物料信息同步.ToString(), method: "get");
                if (response.Code == 200)
                {
                    //do someing 
                    var cictResult = JsonConvert.DeserializeObject<MaterialUpCictEntity>(response.Result);
                    Response res = HandleData(cictResult.data.list);
                    if (res.Code != 200)
                    {
                        return response.ResponseError($"【信科】物料基础信息有错误:{response.Message}");
                    }
                    response.Message = SystemVariable.dataActionOk + $"</br>在{DateTime.Now}收到【信科】物料基础信息数据共{cictResult.data.total}条,成功同步{res.Count}条";
                    response.Result = null;
                    response.Count = res.Count;
                    return response;
                }
                //Log4NetHelper.Instance.Error($"【返回结果】" + response.Result);
                response.Result = null;
                return response.ResponseError($"【信科】物料基础信息同步错误,原因:{response.Message}");
            });
        }

        /// <summary>
        /// 同步物料逻辑 存在就跳过,不存新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private Response HandleData(List<ListItem> list)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                int updatedCount = 0;
                foreach (var model in list)
                {

                    var isExist = Context.Queryable<base_material>().Any(i => i.materialCode == model.sapCode);
                    if (!isExist)
                    {

                        var unit = Context.Queryable<base_material_unit>().Any(i => i.code == model.materialUnit);//存在单位名称,要取表里code值,后续展示用,展示区分大小写
                        var unitCode = unit ? Context.Queryable<base_material_unit>().Where(i => i.code == model.materialUnit).First().code : model.materialUnit;
                        var keys = Guid.NewGuid();
                        var material = new base_material
                        {
                            keys = keys,
                            materialCode = model.sapCode,
                            materialName = model.materialName,
                            unitCode = unitCode,
                            mtClassify = model.categoryCode,
                            mtTypeCode = model.materialType.ToString(),
                            specifications = model.spec,
                            createBy = model.createByName,
                            createTime = DateTime.Now,
                        };
                        var rate = new base_material_rate
                        {
                            materialCode = model.sapCode,
                            materialName = model.materialName,
                            mtClassify = model.categoryCode,
                            unitCode = unitCode,
                            specifications = model.spec,
                            materialKeys = keys,
                            quantityRate = 1,
                            createBy = model.createByName,
                            createTime = DateTime.Now,
                        };

                        var bmu = new base_material_unit();//物料单位(不存在的单位新增,统一大写)
                        var records = Context.Queryable<base_material_unit>().Where(t => true).ToList();
                        if (!records.Any(t => t.code.ToUpper() == model.materialUnit.ToUpper()))
                        {
                            bmu.code = model.materialUnit;
                            bmu.name = "unit_" + model.materialUnit;
                            bmu.createBy = model.createByName;
                            bmu.createTime = DateTime.Now;
                            Context.Insertable(bmu).AddQueue();
                        }

                        var bmt = new base_material_type();//物料类别
                        var bootKeys = Context.Queryable<base_material_type>().Where(i => i.mtTypeCode == "boot").First().keys;//根节点key
                        if (!Context.Queryable<base_material_type>().Any(i => i.mtTypeCode == model.materialType.ToString()))
                        {
                            bmt.keys = Guid.NewGuid();
                            bmt.mtTypeCode = model.materialType.ToString();
                            bmt.mtTypeName = "material_" + model.materialType.ToString();
                            bmt.parentTypeId = bootKeys;
                            bmt.isDelete = 1;
                            bmt.createBy = model.createByName;
                            bmt.createTime = DateTime.Now;
                            Context.Insertable(bmt).AddQueue();
                        }

                        var classify = new sys_dict_data();//物料分类
                        if (!Context.Queryable<sys_dict_data>().Any(i => i.dictValue == model.categoryCode && i.dictType == "mtClassify"))
                        {
                            classify.headerId = 2199;
                            classify.dictType = "mtClassify";
                            classify.dictValue = model.categoryCode;
                            classify.dictLabel = "label_" + model.categoryCode;
                            classify.createBy = model.createByName;
                            classify.createTime = DateTime.Now;
                            Context.Insertable(classify).AddQueue();
                        }

                        Context.Insertable(material).AddQueue();
                        Context.Insertable(rate).AddQueue();
                        Context.SaveQueues();
                        updatedCount++;
                    }
                    response.Count = updatedCount;
                    //else
                    //{
                    //    response.Status = Context.Updateable(model).Where(u => u.id == model.id).ExecuteCommand() > 0;
                    //    if (!response.Status) response.Message = SystemVariable.dataActionError;
                    //}
                }
                return response;
            });
        }
    }
}