using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 字典明细
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysDataItemDetailController : ControllerBase
    {
        SysDataItemDetailApp dataItemDetailApp;
        SysDataItemApp dataItemApp;
        public SysDataItemDetailController(
            SysDataItemDetailApp sysDataItemDetailApp, SysDataItemApp dataItemApp)
        {
            dataItemDetailApp = sysDataItemDetailApp;
            this.dataItemApp = dataItemApp;
        }

        /// <summary>
        /// 获取数据字典明显根据分类编号
        /// </summary>
        /// <param name="code">分类编号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<SysDataItemDetail>>> Load(string code,string key)
        {
            var list = await dataItemDetailApp.Load(code, key);
            return new Response<List<SysDataItemDetail>>
            {
                Result = list,
                Message = "success"
            };
        }

        /// <summary>
        /// 获取字段数据（有父级编号）
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="keyWord">筛选条件</param>
        /// <param name="pid">父级编号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<SysDataItemDetail>>> LoadWithPid(string code, string key, string pid)
        {
            var list = await dataItemDetailApp.LoadWithPid(code, key,pid);
            return new Response<List<SysDataItemDetail>>
            {
                Result = list,
                Message = "success"
            };
        }

        /// <summary>
        /// 新增明细数据实体
        /// </summary>
        /// <param name="code">分类编码</param>
        /// <param name="entity">实体</param>
        [HttpPost]
        public async Task<Response<bool>> Add(string code, SysDataItemDetail entity)
        {
            var result = new Response<bool>();
            try
            {
                var data = await dataItemApp.GetDataItem(code);
                entity.ItemId = data.ItemId;
                result = await dataItemDetailApp.SaveDetailEntity(entity);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 更新明细数据实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="entity">实体</param>
        [HttpPost]
        public async Task<Response<bool>> Update(SysDataItemDetail entity)
        {
            var result = new Response<bool>();
            try
            {
                result = await dataItemDetailApp.SaveDetailEntity(entity);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;

        }

        /// <summary>
        /// 删除明细数据实体
        /// </summary>
        /// <param name="id">主键</param>
        [HttpPost]
        public async Task<Response<bool>> Delete(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await dataItemDetailApp.Delete(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;

        }
    }
}
