using FactorySystemCommon;
using FactorySystemModel.SqlSugarModel;
using SqlSugar;
using System.Collections.Generic;
using FactorySystemModel.EnumModel;

namespace FactorySystemBll
{
    public class FactoryBll
    {
        /// <summary>
        /// 获取配置信息
        /// </summary>
        public static List<TFS_Factory> GetFactoryList()
        {
            int isDelete = (int)Constant.DeleteCode.已删除;
            return AppSettingsHelper.GetSqlSugar().Queryable<TFS_Factory>().Where(s => s.FDeleted != isDelete).ToList();
        }

        /// <summary>
        /// 验证工厂代码是否重复
        /// </summary>
        public object CheckHasCode(Dictionary<string, object> insertData)
        {
            SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
            int isDelete = (int)Constant.DeleteCode.已删除;
            string code = insertData["FCode"].ToString();
            if (int.TryParse(insertData["FID"].ToString(), out int dataId) && dataId > 0)
            {
                return AppSettingsHelper.GetSqlSugar().Queryable<TFS_Factory>().Where(s => s.FDeleted != isDelete 
                && s.FCode == code && s.FID != dataId).Count();
            }
            else
            {
                return AppSettingsHelper.GetSqlSugar().Queryable<TFS_Factory>().Where(s => s.FDeleted != isDelete && s.FCode == code).Count();
            }
        }
    }
}
