using Furion.DependencyInjection; using Mapster; using Microsoft.AspNetCore.Hosting; using MiniExcelLibs; using Nirvana.Common; using Nirvana.Common.ApiBase; using SqlSugar; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using YBDevice.Entity; using YBDevice.NApi.Application.UserInfo; namespace YBDevice.NApi.Application.DeviceInfo { public class DeviceService : IDeviceService, ITransient { private readonly ISqlSugarRepository repository; private readonly SqlSugarClient dbClient; private readonly IWebHostEnvironment _hostingEnvironment; public DeviceService(ISqlSugarRepository sqlSugarRepository, IWebHostEnvironment hostingEnvironment) { repository = sqlSugarRepository; dbClient = repository.Context; _hostingEnvironment = hostingEnvironment; } /// /// 根据扫码的设备列表导出EXCEL /// /// /// public async Task ExportExcelByScanListAsync(ScanListC2SDto data) { var list = data.Data.Split(',').ToList(); string savefolder = _hostingEnvironment.WebRootPath; string rootname = $"/files/scan/download"; string filename = $"{data.Name}_{DateTime.Now.ToString("yyyyMMdd")}.csv"; string path = Path.Combine(savefolder + "/" + rootname, filename); string virtualpath = $"{rootname}/{filename}"; if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } else { //检查文件是否存在 if (File.Exists(path)) { return new ResultInfo(ResultState.SUCCESS, "success", virtualpath); } } var savedata = list.Select(x => new { code = x }).ToList(); var config = new MiniExcelLibs.Csv.CsvConfiguration() { StreamReaderFunc = (stream) => new StreamReader(stream, Encoding.UTF8) }; await MiniExcel.SaveAsAsync(path, savedata, excelType: ExcelType.CSV, configuration: config); return new ResultInfo(ResultState.SUCCESS, "success", virtualpath); } /// /// 获取设备信息 /// /// 设备序列号 /// public async Task GetDeviceAsync(string facecode) { var equ = await dbClient.Queryable().FirstAsync(x => x.FacCode == facecode); //如果设备类型是53或者54,则使用默认设备 if(equ !=null && (equ.Type == 53 || equ.Type == 54)) { equ = equ.Type == 53? await dbClient.Queryable().FirstAsync(x => x.Id == 29107): await dbClient.Queryable().FirstAsync(x => x.Id == 29108); } return equ; } /// /// 获取设备信息 /// /// 设备机器码 /// public async Task GetDeviceByEcodeAsync(string ecode) { return await dbClient.Queryable().FirstAsync(x => x.Ecode == ecode); } /// /// 获取设备 /// /// /// public async Task GetEquAsync(string ecode) { var data = await dbClient.Queryable().FirstAsync(x => x.Ecode == ecode); var types = await dbClient.Queryable().ToListAsync(); return new DeviceAndTypeModel { equ = data, types = types }; } /// /// 设备列表 /// /// public async Task> GetListAsync() { return await dbClient.Queryable().ToListAsync(); } /// /// 根据设备确定要使用的公众号或者小程序 /// /// /// public async Task GetOfficeByDeviceAsync(YB_Device data) { var devicetype = await dbClient.Queryable().FirstAsync(x => x.Code == data.Type); if (devicetype.VerType == DeviceVerType.Base) { return "gh_e2e73d281def"; //每日一称 } return "gh_5efb6d7330a5"; } /// /// 获取设备类型列表 /// ///小程序appid /// public async Task GetTypeListAsync(string appid) { var newlist = new List(); var AppData = await dbClient.Queryable().Where(x => x.Status == StatusType.Enabled && x.AppId == appid).FirstAsync(); if (AppData != null) { var ids = AppData.DevType.Split(",").ToList(); var types = await dbClient.Queryable().ToListAsync(); // var InIds = new List(); types.ForEach(x => { //List newids = x.Content.Split(",").ToList(); //bool IsExist = false; //for (var i = 0; i < InIds.Count; i++) //{ // if (newids.Contains(InIds[i])) // { // IsExist = true; // break; // } //} if (ids.Contains($"{x.Code}") // && !IsExist ) { newlist.Add(new YB_DeviceTypeExt { Name = x.ExtName, HeadImg = x.HeadImg, Id = x.Code == 911 ? 11 : x.Code }); // InIds.Add($"{x.Code}"); } }); } var returnlist = newlist.Adapt>(); return new ResultInfo(ResultState.SUCCESS, "success", returnlist); } /// /// 设备注册 /// /// 设备资料 /// public async Task RegAsync(YB_Device yB_Device) { yB_Device.Ecode = yB_Device.Ecode.ToStr(); yB_Device.FacCode = yB_Device.FacCode.ToStr(); if (await dbClient.Queryable().AnyAsync(x => x.Ecode == yB_Device.Ecode)) { return new ResultInfo(ResultState.FAIL, "此机器码已存在"); } if (await dbClient.Queryable().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 = DeviceStatus.UnActive; yB_Device.Remark = string.Empty; yB_Device.CreateTime = DateTime.Now; await dbClient.Insertable(yB_Device).ExecuteCommandAsync(); return new ResultInfo(ResultState.SUCCESS, "注册成功"); } /// /// YB_Result迁移到YB_nResult /// public void Result2NewResult() { //获取所有的YB_ResultExt,YB_Measure,YB_BodyMeasure //var starttime = DateTime.Parse("2021-09-06 07:37:01"); //var list = dbClient.Queryable((r, m, s) => new JoinQueryInfos( // JoinType.Left, r.ResultId == m.ResultId, // JoinType.Left, m.ResultId == s.ResultId // )) // .Where((r, m, s) => r.CreateTime > starttime) // .Select((r, m,s) => new ttt // { // Id=r.Id, // ResultId = r.ResultId, // sfr = m.sfr, // Sex=m.Sex, // SfrVal=s.SfrVal, // SkeletalMuscle=s.SkeletalMuscle, // SourceType=r.SourceType, // TakeStatus=1, // Age=m.Age, // bmi=m.bmi, // cmi=m.cmi, // body=m.body, // bodyage=m.bodyage, // BodyFat=s.BodyFat, // BodyFatVal=s.BodyFatVal, // BodyMuscle=s.BodyMuscle, // BodyMuscleVal=s.BodyMuscleVal, // bone=m.bone, // BusinessId=r.BusinessId, // createtime=r.CreateTime, // CreateTime=r.CreateTime, // Ecode=r.Ecode, // EquId=r.EquId, // FacEcode=r.FacEcode, // FamilyId=m.FamilyId, // FansId=r.FansId, // fatlevel=m.fatlevel, // fat_r=m.fat_r, // fat_w=m.fat_w, // Height=m.Height, // IdealWeight=s.IdealWeight, // Imp=r.Imp, // kcal=m.kcal, // lbm=m.lbm, // LeftFootFat=s.LeftFootFat, // LeftFootFatVal=s.LeftFootFatVal, // LeftFootMuscle=s.LeftFootMuscle, // LeftFootMuscleVal=s.LeftFootMuscleVal, // LeftHandFat=s.LeftHandFat, // LeftHandFatVal=s.LeftHandFatVal, // LeftHandMuscle=s.LeftHandMuscle, // LeftHandMuscleVal=s.LeftHandMuscleVal, // LevelJson=s.LevelJson, // Month=m.Month, // muscle=m.muscle, // muscleval=m.muscleval, // OrderId=r.OrderId, // protein=m.protein, // proteinval=m.proteinval, // PublicId=r.PublicId, // RightFootFat=s.RightFootFat, // RightFootFatVal=s.RightFootFatVal, // RightFootMuscle=s.RightFootMuscle, // RightFootMuscleVal=s.RightFootMuscleVal, // RightHandFat=s.RightHandFat, // RightHandFatVal=s.RightHandFatVal, // RightHandMuscle=s.RightHandMuscle, // RightHandMuscleVal=s.RightHandMuscleVal, // UserId=r.UserId, // visceral= m.visceral, // water = m.water, // Weight = m.Weight // }) // .ToList(); //List rlist = new List(); //List mlist = new List(); //List ulist = new List(); //List alist = new List(); //list.ForEach(x => { // //添加yb_nresult // var equ = dbClient.Queryable().First(e => e.Id == x.EquId); // var ybresult = new YB_nResult // { // Id = IDGen.NextID(), // SourceType = x.SourceType, // BusinessId = x.BusinessId, // CreateTime = x.CreateTime, // DevType = equ !=null?equ.Type:1, // EquId = x.EquId, // Height = x.Height, // Imp = x.Imp, // LeftArmImp = 0, // LeftLegImp =0, // RightArmImp = 0, // RightLegImp =0, // Weight = x.Weight // }; // rlist.Add(ybresult); // //添加yb_nmeasureresult // var measureresult = new YB_nMeasureResult { // Id = ybresult.Id, // DevType = ybresult.DevType, // sfr = x.sfr, // Sex = x.Sex, // SfrVal =x.SfrVal, // SkeletalMuscle = x.SkeletalMuscle, // Age = x.Age, // bmi = x.bmi, // body = x.body.ToStr(), // bodyage = x.bodyage, // BodyFat = x.BodyFat, // BodyFatVal = x.BodyFatVal, // BodyMuscle =x.BodyMuscle, // BodyMuscleVal =x.BodyMuscleVal, // bone = x.bone, // cmi = x.cmi, // createtime = x.createtime, // fatlevel = x.fatlevel.ToStr(), // fat_r = x.fat_r, // fat_w = x.fat_w, // Height = x.Height, // IdealWeight = x.IdealWeight, // kcal = x.kcal, // lbm = x.lbm, // LeftFootFat =x.LeftFootFat, // LeftFootFatVal = x.LeftFootFatVal, // LeftFootMuscle = x.LeftFootMuscle, // LeftFootMuscleVal =x.LeftFootMuscleVal, // LeftHandFat =x.LeftHandFat, // LeftHandFatVal =x.LeftHandFatVal, // LeftHandMuscle = x.LeftHandMuscle, // LeftHandMuscleVal = x.LeftHandMuscleVal, // Month = x.Month, // muscle = x.muscle, // muscleval = x.muscleval, // protein = x.protein, // proteinval = x.proteinval, // RightFootFat = x.RightFootFat, // RightFootFatVal = x.RightFootFatVal, // RightFootMuscle = x.RightFootMuscle, // RightFootMuscleVal = x.RightFootMuscleVal, // RightHandFat = x.RightHandFat, // RightHandFatVal = x.RightHandFatVal, // RightHandMuscle = x.RightHandMuscle, // RightHandMuscleVal = x.RightHandMuscleVal, // visceral = x.visceral, // water = x.water, // Weight = x.Weight // }; // mlist.Add(measureresult); // //添加yb_nuserreasult // var userresult = new YB_nUserResult { // CreateTime = x.CreateTime, // DevType = ybresult.DevType, // FamilyId = x.FamilyId, // FansId = !string.IsNullOrEmpty(x.FansId)?Guid.Parse(x.FansId):null, // Id = ybresult.Id, // UserId = x.UserId // }; // ulist.Add(userresult); //}); //if (rlist.Count > 0) //{ // dbClient.Insertable(rlist).ExecuteCommand(); //} //if (mlist.Count > 0) //{ // dbClient.Insertable(mlist).ExecuteCommand(); //} //if (ulist.Count > 0) //{ // dbClient.Insertable(ulist).ExecuteCommand(); //} } //public class ttt:YB_Measure //{ // /// // /// 躯干肌肉率 // /// // public System.Decimal BodyMuscle { get; set; } // /// // // /// 左脚肌肉率 /// // // public System.Decimal LeftFootMuscle { get; set; } // /// // // /// 右脚肌肉率 /// // // public System.Decimal RightFootMuscle { get; set; } // /// // // /// 左手肌肉率 /// // // public System.Decimal LeftHandMuscle { get; set; } // /// // // /// 右手肌肉率 /// // // public System.Decimal RightHandMuscle { get; set; } // /// // // /// 躯干脂肪率 /// // // public System.Decimal BodyFat { get; set; } // /// // // /// 左脚脂肪率 /// // // public System.Decimal LeftFootFat { get; set; } // /// // // /// 右脚脂肪率 /// // // public System.Decimal RightFootFat { get; set; } // /// // // /// 左手脂肪率 /// // // public System.Decimal LeftHandFat { get; set; } // /// // // /// 右手脂肪率 /// // // public System.Decimal RightHandFat { get; set; } // /// // // /// 躯干肌肉量 /// // // public System.Decimal BodyMuscleVal { get; set; } // /// // // /// 左脚肌肉量 /// // // public System.Decimal LeftFootMuscleVal { get; set; } // /// // // /// 右脚肌肉量 /// // // public System.Decimal RightFootMuscleVal { get; set; } // /// // // /// 左手肌肉量 /// // // public System.Decimal LeftHandMuscleVal { get; set; } // /// // // /// 右手肌肉量 /// // // public System.Decimal RightHandMuscleVal { get; set; } // /// // // /// 躯干脂肪量 /// // // public System.Decimal BodyFatVal { get; set; } // /// // // /// 左脚脂肪量 /// // // public System.Decimal LeftFootFatVal { get; set; } // /// // // /// 右脚脂肪量 /// // // public System.Decimal RightFootFatVal { get; set; } // /// // // /// 左手脂肪量 /// // // public System.Decimal LeftHandFatVal { get; set; } // /// // // /// 右手脂肪量 /// // // public System.Decimal RightHandFatVal { get; set; } // /// // // /// 皮下脂肪量,kg /// // // public System.Decimal SfrVal { get; set; } // /// // // /// 理想体重 /// // // public System.Decimal IdealWeight { get; set; } // /// // // /// 骨骼肌量,kg /// // // public System.Decimal SkeletalMuscle { get; set; } // /// // // /// 等级标准 /// // // public System.String LevelJson { get; set; } /// // // /// 阻抗值 /// // // public System.Decimal Imp { get; set; } // /// // // /// 设备ID /// // // public System.Int32 EquId { get; set; } // /// // // /// 设备机器码 /// // // public System.String Ecode { get; set; } // /// // // /// 设备序列号 /// // // public System.String FacEcode { get; set; } // /// // // /// 来源,1-蓝牙传输,2-手动记录,3-扫码记录 /// // // public System.Int32 SourceType { get; set; } // /// // // /// 使用的订单ID /// // // public System.Guid OrderId { get; set; } // /// // // /// 所属的公众号/小程序 /// // // public System.String PublicId { get; set; } // /// // // /// 扫码时使用的用户唯一标识 /// // // public System.String FansId { get; set; } // /// // // /// 用户ID /// // // public System.Int32 UserId { get; set; } // /// // // /// 客户ID /// // // public System.Int32 BusinessId { get; set; } // /// // /// 创建时间 // /// // public System.DateTime CreateTime { get; set; } //} } }