using Infrastructure;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using OpenAuth.App.Base;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
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 WFStampApp : SqlSugarBaseApp<WFStamp, SugarDbContext>
    {
        public WFStampApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<WFStamp> repository, IAuth auth) : base(unitWork, repository, auth)
        {
        }

        #region 获取数据
        /// <summary>
        /// 获取签章列表
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public async Task<List<WFStamp>> GetList(string userId)
        {
            return await base.Repository.GetListAsync(t => t.UserIds.Contains(userId) && t.EnabledMark == 1);
        }
        /// <summary>
        /// 获取管理分页列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryParams">查询参数</param>
        /// <returns></returns>
        public async Task<PageInfo<List<WFStamp>>> GetPageList(PageReq pageReq, WFStamp queryParams)
        {
            RefAsync<int> totalCount = 0;

            var expression = Expressionable.Create<WFStamp>()
                .AndIF(!string.IsNullOrEmpty(queryParams.StampName), t => t.StampName.Contains(queryParams.StampName))
                .AndIF(queryParams.EnabledMark != null, (t => t.EnabledMark == queryParams.EnabledMark))
                .AndIF(!string.IsNullOrEmpty(queryParams.StampType), t => t.StampType == queryParams.StampType);

            var list = await base.Repository
                .GetPageListAsync(expression.ToExpression(),
                new PageModel
                {
                    PageIndex = pageReq.page,
                    PageSize = pageReq.limit,
                    TotalCount = totalCount
                });

            return new PageInfo<List<WFStamp>>
            {
                Items = list,
                Total = totalCount
            };
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存印章信息（新增/编辑）
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="entity">实体</param>
        public async Task<Response<bool>> SaveEntity(string keyValue, WFStamp entity)
        {
            var flag = false;
            entity.User = _auth.GetUserId();
            entity.CreateDate = DateTime.Now;
            if (string.IsNullOrEmpty(keyValue))
            {
                entity.StampId = Guid.NewGuid().ToString(); //产生印章编号
                flag = await base.Repository.InsertAsync(entity);
            }
            else
            {
                entity.StampId = keyValue;
                flag = await base.Repository.UpdateAsync(entity);
            }

            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }

        /// <summary>
        /// 删除印章信息
        /// </summary>
        /// <param name="keyVlaue">主键</param>
        public async Task<Response<bool>> DeleteEntity(string keyVlaue)
        {
            var flag = await base.Repository.DeleteByIdAsync(keyVlaue);//删除操作
            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }
        #endregion

        #region 扩展方法
        /// <summary>
        /// 更新数据状态
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="state">状态 1启用 0禁用</param>
        public async Task<Response<bool>> UpdateState(string keyValue, int state)
        {
            var flag = await base.Repository
                .UpdateSetColumnsTrueAsync(
                    a => new WFStamp { EnabledMark = state },
                    a => a.StampId == keyValue
                );//删除操作
            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }

        public async Task<WFStamp> GetEntity(string keyValue)
        {
            return await base.Repository.GetByIdAsync(keyValue);
        }

        /// <summary>
        /// 密码匹配
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public async Task<Response<bool>> EqualPassword(string keyValue, string password)
        {
            var reponse = new Response<bool>();

            WFStamp entity = await GetEntity(keyValue);
            if (entity.Password.Equals(password))//加密后进行对比
            {
                reponse.Result = true;
                reponse.Message = "success";
            }
            else
            {
                reponse.Result = false;
                reponse.Message = "error";
            }
            return reponse;
        }

        /// <summary>
        /// 签章图片归档并验证密码
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public Task<string> ToWfImg(string keyValue, string password) { return null; }
        #endregion
    }
}
