using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using ZR.Model.Dto;
using ZR.Model.Models;
using ZR.Service.Business.IBusinessService;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Common;

namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 基础资料/库区信息表Controller
    /// 
    /// @tableName bas_region
    /// @author admin
    /// @date 2023-04-07
    /// </summary>
    [Verify]
    [Route("business/BasRegion")]
    public class BasRegionController : BaseController
    {
        /// <summary>
        /// 基础资料/库区信息表接口
        /// </summary>
        private readonly IBasRegionService _BasRegionService;

        public BasRegionController(IBasRegionService BasRegionService)
        {
            _BasRegionService = BasRegionService;
        }

        /// <summary>
        /// 查询基础资料/库区信息表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "business:basregion:list")]
        public IActionResult QueryBasRegion([FromQuery] BasRegionQueryDto parm)
        {
            var response = _BasRegionService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询基础资料/库区信息表详情
        /// </summary>
        /// <param name="RegionId"></param>
        /// <returns></returns>
        [HttpGet("{RegionId}")]
        [ActionPermissionFilter(Permission = "business:basregion:query")]
        public IActionResult GetBasRegion(long RegionId)
        {
            var response = _BasRegionService.GetFirst(x => x.RegionId == RegionId);
            
            return SUCCESS(response);
        }

        /// <summary>
        /// 添加基础资料/库区信息表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "business:basregion:add")]
        [Log(Title = "基础资料/库区信息表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddBasRegion([FromBody] BasRegionDto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求参数错误");
            }
            var modal = parm.Adapt<BasRegion>().ToCreate(HttpContext);

            var response = _BasRegionService.AddBasRegion(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 更新基础资料/库区信息表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "business:basregion:edit")]
        [Log(Title = "基础资料/库区信息表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateBasRegion([FromBody] BasRegionDto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求实体不能为空");
            }
            var modal = parm.Adapt<BasRegion>().ToUpdate(HttpContext);

            var response = _BasRegionService.UpdateBasRegion(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除基础资料/库区信息表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [ActionPermissionFilter(Permission = "business:basregion:delete")]
        [Log(Title = "基础资料/库区信息表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteBasRegion(string ids)
        {
            int[] idsArr = Tools.SpitIntArrary(ids);
            if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }

            var response = _BasRegionService.Delete(idsArr);

            return ToResponse(response);
        }



    }
}