using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Http;
using WebAPI.Models;
namespace WebAPI.Controllers
{
    public class Gy_SourceWorkStationSetController : ApiController
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
        /// 
        /// 返回生产工位列表
        ///参数:string sql。
        ///返回值:object。
        ///  
        [Route("Gy_SourceWorkStationSet/list")]
        [HttpGet]
        public object list(string sWhere, string user)
        {
            try
            {
                List columnNameList = new List();
                //编辑权限
                if (!DBUtility.ClsPub.Security_Log_second("Gy_SourceWorkStationSet_Query", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无查看权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                string sql1 = string.Format(@"select * from h_v_Gy_SourceWorkStationSetList where 1 = 1");
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn(sql1 + sWhere + " order by HItemID ", "h_v_Gy_SourceWorkStationSetList");
                }
                else
                {
                    string sql = sql1 + sWhere + " order by HItemID ";
                    ds = oCN.RunProcReturn(sql, "h_v_Gy_SourceWorkStationSetList");
                }
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #region 产线工位保存
        [Route("Gy_SourceWorkStationSet/Save")]
        [HttpPost]
        public object set_SaveBill([FromBody] JObject oMain)
        {
            var _value = oMain["oMain"].ToString();
            string msg1 = _value.ToString();
            //保存单据
            return objJsonResult = AddBillMain(msg1);
        }
        public json AddBillMain(string msg1)
        {
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString(); //用户           
            bool bResult;
            try
            {
                //判断权限
                if (!DBUtility.ClsPub.Security_Log("Gy_SourceWorkStationSet_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = "[" + msg2.ToString() + "]";
                List mainList = Newtonsoft.Json.JsonConvert.DeserializeObject>(msg2);
                DAL.ClsGy_SourceWorkStationSet BillNew = new DAL.ClsGy_SourceWorkStationSet();
                //判断会计期是否合理
                string s = "";
                int sYear = 0;
                int sPeriod = 0;
                DateTime HDate = mainList[0].HCreateDate;//日期
                if (DBUtility.Xt_BaseBillFun.Fun_AllowYearPeriod(HDate, ref sYear, ref sPeriod, ref s) == false)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = s;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                //固定赋值=================================
                BillNew.oModel.HMacAddr = mainList[0].HMacAddr;
                BillNew.oModel.HSourceID = mainList[0].HSourceID;
                BillNew.oModel.HProdOrgID = mainList[0].HProdOrgID;
                BillNew.oModel.HWorkStationID = mainList[0].HWorkStationID;
                BillNew.oModel.HProcID = mainList[0].HProcID;
                BillNew.oModel.HType = ClsPub.isStrNull(mainList[0].HType);
                BillNew.oModel.HVideo = ClsPub.isLong(mainList[0].HVideo);
                BillNew.oModel.HCreateDate = ClsPub.isDate(mainList[0].HCreateDate);
                BillNew.oModel.HCreator = ClsPub.isStrNull(mainList[0].HCreator);
                            
                //保存完毕后处理
                if (mainList[0].HItemID == 0)
                {
                    bResult = BillNew.AddNew();
                }
                else
                {
                    bResult = BillNew.ModifyByID(mainList[0].HItemID);
                }
                //提示
                if (bResult == true)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    //objJsonResult.Message = "单据存盘完毕!单据号:" + mainList[0].HBillNo.Trim();
                    objJsonResult.Message = "保存成功!" + ClsPub.sExeReturnInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "保存失败!原因:" + ClsPub.sExeReturnInfo;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        /// 
        /// 生产工位列表删除功能
        ///  
        ///