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

namespace OpenAuth.App.ServiceApp.TaxGongxianManage
{
    public  class TaxGongxianApp : SqlSugarBaseApp<TaxGongxiandi, SugarDbContext>
    {
        private ISqlSugarClient client;
        CommonDataManager _commonDataManager;

        #region 构造函数
        public TaxGongxianApp(
             CommonDataManager commonDataManager,
            ISugarUnitOfWork<SugarDbContext> unitWork,
            ISimpleClient<TaxGongxiandi> repository,
            IAuth auth,
            ISqlSugarClient sqlSugarClient
            ) : base(unitWork, repository, auth)
        {
            this.client = sqlSugarClient;
            _commonDataManager = commonDataManager;
        }
        #endregion

        #region  土地贡献
        #region  获取分页数据列表
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<Response<PageInfo<List<TaxGongxiandi>>>> LoadGongxiandiPageList(string sszdbm, int page, int limit)
        {
            RefAsync<int> totalCount = 0;
            var query = client.Queryable<TaxGongxiandi>()
                .WhereIF(!string.IsNullOrEmpty(sszdbm), r => r.sszdbm == sszdbm);


            var result = await query.ToPageListAsync(page, limit, totalCount);

            return new Response<PageInfo<List<TaxGongxiandi>>>
            {
                Code = 200,
                Message = "success",
                Result = new PageInfo<List<TaxGongxiandi>>
                {
                    Items = result,
                    Total = totalCount
                }
            };
        }

        /// <summary>
        /// 根据id获取单个土地贡献信息
        /// </summary>
        /// <param name="gid">土地贡献gid</param>
        /// <returns></returns>
        public async Task<TaxGongxiandi> GetGongxiandiInfoById(string id)
        {
            var list = await client.Queryable<TaxGongxiandi>().Where(r => r.Id == id).FirstAsync();
            return list;
        }

        /// <summary>
        /// 根据所属宗地编码获取地线信息
        /// </summary>
        /// <param name="sszdbm">所属宗地编码</param>
        /// <returns></returns>
        public async Task<List<TaxGongxiandi>> GetGongxiandiInfoByBM(string sszdbm)
        {
            var list = await client.Queryable<TaxGongxiandi>().Where(r => r.sszdbm == sszdbm).ToListAsync();
            return list;
        }
        #endregion

        #region 编辑土地贡献信息
        /// <summary>
        /// 添加土地贡献信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<Response<bool>> AddGongxiandi(TaxGongxiandi req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                req.Id=Guid.NewGuid().ToString();
                bool insert = await uow.TaxGongxiandi.InsertAsync(req);
                bool flag = uow.Commit();

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


        /// <summary>
        /// 修改土地贡献信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<Response<bool>> UpdateGongxiandi(TaxGongxiandi req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {

                bool update = await uow.TaxGongxiandi.UpdateAsync(req);
                bool flag = uow.Commit();

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

        /// <summary>
        /// 删除土地贡献
        /// </summary>
        /// <param name="ids"></param>
        public async Task<Response<bool>> DeleteGongxiandi(string[] ids)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                var delete = await uow.TaxGongxiandi.DeleteAsync(a => ids.Contains(a.Id));
                var flag = uow.Commit();
                return new Response<bool>
                {
                    Result = flag && delete,
                    Message = flag && delete == true ? "success" : "error"
                };
            }
        }
        #endregion
        #endregion

        #region  房屋贡献
        #region  获取分页数据列表
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<Response<PageInfo<List<TaxGongxianfang>>>> LoadGongxianfangPageList(string zzwbm, int page, int limit)
        {
            RefAsync<int> totalCount = 0;
            var query = client.Queryable<TaxGongxianfang>()
                .WhereIF(!string.IsNullOrEmpty(zzwbm), r => r.zzwbm == zzwbm);


            var result = await query.ToPageListAsync(page, limit, totalCount);

            return new Response<PageInfo<List<TaxGongxianfang>>>
            {
                Code = 200,
                Message = "success",
                Result = new PageInfo<List<TaxGongxianfang>>
                {
                    Items = result,
                    Total = totalCount
                }
            };
        }

        /// <summary>
        /// 根据id获取单个房屋贡献信息
        /// </summary>
        /// <param name="gid">房屋贡献gid</param>
        /// <returns></returns>
        public async Task<TaxGongxianfang> GetGongxianfangInfoById(string id)
        {
            var list = await client.Queryable<TaxGongxianfang>().Where(r => r.Id == id).FirstAsync();
            return list;
        }

        /// <summary>
        /// 根据所属宗地编码获取地点信息
        /// </summary>
        /// <param name="sszdbm">所属宗地编码</param>
        /// <returns></returns>
        public async Task<List<TaxGongxianfang>> GetGongxianfangInfoByBM(string zzwbm)
        {
            var list = await client.Queryable<TaxGongxianfang>().Where(r => r.zzwbm == zzwbm).ToListAsync();
            return list;
        }
        #endregion

        #region 编辑房屋贡献信息
        /// <summary>
        /// 添加房屋贡献信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<Response<bool>> AddGongxianfang(TaxGongxianfang req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                req.Id = Guid.NewGuid().ToString();
                bool insert = await uow.TaxGongxianfang.InsertAsync(req);
                bool flag = uow.Commit();

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


        /// <summary>
        /// 修改房屋贡献信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<Response<bool>> UpdateGongxianfang(TaxGongxianfang req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {

                bool update = await uow.TaxGongxianfang.UpdateAsync(req);
                bool flag = uow.Commit();

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

        /// <summary>
        /// 删除房屋贡献
        /// </summary>
        /// <param name="ids"></param>
        public async Task<Response<bool>> DeleteGongxianfang(string[] ids)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                var delete = await uow.TaxGongxianfang.DeleteAsync(a => ids.Contains(a.Id));
                var flag = uow.Commit();
                return new Response<bool>
                {
                    Result = flag && delete,
                    Message = flag && delete == true ? "success" : "error"
                };
            }
        }
        #endregion
        #endregion
    }
}
