PDAService.cs 13.5 KB
using ApkInfo;
using Hh.Mes.Common;
using Hh.Mes.Common.Json;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Redis;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.Service.Repository;
using Hh.Mes.Service.SystemAuth;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Hh.Mes.Service
{
    public class PDAService : RepositorySqlSugar<sys_user>
    {
        AuthContextFactory authContextFactory;
        public PDAService(AuthContextFactory authContextFactory)
        {
            this.authContextFactory = authContextFactory;
        }

        /// <summary>
        /// PDA物料追溯查询
        /// (扫码查询物料追溯码,查看物料的批次,物料名称,壁厚等物料信息)
        /// </summary>
        /// <param name="barCode"></param>
        /// <returns></returns>
        public dynamic GetMaterialInfoByBarCode(dynamic requestData)
        {
            return ExceptionsHelp.Instance.ExecuteT<dynamic>(() =>
            {
                var reqData = DynamicJson.Parse(requestData.ToString());
                var response = new Response();
                if (reqData == null||!reqData.IsDefined("barCode"))
                {
                    return response.ResponseError($"barCode:参数字段不正确,请核对!");
                }
 
                string barCode = reqData.barCode;
                if (string.IsNullOrEmpty(barCode)) return response.ResponseError($"barCode:值不能为空,请核对!");
                //1:扫码 BarCode 查询bus_workOrder_detail 表确认BarCode是否存在。
                var bwdInfo = Context.Queryable<bus_workOrder_detail>().First(x => x.barCode == barCode);
                if (bwdInfo == null || string.IsNullOrEmpty(bwdInfo.barCode))
                {
                    return response.ResponseError($"barCode:【{barCode}】不存在工序任务明细表中,请核对!");
                }
                if (string.IsNullOrEmpty(bwdInfo.cutMaterCode))
                {
                    return response.ResponseError($"根据barCode:【{barCode}】未查询到工序任务明细表中的物料编码信息,请核对!");
                }
                //2:根据BarCode 查询到当前行的 materialCode,在去物料表查询 返回物料表的基础信息。
                var materialInfo = Context.Queryable<base_material>().First(x => x.materialCode == bwdInfo.cutMaterCode);
                if (materialInfo == null) return response.ResponseError($"未查询到{bwdInfo.cutMaterCode}的物料基础信息,请检查!");

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


        #region 组对、焊接
        /// <summary>
        /// 获取匹配的组对信息
        /// </summary>
        /// <param name="pipe">管段码</param>
        /// <param name="batch">批次码</param>
        /// <returns></returns>
        public dynamic GetMatchTeamList(string pipe, string batch)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                if (string.IsNullOrWhiteSpace(pipe))
                {
                    response.ResponseErr($"管段码【{pipe}】是空,请重新扫描管段码!");
                    return response;
                }
                if (string.IsNullOrWhiteSpace(batch))
                {
                    response.ResponseErr($"批次码【{batch}】是空,请重新扫描批次码!");
                    return response;
                }

                var materialCode = batch;//todo:批次需要调用接口获取物料码

                var resultDetail = Context.Queryable<bus_workOrder_detail>().Where(x => 
                    x.barCode.Equals(pipe) && 
                    x.state == (int)EnumOrderBodyStatus.初始化 && 
                    x.weldMaterCode.Equals(materialCode) &&
                    x.workCenterCode == "Polish").ToList();

                bus_workOrder_head result = null;
                if (resultDetail.Count > 0)
                {
                    result = Context.Queryable<bus_workOrder_head>().Where(x => x.keys == resultDetail[0].headKeys).First();
                    result.teamList = resultDetail;
                }
                response.Result = result;
                return response;
            });
        }

        /// <summary>
        /// 获取组对开始列表
        /// </summary>
        /// <returns></returns>
        public dynamic GetTeamStartList()
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();

                var resultDetail = Context.Queryable<bus_workOrder_detail>().Where(x =>
                    (x.state > (int)EnumOrderBodyStatus.初始化 && x.state < (int)EnumOrderBodyStatus.已完成) &&
                    x.workCenterCode == "Polish").ToList();

                var headKeyList = Context.Queryable<bus_workOrder_detail>().Where(x =>
                    (x.state > (int)EnumOrderBodyStatus.初始化 && x.state < (int)EnumOrderBodyStatus.已完成) &&
                    x.workCenterCode == "Polish").GroupBy(x => x.headKeys).Select(x => x.headKeys).ToList();

                List<bus_workOrder_head> result = new List<bus_workOrder_head>();
                foreach (var key in headKeyList)
                {
                    var item = Context.Queryable<bus_workOrder_head>().Where(x => x.keys == key).First();
                    item.teamList = new List<bus_workOrder_detail>();
                    foreach (var detail in resultDetail)
                    {
                        if(detail.headKeys == key)
                        {
                            item.teamList.Add(detail);
                        }
                    }
                    result.Add(item);
                }
                response.Result = result;
                return response;
            });
        }

        /// <summary>
        /// 保存组对开始列表
        /// </summary>
        /// <returns></returns>
        public dynamic SaveTeamStartList(List<bus_workOrder_detail> details)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();

                foreach(var item in details)
                {
                    var resultItem = Context.Queryable<bus_workOrder_detail>().Where(x => x.bodyKeys == item.bodyKeys).First();
                    if (resultItem != null)
                    {
                        resultItem.stationCode = item.stationCode;
                        resultItem.batchNo = item.batchNo;
                        resultItem.state = (int)EnumOrderBodyStatus.生产中;
                        resultItem.updateBy = sysWebUser?.Account;
                        resultItem.updateTime = DateTime.Now;
                        Context.Updateable(resultItem).ExecuteCommand();
                    }        
                }
                return response;
            });
        }

        /// <summary>
        /// 保存组对结束列表
        /// </summary>
        /// <returns></returns>
        public dynamic SaveTeamEndList(List<bus_workOrder_detail> details)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();

                foreach (var item in details)
                {
                    var resultItem = Context.Queryable<bus_workOrder_detail>().Where(x => x.bodyKeys == item.bodyKeys).First();
                    if (resultItem != null)
                    {
                        resultItem.state = (int)EnumOrderBodyStatus.已完成;
                        resultItem.updateBy = sysWebUser?.Account;
                        resultItem.updateTime = DateTime.Now;
                        Context.Updateable(resultItem).ExecuteCommand();

                        var result = Context.Queryable<bus_workOrder_detail>().Where(x => 
                            x.barCode == resultItem.barCode && 
                            x.weldNo == resultItem.weldNo && 
                            x.workCenterCode == "Weld").First();
                        if (result != null)
                        {
                            result.batchNo = resultItem.batchNo;
                            result.updateBy = sysWebUser?.Account;
                            result.updateTime = DateTime.Now;
                            Context.Updateable(result).ExecuteCommand();
                        }
                    }
                }

                return response;
            });
        }






        /// <summary>
        /// 获取匹配的焊接信息
        /// </summary>
        /// <param name="pipe">管段码</param>
        /// <param name="batch">批次码</param>
        /// <returns></returns>
        public dynamic GetMatchWeldList(string pipe)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                if (string.IsNullOrWhiteSpace(pipe))
                {
                    response.ResponseErr($"管段码【{pipe}】是空,请重新扫描管段码!");
                    return response;
                }

                var resultDetail = Context.Queryable<bus_workOrder_detail>().Where(x =>
                    x.barCode.Equals(pipe) &&
                    x.state == (int)EnumOrderBodyStatus.初始化 &&
                    x.workCenterCode == "Weld" && 
                    !string.IsNullOrEmpty(x.batchNo)).ToList();

                bus_workOrder_head result = null;
                if (resultDetail.Count > 0)
                {
                    result = Context.Queryable<bus_workOrder_head>().Where(x => x.keys == resultDetail[0].headKeys).First();
                    result.teamList = resultDetail;
                }
                response.Result = result;
                return response;
            });
        }

        /// <summary>
        /// 获取焊接开始列表
        /// </summary>
        /// <returns></returns>
        public dynamic GetWeldStartList()
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();

                var resultDetail = Context.Queryable<bus_workOrder_detail>().Where(x =>
                    (x.state > (int)EnumOrderBodyStatus.初始化 && x.state < (int)EnumOrderBodyStatus.已完成) &&
                    x.workCenterCode == "Weld").ToList();

                var headKeyList = Context.Queryable<bus_workOrder_detail>().Where(x =>
                    (x.state > (int)EnumOrderBodyStatus.初始化 && x.state < (int)EnumOrderBodyStatus.已完成) &&
                    x.workCenterCode == "Weld").GroupBy(x => x.headKeys).Select(x => x.headKeys).ToList();

                List<bus_workOrder_head> result = new List<bus_workOrder_head>();
                foreach (var key in headKeyList)
                {
                    var item = Context.Queryable<bus_workOrder_head>().Where(x => x.keys == key).First();
                    item.teamList = new List<bus_workOrder_detail>();
                    foreach (var detail in resultDetail)
                    {
                        if (detail.headKeys == key)
                        {
                            item.teamList.Add(detail);
                        }
                    }
                    result.Add(item);
                }
                response.Result = result;
                return response;
            });
        }

        /// <summary>
        /// 保存焊接开始列表
        /// </summary>
        /// <returns></returns>
        public dynamic SaveWeldStartList(List<bus_workOrder_detail> details)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();

                foreach (var item in details)
                {
                    var resultItem = Context.Queryable<bus_workOrder_detail>().Where(x => x.bodyKeys == item.bodyKeys).First();
                    if (resultItem != null)
                    {
                        resultItem.stationCode = item.stationCode;
                        resultItem.state = (int)EnumOrderBodyStatus.生产中;
                        resultItem.updateBy = sysWebUser?.Account;
                        resultItem.updateTime = DateTime.Now;
                        Context.Updateable(resultItem).ExecuteCommand();
                    }
                }
                return response;
            });
        }

        /// <summary>
        /// 保存焊接结束列表
        /// </summary>
        /// <returns></returns>
        public dynamic SaveWeldEndList(List<bus_workOrder_detail> details)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();

                foreach (var item in details)
                {
                    var resultItem = Context.Queryable<bus_workOrder_detail>().Where(x => x.bodyKeys == item.bodyKeys).First();
                    if (resultItem != null)
                    {
                        resultItem.state = (int)EnumOrderBodyStatus.已完成;
                        resultItem.updateBy = sysWebUser?.Account;
                        resultItem.updateTime = DateTime.Now;
                        Context.Updateable(resultItem).ExecuteCommand();
                    }
                }

                return response;
            });
        }
        #endregion
    }
}