using DBUtility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SQLHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
using WebAPI.Code;
using WebAPI.Models;
using WebAPI.Utility;
namespace WebAPI.Controllers
{
public class SRM_MaterialBarCodeBillController : ApiController
{
private JsonResult objJsonResult = new JsonResult();
private json objjson = new json();
public new ClsCN oCn = new ClsCN();
//获取系统参数
Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
// GET: SRM_MaterialBarCodeBill
#region 条码批量生辰
[Route("GetSRMHBarCodeShowBillMain")]
public object GetSRMHBarCodeShowBillMain(string HInterID)
{
try
{
DataSet ds;
string s = "";
string ShowItem = " * ";//显示的字段
//if (DBUtility.ClsPub.isLong(HInterID) == 0)
//{
// objjson.code = "0";
// objjson.count = 0;
// objjson.Message = "获取失败";
// objjson.data = null;
// return objjson;
//}
DAL.ClsWeb_BarCodeBill oClsWeb_BarCodeBill = new DAL.ClsWeb_BarCodeBill();
//string sql = string.Format(@"select * from h_v_Cg_POOrderBillMain_ForWeb2 where HInterID = " + sMsg);
string sql = string.Format(@"select * from h_v_SRM_BarCodeBill where HItemID in ( " + HInterID + ")");
ds = oCn.RunProcReturn(sql, "h_v_SRM_BarCodeBill");
//ds = oClsWeb_BarCodeBill.ShowBillMain1(DBUtility.ClsPub.isLong(sMsg), ShowItem, ref DBUtility.ClsPub.sExeReturnInfo);
if (ds == null || ds.Tables[0].Rows.Count == 0)
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "获取失败";
objjson.data = null;
return objjson;
}
else
{
objjson.code = "0";
objjson.count = 10000;
objjson.Message = "获取成功";
objjson.data = ds.Tables[0];
return objjson;
}
}
catch (Exception e)
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "获取失败" + e.ToString();
objjson.data = null;
return objjson;
}
}
#endregion
#region 条码保存
[Route("SRM_MaterialBarCodeBillController/GetBarcodeSaveBillBoxBycg")]
[HttpPost]
///
/// 生成条码
///
///
///
///
public object GetBarcodeSaveBillBoxBycg([FromBody] JObject msg)
{
string FCusName = oSystemParameter.GetSingleSystemParameter("WMS_CampanyName", ref DBUtility.ClsPub.sExeReturnInfo);
var _value = msg["msg"].ToString();
string msg1 = _value.ToString();
string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
string msg2 = sArray[0].ToString();//
string msg3 = sArray[1].ToString();//
string UserName = sArray[2].ToString();
string OrganizationID = sArray[3].ToString();
ListModels oListModels = new ListModels();
try
{
#region 获取用户以及相关参数
List lsmain = new List();
msg2 = msg2.Replace("\\", "");
msg2 = msg2.Replace("\n", ""); //\n
lsmain = oListModels.getObjectByJson_MaterialHanding(msg2);
int get_BarCodecount = 0;
foreach (Model.Cls_MaterialHandingModel oItem in lsmain)
{
get_BarCodecount++;
//UserName = oItem.HMaker;
}
DAL.ClsWeb_BarCodeBill oBill = new DAL.ClsWeb_BarCodeBill();
msg3 = msg3.Substring(1, msg3.Length - 2);
msg3 = msg3.Replace("\\", "");
msg3 = msg3.Replace("\n", ""); //\n
#endregion
List ls = new List();
ls = oListModels.getObjectByJson_BarCodeBill(msg3);//列表数据model
if (ls.Count > 1)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "每次只允许选择一个物料生成条码,请退出重新选择一行物料,不允许选择多行物料!";
objJsonResult.data = null;
return objJsonResult;
}
#region 数据准备
long linterid = Pub_Class.ClsPub.CreateBillID_SRMProd("8888", ref DBUtility.ClsPub.sExeReturnInfo);
int LSHlen = 6; //流水号长度
int SumLen = 10; //总长度
string TM = ""; //条码
double HSumQty = 0; //产品数量
int LSH = 0; //流水号
string LSH2 = ""; //流水号转换成字符
string sDate = ""; //日期
string sYear = ""; //年
string sPeriod = ""; //月
string sDay = ""; //日
string WeiShu = ""; //尾数
string HKFDQDate= ls[0].HKFDQDate;//到期日期
long HKFPeriod= (ls[0].HKFPeriod.ToString()==""?0: ls[0].HKFPeriod);//保质期
string HKFDate= ls[0].HKFDQDate==null? DateTime.Now.ToString("yyyy/MM/dd"): ls[0].HKFDQDate;//生产日期
string HKFDate_1 = string.Join("", HKFDate.Split('/').Select(x => x.PadLeft(2, '0')));
double HMinQty= ls[0].HMinQty;//标准包装数
string HStatusMan = ls[0].HStatusMan;//工号
//string HBarCodeDate = ls[0].HBarCodeDate;//工号
string HBarCodeDate = ls[0].HBarCodeDate == null ? DateTime.Now.ToString("yyyy/MM/dd") : ls[0].HBarCodeDate;//条码日期
string HBarCodeDate_1 = string.Join("", HBarCodeDate.Split('-').Select(x => x.PadLeft(2, '0')));
double HBQty = ls[0].HBQty;//最小包装数
long HISKFPERIOD= ls[0].HISKFPERIOD;//是否启用保质期
long HBatchManager= ls[0].HBatchManager;//是否启用批号
string HSourceBillType= ls[0].HSourceBillType;//条码类型
string HBatchNo= ls[0].HBatchNo==null?"": ls[0].HBatchNo;//批号
//物料
long HMaterID=ls[0].HMaterID;
string HMaterNumber= ls[0].HMaterNumber;
string HMaterShortNumber = ls[0].HMaterShortNumber;
string HMaterName = ls[0].HMaterName;
//供应商
long HSupID = lsmain[0].HSupID;
string HMaker= ls[0].HMaker;
DateTime HMakeDate= ls[0].HMakeDate;
double HQty= ls[0].HQty; //本次生成条码数量
double biaoqianzhangshu= ls[0].biaoqianzhangshu;
//流水号
DataSet Ds;
SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
oCn.BeginTran();
//Ds = oCn.RunProcReturn("exec GetLSH '" + DateTime.Today + "'", "GetLSH");
Ds = oCn.RunProcReturn("exec h_p_WMS_GetMaxNo '" + HSupID+"_"+ HMaterID+"_" + HBarCodeDate + "'", "GetLSH");
LSH = ClsPub.isInt(Ds.Tables[0].Rows[0][0]);
#endregion
//// 存储生成的条码列表
List generatedBarcodes = new List();
var biaoqianCount = 0;
foreach (Model.ClsGy_MaterialBarCodeBillModel oItemSub in ls)
{
biaoqianzhangshu = ls[biaoqianCount].biaoqianzhangshu;
biaoqianCount++;
//数量为0 跳过
if (oItemSub.HQty == 0 || oItemSub.HMinQty == 0 )
continue;
if(oItemSub.HBatchManager!=0&&oItemSub.HBatchNo=="")
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "已启用批号,批号不能为空,请核对";
objJsonResult.data = null;
return objJsonResult;
}
if (oItemSub.HISKFPERIOD != 0 && oItemSub.HKFPeriod == 0)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "已启用保质期,保质期不能为0,请核对";
objJsonResult.data = null;
return objJsonResult;
}
string sql = "";
if (ClsPub.isLong(oItemSub.HMaterID) != 0)
{
HMaterID = oItemSub.HMaterID;//物料代码
HQty = ClsPub.isDoule(oItemSub.HQty);//本次生成条码数量
HBQty = ClsPub.isInt(oItemSub.HBQty);//最小包装数(内箱数)
HMinQty = ClsPub.isDoule(oItemSub.HMinQty);//标准包装数
int virtualCount = 1;//实际上插入数量
// HSumQty = ClsPub.isDoule(oItemSub.HQty); //产品数量
for (int i = 0; i < biaoqianzhangshu; i++)
{
double currentQty = (i == biaoqianzhangshu - 1 && (HQty % HMinQty != 0))
? (HQty % HMinQty)
: HMinQty;
///供应商名称+@+物料代码+@+物料简称+@+数量+@条码数+@+日期+@+流水号
LSH += 1;
TM = $"{oItemSub.HSupName}@{oItemSub.HMaterNumber}@{oItemSub.HSubjoin}@{currentQty}@{HBarCodeDate_1}@{LSH.ToString("D7")}";
//TM = $"{oItemSub.HSupName}@{oItemSub.HMaterID}@{oItemSub.HMaterName}@{oItemSub.biaoqianzhangshu}@{HKFDate_1}@{LSH}";
// 添加到生成的条码列表
generatedBarcodes.Add(TM);
Model.ClsGy_BarCodeBill_WMS_Model bar = new Model.ClsGy_BarCodeBill_WMS_Model();
// var HBarCode_verify = TM.Split('@'); //校验条码信息
// var HMaterNumber_verify = HBarCode_verify[0]; //校验物料编码
// var HMaterName_verify = HBarCode_verify[1]; //校验物料名称
// //var HMaterModel_verify = HBarCode_verify[2]; //校验规格型号
// var HSupName_verify = HBarCode_verify[7]; //校验供应商
//// var HSourceBillNo_verify = HBarCode_verify[8]; //校验采购订单号
// sql = $"select * from Gy_Material where HNumber = '{HMaterNumber_verify}' and HName = '{HMaterName_verify}'";
// DataSet verifyMatInfo = oCn.RunProcReturn(sql, "tables");
// sql = $"select * from Gy_Supplier where HName = '{HSupName_verify}'";
// DataSet verifySupInfo = oCn.RunProcReturn(sql, "tables");
string HBarCodeType = "唯一条码";
//sql = "insert into Gy_BarCodeBill (HInterID,HBarCode,HBarCodeType,HMaterID,HUnitID,HQty,HKFDate,HKFPeriod,HKFDQDate" +
// ",HBatchNo,HBarcodeQtys,HBarcodeNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HEndQty,HSupflag" +
// ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HMTONO,HProduceDate,HExpiryDate,HISKFPERIOD" +
// ") values ("
// + linterid.ToString() + ",'" + TM + "','" + HBarCodeType + "'," + HMaterID + "," + "" + "," + HQty + ",'" + HKFDate + "'," + HKFPeriod + ",'" + HKFDQDate
// + "','" + HBatchNo + "','" + get_BarCodecount + "','" + virtualCount + "'," + HSupID + "," + "" + ",'" + ClsPub.CurUserName + "','" + DateTime.Today + "'," + "1" + ",'" + DateTime.Today + "',1"
// + "," + "1" + "," + "1" + ",'" + "1" + "','" + HSourceBillType + "','" + "1" + "','"
// + HKFDate + "','" + HKFDQDate + "'," + HISKFPERIOD + ")";
sql = "INSERT INTO Gy_BarCodeBill (HInterID, HBarCode, HBarCodeType, HMaterID, HUnitID, HQty, HKFDate, HKFPeriod, HKFDQDate, " +
"HBatchNo, HBarcodeQtys, HBarcodeNo, HSupID, HGroupID, HMaker, HMakeDate, HPrintQty, HEndQty, HSupflag, " +
"HSourceInterID, HSourceEntryID, HSourceBillNo, HSourceBillType, HMTONO, HProduceDate, HExpiryDate, HISKFPERIOD,HStatusMan,HBarCodeDate,HinitQty,HSTOCKORGID,HOWNERID) " +
"VALUES (" +
linterid.ToString() + ", '" + TM + "', '" + HBarCodeType + "', " + HMaterID + ", " + "0" + ", " + currentQty + ", '" + null + "', " + HKFPeriod + ", '" + null + "', " +
"'" + HBatchNo + "', " + get_BarCodecount + ", '" + virtualCount + "', " + HSupID + ", " + "0" + ", '" + UserName + "',getdate()" + "," + "0" + ", " + "0" + ", 1, " +
"0, 0, '', '" + HSourceBillType + "', '', '" +
null + "', '" + null + "', " + HISKFPERIOD + ",'" + HStatusMan + "','" + HBarCodeDate + "','" + currentQty + "'," + OrganizationID + "," + OrganizationID + "," + null + ")";
LogService.Write("生成条码: " + sql);
oCn.RunProc(sql);
oCn.RunProc("exec h_p_WMS_SetMaxNo '" + HSupID + "_" + HMaterID + "_" + HBarCodeDate + "'");
virtualCount++;
}
}
}
oCn.Commit();
// 返回生成的条码列表
objJsonResult.code = "0";
objJsonResult.count = 1;
objJsonResult.Message = "生成条码成功!";
objJsonResult.data =string.Join("~", generatedBarcodes); // 返回生成的条码列表
WebAPIController.Add_Log("条码下推", UserName, "生成条码");
return objJsonResult;
}
catch (Exception e)
{
LogService.Write("生成条码发生错误: " + e);
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "" + e.Message + DBUtility.ClsPub.sExeReturnInfo;
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
#region 条码列表
[Route("showHBarCodeBillDetail")]
[HttpGet]
public object showHBarCodeBillDetail(string sMsg,string user)
{
try
{
DataSet ds;
string s = "";
string ShowItem = " * ";//显示的字段
if (sMsg == "")
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "获取失败";
objjson.data = null;
return objjson;
}
string sql = string.Format(@"select * from h_v_Gy_UserMaterRelationPrint where HItemID in ( " + sMsg)+")";
ds = oCn.RunProcReturn(sql, "h_v_Gy_UserMaterRelationPrint");
if (ds == null || ds.Tables[0].Rows.Count == 0)
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "获取失败" + DBUtility.ClsPub.sExeReturnInfo;
objjson.data = null;
return objjson;
}
else
{
objjson.code = "0";
objjson.count = 1;
objjson.Message = "获取成功!";
objjson.data = ds.Tables[0];
return objjson;
}
}
catch (Exception e)
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "获取单据表体失败:" + e.Message;
objjson.data = null;
return objjson;
}
}
#endregion
#region 根据条码获取条码详情
[Route("GetBarcodeDetails")]
[HttpGet]
public object GetBarcodeDetails(string barcodes)
{
try
{
if (string.IsNullOrEmpty(barcodes))
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "条码参数不能为空";
objjson.data = null;
return objjson;
}
// 将逗号分隔的条码转换为SQL IN查询条件
var barcodeList = barcodes.Split('~');
// 构建IN子句,为每个条码添加单引号
var inValues = new List();
foreach (var barcode in barcodeList)
{
inValues.Add("'" + barcode.Replace("'", "''") + "'"); // 处理单引号转义
}
string inClause = string.Join(",", inValues);
string sql = $"SELECT * FROM h_v_Gy_UserMaterRelationPrint WHERE HBarCode IN ({inClause})" + " order by 物料代码,cast(流水号 as int)";
// 直接执行SQL查询
DataSet ds = oCn.RunProcReturn(sql, "h_v_Gy_UserMaterRelationPrint");
if (ds == null || ds.Tables[0].Rows.Count == 0)
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "未找到相关条码信息";
objjson.data = null;
return objjson;
}
objjson.code = "0";
objjson.count = ds.Tables[0].Rows.Count;
objjson.Message = "获取成功";
objjson.data = ds.Tables[0];
return objjson;
}
catch (Exception e)
{
objjson.code = "0";
objjson.count = 0;
objjson.Message = "获取条码详情失败:" + e.Message;
objjson.data = null;
return objjson;
}
}
#endregion
}
}