using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

namespace FineUIPro.Web
{
    /// <summary>
    /// MyWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "https://fcl-test.basf-ypc.net/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class MyWebService : System.Web.Services.WebService
    {
        /// <summary>
        /// 根据月份获取评价记录
        /// </summary>
        /// <param name="month">年月,年和月用-隔开</param>
        /// <returns></returns>
        [WebMethod(Description = "根据月份获取评价记录,年和月用-隔开")]
        public string GetContractEvaluation(string month)
        {
            var sTime = DateTime.Parse(month + "-01").AddMonths(1).ToString("yyyy-MM-dd");
            var eTime = DateTime.Parse(month + "-12").AddMonths(1).ToString("yyyy-MM-dd");

            DataTable dt = BLL.SQLHelper.GetDataTableRunText("exec FN_Evaluation_Interface '" + sTime + "','" + eTime + "'", new SqlParameter[0]);
            if (dt.Rows.Count > 0)
            {
                return BLL.JsonHelper.DataTableToJson(dt);
            }
            else
            {
                return "该月份没有评价记录";
            }
        }

        /// <summary>
        /// 获取FC List 列表
        /// </summary>
        /// <returns></returns>
        [WebMethod(Description = "获取FC List 列表")]
        public string GetFCList()
        {
            string strSql = @"SELECT * from View_FC_SESRelatedData ";
            DataTable dt = BLL.SQLHelper.GetDataTableRunText(strSql,null);
            if (dt.Rows.Count > 0)
            {
                return BLL.JsonHelper.DataTableToJson(dt);
            }
            else
            {
                return "没有记录";
            }
        }

        /// <summary>
        /// 根据SES_Confirmed_on最近月份获取 SES Report 列表
        /// </summary>
        /// <param name="lastMonth"></param>
        /// <returns></returns>
        [WebMethod(Description = "根据SES_Confirmed_on最近月份获取 SES Report 列表")]
        public string GetSESReport(string lastMonth)
        {
            string strSql = @"SELECT  Accepted, Blocked , Start_Date, End_Date,Created_by, TECO_Date, SES_No,  Work_Order,
                                      Short_Descrption, FO, Item,  Vendor_Name,SSR_budget,Contractor_quotation,SSR_Actual_cost, 
		                              WBS,Created_on,Claim_sheets_receive, SES_Confirmed_on, Invoiced_on, Payment_made_on, 
		                              Requisitioner, Currency,    Tax_rate,Changed_by, Deviation, Deviation_Percentage,Long_text,
		                              Function_location, Main_work_center,Work_Center,Cost_center,Network, Contractor_duration,
		                              Engineer_confirmed_o, BoQ_confirmation_dur,  Settlement_duration,Invoice_duration,Payment_duration
		                         FROM FC_SESReport 
								WHERE SES_Confirmed_on IS NULL or SES_Confirmed_on='' ";
            int? m = BLL.Funs.GetNewInt(lastMonth);
            if (m != null)
            {
                strSql += " OR DATEDIFF(MONTH, SES_Confirmed_on, GETDATE()) <= @lastMonth";

                List<SqlParameter> listStr = new List<SqlParameter>();
                listStr.Add(new SqlParameter("@lastMonth", m.Value));
                SqlParameter[] parameter = listStr.ToArray();
                DataTable dt = BLL.SQLHelper.GetDataTableRunText(strSql, parameter);

                if (dt.Rows.Count > 0)
                {
                    return BLL.JsonHelper.DataTableToJson(dt);
                }
                else
                {
                    return "没有记录";
                }
            }
            else
            {
                return "请输入最近月份!";
            }
        }

        [WebMethod(Description = "获取 SSR Collection 列表")]
        public string GetSSRCollection()
        {
            string strSql = @"select [SSRId], [SES_No], 
                                    (CASE WHEN WorkOrder IS NOT NULL AND Op IS NOT NULL
		                                    THEN WorkOrder + '-' + Op
		                                    ELSE (ISNULL(WorkOrder,'') + ISNULL(Op,'')) END) AS WorkOrderOp,
                                    [RequisitionerId], [RequisitionerName], [DepartmentId], 
                                    [DepartmentName], 
                                    CONVERT(nvarchar(10),SubmmisionDate,23) AS SubmmisionDate, 
                                    CONVERT(nvarchar(10),ReturnDate,23) AS ReturnDate, 
                                    CONVERT(nvarchar(10),CompletionDate,23) as CompletionDate,  
                                    [ContractNo], [Address], [Remark], 
                                     CONVERT(nvarchar(10),InputDate,23) as InputDate,
                                    [IsRetruned], [RetrunDuration], [NotRetrunDuration], [ContractAdmin]
                                from SSR ";
            DataTable dt = BLL.SQLHelper.GetDataTableRunText(strSql, null);
            if (dt.Rows.Count > 0)
            {
                return BLL.JsonHelper.DataTableToJson(dt);
            }
            else
            {
                return "没有记录";
            }
        }

        /// <summary>
        /// 获取用户账号,名称,部门信息
        /// </summary>
        /// <returns></returns>
        [WebMethod(Description = "获取用户账号,名称,部门信息")]
        public string GetUserInfo()
        {
            string strSql = @"SELECT  Account,UserName,d.DepartCode FROM dbo.Sys_User u
                              LEFT JOIN dbo.Base_Depart d ON d.DepartId = u.DepartId";
            DataTable dt = BLL.SQLHelper.GetDataTableRunText(strSql, null);
            if (dt.Rows.Count > 0)
            {
                return BLL.JsonHelper.DataTableToJson(dt);
            }
            else
            {
                return "没有记录";
            }
        }
    }
}