using Nirvana.Common;
using Nirvana.Common.ApiBase;
using Nirvana.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.NApi.DBServices
{
    /// <summary>
    /// 设备管理
    /// </summary>
    public partial class DeviceApp : Repository<YB_Device>
    {
        /// <summary>
        /// 设备注册
        /// </summary>
        /// <param name="yB_Device">设备资料</param>
        /// <returns></returns>
        public async Task<ResultInfo> RegAsync(YB_Device yB_Device)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                yB_Device.Ecode = yB_Device.Ecode.ToStr();
                yB_Device.FacCode = yB_Device.FacCode.ToStr();
                if (await dbClient.Queryable<YB_Device>().AnyAsync(x => x.Ecode == yB_Device.Ecode))
                {
                    return new ResultInfo(ResultState.FAIL, "此机器码已存在");
                }
                if (await dbClient.Queryable<YB_Device>().AnyAsync(x => x.FacCode == yB_Device.FacCode))
                {
                    return new ResultInfo(ResultState.FAIL, "此序列号已存在");
                }
                //注册设备
                yB_Device.Name = yB_Device.FacCode;
                yB_Device.BusinessId = 0;
                yB_Device.Status = (int)DeviceStatus.UnActive;
                yB_Device.Remark = string.Empty;
                yB_Device.CreateTime = DateTime.Now;
                await dbClient.Insertable<YB_Device>(yB_Device).ExecuteCommandAsync();
                return new ResultInfo(ResultState.SUCCESS, "注册成功");
            }
        }

        
    }
}
