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_bin
    /// @author admin
    /// @date 2023-04-10
    /// </summary>
    [Verify]
    [Route("business/BasBin")]
    public class BasBinController : BaseController
    {
        /// <summary>
        /// 基础资料/库位信息表接口
        /// </summary>
        private readonly IBasBinService _BasBinService;

        public BasBinController(IBasBinService BasBinService)
        {
            _BasBinService = BasBinService;
        }

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


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

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

            var response = _BasBinService.AddBasBin(modal);

            return ToResponse(response);
        }

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

            var response = _BasBinService.UpdateBasBin(modal);

            return ToResponse(response);
        }

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

            var response = _BasBinService.Delete(idsArr);

            return ToResponse(response);
        }



    }
}