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

namespace ZR.Admin.WebApi.Controllers.Balance
{
    /// <summary>
    /// 库存管理/库存明细表Controller
    /// 
    /// @tableName stk_balance_dtl
    /// @author admin
    /// @date 2023-04-27
    /// </summary>
    [Verify]
    [Route("business/StkBalanceDtl")]
    public class StkBalanceDtlController : BaseController
    {
        /// <summary>
        /// 库存管理/库存明细表接口
        /// </summary>
        private readonly IStkBalanceDtlService _StkBalanceDtlService;

        public StkBalanceDtlController(IStkBalanceDtlService StkBalanceDtlService)
        {
            _StkBalanceDtlService = StkBalanceDtlService;
        }

        /// <summary>
        /// 查询库存管理/库存明细表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "business:stkbalancedtl:list")]
        public IActionResult QueryStkBalanceDtl([FromQuery] StkBalanceDtlQueryDto parm)
        {
            var response = _StkBalanceDtlService.GetList(parm);
            return SUCCESS(response);
        }
        /// <summary>
        /// 查询库存管理/库存统计列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("statlist")]
        [ActionPermissionFilter(Permission = "business:stkbalancedtl:statlist")]
        public IActionResult StatStkBalanceDtl([FromQuery] StkBalanceDtlQueryDto parm)
        {
            var response = _StkBalanceDtlService.StatList(parm);
            return SUCCESS(response);
        }

        /// <summary>
        /// 查询库存管理/库存明细表详情
        /// </summary>
        /// <param name="BalanceId"></param>
        /// <returns></returns>
        [HttpGet("{BalanceId}")]
        [ActionPermissionFilter(Permission = "business:stkbalancedtl:query")]
        public IActionResult GetStkBalanceDtl(int BalanceId)
        {
            var response = _StkBalanceDtlService.GetFirst(x => x.BalanceId == BalanceId);

            return SUCCESS(response);
        }

        /// <summary>
        /// 添加库存管理/库存明细表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "business:stkbalancedtl:add")]
        [Log(Title = "库存管理/库存明细表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddStkBalanceDtl([FromBody] StkBalanceDtlDto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求参数错误");
            }
            var modal = parm.Adapt<StkBalanceDtl>().ToCreate(HttpContext);

            var response = _StkBalanceDtlService.AddStkBalanceDtl(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 更新库存管理/库存明细表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "business:stkbalancedtl:edit")]
        [Log(Title = "库存管理/库存明细表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateStkBalanceDtl([FromBody] StkBalanceDtlDto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求实体不能为空");
            }
            var modal = parm.Adapt<StkBalanceDtl>().ToUpdate(HttpContext);

            var response = _StkBalanceDtlService.UpdateStkBalanceDtl(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除库存管理/库存明细表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [ActionPermissionFilter(Permission = "business:stkbalancedtl:delete")]
        [Log(Title = "库存管理/库存明细表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteStkBalanceDtl(string ids)
        {
            int[] idsArr = Tools.SpitIntArrary(ids);
            if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }

            var response = _StkBalanceDtlService.Delete(idsArr);

            return ToResponse(response);
        }

        #region 2023 0524 孙亚龙 新增
        [HttpGet("GetStkBalanceDtlList")]
        public IActionResult GetStkBalanceDtlList([FromQuery] GetOutOperateBalanceQueryDto parm)
        {
            var response = _StkBalanceDtlService.GetBalanceDtlList(parm);
            int qty = 0;
            foreach (var item in response)
            {
                qty += item.BalanceQty;
            }
            return SUCCESS(qty);
        }
        #endregion

        #region 2023 0524 孙亚龙 新增

        #endregion

    }
}