using Dapper;
using Ewide.Core.Extension;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ewide.Application.Service.HouseSafety.HouseQuery
{
    /// <summary>
    /// 住宅查询
    /// </summary>
    [ApiDescriptionSettings(Name = "HouseQuery", Order = 210)]
    public class HouseQueryService : IHouseQueryService, IDynamicApiController, ITransient
    {
        private readonly IRepository<BsHouseCode> _houseCodeRep;
        private readonly IRepository<BsHouseInfo> _houseInfoRep;
        private readonly IRepository<BsHouseTask> _houseTaskRep;
        private readonly IDapperRepository _dapperRepository;

        public HouseQueryService(IRepository<BsHouseCode> HouseCodeRep, IRepository<BsHouseInfo> HouseInfoRep, IRepository<BsHouseTask> HouseTaskRep, IDapperRepository dapperRepository)
        {
            _houseCodeRep = HouseCodeRep;
            _houseInfoRep = HouseInfoRep;
            _houseTaskRep = HouseTaskRep;
            _dapperRepository = dapperRepository;
        }

        [HttpPost("/houseQuery/page")]
        public async Task<dynamic> QueryPage([FromBody] HouseQueryInput input)
        {
            var sql = @"SELECT 
    HC.ID,
    HC.HouseCode,
    AA.Name AreaName,
    RA.Name RoadName,
    CA.Name CommName,
    Proj.AreaCode,
    Proj.Note,
    Proj.Name,
    CONCAT(Proj.Name,'(',Proj.Note,')') FullProjName,
    HC.Address,
    IFNULL(HI.BuildingName,'') BuildingName,
    IFNULL(HI.TotalFloor,0) TotalFloor,
    IFNULL(HI.TotalArea,0) TotalArea,
    HI.LandAttribute,
    IFNULL(HI.HouseGrade,0) HouseGrade,
    HC.Type,
    HC.No,
    HI.State,
    HI.CompletedDate,
    HI.CreatedTime
FROM bs_house_code HC 
LEFT JOIN bs_house_info HI ON HI.HouseCodeId = HC.Id
LEFT JOIN bs_house_projectinfo Proj ON Proj.Id=HC.ProjectId
LEFT JOIN sys_area_code CA ON CA.Code = Proj.AreaCode
LEFT JOIN sys_area_code RA ON RA.AdCode = SUBSTR(CA.AdCode,1,9)
LEFT JOIN sys_area_code AA ON AA.AdCode = SUBSTR(CA.AdCode,1,6)
WHERE 1=1";

            return await _dapperRepository.QueryPageDataDynamic(sql, input, filterFields: new string[] { 
                "HouseCode", 
                "Address", 
                "BuildingName", 
                "State", 
                "AreaCode", 
                "LandAttribute", 
                "HouseGrade",
                "CompletedDate", 
                "CreatedTime",
                "TotalArea",
                "TotalFloor"
            });
        }

        [HttpGet("/houseQuery/detail")]
        public async Task<dynamic> Detail([FromQuery] HouseQueryDetailInput input) 
        {
            var houseCodeOutputAsync = await _dapperRepository.QueryAsync<HouseCodeOutput>(
                @"SELECT HC.Id,HC.Address,HC.ProjectId,HC.ZoneId,HC.Type,AA.Name AreaName,RA.Name RoadName,CA.Name CommName,O.Name ZoneName,Proj.Note ProjectNote,CONCAT(Proj.Name,'(',Proj.Note,')') ProjectFullName,HC.HouseCode,HC.Lng,HC.Lat,HC.No FROM bs_house_code HC
LEFT JOIN bs_house_projectinfo Proj ON Proj.Id=HC.ProjectId
LEFT JOIN sys_org O ON HC.ZoneId = O.Id
LEFT JOIN sys_area_code CA ON CA.Code = Proj.AreaCode
LEFT JOIN sys_area_code RA ON RA.AdCode = SUBSTR(CA.AdCode,1,9)
LEFT JOIN sys_area_code AA ON AA.AdCode = SUBSTR(CA.AdCode,1,6)
WHERE HC.Id=@Id", new { input.Id }
                );

            var houseCodeOutput = houseCodeOutputAsync.SingleOrDefault();

            
            var houseInfoOutputForDetailPage = new HouseInfoOutputForDetailPage
            {
                HouseCode = houseCodeOutput
            };

            var houseInfoOutput = (await _houseInfoRep.DetachedEntities.FirstOrDefaultAsync(p => p.HouseCodeId == houseCodeOutput.Id)).Adapt<HouseInfoOutput>();
            houseInfoOutputForDetailPage.HouseInfo = houseInfoOutput;

            return houseInfoOutputForDetailPage;
        }
    }
}
