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 Xt_DataDictionary_TableController : ApiController
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
        /// 
        /// 返回列表
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("Xt_DataDictionary_Table/list")]
        [HttpGet]
        public object list(string sWhere)
        {
            try
            {
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_IF_DataDictionary_Table where 1=1 ", "h_v_IF_DataDictionary_Table");
                }
                else
                {
                    string sql1 = "select * from h_v_IF_DataDictionary_Table where 1=1 ";
                    string sql = sql1 + sWhere;
                    ds = oCN.RunProcReturn(sql, "h_v_IF_DataDictionary_Table");
                }
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "false!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = ds.Tables[0];
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// 
        /// 根据基础资料ID 查找记录
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("Xt_DataDictionary_Table/cx")]
        [HttpGet]
        public object cx(long HInterID)
        {
            try
            {
                ds = oCN.RunProcReturn("select * from h_v_IF_DataDictionary_TableDetail where HitemID =  " + HInterID, "h_v_IF_DataDictionary_TableDetail");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "false!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = ds.Tables[0];
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// 
        /// 删除功能
        /// 
        /// 
        [Route("DeltetXt_DataDictionary_Table")]
        [HttpGet]
        public object DeltetXt_DataDictionary_Table(string HItemID)
        {
            DataSet ds;
            //DataSet ds1;
            try
            {
                //删除权限
                //if (!DBUtility.ClsPub.Security_Log(ModRightNameCheck, 1, true, CurUserName))
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "审核失败!无权限!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                if (string.IsNullOrWhiteSpace(HItemID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HItemID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                oCN.BeginTran();//开始事务
                ds = oCN.RunProcReturn("select * from Xt_DataDictionary_Table where HItemID=" + HItemID, "Xt_DataDictionary_Table");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有数据,无法删除!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
                //ds1 = oCN.RunProcReturn("Select HItemID from Xt_DataDictionary_Table  Where HParentID='" + HItemID + "'", "Xt_DataDictionary_Table");
                //if (ds1.Tables[0].Rows.Count != 0)
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "此项目存在子项目,不能删除!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
               
              
                oCN.RunProc("delete from Xt_DataDictionary_Table where HItemID=" + HItemID);
                oCN.Commit();//提交事务
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "* 数据删除成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
       
        /// 
        /// 禁用按钮
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("Xt_DataDictionary_Table/JY_Json")]
        [HttpGet]
        public object JY_Json(long HItemID)
        {
            try
            {
                ds = oCN.RunProcReturn("update Xt_DataDictionary_Table set HStopFlag = 1 where HItemID =  " + HItemID, "Xt_DataDictionary_Table");
                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("Xt_DataDictionary_Table/FJY_Json")]
        [HttpGet]
        public object FJY_Json(long HItemID)
        {
            try
            {
                ds = oCN.RunProcReturn("update Xt_DataDictionary_Table set HStopFlag = 0 where HItemID =  " + HItemID, "Xt_DataDictionary_Table");
                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("Xt_DataDictionary_Table/AddBill")]
        [HttpPost]
        public object AddBill([FromBody] JObject oMain)
        {
            try
            {
                var _value = oMain["oMain"].ToString();
                string msg1 = _value.ToString();
                //反序列化
                msg1 = "[" + msg1.ToString() + "]";
                //DAL.ClsGy_Department_Ctl oDept = new DAL.ClsGy_Department_Ctl();
                //DAL.ClsGy_Department_View oDeptHlp = new DAL.ClsGy_Department_View();
                List list = Newtonsoft.Json.JsonConvert.DeserializeObject>(msg1);
                long HItemID = list[0].HItemID;
                long HModNameID = list[0].HModNameID;
                long HBillTypeID = list[0].HBillTypeID;
                string HTableName = list[0].HTableName;
                string HTableCaption = list[0].HTableCaption;
                string HFieldName = list[0].HFieldName;
                string HFieldCaption = list[0].HFieldCaption;
                string HDataType = list[0].HDataType;
                string HDefaultValue = list[0].HDefaultValue;
                string HRemark = list[0].HRemark;
                bool HStopflag = list[0].HStopflag;
                DateTime HMakeTime = list[0].HMakeTime;
                string HMakeEmp = list[0].HMakeEmp;
                DateTime HModifyTime = list[0].HModifyTime;
                string HModifyEmp = list[0].HModifyEmp;
                //若MAINDI重复则重新获取
                oCN.BeginTran();
                //主表
                oCN.RunProc("Insert Into Xt_DataDictionary_Table " +
                "(HModNameID,HBillTypeID,HTableName,HTableCaption" +
                ",HFieldName,HFieldCaption,HDataType,HDefaultValue,HRemark" +
                ",HStopflag,HMakeTime,HMakeEmp,HModifyTime" +
                ",HModifyEmp" +
                ") " +
                " values(" + HModNameID + "," + HBillTypeID + ",'" + HTableName + "'"+
                ",'" + HTableCaption + "','" + HFieldName + "','" + HFieldCaption + "','" + HDataType + "'" +
                ",'" + HDefaultValue + "','" + HRemark + "'," + Convert.ToString(HStopflag ? 1 : 0) + "" +
                ",'" + HMakeTime + "','" + HMakeEmp + "','" + HModifyTime + "','" + HModifyEmp + "'" +
              ") ");
                //修改上级为非末级代码
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = null;
                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("Xt_DataDictionary_Table/AddBill1")]
        [HttpPost]
        public object AddBill1([FromBody] JObject oMain)
        {
            try
            {
                //DAL.ClsGy_Department_Ctl oDept = new DAL.ClsGy_Department_Ctl();
                //DAL.ClsGy_Department_View oDeptHlp = new DAL.ClsGy_Department_View();
                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].HItemID;
                long HModNameID = list[0].HModNameID;
                long HBillTypeID = list[0].HBillTypeID;
                string HTableName = list[0].HTableName;
                string HTableCaption = list[0].HTableCaption;
                string HFieldName = list[0].HFieldName;
                string HFieldCaption = list[0].HFieldCaption;
                string HDataType = list[0].HDataType;
                string HDefaultValue = list[0].HDefaultValue;
                string HRemark = list[0].HRemark;
                bool HStopflag = list[0].HStopflag;
                DateTime HMakeTime = list[0].HMakeTime;
                string HMakeEmp = list[0].HMakeEmp;
                DateTime HModifyTime = list[0].HModifyTime;
                string HModifyEmp = list[0].HModifyEmp;
                //保存
                //保存完毕后处理
                if (HItemID == 0)
                {
                    //新增
                    oCN.BeginTran();
                    oCN.RunProc("Insert Into Xt_DataDictionary_Table " +
                "(HModNameID,HBillTypeID,HTableName,HTableCaption" +
                ",HFieldName,HFieldCaption,HDataType,HDefaultValue,HRemark" +
                ",HStopflag,HMakeTime,HMakeEmp,HModifyTime" +
                ",HModifyEmp" +
                ") " + " values(" + HModNameID + "," + HBillTypeID + ",'" + HTableName + "'" +
                ",'" + HTableCaption + "','" + HFieldName + "','" + HFieldCaption + "','" + HDataType + "'" +
                ",'" + HDefaultValue + "','" + HRemark + "'," + Convert.ToString(HStopflag ? 1 : 0) + "" +
                ",'" + HMakeTime + "','" + HMakeEmp + "','" + HModifyTime + "','" + HModifyEmp + "'" +
                ") ", ref DBUtility.ClsPub.sExeReturnInfo);
                    //修改上级为非末级代码
                    oCN.RunProc("Update Xt_DataDictionary_Table set HStopflag=0 where HItemID=" + HItemID, ref DBUtility.ClsPub.sExeReturnInfo);
                    oCN.Commit();
                }
                else
                {
                    oCN.BeginTran();
                    //主表
                    oCN.RunProc("Update Xt_DataDictionary_Table set " +
                        " HModNameID= "+ HModNameID +
                        " HBillTypeID= " + HBillTypeID +
                        ",HTableName='" + HTableName + "'" +
                        ",HTableCaption='" + HTableCaption + "'" +
                        ",HFieldName='" + HFieldName + "'" +
                        ",HDataType='" + HDataType + "'" +
                        ",HFieldName='" + HFieldName + "'" +
                        ",HFieldCaption='" + HFieldCaption + "'" +
                        //",HRemark='" + HRemark + "'" +
                        ",HStopflag=" + Convert.ToString(HStopflag ? 1 : 0) +
                        ",HMakeTime='" + HMakeTime + "'" +
                        ",HMakeEmp='" + HMakeEmp + "'" +
                        ",HModifyTime='" + HModifyTime + "'" +
                        ",HModifyEmp='" + HModifyEmp + "'" +
                        //",HUpdater='" + HUpdater + "'" +
                        //",HUpdateDate= getdate()" +
                        ",HRemark= '" + HRemark + "' Where HItemID=" + HItemID, ref DBUtility.ClsPub.sExeReturnInfo);
                    //修改子项目代码
                    //oCN.RunProc("exec h_p_Gy_UpdateNumber Gy_Department,'" + HNumber + ".','" + this.HOldNumber + ".'", ref DBUtility.ClsPub.sExeReturnInfo);
                    //将上级 为非末级
                    oCN.RunProc("Update Xt_DataDictionary_Table set HStopflag=0 where HItemID=" + HItemID, ref DBUtility.ClsPub.sExeReturnInfo);
                    oCN.Commit();
                }
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "保存成功!";
                //WebAPIController.Add_Log("送货单下推", UserName, "生成送货单");
                objJsonResult.data = 1;
                return objJsonResult;
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.Message;
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        //
    }
}