using FactorySystemCommon;
using FactorySystemModel.SqlSugarModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace FactorySystemBll
{
    public class MaterialTypeBll
    {
        /// <summary>
        /// 导入分类
        /// </summary>
        public int InsertBatchMaterialTypeData(List<TFS_MaterialInfo> mainList, List<PropertyInfo> mainProp, int userId)
        {
            int result = 0;
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            foreach (TFS_MaterialInfo item in mainList)
            {
                item.FQualityTest2 = item.FQualityTest2.Replace("~", "@$&|&$@").Replace("&$&||&$&", "~");
                string[] typeNames = item.FQualityTest2.Split('~');
                item.FQualityTest2 = typeNames[0].Replace("@$&|&$@", "~");
                typeNames[1] = typeNames[1].Replace("@$&|&$@", "~");
                typeNames[2] = typeNames[2].Replace("@$&|&$@", "~");
                string typeName = typeNames[1];

                List<int> typeTemps1 = db.Queryable<TFS_MaterialType>().Where(s => s.FName == typeName && s.FDepth == 1
                && s.FDeleted != 1).Select(s => s.FID).ToList();
                if (typeTemps1.Count == 0) typeTemps1 = new List<int>() { CreateType(db, typeNames[1], 1, userId).FID };

                typeName = typeNames[2];
                TFS_MaterialType typeTemp2 = db.Queryable<TFS_MaterialType>().Where(s => s.FName == typeName &&
                s.FDepth == 2 && s.FDeleted != 1 && typeTemps1.Contains(s.FParentID)).First();
                if (typeTemp2 == null) typeTemp2 = CreateType(db, typeNames[2], 2, userId, typeTemps1.First());

                item.FType = 1;
                item.FDataID = typeTemp2.FID;

                TFS_MaterialInfo dbInfo = db.Queryable<TFS_MaterialInfo>().Where(s => s.FType == 1 && s.FDataID == typeTemp2.FID).First();
                if (dbInfo == null)
                {
                    item.FAddUser = userId;
                    item.FID = db.Insertable(item).IgnoreColumns(true).ExecuteReturnIdentity();
                }
                else
                {
                    item.FID = dbInfo.FID;
                    item.FEditUser = userId;
                    item.FEditDate = DateTime.Now;
                    db.Updateable(item).IgnoreColumns(true).ExecuteCommand();
                }
                result += 1;
            }
            ExceptionHelper.AddSystemJournal(null, mainList, result, userId, "InsertBatchMaterialTypeData");
            return result;
        }

        private TFS_MaterialType CreateType(SqlSugarClient db, string name, int depth = 1, int userId = -1, int parentId = -1)
        {
            TFS_MaterialType temp = new TFS_MaterialType()
            {
                FName = name,
                FDepth = depth,
                FParentID = parentId,
                FState = 1,
                FAddUser = userId
            };
            temp.FID = db.Insertable(temp).ExecuteReturnIdentity();
            return temp;
        }

        public List<TFS_MaterialType> GetMaterialTypeByID(int id)
        {
            return AppSettingsHelper.GetSqlSugar()
                .Queryable<TFS_MaterialType>()
                .Where(a=>a.FID==id).ToList();
        }

    }
}