using Infrastructure;
using Microsoft.AspNetCore.Http;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OpenAuth.App.BasicQueryService;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository;
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.ServiceApp.TaxYongdixianManage
{
    public class TaxYongdixianApp : SqlSugarBaseApp<TaxYongdixian, SugarDbContext>
    {
        private ISqlSugarClient client;
        CommonDataManager _commonDataManager;

        #region 构造函数
        public TaxYongdixianApp(
             CommonDataManager commonDataManager,
            ISugarUnitOfWork<SugarDbContext> unitWork,
            ISimpleClient<TaxYongdixian> 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<TaxYongdixian>>>> LoadYongdixianPageList(string sszdbm,int page,int limit)
        {
            RefAsync<int> totalCount = 0;
            var query = client.Queryable<TaxYongdixian>()
                .WhereIF(!string.IsNullOrEmpty(sszdbm), r => r.sszdbm == sszdbm);


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

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

        /// <summary>
        /// 根据id获取单个用地线信息
        /// </summary>
        /// <param name="gid">用地线gid</param>
        /// <returns></returns>
        public async Task<TaxYongdixian> GetYongdixianInfoById(int gid)
        {
            var list = await client.Queryable<TaxYongdixian>().Where(r => r.gid == gid).FirstAsync();
            return list;
        }

        /// <summary>
        /// 根据所属宗地编码获取地线信息
        /// </summary>
        /// <param name="sszdbm">所属宗地编码</param>
        /// <returns></returns>
        public async Task<List<TaxYongdixian>> GetYongdixianInfoByBM(string sszdbm)
        {
            var list = await client.Queryable<TaxYongdixian>().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>> AddYongdixian(TaxYongdixian req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                //获取主键
                string _gid = _commonDataManager.GetMaxKeyVal("gid", "tax_yongdixian", 1);
                req.gid = int.Parse(_gid);//转为数字类型

                //获取objectid
                string _objid = _commonDataManager.GetMaxKeyVal("objectid", "tax_yongdixian", 1);
                req.objectid = int.Parse(_objid);//转为数字类型

                bool insert = await uow.TaxYongdixian.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>> UpdateYongdixian(TaxYongdixian req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {

                bool update = await uow.TaxYongdixian.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>> DeleteYongdixian(int[] ids)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                var delete = await uow.TaxYongdixian.DeleteAsync(a => ids.Contains(a.gid));
                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<TaxYongdidian>>>> LoadYongdidianPageList(string sszdbm, int page, int limit)
        {
            RefAsync<int> totalCount = 0;
            var query = client.Queryable<TaxYongdidian>()
                .WhereIF(!string.IsNullOrEmpty(sszdbm), r => r.sszdbm == sszdbm);


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

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

        /// <summary>
        /// 根据id获取单个用地点信息
        /// </summary>
        /// <param name="gid">用地点gid</param>
        /// <returns></returns>
        public async Task<TaxYongdidian> GetYongdidianInfoById(int gid)
        {
            var list = await client.Queryable<TaxYongdidian>().Where(r => r.gid == gid).FirstAsync();
            return list;
        }

        /// <summary>
        /// 根据所属宗地编码获取地点信息
        /// </summary>
        /// <param name="sszdbm">所属宗地编码</param>
        /// <returns></returns>
        public async Task<List<TaxYongdidian>> GetYongdidianInfoByBM(string sszdbm)
        {
            var list = await client.Queryable<TaxYongdidian>().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>> AddYongdidian(TaxYongdidian req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                //获取主键
                string _gid = _commonDataManager.GetMaxKeyVal("gid", "tax_yongdidian", 1);
                req.gid = int.Parse(_gid);//转为数字类型

                //获取objectid
                string _objid = _commonDataManager.GetMaxKeyVal("objectid", "tax_yongdidian", 1);
                req.objectid = int.Parse(_objid);//转为数字类型

                bool insert = await uow.TaxYongdidian.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>> UpdateYongdidian(TaxYongdidian req)
        {
            using (var uow = base.UnitWork.CreateContext())
            {

                bool update = await uow.TaxYongdidian.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>> DeleteYongdidian(int[] ids)
        {
            using (var uow = base.UnitWork.CreateContext())
            {
                var delete = await uow.TaxYongdidian.DeleteAsync(a => ids.Contains(a.gid));
                var flag = uow.Commit();
                return new Response<bool>
                {
                    Result = flag && delete,
                    Message = flag && delete == true ? "success" : "error"
                };
            }
        }
        #endregion
        #endregion
    }
}
