SysAppService.cs 6.21 KB
using ApkInfo;
using Google.Protobuf.WellKnownTypes;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Threading;

namespace Hh.Mes.Service.WebService.Base
{
    /// <summary>
    /// app管理服务
    /// </summary>
    public class SysAppService : RepositorySqlSugar<sys_app>
    {
        private string appId { get; set; }

        #region app管理
        /// <summary>
        /// //获取列表
        /// </summary>
        public Response Load(PageReq pageRequest, sys_app model)
        {
            int total = 0;
            var sysdata = Context.Queryable<sys_app>().OrderBy(x => x.id, OrderByType.Desc).ToOffsetPage(pageRequest.page, pageRequest.limit, ref total);
            return new Response()
            {
                Result = sysdata,
                Count = total
            };
        }


        /// <summary>
        /// //获取列表
        /// </summary>
        public string Download()
        {
            var filePath = Context.Queryable<sys_app>().OrderBy(x => x.id, OrderByType.Desc).Select(x => x.filePath).First();
            return filePath != null ? filePath : "";
        }


        /// <summary>
        /// 删除
        /// </summary>
        public dynamic DelByIds(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = Context.Deleteable<sys_app>().In(ids).ExecuteCommand() > 0;
                if (!result)
                {
                    response.Message = "删除失败";
                }
                return response;

            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(sys_app model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                response.Status = Update(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }


        /// <summary>
        /// 导入 文件格式:xxx-v1.apk
        /// </summary>
        public dynamic Import(string http, int port, IFormFileCollection files, string contentRootPath)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                #region before
                if (files.Count != 1)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = "请选择上传一个apk文件";
                    return response;
                }
                var fileName = Path.GetFileName(files[0].FileName);
                if (fileName.IndexOf(".apk", StringComparison.Ordinal) == -1)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = "请选择.apk文件的正确格式:xxx-v1.apk";
                    return response;
                }
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(files[0].FileName);// 没有扩展名的文件名 “Default”
                                                                                                      //var ver = double.Parse(fileNameWithoutExtension.Split("-")[1].Replace("v", ""));
                #endregion

                #region apk 文件上传
                var upFloder = Path.Combine(contentRootPath + "\\wwwroot", uploadFolder + "\\APP");
                if (!Directory.Exists(upFloder)) Directory.CreateDirectory(upFloder);
                var suffix = Path.GetExtension(files[0].FileName);
                var newFileName = DateTime.Now.ToString("yyMMddhhmmss") + suffix;
                var filePath = Path.Combine(upFloder, newFileName);
                using (var fs = File.Create(filePath))
                {
                    files[0].CopyTo(fs);
                    fs.Flush();
                }
                #endregion

                ApkDecoder apkDecoder = new ApkDecoder(filePath, contentRootPath);
                Thread.Sleep(500);
                #region 数据保存到数据库
                var url =http + "://" + base.GetDictionaryDictValue("UrlApp", "GetUrl") + ":" + port;
                if (apkDecoder.AppVersionCode != null && apkDecoder.PkgName != null)
                {
                    //通过appid标识以及版本号判断是否已存在该版本(不能通过文件名称判断,因为此值用户可随意更改)
                    var isExtFile = Context.Queryable<sys_app>().Any(x => x.appId == apkDecoder.PkgName && x.ver >= Convert.ToDouble(apkDecoder.AppVersionCode));
                    if (isExtFile)
                    {
                        response.Code = 500;
                        response.Status = false;
                        response.Message = $"文件名:{fileName},已存在该版本或者不能小于历史版本号!";
                        return response;
                    }
                    sys_app model = new sys_app
                    {
                        appType = "",
                        filePath = url + "/UploadFile/APP/" + newFileName,
                        ver = Convert.ToInt32(apkDecoder.AppVersionCode),
                        fileName = fileName,
                        keys = Guid.NewGuid(),
                        createBy = sysWebUser?.Account,
                        createTime = DateTime.Now,
                        appId = apkDecoder.PkgName,
                    };
                    response.Status = base.Insert(model);
                    response.Code = response.Status ? 200 : 500;
                }
                #endregion
                return response;
            });
        }
        #endregion

        private void apkDecoder_InfoParsed(ApkDecoder apkDecoder)
        {
            this.appId = apkDecoder.PkgName;
        }
    }
}