using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using ICS.WCF.Base;
using UFSoft.UBF.Util.Context;
using System.Configuration;
using www.ufida.org.EntityData;

namespace UFIDA.U9.Cust.CloseShipPlanLine
{
    public class CloseShipPlanLineSrv
    {
        public CloseShipPlanLineRtnModel Action(List<CloseShipPlanLineModel> list)
        {
            var res = new CloseShipPlanLineRtnModel();
            try
            {
                if (list == null || list.Count == 0)
                {
                    throw new Exception("参数为空");
                }

                string sql = "select ID from Base_Organization Where Code ='" + list[0].WorkPoint + "'";
                DataTable dt = DBhlper.Query(sql, Appconfig.GetU9ConnStr());
                if (dt.Rows.Count == 0)
                {
                    throw new Exception("组织不存在");
                }

                string OrgID = dt.Rows[0]["ID"].ToString();

                sql = "select ID from Base_User Where Code ='" + list[0].User + "'";
                dt = DBhlper.Query(sql, Appconfig.GetU9ConnStr());
                if (dt.Rows.Count == 0)
                {
                    throw new Exception("用户不存在");
                }

                string UserID = dt.Rows[0]["ID"].ToString();

                object context = CreateContextObj(OrgID, UserID, list[0].User);

                var client = new UFIDAU9CustFynCloseShipPlanLineSV_ServiceProjectICloseServiceClient();
                UFSoft.UBF.Exceptions.MessageBase[] RtnMsg;
                List<UFIDAU9CustFynCloseShipPlanLineSV_ServiceProjectCloseDtoData> datalist = new List<UFIDAU9CustFynCloseShipPlanLineSV_ServiceProjectCloseDtoData>();
                foreach (var item in list)
                {
                    sql = @"select b.ID,b.Status from SM_ShipPlan a
                            left join SM_ShipPlanLine b on a.ID = b.ShipPlan
                            where a.DocNo ='" + item.ShipPlanCode + "' and a.Org ='" + OrgID + "' and b.DocLineNo ='" + item.LineNo + "'";
                    dt = DBhlper.Query(sql, Appconfig.GetU9ConnStr());
                    if (dt.Rows.Count == 0)
                    {
                        throw new Exception("出货计划行不存在");
                    }

                    if (dt.Rows[0]["Status"].ToString() =="5"|| dt.Rows[0]["Status"].ToString() == "6"|| dt.Rows[0]["Status"].ToString() == "4"
                        || dt.Rows[0]["Status"].ToString() == "9")
                    {
                        throw new Exception("出货计划行已关闭，无法重复关闭");
                    }

                    datalist.Add(new UFIDAU9CustFynCloseShipPlanLineSV_ServiceProjectCloseDtoData 
                    {
                        m_shipPlanLine = dt.Rows[0]["ID"].ToString()
                    });
                }

                UFIDAU9CustFynCloseShipPlanLineSV_ServiceProjectCloseRtnDtoData resdata = client.Do(out RtnMsg,context,datalist.ToArray());
                if (!resdata.m_success)
                {
                    throw new Exception(resdata.m_message);
                }

                res.Success = true;
                res.Message = "成功";
            }
            catch (Exception ex)
            {
                res.Success = false;
                res.Message = ex.Message;
            }
            return res;
        }


        #region 给上下文信息赋值
        private ThreadContext CreateContextObj(string OrgID, string UserID, string UserCode)
        {
            // 实例化应用上下文对象
            ThreadContext thContext = new ThreadContext();

            System.Collections.Generic.Dictionary<object, object> ns = new Dictionary<object, object>();
            ns.Add("OrgID", OrgID);
            ns.Add("UserID", UserID);
            ns.Add("UserCode", UserCode);
            ns.Add("MTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            ns.Add("CultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
            ns.Add("EnterpriseID", ConfigurationManager.AppSettings["enterpriseID"].ToString());
            ns.Add("DefaultCultureName", ConfigurationManager.AppSettings["cultureName"].ToString());
            thContext.nameValueHas = ns;

            return thContext;
        }
        #endregion

    }

    public class CloseShipPlanLineModel
    {
        public string ShipPlanCode { get; set; }

        public string LineNo { get; set; }

        public string WorkPoint { get; set; }

        public string User { get; set; }
    }

    public class CloseShipPlanLineRtnModel
    {
        public bool Success { get; set; }

        public string Message { get; set; }

    }
}
