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 Pay_PurchaseWallclothBillController : ApiController
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        public WebServer webserver = new WebServer();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        /// 
        /// 返回工资结算个人列表(个人)
        ///参数:string sql。
        ///返回值:object。
        /// 
        [Route("Pay_PurchaseWallclothBill/GetSingleBalBill")]
        [HttpGet]
        public object GetSingleBalBill(string sWhere, string user)
        {
            try
            {
               
                ds = Sc_GetSingleBalBill(sWhere);
                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;
            }
        }
        public static DataSet Sc_GetSingleBalBill(string sWhere)
        {
            if (sWhere == null || sWhere.Equals(""))
            {
                return new SQLHelper.ClsCN().RunProcReturn("select * from h_v_Pay_PurchaseWallclothBillMain order by hinterid desc", "h_v_Pay_PurchaseWallclothBillList");
            }
            else
            {
                string sql1 = "select * from h_v_Pay_PurchaseWallclothBillMain where 1 = 1 ";
                string sql = sql1 + sWhere + " order by hinterid desc";
                return new SQLHelper.ClsCN().RunProcReturn(sql, "h_v_Pay_PurchaseWallclothBillList");
            }
        }
        #region[工资结算个人单编辑时获取表头数据]
        [Route("Pay_PurchaseWallclothBill/Pay_PurchaseWallclothBillListCheckDetail")]
        [HttpGet]
        public ApiResult Pay_PurchaseWallclothBillListCheckDetail(string HID)
        {
            if (string.IsNullOrEmpty(HID))
                return new ApiResult { code = -1, msg = "ID不能为空" };
            SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
            var dataSet = oCN.RunProcReturn("select top 1 * from h_v_Pay_PurchaseWallclothBillMain  where HInterID= " + HID + " ", "Pay_PurchaseWallclothBillMain");
            if (dataSet == null || dataSet.Tables[0].Rows.Count == 0)
                return new ApiResult { code = -1, msg = "不存在结算单号" };
            return new ApiResult { code = 1, msg = "查询成功", data = dataSet };
        }
        #endregion
        #region[工资结算个人单编辑时获取表体数据]
        [Route("Pay_PurchaseWallclothBill/Pay_PurchaseWallclothBillListProjectDetai")]
        [HttpGet]
        public object Pay_PurchaseWallclothBillListProjectDetai(string sqlWhere)
        {
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                string sql1 = "SELECT * FROM Pay_PurchaseWallclothBillSub where 1 = 1 ";
                string sql = sql1 + sqlWhere;
                ds = oCN.RunProcReturn(sql, "h_v_Pay_PurchaseWallclothBillList");
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "获取信息成功!";
                objJsonResult.data = ds.Tables[0];
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
            }
            return objJsonResult;
        }
        #endregion
        [Route("Pay_PurchaseWallclothBill/SaveGetSingleBalBillList")]
        [HttpPost]
        /// 
        /// 新增单据-保存按钮
        ///参数:string sql。
        ///返回值:object。
        /// 
        public object SaveGetSingleBalBillList([FromBody] JObject msg)
        {
            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 refSav = sArray[2].ToString();//add update
            string msg4 = sArray[3].ToString();//操作用户
            string sql = string.Empty;
            ListModels oListModels = new ListModels();
            try
            {
                var lsmain = Newtonsoft.Json.JsonConvert.DeserializeObject(msg2);
                var DetailColl = Newtonsoft.Json.JsonConvert.DeserializeObject>(msg3);
                oCN.BeginTran();
                if (refSav == "Add")
                {
                    sql = string.Format(@"
INSERT INTO [dbo].[Pay_PurchaseWallclothBillMain]([HInterID],[HBillType]
,[HBillSubType],[HDate],[HBillNo],[HBillStatus],[HGroupID1],[HGroupID2],[HGroupID3]
,[HEmpID1],[HEmpID2],[HEmpID3],[HEmpID4],[HEmpID5],[HEmpID6],[HRemark],[HMaker],[HMakeDate]
,[HUpDater],[HUpDateDate]) select '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}'"
, lsmain.HInterID, "7789", lsmain.HBillSubType, lsmain.HDate, lsmain.HBillNo, lsmain.HBillStatus, lsmain.HGroupID1
, lsmain.HGroupID2, lsmain.HGroupID3, lsmain.HEmpID1, lsmain.HEmpID2, lsmain.HEmpID3, lsmain.HEmpID4, lsmain.HEmpID5, lsmain.HEmpID6, lsmain.HRemark
, lsmain.HMaker, lsmain.HMakeDate, lsmain.HUpDater, lsmain.HUpDateDate);
                    oCN.RunProc(sql);
                    int HEntryID = 1;
                    foreach (var item in DetailColl)
                    {
                        sql = string.Format(@"insert into[Pay_PurchaseWallclothBillSub]([HInterID],[HEntryID],[HSelOrderNo],[HRemark]) select {0},{1},'{2}','{3}'"
, lsmain.HInterID, HEntryID, item.HSelOrderNo,item.HRemark);
                        oCN.RunProc(sql);
                        HEntryID++;
                    }
                }
                else if (refSav == "Update")
                {
                    sql = string.Format(@"
update [Pay_PurchaseWallclothBillMain] set 
[HGroupID1] = {0},
[HGroupID2] = {1},
[HGroupID3] = {2},
[HEmpID1] = {3},
[HEmpID2] = {4},
[HEmpID3] = {5},
[HEmpID4] = {6},
[HEmpID5] = {7},
[HEmpID6] = {8},
[HRemark] = '{9}',
[HUpDater] = {10},
[HUpDateDate] = '{11}',
where HInterID = {12}",
lsmain.HGroupID1,
lsmain.HGroupID2,
lsmain.HGroupID3,
lsmain.HEmpID1,
lsmain.HEmpID2,
lsmain.HEmpID3,
lsmain.HEmpID4,
lsmain.HEmpID5,
lsmain.HEmpID6,
lsmain.HRemark,
lsmain.HUpDater,
lsmain.HUpDateDate,
lsmain.HInterID);
                    oCN.RunProc(sql);
                    sql = $"delete Pay_PurchaseWallclothBillSub where HinterID = {lsmain.HInterID}";
                    oCN.RunProc(sql);
                    int HEntryID = 1;
                    foreach (var item in DetailColl)
                    {
                        sql = string.Format(@"insert into[Pay_PurchaseWallclothBillSub]([HInterID],[HEntryID],[HSelOrderNo],[HRemark]) select {0},{1},'{2}','{3}'"
, lsmain.HInterID, HEntryID, item.HSelOrderNo, item.HRemark);
                        oCN.RunProc(sql);
                        HEntryID++;
                    }
                }
                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 = "保存失败!" + e.ToString();
                objJsonResult.data = 1;
                return objJsonResult;
            }
        }
        /// 
        ///工资结算个人单删除功能
        /// 
        /// 
        [Route("Pay_PurchaseWallclothBill/DeltetSingleBalBill")]
        [HttpGet]
        public object Pay_PurchaseWallclothBill(string HInterID, string user)
        {
            try
            {
                oCN.BeginTran();
                oCN.RunProc("Delete From Pay_PurchaseWallclothBillMain where HInterID = " + HInterID);
                oCN.RunProc("Delete From Pay_PurchaseWallclothBillSub where HInterID = " + HInterID);
                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.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
    }
}