using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Http;
using WebAPI.Models;
using Pub_Class;
namespace WebAPI.Controllers
{
    /**
     * 找货单单
     */
    public class Sc_MouldOtherInBillController : ApiController
    {
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        public WebServer webserver = new WebServer();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        /// 
        /// 根据包装单 返回此包装单内的相关信息
        /// 
        /// 
        /// 
        /// 模具其他入库单列表
        /// 
        /// 
        [Route("Sc_MouldOtherInBill/GetMouldOtherInBillList")]
        [HttpGet]
        public object GetMouldOtherInBillList(string sWhere)
        {
            try
            {
                ds = Sc_MouldOtherInBillList_s(sWhere);
                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有返回任何记录!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "1";
                    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;
            }
        }
        #region sql语句
        public static DataSet Sc_MouldOtherInBillList_s(string sWhere)
        {
            return new SQLHelper.ClsCN().RunProcReturn("select * from h_v_Sc_MouldOtherInBillList ", "h_v_Sc_MouldOtherInBillList");
        }
        #endregion
        /// 
        /// 修改单据-保存按钮
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("Sc_MouldOtherInBill/AddBill1")]
        [HttpPost]
        public object AddBill1([FromBody] JObject oMain)
        {
            try
            {
                DAL.ClsSc_MouldOtherInBill oMould = new DAL.ClsSc_MouldOtherInBill();
                var _value = oMain["oMain"].ToString();
                string msg1 = _value.ToString();
                string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                string msg2 = sArray[0].ToString();
                string msg3 = sArray[1].ToString();
                string msg4 = sArray[2].ToString();
                string msg5 = sArray[3].ToString();
                //反序列化
                msg2 = "[" + msg2.ToString() + "]";
                List list = Newtonsoft.Json.JsonConvert.DeserializeObject>(msg2);
                long HItemID = list[0].HInterID;
                string HBillNo = list[0].HBillNo;
                DateTime HDate = list[0].HDate;
                long HSCWHID = list[0].HSCWHID;
                long HWHID = list[0].HWHID;
                long HSupID = list[0].HSupID;
                long HSecManagerID = list[0].HSecManagerID;
                long HRepairID = list[0].HRepairID;
                long HManagerID = list[0].HManagerID;
                string HUpDater = list[0].HUpDater;
                long HDeptID = list[0].HDeptID;
               
                //若MAINDI重复则重新获取
                oCN.BeginTran();
                //主表
                oCN.RunProc("Update Sc_MouldStockBillMain set " +
                    " HSCWHID=" + HSCWHID +
                    ",HWHID=" + HWHID +
                    ",HSupID=" + HSupID +
                    ",HSecManagerID=" + HSecManagerID +
                    ",HManagerID= " + HManagerID + " Where HInterID=" + HItemID, ref DBUtility.ClsPub.sExeReturnInfo);
                //修改子项目代码
                //oCN.RunProc("exec h_p_Gy_UpdateNumber Gy_EquipMent,'" + HNumber + ".','" + this.HOldNumber + ".'", ref DBUtility.ClsPub.sExeReturnInfo);
                //将上级 为非末级
                //oCN.RunProc("Update Gy_EquipMent set HEndflag=0 where HItemID=" + HParentID, ref DBUtility.ClsPub.sExeReturnInfo);
                //
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "修改单据成功!";
                //objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.Message;
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// 
        /// 模具入库单列表删除按钮
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("Sc_MouldOtherInBill/Delete_Json")]
        [HttpGet]
        public object Delete_Json(long HItemID)
        {
            DAL.ClsSc_MouldOtherInBill oMould = new DAL.ClsSc_MouldOtherInBill();
            try
            {
                if (oMould.omodel.HBillStatus > 1)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据当前处于不能删除状态,不能删除!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                if (oMould.omodel.HChecker != "")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据已经审核,不能删除!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                    //
                    if (oMould.DeleteBill(HItemID, ref DBUtility.ClsPub.sExeReturnInfo))
                    {
                        ////写入日志
                        //ClsPub.Add_Log("", "删除项目,代码:" + oMould.omodel.hn + ",名称:" + oMould.omodel.HName, ClsPub.CurUserName);
                        //更新上级为 末级
                        objJsonResult.code = "1";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "删除成功";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "删除失败";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                //ds = oCN.RunProcReturn("delete from Gy_Department where HItemID =  " + HItemID, "Gy_Department");
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "删除成功";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
/// 
/// 新增单据-保存按钮
///参数:string sql。
///返回值:object。
/// 
[Route("Sc_MouldOtherInBill/AddBill")]
        [HttpPost]
        public object AddBill([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            oCN.BeginTran();
            //保存主表
            objJsonResult = AddBillMain(msg1);
            if (objJsonResult.code == "0")
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = objJsonResult.Message;
                objJsonResult.data = null;
                return objJsonResult;
            }
            oCN.Commit();
            objJsonResult.code = "1";
            objJsonResult.count = 1;
            objJsonResult.Message = "新增单据成功!";
            objJsonResult.data = null;
            return objJsonResult;
        }
        public json AddBillMain(string msg1)
        {
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string msg3 = sArray[1].ToString();
            try
            {
                msg2 = "[" + msg2.ToString() + "]";
                List mainList = Newtonsoft.Json.JsonConvert.DeserializeObject>(msg2);
                string BillType = "3795";
                long HInterID = mainList[0].HInterID;//递入type得到的单据ID
                string HBillNo = mainList[0].HBillNo;//递入type得到的单据号
                DateTime HDate = mainList[0].HDate;//日期
                int HYear = 2021;
                double HPeriod = 1;
                string HRemark = mainList[0].HRemark;//备注
                string HMaker = mainList[0].HMaker;//制单人
                long HSCWHID = mainList[0].HSCWHID;//
                long HSupID = mainList[0].HSupID;//
                long HEmpID = mainList[0].HEmpID;
                long HManagerID = mainList[0].HManagerID;//
                long HWHID = mainList[0].HWHID;//
                long HProcID = mainList[0].HProcID;//
                long HSecManagerID = mainList[0].HSecManagerID;//
                long HKeeperID = mainList[0].HKeeperID;//
                long HDeptID = mainList[0].HDeptID;
                string HExplanation = mainList[0].HExplanation;//
                string HInnerBillNo = mainList[0].HInnerBillNo;//
                string HRedBlueFlag = mainList[0].HRedBlueFlag;//
                string HRepairType = mainList[0].HRepairType;//
                long HRepairID = mainList[0].HRepairID;//
                //主表
                oCN.RunProc("Insert Into Sc_MouldStockBillMain   " +
                "(HBillType,HBillSubType,HInterID,HBillNo,HDate" +
                ",HYear,HPeriod,HRemark,HMaker,HMakeDate" +
                ",HSupID,HWHID,HSCWHID,HEmpID,HManagerID,HSecManagerID" +
                ",HKeeperID,HDeptID,HExplanation,HInnerBillNo,HRedBlueFlag" +
                ") " +
                " values('" + BillType + "','" + BillType + "'," + HInterID + ",'" + HBillNo + "','" + HDate + "'" +
                "," + HYear + "," + HPeriod + ",'" + HRemark + "','" + HMaker + "',getdate()" + 
                "," + HSupID + "," + HWHID + "," + HSCWHID + "," + HEmpID + "," + HManagerID + "," + HSecManagerID +
                "," + HKeeperID + "," + HDeptID + ",'" + HExplanation + "','" + HInnerBillNo + "','" + HRedBlueFlag + "'" +
                ") ");
                //保存子表
                objJsonResult = AddBillSub(msg3, HInterID);
                if (objJsonResult.code == "0")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = objJsonResult.Message;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = null;
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        public json AddBillSub(string msg3, long HInterID)
        {
            List subList = Newtonsoft.Json.JsonConvert.DeserializeObject>(msg3);
            for (int i = 0; i < subList.ToArray().Length; i++)
            {
                long HMaterID = 0;//物料ID
                long HPropertyID = 0;//
                long HSecUnitID = 0;//
                long HUnitID = 0;//
                long HWHID = 0;//
                long HSCWHID = 0;//
                long HSPID = 0;//
                long HSCSPID = 0;//
                long HSPGroupID = 0;//
                long HLifeUnitID = 0;//
                oCN.RunProc("Insert into Sc_MouldStockBillSub " +
                         " (HInterID,HEntryID,HCloseMan,HEntryCloseDate,HCloseType,HRemark" +
                      ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                      ",HMaterID,HPropertyID,HSecUnitID,HSecUnitRate,HUnitID,HQtyMust" +
                      ",HQty,HPrice,HMoney,HWHID,HSCWHID,HSPID" +
                      ",HDesignLife,HLeaveLife,HUseLife,HUseLifeQty" +
                      ",HSCSPID,HSPGroupID,HBatchNo" +
                      ",HNewModel,HWallThickness,HHardness,HLastProdModel,HDiameter,HBackFlag" +
                        ",HQtyRel,HNewLifeQty,HNowModel,HNowLife" +
                        ",HMouldType,HInitModel,HInitDesignLife,HLifeUnitID" +
                         ") values("
                      + HInterID + "," + i + ",'','','','" + subList[i].HRemark + "'" +
                      "," + subList[i].HSourceInterID + "," + subList[i].HSourceEntryID + ",'" + subList[i].HSourceBillNo + "','" + subList[i].HSourceBillType + "','" + subList[i].HrelationQty + "','" + subList[i].HrelationQty + "'" +
                      "," + HMaterID + "," + HPropertyID + "," + HSecUnitID + ",'" + subList[i].HSecUnitRate + "','" + HUnitID + "','" + subList[i].HQtyMust + "'" +
                      ",'" + subList[i].HQty + "','" + subList[i].HPrice + "','" + subList[i].HMoney + "'," + HWHID + "," + HSCWHID + "," + HSPID +
                      ",'" + subList[i].HDesignLife + "','" + subList[i].HLeaveLife + "','" + subList[i].HUseLife + "','" + subList[i].HUseLifeQty + "'" +
                      "," + HSCSPID + "," + HSPGroupID + ",'" + subList[i].HBatchNo + "'" + 
                     ",'" + subList[i].HNewModel + "','" + subList[i].HWallThickness + "','" + subList[i].HHardness + "','" + subList[i].HLastProdModel + "','" + subList[i].HDiameter + "','" + subList[i].HBackFlag + "'" +
                     ",'" + subList[i].HQtyRel + "','" + subList[i].HNewLifeQty + "','" + subList[i].HNowModel + "','" + subList[i].HNowLife + "'" +
                     ",'" + subList[i].HMouldType + "','" + subList[i].HInitModel + "','" + subList[i].HInitDesignLife + "'," + HLifeUnitID +
                     ") ");
            }
            objJsonResult.code = "1";
            objJsonResult.count = 1;
            objJsonResult.Message = null;
            objJsonResult.data = null;
            return objJsonResult;
        }
    }
}