using DBUtility;
using Model;
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 WebAPI;
using WebAPI.Code;
using WebAPI.Models;
namespace WebAPI.Controllers
{
    public class POStockInBackBillController : ApiController
    {
        public Int64 HSupID;//供应商ID
        public Int64 HDeptID;//部门ID
        public Int64 HWhID;//仓库ID
        public Int64 HSPID;//仓位ID
        public string HSupName=string.Empty;
        public string HDeptName=string.Empty;
        public string HWhName=string.Empty;
        public string HSPName=string.Empty;
        public Int64 HInterID;//本单ID
        public string HBillNo;
        public string HSourceBillNo=string.Empty;
        public string HBillType = "1239";//单据类型
        public string sBillCode = "ICStockBill";
        public string sTranType = "1";
        public bool sRedBlueFlag = false;
        public bool SourceFlag = false;
        //public string sSourceType = "1103";
        public string sSourceTypeName = "采购订单";
        public string sRedBlue2 = "红字";
        //public bool sRedBlue = false;
        public WebServer webserver =new WebServer();
        public DataSet ds =new DataSet();
        public ClsGy_BarCodeBill_WMS_Model_View oView =new ClsGy_BarCodeBill_WMS_Model_View();
        public ClsKF_PonderationBillMain_Temp model =new ClsKF_PonderationBillMain_Temp();
        public ClsKf_ICStockBill_WMS oBar = new Model.ClsKf_ICStockBill_WMS();
        public double sRelQty = 0;
       // private DataBaseServices objDataBaseServices = new DataBaseServices();
        private JsonResult objJsonResult = new JsonResult();
       // private string msgModel = "{{\"count\":{0},\"code\":{0},\"message\":\"{1}\",\"result\":{2}}}";
        public static string GetSession(string sMsg)
        {
            try
            {
                string s = Common.GetSession();
                return s;
            }
            catch (Exception e)
            {
                return "";
            }
        }
        /// 
        /// 一键扫码红字
        /// 
        /// 
        [Route("POStockInBackBill/get_CheckTypeByBarCode_All_Json")]
        [HttpGet]
        public Object get_CheckTypeByBarCode_All_Json(string sCode, Int64 sInterID, string HBillType, string sBillNo, string sMaker, Int64 WhID, Int64 SPID, Double sQty, bool sRedBlue, bool SourceFlag, string sSourceBillNo, string sSourceType, Int64 HOWNERID, string sExpressNumber)
        {
            if (sRedBlue == true)
            {
                HBillType = "1239";
            }
            oBar = webserver.get_CheckTypeByBarCode_All(sCode, sInterID, HBillType, sBillNo, sMaker, WhID, SPID, sQty, sRedBlue, SourceFlag, sSourceBillNo, sSourceType, HOWNERID,sExpressNumber, ref DBUtility.ClsPub.sErrInfo);
            if (oBar == null)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = DBUtility.ClsPub.sErrInfo;
                objJsonResult.data = null;
                return objJsonResult;
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = DBUtility.ClsPub.sErrInfo;
                objJsonResult.data = oBar;
                return objJsonResult;
            }
        }
        /// 
        /// 根据源单类型及源单号 获取 相关信息
        /// 
        /// 
        /// 
        [Route("POStockInBackBill/get_GetCg_POOrderBackBillList")]
        [HttpGet]
        public object get_GetCg_POOrderBackBillList(string HSourceBillType, string HSourceBillNo, Int64 sInterID, string sBillNo)
        {
            try
            {
                string sWhere = " Where 单据号 like '%" + HSourceBillNo.Trim() + "'";
                //根据源单类型来调用不同的WEBS方法 new 
                if (HSourceBillType == "1102")
                {
                    ds = webserver.GetCg_POOrderBackBillList(sWhere + "  ");
                }
                else if (HSourceBillType == "1105")
                {
                    ds = webserver.GetCg_POInStockBackBillList(sWhere + "  and isnull(HBillSubType,0)=12510 ");
                }
                else if (HSourceBillType == "1201")
                {
                    ds = webserver.GetKf_POStockInBillList(sWhere + "  ");
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有返回任何记录,源单类型有错误!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
                //
                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有返回任何记录!" + HSourceBillType;
                    objJsonResult.data = null;
                    return objJsonResult;
                    //DBUtility.ClsPub.MessageBeep((int)DBUtility.ClsPub.BeepType.Warning);
                }
                else
                {
                    
                    objJsonResult.code = "0";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "返回记录成功!";
                    objJsonResult.data = ds.Tables[0];
                    return objJsonResult;
                    
                }
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// 
        /// 写入临时表  扫源单的方法
        /// 
        /// 
        /// 
        [Route("POStockInBackBill/set_SavePonderationBillMain_Temp_Source_Fast_Json")]
        [HttpGet]
        public object set_SavePonderationBillMain_Temp_Source_Fast_Json(string HSourceBillType,string HSourceBillNo, Int64 sInterID, string sBillNo)
        {
            try
            {
               // DataSet ds = new DataSet();
               // WebServer webserver = new WebServer();
                string sWhere = " Where 单据号 like '%" + HSourceBillNo.Trim() + "'";
                //根据源单类型来调用不同的WEBS方法 new 
                if (HSourceBillType == "1102")
                {
                    ds = webserver.GetCg_POOrderBackBillList(sWhere + "  ");
                }
                else if (HSourceBillType == "1105")
                {
                    ds = webserver.GetCg_POInStockBackBillList(sWhere + "  and isnull(HBillSubType,0)=12510 ");
                }
                else if (HSourceBillType == "1201")
                {
                    ds = webserver.GetKf_POStockInBillList(sWhere + "  ");
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有返回任何记录,源单类型有错误!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
                //
                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有返回任何记录!" + HSourceBillType;
                    objJsonResult.data = null;
                    return objJsonResult;
                    //DBUtility.ClsPub.MessageBeep((int)DBUtility.ClsPub.BeepType.Warning);
                }
                else
                {
                    HSupID = DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["HSupID"]);
                    HSupName = Convert.ToString(ds.Tables[0].Rows[0]["HSupName"]);
                    HDeptID = DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["HDeptID"]);
                    HDeptName = Convert.ToString(ds.Tables[0].Rows[0]["HDeptName"]);
                    if (webserver.set_SavePonderationBillMain_Temp_Source_Fast(sInterID, sBillNo, HBillType, HSourceBillNo, HSourceBillType, "红字", ref DBUtility.ClsPub.sErrInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "返回记录成功!";
                        objJsonResult.data = ds.Tables[0];
                        return objJsonResult;
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "写入临时表失败!" + DBUtility.ClsPub.sErrInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
            }
            catch (Exception ex)
            {
                
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!"+ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// 
        /// 返回出入库条码临时表     
        /// 
        /// 
        [Route("POStockInBackBill/DisBillEntryList_Webs_Json")]
        [HttpGet]
        public object DisBillEntryList_Webs_Json(long HBillID, string HBillType, string sWhere)
        {
            try
            {
                ds = webserver.GetKf_PonderationBillMain_Temp(HBillID, HBillType, sWhere);
                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有返回任何记录!";
                    objJsonResult.data = null;
                    return objJsonResult;
                    //DBUtility.ClsPub.MessageBeep((int)DBUtility.ClsPub.BeepType.Warning);
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "获取信息成功!";
                    objJsonResult.data = ds.Tables[0];
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有返回任何记录!"+e.ToString();
                    objJsonResult.data = null;
                    return objJsonResult;
            }
        }
        /// 
        /// 扫码入库时获取仓库仓位信息
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        //[Route("POStockInBill/Get_CheckWhAndSP_Josn")]
        //[HttpGet]
        //public object Get_CheckWhAndSP_Josn(Int64 sHInterID, string sHBillType, string sBarCode, Int64 sHWHID, Int64 sHSPID)
        //{
        //    long WhID = sHWHID;
        //    long SPID = sHSPID;
        //    if (webserver.Get_CheckWhAndSP(sHInterID, sHBillType, sBarCode, sHWHID, SPID, ref WhID, ref SPID, ref DBUtility.ClsPub.sErrInfo))
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 1;
        //        objJsonResult.Message ="";
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //    else
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 0;
        //        objJsonResult.Message = "失败!"+DBUtility.ClsPub.sErrInfo;
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //}
        /// 
        /// //根据物料条码得到对应源单信息并将源单信息新增到条码出入库临时表,带出表头部门、供应商
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        //[Route("POStockInBill/set_SavePonderationBillMain_Temp_Source_SourceBillType_Json")]
        //[HttpGet]
        //public object set_SavePonderationBillMain_Temp_Source_SourceBillType_Json(string sBarCode, Int64 sInterID, string sBillNo, Double sQty)
        //{
        //    if (SourceFlag)
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 0;
        //        objJsonResult.Message = "获取失败+";
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //    else
        //    {
        //        if (webserver.set_SavePonderationBillMain_Temp_Source_SourceBillType(sInterID, sBillNo, HBillType, sBarCode, sRedBlue2, ref HSourceBillNo, ref sSourceTypeName, ref HSupID, ref  HSupName, ref HDeptID, ref HDeptName, ref DBUtility.ClsPub.sErrInfo))
        //        {
        //            //----------创建虚表------------------------
        //            DataTable dt_Main = new DataTable("Json");
        //            dt_Main.Columns.Add("HSourceBillNo", typeof(string));
        //            dt_Main.Columns.Add("sSourceTypeName", typeof(string));
        //            dt_Main.Columns.Add("HSupName", typeof(string));
        //            dt_Main.Columns.Add("HSupID", typeof(int));
        //            dt_Main.Columns.Add("HDeptName", typeof(string));
        //            dt_Main.Columns.Add("HDeptID", typeof(int));
        //            dt_Main.Columns.Add("SourceFlag", typeof(bool));
        //            //---------创建新行------------------------
        //            DataRow dr_main = dt_Main.NewRow();//创建新行 
        //            dt_Main.Rows.Add(dr_main);//将新行加入到表中
        //            dr_main["HSourceBillNo"] = DBUtility.ClsPub.isStrNull(HSourceBillNo);
        //            dr_main["sSourceTypeName"] = DBUtility.ClsPub.isLong(sSourceTypeName);
        //            dr_main["HSupName"] = DBUtility.ClsPub.isStrNull(HSupName);
        //            dr_main["HSupID"] = DBUtility.ClsPub.isLong(HSupID);
        //            dr_main["HDeptName"] = DBUtility.ClsPub.isStrNull(HDeptName);
        //            dr_main["HDeptID"] = DBUtility.ClsPub.isLong(HDeptID);
        //            dr_main["SourceFlag"] = DBUtility.ClsPub.isBool(true);
        //            if (dt_Main == null || dt_Main.Rows.Count <= 0)
        //            {
        //                objJsonResult.code = "0";
        //                objJsonResult.count = 0;
        //                objJsonResult.Message = "获取失败" + DBUtility.ClsPub.sErrInfo;
        //                objJsonResult.data = null;
        //                return objJsonResult;
        //            }
        //            else
        //            {
        //                objJsonResult.code = "0";
        //                objJsonResult.count = 1;
        //                objJsonResult.Message = "获取成功";
        //                objJsonResult.data = dt_Main;
        //                return objJsonResult;
        //            }
        //        }
        //        else
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = "获取失败" + DBUtility.ClsPub.sErrInfo;
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //    }
        //}
        /// 
        /// 根据条码得到相应信息(条码档案) 扫条码的方法
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        //[Route("POStockInBill/get_InfoByBarCode_Source_Json")]
        //[HttpGet]
        //public object get_InfoByBarCode_Source_Json(string sBarCode, Int64 sInterID, string sBillNo, Double sQty, Int64 HWHID, Int64 HSPID)
        //{
        //   // string sRedBlue = "蓝字";
        //    try
        //    {
        //        string sErrMsg = "";
        //        bool sBool = false;
        //        oView = webserver.get_InfoByBarCode_Source(sBarCode, sInterID, ref sBool, ref sErrMsg);
        //        model.HInterID = DBUtility.ClsPub.isLong(sInterID);
        //        model.HBillNo = DBUtility.ClsPub.isStrNull(sBillNo);
        //        model.HBillType = this.HBillType;
        //        model.HMaker = "";
        //        //
        //        model.HMaterID = oView.HMaterID;
        //        model.HAuxPropID = oView.HAuxPropID;
        //        model.HErpClsID = oView.HErpClsID;
        //        model.HQty = oView.HQty;
        //        model.HQtyMust = oView.HinitQty;
        //        model.HBarCode = oView.HBarCode;
        //        model.HBatchNo = oView.HBatchNo;
        //        model.HMTONo = oView.HMTONo;
        //        model.HWhID = HWHID;
        //        model.HStockPlaceID = HSPID;
        //        model.HSourceInterID = oView.HSourceInterID;
        //        model.HSourceEntryID = oView.HSourceEntryID;
        //        model.HSourceBillNo = oView.HSourceBillNo;
        //        model.HSourceBillType = oView.HSourceBillType;
        //        model.HRedBlueFlag = false;
        //        model.HPieceQty = 1;
        //        model.HSTOCKORGID = 0;
        //        model.HOWNERID = 0;
        //        if (webserver.set_SavePonderationBillMain_Temp_Qty(model, sQty, ref DBUtility.ClsPub.sErrInfo))
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 1;
        //            objJsonResult.Message = "扫码成功!";
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //        else
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = "扫码失败!" + DBUtility.ClsPub.sErrInfo;
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //    }
        //    catch (Exception e)
        //    {
                
        //         objJsonResult.code = "0";
        //         objJsonResult.count = 0;
        //         objJsonResult.Message = "扫码失败,此条码不存在!"+e.ToString();
        //         objJsonResult.data = null;
        //         return objJsonResult;
        //    }
        //}
        /// 
        /// 生成外购入库单(红字)
        /// 
        /// 
        [Route("POStockInBackBill/set_SavePOStockInBackBill_Json")]
        [HttpPost]
        public object set_SavePOStockInBackBill_Json([FromBody]JObject oMain)
        {
            var _value = oMain["oMain"].ToString();
            string msg1 = _value.ToString();
            //List lsmain = new List();
            //ListModels oListModels = new ListModels();
            //lsmain = oListModels.getICStockBillMainByJson(msg1);
            //lsmain[0].HYear = DBUtility.ClsPub.isLong(DateTime.Now.Year);
            //lsmain[0].HDate = DBUtility.ClsPub.isDate(DateTime.Now.ToString("yyyy-MM-dd"));
            //string sSourceBillType = lsmain[0].HBillType;
            //BLL.ClsKf_POStockInBackBill bll = new BLL.ClsKf_POStockInBackBill();
            //return bll.set_SavePOStockInBackBill_New(lsmain[0], sSourceBillType, ref DBUtility.ClsPub.sErrInfo);
            try
            {
                List lsmain = new List();
                ListModels oListModels = new ListModels();
                lsmain = oListModels.getICStockBillMainByJson(msg1);
                lsmain[0].HYear = DBUtility.ClsPub.isLong(DateTime.Now.Year);
                lsmain[0].HDate = DBUtility.ClsPub.isDate(DateTime.Now.ToString("yyyy-MM-dd"));
                string sSourceBillType = lsmain[0].HBillType;
                WebAPI.WebS.ClsKf_ICStockBillMain websLsmain = new WebS.ClsKf_ICStockBillMain();
                websLsmain.HInterID = lsmain[0].HInterID;
                websLsmain.HDate = lsmain[0].HDate;
                websLsmain.HBillNo = lsmain[0].HBillNo;
                websLsmain.HDeptID = lsmain[0].HDeptID;
                websLsmain.HEmpID = lsmain[0].HEmpID;
                websLsmain.HRemark = lsmain[0].HRemark;
                websLsmain.HMaker = lsmain[0].HMaker;
                websLsmain.HBillType = "1239";
                if (webserver.set_SavePOStockInBackBill_New(websLsmain, sSourceBillType, ref DBUtility.ClsPub.sErrInfo))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 1;
                    objJsonResult.Message = DBUtility.ClsPub.sErrInfo;  //成功!
                    //objJsonResult.Message = "生成外购退料单成功!单据号为:";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = DBUtility.ClsPub.sErrInfo;  //失败!
                    //objJsonResult.Message = "上传失败!" + DBUtility.ClsPub.sErrInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "上传失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        
        //[Route("POStockInBill/DisBillEntryList_Mate_Webs_Json")]
        //[HttpGet]
        //public object DisBillEntryList_Mate_Webs_Json(long HInterID, string sBillType, string sWhere)
        //{
        //    try
        //    {
        //        ds = webserver.GetKf_PonderationBillMain_Temp(HInterID, sBillType, sWhere);
        //        if (ds == null || ds.Tables[0].Rows.Count <= 0)
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = "没有返回任何记录!";
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //            //DBUtility.ClsPub.MessageBeep((int)DBUtility.ClsPub.BeepType.Warning);
        //        }
        //        else
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 1;
        //            objJsonResult.Message = "获取信息成功!";
        //            objJsonResult.data = ds.Tables[0];
        //            return objJsonResult;
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 0;
        //        objJsonResult.Message = "没有返回任何记录!" + e.ToString();
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //}
        //[Route("POStockInBillList/DisBillEntryList_Mate_Webs_Json")]
        //[HttpGet]
        //public object DisBillEntryList_Webs_Json(string HBillType,string sWhere)
        //{
        //    try
        //    {
        //        ds = webserver.GetKf_PonderationBillMain_TempList(HBillType, sWhere);
        //        if (ds == null || ds.Tables[0].Rows.Count <= 0)
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = "没有返回任何记录!";
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //            //DBUtility.ClsPub.MessageBeep((int)DBUtility.ClsPub.BeepType.Warning);
        //        }
        //        else
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 1;
        //            objJsonResult.Message = "获取信息成功!";
        //            objJsonResult.data = ds.Tables[0];
        //            return objJsonResult;
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 0;
        //        objJsonResult.Message = "没有返回任何记录!" + e.ToString();
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //}
        //[Route("POStockInBillList/Delete_Json")]
        //[HttpGet]
        //public object Delete_Json(long sHInterID)
        //{
        //    string sErrMsg = string.Empty;
        //    try
        //    {
        //        if (webserver.DeleteBillList(sHInterID, ref sErrMsg))
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 1;
        //            objJsonResult.Message = "删除成功!";
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //        else
        //        {
        //            objJsonResult.code = "0";
        //            objJsonResult.count = 0;
        //            objJsonResult.Message = "删除失败!";
        //            objJsonResult.data = null;
        //            return objJsonResult;
        //        }
        //    }
        //    catch (Exception)
        //    {
        //        objJsonResult.code = "0";
        //        objJsonResult.count = 0;
        //        objJsonResult.Message = "删除失败!";
        //        objJsonResult.data = null;
        //        return objJsonResult;
        //    }
        //}
    }
}