using ICSSoft.Entity;
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;

namespace UFIDA.U9.Cust.ModifyLot
{
    public class ModifyLotSrv
    {
        public Result Action(List<ModifyLotModel> list)
        {
            var res = new Result();
            try
            {
                if (list == null || list.Count == 0)
                {
                    throw new Exception("入参为空");
                }


                string sqlorg = "select ID from Base_Organization where code ='" + list[0].OrgCode + "'";
                DataTable dtorg = DBhlper.Query(sqlorg, Appconfig.GetU9ConnStr());
                if (dtorg.Rows.Count == 0)
                {
                    throw new Exception("组织不存在");
                }

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

                string sqlUser = "select ID from Base_User where code ='" + list[0].User + "'";
                DataTable dtUser = DBhlper.Query(sqlUser, Appconfig.GetU9ConnStr());
                if (dtUser.Rows.Count == 0)
                {
                    throw new Exception("用户不存在");
                }

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

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

                var client = new UFIDAU9CustModifyItemLotSV_ServiceProjectIModifyLotSerClient();
                UFSoft.UBF.Exceptions1.MessageBase[] rtnMsg;
                List<www.ufida.org.EntityData.UFIDAU9CustModifyItemLotSV_ServiceProjectModifyLotDtoData> datalist = new List<www.ufida.org.EntityData.UFIDAU9CustModifyItemLotSV_ServiceProjectModifyLotDtoData>();
                foreach (var item in list)
                {
                    string sql = @"select a.ID from Lot_LotMaster a
                                    join CBO_ItemMaster c on a.Item = c.ID
                                     join Base_Organization b on c.Org = b.ID
                                     
                                    where a.LotCode ='" + item.LotCode + "' and c.Code ='" + item.ItemCode + "' and b.Code ='" + item.OrgCode + "'";
                    DataTable dt = DBhlper.Query(sql, Appconfig.GetU9ConnStr());
                    if (dt.Rows.Count == 0)
                    {
                        throw new Exception("料品批号未查询到，批号：" + item.LotCode + "，料品：" + item.ItemCode);
                    }

                    datalist.Add(new www.ufida.org.EntityData.UFIDAU9CustModifyItemLotSV_ServiceProjectModifyLotDtoData
                    {
                        m_lotCode = dt.Rows[0]["ID"].ToString(),
                        m_cusFlag = item.CusFlag
                    });
                }

                www.ufida.org.EntityData.UFIDAU9CustModifyItemLotSV_ServiceProjectModifyLotRtnDtoData rtndata = client.Do(out rtnMsg, context, datalist.ToArray());
                if (!rtndata.m_success)
                {
                    throw new Exception(rtndata.m_message);
                }

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

            return res;

        }


        /// <summary>
        /// 创建上下文
        /// </summary>
        /// <returns></returns>
        private static 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("Datetime", 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());
            ns.Add("acttype", ConfigurationManager.AppSettings["acttype"].ToString());
            thContext.nameValueHas = ns;

            return thContext;
        }
    }

    public class ModifyLotModel
    {
        public string LotCode { get; set; }

        public string ItemCode { get; set; }

        public string OrgCode { get; set; }

        public string CusFlag { get; set; }

        public string User { get; set; }
    }
}
