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

namespace MECF.Framework.UI.Client.TrayThickness
{
    public class PMDataViewSqlHelp
    {
        private string minor_cycle, major_cycle, ring_inner_number, ring_outer_number;
        /// <summary>
        /// 获取Truy对象整体的数据
        /// </summary>
        /// <param name="_trayGuid"></param>
        /// <returns></returns>
        public PMCoatingThickness GetTrayCoatingThickness(string _trayGuid)
        {
            //这是一次查询一个对象
            PMCoatingThickness trayCoatingThickness = null;
            try
            {
                //根据TrayID获取对应内外编号
                string cmdID = $"select * from pm_thickness_data where pm_guid = '{_trayGuid}'";
                DataTable DataTable = QueryDataClient.Instance.Service.QueryData(cmdID);
                if (DataTable != null && DataTable.Rows.Count > 0)
                {

                    trayCoatingThickness = new PMCoatingThickness();
                    minor_cycle = DataTable.Rows[0]["pm_minor_cycle"].ToString();
                    major_cycle = DataTable.Rows[0]["pm_major_cycle"].ToString();
                    
                    trayCoatingThickness.MinorCycle = GetCoatingData(minor_cycle, out _);
                    trayCoatingThickness.MajorCycle = GetCoatingData(major_cycle, 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 pm_thickness_main where pm_cycle = '{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]["pm_cycle"].ToString(),
                    MAX = dt.Rows[0]["pm_max_thickness"].ToString(),
                    PmOwned = dt.Rows[0]["pm_number"].ToString(),
                    Current = dt.Rows[0]["pm_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(PMCoatingThickness trayCoatingThickness, string trayUid, List<CoatingData> coatingData)
        {
            List<string> cmdList = new List<string>();

            string cmdID = $"select * from pm_thickness_data where pm_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 minor_cycle, major_cycle from pm_thickness_data WHERE pm_guid = '{trayUid}' limit 1";
            var dt = QueryDataClient.Instance.Service.QueryData(sql);
            if (dt is { Rows.Count: > 0 })
            {
                //partSnList.Add();
            }

            return false;
        }

        /// <summary>
        /// PM中对象编号插入Sql语句拼接
        /// </summary>
        /// <param name="pmCoatingThickness"></param>
        /// <param name="pmGuid"></param>
        /// <returns></returns>
        private string Cmd_data_insert(PMCoatingThickness pmCoatingThickness, string pmGuid)
        {
            string cmd_data_insert =
                "insert into pm_thickness_data" +
                "(pm_guid,pm_minor_cycle, pm_major_cycle)" +
                $"values ('{pmGuid}'," +
                $"'{pmCoatingThickness.MinorCycle.SerialNumber}'," +
                $"'{pmCoatingThickness.MajorCycle.SerialNumber}')";
            return cmd_data_insert;
        }
        /// <summary>
        /// PM中对象编号更新Sql语句拼接
        /// </summary>
        /// <param name="PMCoatingThickness"></param>
        /// <param name="pmGuid"></param>
        /// <returns></returns>
        private string Cmd_update(PMCoatingThickness PMCoatingThickness, string pmGuid)
        {
            string cmd_update =
                $"update pm_thickness_data set " +
                $"pm_minor_cycle = '{PMCoatingThickness.MinorCycle.SerialNumber}', " +
                $"pm_major_cycle= '{PMCoatingThickness.MajorCycle.SerialNumber}'" +
                $" where pm_guid = '{pmGuid}'";
            return cmd_update;
        }
        /// <summary>
        /// 每个环对应的数据插入Sql语句拼接
        /// </summary>
        /// <param name="coatingData"></param>
        /// <returns></returns>
        private string Cmd_main_insert(CoatingData coatingData)
        {
            return "insert into pm_thickness_main" +
            "(pm_guid,pm_cycle,pm_number, pm_coating_thickness, pm_max_thickness,pm_label,create_time)" +
            $"values ('{Guid.NewGuid()}'," +
            $"'{coatingData.SerialNumber}'," +
            $"'{coatingData.PmOwned}'," +
            $"'{coatingData.Current}'," +
            $"'{coatingData.MAX}'," +
            $"'{coatingData.Caption}'," +
            $"'{coatingData.CreatTime}')";
        }
    }
}
