using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlMesDbIterface;
using System.Net.Mail;
using PLMTEST;
using ExternalDataSync.MOM;
using DataLinkMesWork;
using System.Collections;
using System.Drawing.Imaging;

namespace ExternalDataSync
{
    public class AnalysisMsg
    {
        /// <summary>
        /// UUID生成
        /// </summary>
        /// <returns></returns>
        public static string UuidUtil()
        {
            string result = Guid.NewGuid().ToString();

            return result;
        }

        /// <summary>
        /// 物料主数据下发
        /// </summary>
        /// <param name="url"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ProductionCalendarData(string url, string str)
        {
            var result = new msgResHeader();
            result.taskId = UuidUtil();
            result.code = "0";
            result.msg = "";
            result.returnData = "";



            string errorMessage = "";
            string parentID = "";
            int AID = -1;
            int isError = 0;
            try
            {
                //存储日志
                var CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                //Event_MOM_Log_Insert,@创建时间,@内容
                var param1 = new SqlParameter[] {
                new SqlParameter("@接口地址",url),
                new SqlParameter("@接口类型",2), // 1. 主动调用接口  2. 被调用接口 
                new SqlParameter("@请求内容",str),
                new SqlParameter("@请求时间",CreateTime)
                };
                DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_请求记录", Program.ConnectionString, ref param1, out DataTable dt, out errorMessage);
                if (dt.Rows.Count > 0)
                {
                    AID = Convert.ToInt32(dt.Rows[0]["AID"]);
                }
                // 解析订单内容，存储数据库，根据实际业务来写
                ProductionCalendarData BaseData = JsonHelper.JsonDeserialize<ProductionCalendarData>(str);

                var parsingParam = new SqlParameter[] {
                new SqlParameter("@类型",BaseData.typesOf),
                new SqlParameter("@类型编码",BaseData.typeCode),
                new SqlParameter("@日历编码",BaseData.calendarEncoding),
                new SqlParameter("@班次名称",BaseData.shiftName),
                new SqlParameter("@开始时间",BaseData.timeOn),
                new SqlParameter("@结束时间",BaseData.endTime),
                new SqlParameter("@开始日期",BaseData.startDate),
                new SqlParameter("@结束日期",BaseData.endDate),
                new SqlParameter("@班次描述",BaseData.shiftDescription),
                new SqlParameter("@固定休息日",BaseData.fixedRestDays),
                };
                DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_接收_生产日历下发_增加", Program.ConnectionString, ref parsingParam, out DataTable parsingDt, out errorMessage);
                if (parsingDt.Rows.Count > 0)
                {
                    if (Convert.ToInt32(parsingDt.Rows[0]["result"]) == 1)
                    {
                        parentID = BaseData.calendarEncoding;
                        List<ProductionCalendarStatutoryData> statutoryHoliday = BaseData.statutoryHolidays;

                        List<ProductionCalendarNonProductionData> nonProductionTime = BaseData.nonProductionTime;
                        if (statutoryHoliday != null)
                        {
                            foreach (ProductionCalendarStatutoryData item in statutoryHoliday)
                            {
                                var parsingParam2 = new SqlParameter[] {
                                new SqlParameter("@日历编码",parentID),
                                new SqlParameter("@开始日期",item.startDate),
                                new SqlParameter("@结束日期",item.endDate),
                                };
                                DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_接收_生产日历下发_法定节日_增加", Program.ConnectionString, ref parsingParam2, out DataTable parsingDt2, out errorMessage);

                                if (errorMessage != "")
                                {
                                    result.msg = errorMessage;
                                    result.code = "501";
                                    result.returnData = "ERROR";
                                }
                            }
                        }

                        if (nonProductionTime != null)
                        {
                            foreach (ProductionCalendarNonProductionData item in nonProductionTime)
                            {
                                var parsingParam2 = new SqlParameter[] {
                                new SqlParameter("@日历编码",parentID),
                                new SqlParameter("@开始时间",item.timeOn),
                                new SqlParameter("@结束时间",item.endTime),
                                };
                                DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_接收_生产日历下发_非生产时间_增加", Program.ConnectionString, ref parsingParam2, out DataTable parsingDt2, out errorMessage);

                                if (errorMessage != "")
                                {
                                    result.msg = errorMessage;
                                    result.code = "501";
                                    result.returnData = "ERROR";
                                }
                            }
                        }
                    }
                    else
                    {
                        isError = 1;
                    }
                }
                else
                {
                    isError = 1;
                }

                if (isError == 1)
                {
                    result.msg = "存储过程调用失败！";
                    result.code = "501";
                    result.returnData = "ERROR";
                }

                if (errorMessage != "")
                {
                    result.msg = errorMessage;
                    result.code = "501";
                    result.returnData = "ERROR";
                }

                //响应结果
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                var resParam = new SqlParameter[] {
                new SqlParameter("@AID",AID),
                new SqlParameter("@响应时间",CreateTime),
                new SqlParameter("@响应内容",JsonConvert.SerializeObject(result))
                };
                DataLinkMesWork.SQLCommon.ExecuteStoredProcedure("接口_IOT接口交互日志_响应记录", Program.ConnectionString, ref resParam, out errorMessage);
                if (dt.Rows.Count > 0)
                {
                    AID = Convert.ToInt32(dt.Rows[0]["AID"]);
                }
            }
            catch (Exception err)
            {
                result.code = "501";
                result.msg = err.Message;
                result.returnData = "ERROR";
            }
            return JsonConvert.SerializeObject(result);
        }

    }
}
