using Infrastructure;
using Newtonsoft.Json.Linq;
using OpenAuth.App.Base;
using OpenAuth.App.Interface;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.App.BaseApp.Base;

namespace OpenAuth.App
{
    public class SysAppFilesApp : SqlSugarBaseApp<SysAppFiles, SugarDbContext>
    {
        public SysAppFilesApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<SysAppFiles> repository, IAuth auth) : base(unitWork, repository, auth)
        {

        }
        #region app文件

        /// <summary>
        /// 添加App文件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Response<bool> AddAppFiles(SysAppFiles model)
        {
            var response = new Response<bool>();

            var user = _auth.GetCurrentUser().User;

            model.Id = Guid.NewGuid().ToString();
            model.createtime = DateTime.Now;
            model.createuser = user.Account;

            var flag = base.Repository.Insert(model);
            if (flag)
            {
                response.Result = true;
                response.Message = "添加成功";
            }
            else
            {
                response.Result = false;
                response.Message = "添加失败";
            }
            return response;
        }

        /// <summary>
        /// 获取下载更新的文件路径
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public object GetUpdateFiles(string project)
        {

            var model = base.Repository.AsQueryable().Where(c => c.project_name == project).OrderBy(c => c.createtime, OrderByType.Desc).Select(c => new
            {
                c.Id,
                edition = SqlFunc.ToInt32(c.edition),
                c.description,
                c.filepath,
                c.createtime,
                c.createuser,
                c.must_update,
                c.project_name,
            }).First();

            if (model != null)
            {
                JObject obj = new JObject();

                string filename = model.filepath.Substring(model.filepath.LastIndexOf("\\") + 1);

                obj.Add("Id", model.Id);
                obj.Add("edition", model.edition);
                obj.Add("description", model.description);
                obj.Add("filepath", model.filepath);
                obj.Add("filename", filename);
                obj.Add("createtime", model.createtime);
                obj.Add("createuser", model.createuser);
                obj.Add("must_update", model.must_update);
                obj.Add("project_name", model.project_name);

                return obj;
            }
            else
            {
                return null;
            }
        }


        public SysAppFiles GetByProject(string project)
        {
            return base.Repository.AsQueryable().Where(c => c.project_name == project).OrderBy(c => c.createtime, SqlSugar.OrderByType.Desc).First();
        }
        #endregion
    }
}
