using MECF.Framework.Common.DataCenter;
using System;
using System.Collections.Generic;
using System.Data;

namespace MECF.Framework.UI.Client.TrayThickness
{
    /// <summary>
    /// TrayDataViewSqlHelp
    /// 1：查询整个Tray对象数据
    /// 2：插入或者更新整个Tray对象数据
    /// </summary>
    public class TrayDataViewSqlHelp
    {
        private string tray_number, tray_inner_number, ring_inner_number, ring_outer_number;
        /// <summary>
        /// 获取Truy对象整体的数据
        /// </summary>
        /// <param name="_trayGuid"></param>
        /// <returns></returns>
        public TrayCoatingThickness GetTrayCoatingThickness(string _trayGuid)
        {
            //这是一次查询一个对象
            TrayCoatingThickness trayCoatingThickness = new TrayCoatingThickness();
            try
            {
                //根据TrayID获取对应内外编号
                string cmdID = $"select * from tray_thickness_data where tray_guid = '{_trayGuid}'";
                DataTable DataTable = QueryDataClient.Instance.Service.QueryData(cmdID);
                if (DataTable != null && DataTable.Rows.Count > 0)
                {


                    tray_number = DataTable.Rows[0]["tray_number"].ToString();
                    tray_inner_number = DataTable.Rows[0]["tray_inner_number"].ToString();
                    ring_inner_number = DataTable.Rows[0]["ring_inner_number"].ToString();
                    ring_outer_number = DataTable.Rows[0]["ring_outer_number"].ToString();

                    trayCoatingThickness.Tray = GetCoatingData(tray_number, out _);
                    trayCoatingThickness.TrayInner = GetCoatingData(tray_inner_number, out _);
                    trayCoatingThickness.RingInner = GetCoatingData(ring_inner_number, out _);
                    trayCoatingThickness.RingOuter = GetCoatingData(ring_outer_number, out _);

                }


                return trayCoatingThickness;
            }
            catch (Exception)
            {

                return trayCoatingThickness;
            }
        }

        /// <summary>
        /// 获取单个环的数据
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="isExistedInDb"></param>
        /// <returns></returns>
        public CoatingData GetCoatingData(string sn, out bool isExistedInDb)
        {
            var cmd = $"select * from tray_thickness_main where tray_number = '{sn}' ORDER  BY  create_time DESC LIMIT 1";
            var dt = QueryDataClient.Instance.Service.QueryData(cmd);

            if (dt is { Rows.Count: > 0 })
            {
                var coatingData = new CoatingData()
                {
                    SerialNumber = dt.Rows[0]["tray_number"].ToString(),
                    MAX = dt.Rows[0]["tray_max_thickness"].ToString(),
                    PmOwned = dt.Rows[0]["pm_number"].ToString(),
                    Current = dt.Rows[0]["tray_coating_thickness"].ToString()
                };
                isExistedInDb = true;
                return coatingData;

            }
            isExistedInDb = false;
            return new CoatingData();
        }
        /// <summary>
        /// 保存用户输入的数据到数据库
        /// </summary>
        /// <param name="trayCoatingThickness"></param>
        /// <param name="trayUid"></param>
        public bool UpTrayNumberData(TrayCoatingThickness trayCoatingThickness, string trayUid, List<CoatingData> coatingData)
        {
            List<string> cmdList = new List<string>();

            string cmdID = $"select * from tray_thickness_data where tray_guid = '{trayUid}'";
            var dt = QueryDataClient.Instance.Service.QueryData(cmdID);
            if (dt is { Rows.Count: > 0 })
                cmdList.Add(Cmd_update(trayCoatingThickness, trayUid));
            else
                cmdList.Add(Cmd_data_insert(trayCoatingThickness, trayUid));
            foreach (var item in coatingData)
            {
                cmdList.Add(Cmd_main_insert(item));
            }
            bool result= QueryDataClient.Instance.Service.ExcuteTransAction(cmdList);
            return result;
        }

        public bool GetPartSnByTrayUid(string trayUid, out List<CoatingData> coatingData)
        {
            coatingData = new List<CoatingData>();
            var sql =
                $"select tray_number, tray_inner_number, ring_inner_number, ring_outer_number from tray_thickness_data WHERE tray_guid = '{trayUid}' limit 1";
            var dt = QueryDataClient.Instance.Service.QueryData(sql);
            if (dt is { Rows.Count: > 0 })
            {
                //partSnList.Add();
            }

            return false;
        }
        
        /// <summary>
        /// Tray中对象编号插入Sql语句拼接
        /// </summary>
        /// <param name="trayCoatingThickness"></param>
        /// <param name="trayID"></param>
        /// <returns></returns>
        private string Cmd_data_insert(TrayCoatingThickness trayCoatingThickness,string trayID)
        {
            string cmd_data_insert =
                "insert into tray_thickness_data" +
                "(tray_guid,tray_number, tray_inner_number, ring_inner_number, ring_outer_number)" +
                $"values ('{trayID}'," +
                $"'{trayCoatingThickness.Tray.SerialNumber}'," +
                $"'{trayCoatingThickness.TrayInner.SerialNumber}'," +
                $"'{trayCoatingThickness.RingInner.SerialNumber}'," +
                $"'{trayCoatingThickness.RingOuter.SerialNumber}')";
            return cmd_data_insert;
        }
        /// <summary>
        /// Tray中对象编号更新Sql语句拼接
        /// </summary>
        /// <param name="trayCoatingThickness"></param>
        /// <param name="trayID"></param>
        /// <returns></returns>
        private string Cmd_update(TrayCoatingThickness trayCoatingThickness, string trayID)
        {
            string cmd_update =
                $"update tray_thickness_data set " +
                $"tray_number = '{trayCoatingThickness.Tray.SerialNumber}', " +
                $"tray_inner_number= '{trayCoatingThickness.TrayInner.SerialNumber}'," +
                $"ring_inner_number= '{trayCoatingThickness.RingInner.SerialNumber}'," +
                $"ring_outer_number= '{trayCoatingThickness.RingOuter.SerialNumber}'" +
                $"where tray_guid = '{trayID}'";
            return cmd_update;
        }
        /// <summary>
        /// 每个环对应的数据插入Sql语句拼接
        /// </summary>
        /// <param name="coatingData"></param>
        /// <returns></returns>
        private string Cmd_main_insert(CoatingData coatingData)
        {
            return "insert into tray_thickness_main" +
            "(tray_guid,tray_number,pm_number, tray_coating_thickness, tray_max_thickness,tray_label,create_time)" +
            $"values ('{Guid.NewGuid()}'," +
            $"'{coatingData.SerialNumber}'," +
            $"'{coatingData.PmOwned}'," +
            $"'{coatingData.Current}'," +
            $"'{coatingData.MAX}'," +
            $"'{coatingData.Caption}'," +
            $"'{coatingData.CreatTime}')";
        }
    }
}
