SysAppService.cs 4.82 KB
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
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.IO;

namespace Hh.Mes.Service.WebService.Base
{
    /// <summary>
    /// app管理服务
    /// </summary>
    public class SysAppService : RepositorySqlSugar<sys_app>
    {
        #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>
        /// 导入 文件格式:xxx-v1.apk
        /// </summary>
        public dynamic Import(string appType, string http, int port, IFormFileCollection files, IHostingEnvironment environment)
        {
            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 || fileName.IndexOf("-v", 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", ""));
                var isExtFile = Context.Queryable<sys_app>().Any(x => x.fileName == fileName && x.ver >= ver);
                if (isExtFile)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = $"文件名:{fileName},已存在该版本或者不能小于历史版本号!";
                    return response;
                }
                #endregion

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

                #region 数据保存到数据库
                var url = http + "://" + base.GetDictionaryDictValue("UrlApp", "GetUrl") + ":" + port;
                sys_app model = new sys_app
                {
                    appType = appType,
                    filePath = url + "/UploadFile/" + newFileName,
                    ver = ver,
                    fileName = fileName,
                    keys = Guid.NewGuid(),
                    createBy = sysWebUser?.Account,
                    createTime = DateTime.Now
                };
                response.Status = base.Insert(model);
                response.Code = response.Status ? 200 : 500;
                #endregion
                return response;
            });
        }

        #endregion
    }
}