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.InStore;
using ZR.Model.Dto.InStore;
using ZR.Model.Models.InStore;
using ZR.Model.Dto.Inspect;
using ZR.Model.Models.Balance;
using ZR.Model.Models.Inspect;
using ZR.Service.Business.IBusinessService.Balance;
using ZR.Service.Business.IBusinessService.Inspect;
using UAParser;

namespace ZR.Admin.WebApi.Controllers.InStore
{
    /// <summary>
    /// 入库管理/入库记录表Controller
    /// 
    /// @tableName in_instore_record
    /// @author admin
    /// @date 2023-04-13
    /// </summary>
    [Verify]
    [Route("business/InInstoreRecord")]
    public class InInstoreRecordController : BaseController
    {
        #region 依赖注入项
        private readonly IInInstoreRecordService _InInstoreRecordService;
        private readonly IQaInspectRecordService _QaInspectRecordService;
        private readonly IStkBalanceDtlService _StkBalanceDtlService;
        /// <summary>
        /// 构造函数
        /// </summary>
        public InInstoreRecordController(
            IInInstoreRecordService InInstoreRecordService,
            IQaInspectRecordService QaInspectRecordService,
            IStkBalanceDtlService StkBalanceDtlService)
        {
            _InInstoreRecordService = InInstoreRecordService;
            _QaInspectRecordService = QaInspectRecordService;
            _StkBalanceDtlService = StkBalanceDtlService;
        }
        #endregion

        /// <summary>
        /// 查询入库管理/入库记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "business:ininstorerecord:list")]
        public IActionResult QueryInInstoreRecord([FromQuery] InInstoreRecordQueryDto parm)
        {
            bool isPDA = false;
            ClientInfo clientInfo = HttpContext.GetClientInfo();
            if (clientInfo.OS.ToString().Contains("Android 10"))
            {
                isPDA = true;
            }
            var response = _InInstoreRecordService.GetList(parm, isPDA);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询入库管理/入库记录表详情
        /// </summary>
        /// <param name="InStoreId"></param>
        /// <returns></returns>
        [HttpGet("{InStoreId}")]
        [ActionPermissionFilter(Permission = "business:ininstorerecord:query")]
        public IActionResult GetInInstoreRecord(int InStoreId)
        {
            var response = _InInstoreRecordService.GetFirst(x => x.InStoreId == InStoreId);

            return SUCCESS(response);
        }

        /// <summary>
        /// 添加入库管理/入库记录表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "business:ininstorerecord:add")]
        [Log(Title = "入库管理/入库记录表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddInInstoreRecord([FromBody] InInstoreRecordDto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求参数错误");
            }
            var modal = parm.Adapt<InInstoreRecord>().ToCreate(HttpContext);

            var response = _InInstoreRecordService.AddInInstoreRecord(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 更新入库管理/入库记录表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "business:ininstorerecord:edit")]
        [Log(Title = "入库管理/入库记录表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateInInstoreRecord([FromBody] InInstoreRecordDto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求实体不能为空");
            }
            var modal = parm.Adapt<InInstoreRecord>().ToUpdate(HttpContext);

            var response = _InInstoreRecordService.UpdateInInstoreRecord(modal);

            return ToResponse(response);
        }

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

            var response = _InInstoreRecordService.Delete(idsArr);

            return ToResponse(response);
        }


        #region 2023 0524 孙亚龙 新增
        /// <summary>
        /// 添加入库记录数据， 并更新对应的质检记录
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        [HttpPost("AddCheckedSapDeliverInstoreDtlResult")]
        [ActionPermissionFilter(Permission = "business:ininstorerecord:AddCheckedSapDeliverInstoreDtlResult")]
        public IActionResult AddCheckedSapDeliverInstoreDtlResult([FromBody] QaInspectRecordDto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求实体不能为空");
            }
            QaInspectRecord qaInspectRecordMd = parm.Adapt<QaInspectRecord>().ToUpdate(HttpContext);
            TypeAdapterConfig config = new();
            config.ForType<QaInspectRecordDto, InInstoreRecord>()
                .Ignore(dest => dest.Result)
                .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);

            InInstoreRecord inInstoreRecordMd = parm.Adapt<InInstoreRecord>(config);
            if (qaInspectRecordMd.InstoreQty >= qaInspectRecordMd.CheckQty)
            {
                qaInspectRecordMd.InstoreResult = 99;
            }
            inInstoreRecordMd.InStoreNo = parm.DeliverNo;
            inInstoreRecordMd.Result = 99;
            inInstoreRecordMd.CreateBy = HttpContext.GetName();
            inInstoreRecordMd.CreateTime = DateTime.Now;
            inInstoreRecordMd.CreateBy = HttpContext.GetName();
            inInstoreRecordMd.CreateTime = DateTime.Now;
            inInstoreRecordMd.UpdateBy = HttpContext.GetName();
            inInstoreRecordMd.UpdateTime = DateTime.Now;

            StkBalanceDtl stkBalanceDtlMd = parm.Adapt<StkBalanceDtl>();
            stkBalanceDtlMd.RegionId = 1;
            stkBalanceDtlMd.BinId = 1;
            stkBalanceDtlMd.CreateBy = HttpContext.GetName();
            stkBalanceDtlMd.CreateTime = DateTime.Now;
            stkBalanceDtlMd.CreateBy = HttpContext.GetName();
            stkBalanceDtlMd.CreateTime = DateTime.Now;
            stkBalanceDtlMd.UpdateBy = HttpContext.GetName();
            stkBalanceDtlMd.UpdateTime = DateTime.Now;
            stkBalanceDtlMd.BalanceQty = inInstoreRecordMd.InStoreQty;
            stkBalanceDtlMd.Status = 55;



            int response = _QaInspectRecordService.UpdateQaInspectRecord(qaInspectRecordMd);
            int response1 = _InInstoreRecordService.AddInInstoreRecord(inInstoreRecordMd);
            int response2 = _StkBalanceDtlService.AddStkBalanceDtl(stkBalanceDtlMd);

            return ToResponse(response + response1 + response2);
        }
        #endregion


    }
}