222
schangxiang@126.com
2 天以前 d6284b6874941babb2c9883296594f83bc595239
DEmon/WebWIPAPI/Controllers/MesIntegrController.cs
@@ -1,12 +1,21 @@
using iWareDataCore.BASE.Entity;
using AutoMapper;
using iWareCommon.Common.Entity;
using iWareCommon.Utils;
using iWareDataCore.BASE.Entity;
using iWareDataCore.BASE.Service;
using iWareDataCore.TASK.Service;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web.Hosting;
using System.Web.Mvc;
using WebWIPAPI.Models;
using WebWIPAPI.Properties;
using WebWIPAPI.Utils;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using LogTextHelper = WebWIPAPI.Utils.LogTextHelper;
namespace WebWIPAPI.Controllers
{
@@ -17,35 +26,488 @@
    {
        /// <summary>
        /// 同步立库的物料信息
        /// WCS>>WIP:每天定时WCS推送立库里的物料信息
        /// 提供库存信息
        /// </summary>
        /// <param name="sysCode"></param>
        /// <param name="wareHouseInfo"></param>
        /// <param name="processCardNumber"></param>
        /// <param name="wareHouseId"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult syncMaterialInfo()
        {
            var msg = "";
            var datalist = PlaceMaterialViewService.GetInstance().GetPlacePreview(out msg);
            var responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>()
            var responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>();
            try
            {
                Code = 200,
                Success = true,
                Message = "成功",
                Data = datalist,
            };
                var msg = "";
                var datalist = PlaceMaterialViewService.GetInstance().GetPlacePreview(out msg);
                datalist = datalist.Where(x => !string.IsNullOrEmpty(x.MaterialCode)).ToList();
                if (!string.IsNullOrEmpty(msg))
                {
                    responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "获取异常:" + msg,
                        Data = null,
                    };
                }
                else
                {
                    responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>()
                    {
                        Code = 200,
                        Success = true,
                        Message = "成功",
                        Data = datalist,
                    };
                }
            }
            catch (Exception ex)
            {
                responseMessage = new ApiResponse<List<PlaceMaterialViewEntity>>()
                {
                    Code = 500,
                    Success = false,
                    Message = "异常:" + ex.Message,
                    Data = null,
                };
            }
            //将对象转化为json格式
            var responseStr = JsonConvert.SerializeObject(responseMessage);
            LogTextHelper.WriteLine(Resources.LogDir, "WIPAPI:{0},{1}, {2}", "syncMaterialInfo", "请求参数:" + "", "响应信息;" + responseStr);
            return Json(responseMessage, JsonRequestBehavior.DenyGet);
        }
        /// <summary>
        /// 导入入库单
        /// </summary>
        /// <param name="inputmaterials"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult importInOrder(List<MaterialViewForMesEntity> importMaterials)
        {
            var responseMessage = new ApiResponse<string>();
            var msg = "";
            try
            {
                #region 验证
                if (importMaterials?.Count() == 0)
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "数据条数为0",
                        Data = null,
                    };
                    return ApiResponseHelper.ReturnApiResponse(responseMessage, "importInOrder", "");
                }
                if (importMaterials.Select(x => x.ListNo).Distinct().Count() != 1)
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "本次请求单号必须为同一值",
                        Data = null,
                    };
                    return ApiResponseHelper.ReturnApiResponse(responseMessage, "importInOrder", "");
                }
                if (importMaterials.Select(x => x.Code).Distinct().Count() != 1)
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "本次请求中物料号不能重复",
                        Data = null,
                    };
                    return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
                }
                #endregion
                #region 业务处理
                //第一步:先新增到物料基础表
                List<MaterialViewEntity> materials = new List<MaterialViewEntity>();
                materials = ClassHelper.RotationMapping_Json<List<MaterialViewEntity>, List<MaterialViewForMesEntity>>(importMaterials);
                MaterialViewService.GetInstance().ImportExcelFromMes(materials, out msg);
                if (!string.IsNullOrEmpty(msg))
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "新增到物料基础表异常:" + msg,
                        Data = null,
                    };
                }
                else
                {
                    Thread.Sleep(400);//注意:休眠毫秒,用于数据库事务提交
                    string typeName = "入库";
                    List<InputMaterialEntity> inputmaterials = new List<InputMaterialEntity>();
                    inputmaterials = ClassHelper.RotationMapping_Json<List<InputMaterialEntity>, List<MaterialViewForMesEntity>>(importMaterials);
                    handler_importOutOrder(typeName, inputmaterials, out msg);
                    #endregion
                    if (!string.IsNullOrEmpty(msg))
                    {
                        responseMessage = new ApiResponse<string>()
                        {
                            Code = 500,
                            Success = false,
                            Message = "生成入库任务异常:" + msg,
                            Data = null,
                        };
                    }
                    else
                    {
                        responseMessage = new ApiResponse<string>()
                        {
                            Code = 200,
                            Success = true,
                            Message = "成功",
                            Data = null,
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                responseMessage = new ApiResponse<string>()
                {
                    Code = 500,
                    Success = false,
                    Message = "异常:" + ex.Message,
                    Data = null,
                };
            }
            return ApiResponseHelper.ReturnApiResponse(responseMessage, "importInOrder", "");
        }
        ///// <summary>
        ///// 导入出库单-版本1(使用出库单据的版本)
        ///// </summary>
        ///// <param name="inputmaterials"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public ActionResult importOutOrder(List<InputMaterialEntity> inputmaterials)
        //{
        //    var responseMessage = new ApiResponse<string>();
        //    try
        //    {
        //        var msg = "";
        //        #region 验证
        //        if (inputmaterials?.Count() == 0)
        //        {
        //            responseMessage = new ApiResponse<string>()
        //            {
        //                Code = 500,
        //                Success = false,
        //                Message = "数据条数为0",
        //                Data = null,
        //            };
        //            return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
        //        }
        //        if (inputmaterials.Select(x => x.ListNo).Distinct().Count() != 1)
        //        {
        //            responseMessage = new ApiResponse<string>()
        //            {
        //                Code = 500,
        //                Success = false,
        //                Message = "本次请求单号必须为同一值",
        //                Data = null,
        //            };
        //            return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
        //        }
        //        #endregion
        //        #region 业务处理
        //        string typeName = "出库";
        //        handler_importOutOrder(typeName, inputmaterials, out msg);
        //        #endregion
        //        if (!string.IsNullOrEmpty(msg))
        //        {
        //            responseMessage = new ApiResponse<string>()
        //            {
        //                Code = 500,
        //                Success = false,
        //                Message = "生成出库任务异常:" + msg,
        //                Data = null,
        //            };
        //        }
        //        else
        //        {
        //            responseMessage = new ApiResponse<string>()
        //            {
        //                Code = 200,
        //                Success = true,
        //                Message = "成功",
        //                Data = null,
        //            };
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        responseMessage = new ApiResponse<string>()
        //        {
        //            Code = 500,
        //            Success = false,
        //            Message = "异常:" + ex.Message,
        //            Data = null,
        //        };
        //    }
        //    return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
        //}
        /// <summary>
        /// 导入出库单-版本2(选择库存出库的版本)
        /// </summary>
        /// <param name="inputmaterials"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult importOutOrder(List<InputMaterialEntity> inputmaterials)
        {
            var responseMessage = new ApiResponse<string>();
            try
            {
                var msg = "";
                #region 验证
                if (inputmaterials?.Count() == 0)
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "数据条数为0",
                        Data = null,
                    };
                    return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
                }
                if (inputmaterials.Select(x => x.ListNo).Distinct().Count() != 1)
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "本次请求单号必须为同一值",
                        Data = null,
                    };
                    return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
                }
                if (inputmaterials.Select(x => x.Code).Distinct().Count() != 1)
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "本次请求中物料号不能重复",
                        Data = null,
                    };
                    return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
                }
                #endregion
                #region 业务处理
                string typeName = "出库";
                handler_OutOrder(inputmaterials, out msg);
                //handler_importOutOrder(typeName, inputmaterials, out msg);
                #endregion
                if (!string.IsNullOrEmpty(msg))
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 500,
                        Success = false,
                        Message = "生成出库任务异常:" + msg,
                        Data = null,
                    };
                }
                else
                {
                    responseMessage = new ApiResponse<string>()
                    {
                        Code = 200,
                        Success = true,
                        Message = "成功",
                        Data = null,
                    };
                }
            }
            catch (Exception ex)
            {
                responseMessage = new ApiResponse<string>()
                {
                    Code = 500,
                    Success = false,
                    Message = "异常:" + ex.Message,
                    Data = null,
                };
            }
            return ApiResponseHelper.ReturnApiResponse(responseMessage, "importOutOrder", "");
        }
        /// <summary>
        /// 公共导入 入库单或出库单
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="inputmaterials"></param>
        /// <param name="msg"></param>
        private void handler_importOutOrder(string typeName, List<InputMaterialEntity> inputmaterials, out string msg)
        {
            try
            {
                #region 业务处理
                msg = "";
                //var inputmaterials = InputMaterialService.GetInstance().QueryByParam(new QueryParam { Filter = new Dictionary<string, object> { { "Status", "未生成" } } }, out msg);
                if (inputmaterials != null && inputmaterials.Count > 0)
                {
                    var placeMaterial = PlaceMaterialViewService.GetInstance().QueryByParam(new QueryParam { }, out msg);
                    var inoutdetails = InOutListDetailViewService.GetInstance().QueryByParam(new QueryParam { }, out msg);
                    List<string> codes = new List<string>();
                    inputmaterials.ForEach(x => codes.Add(x.Code));
                    List<int> ids = new List<int>();
                    var materials = InputMaterialService.GetInstance().GetIds(codes);
                    List<InOutListDetailEntity> detail = new List<InOutListDetailEntity>();
                    if (materials?.Count == 0)
                    {
                        msg = ("物料明细查询为空!");
                        return;
                    }
                    for (int i = 0; i < materials.Count; i++)
                    {
                        if (typeName == "入库")
                        {
                            if (placeMaterial.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                msg = materials[i].code + "已存在立库中,不能添加到入库单明细中!";
                                break;
                            }
                            if (inoutdetails.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                msg = materials[i].code + "已存在之前的入库单明细中,不能添加到当前入库单明细中!";
                                break;
                            }
                        }
                        else
                        {
                            if (!placeMaterial.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                msg = materials[i].code + "不在立库中,不能添加到出库单明细中!";
                                break;
                            }
                            if (inoutdetails.Select(x => x.MaterialId).Contains(materials[i].id))
                            {
                                var inoutdeta = inoutdetails.FirstOrDefault(x => x.MaterialId == materials[i].id);
                                if (inoutdeta.TypeName == "出库")
                                {
                                    msg = materials[i].name + "已存在之前的出库单明细中,不能添加到当前出库单明细中!";
                                    break;
                                }
                            }
                        }
                        InOutListDetailEntity inoutdetail = new InOutListDetailEntity()
                        {
                            MaterialId = materials[i].id,
                            IsFinish = 0
                        };
                        detail.Add(inoutdetail);
                    }
                    if (!string.IsNullOrEmpty(msg))
                    {
                        return;
                    }
                    InOutListService.GetInstance().CreateListAndDetial(
                        new InOutListEntity
                        {
                            ListNo = inputmaterials.First().ListNo,
                            CreateTime = DateTime.Now,
                            TypeName = typeName,
                            Status = 0,
                            Remark = "",
                            InOutListDetail = detail
                        }, out msg);
                }
                else
                {
                    msg = ("请先确保出入库单物料明细不为空!");
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 选择库存出库的版本
        /// </summary>
        /// <param name="inputmaterials"></param>
        /// <param name="msg"></param>
        private void handler_OutOrder(List<InputMaterialEntity> inputmaterials, out string msg)
        {
            msg = "";
            //查找库存表中的id
            var qList = inputmaterials.Select(x => x.Code).ToList();
            List<int> ids = PlaceMaterialViewService.GetInstance().GetPlaceMaterialViewsByCodeList(qList, out msg);
            if (!string.IsNullOrEmpty(msg))
            {
                return;
            }
            // List<int> ids = GetSelectedDataKeyIDs(PlaceGrid);
            if (ids.Count > 0)
            {
                int count = MainTaskService.GetInstance().SaveOutMainTask(ids, out msg);
                var retMsg = msg;
                if (count > 0)
                {
                    //成功不需要写文字
                    //msg = "出库任务生成成功:" + retMsg;
                }
                else if (count == 0)
                {
                    msg = "出库任务已在任务队列中:" + retMsg;
                }
                else
                {
                    msg = "出库任务生成失败!:" + retMsg;
                }
            }
            else
            {
                msg = "要出库的物料没有可用库存";
            }
            //Alert.Show(msg);
            //WriteLog("库位物料出库" + msg, "库存管理");
        }
    }
}