using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using TouchSocket.Core;
using WCS.BLL.DbModels;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Tool;
using WCS.DAL;
using WCS.DAL.Db;
using WCS.DAL.Db.AuthDb;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.SingleLight;
using WCS.Model.ApiModel.UpLoad;
using WCS.Model.ApiModel.User;
using WCS.Model.WebSocketModel;
using static System.Formats.Asn1.AsnWriter;
using static WCS.BLL.Tool.Helper;

namespace WCS.BLL.Services.Service
{
    public class UploadService : IUploadService
    {
        public async Task<ResponseBase> GetReelInfo(string MatSn)
        {
            if (string.IsNullOrEmpty(MatSn))
            {
                return new ResponseBase<object>()
                {
                    Code = 201,
                    Message = "操作失败：条码不能为空"
                };
            }

            //查询物料条码是否存在
            var matInfo = await DbHelp.db.Queryable<MatInfo>()
                                         .Where(t => t.MatSn == MatSn)
                                         .FirstAsync();
            return new ResponseBase<object>()
            {
                Code = 200,
                Message = "success",
                Data = new
                {
                    MatSN = matInfo?.MatSn,
                    Qty = matInfo?.MatQty
                }
            };
        }

        public async Task<ResponseBase> UploadReelInfo(UploadReelInfoRequest request)
        {
            //校验参数
            if (string.IsNullOrEmpty(request.MatSN) || request.Qty <= 0)
            {
                return new ResponseBase()
                {
                    Code = 201,
                    Message = "操作失败：条码或数量不合法！"
                };
            }

            //查询物料条码是否存在
            var matInfo = await DbHelp.db.Queryable<MatInfo>()
                                         .Where(t => t.MatSn == request.MatSN)
                                         .FirstAsync();

            if (matInfo == null)
            {
                return new ResponseBase()
                {
                    Code = 201,
                    Message = $"操作失败：不存在此条码【{request.MatSN}】"
                };
            }

            //是否在库存中
            var inventoryDetail = await DbHelp.db.Queryable<InventoryDetail>()
                             .Where(t => t.MatSN == request.MatSN)
                             .FirstAsync();

            if (inventoryDetail != null)
            {
                return new ResponseBase()
                {
                    Code = 201,
                    Message = $"操作失败：此条码在库位{inventoryDetail.StoreCode}上，请下架后再进行操作。"
                };
            }
            try
            {
                DbHelp.db.BeginTran();

                //记录日志
                var MatInfoLog = new MatInfoLog()
                {
                    MatQty = request.Qty,
                    MatSn = request.MatSN,
                    Info = $"[{request.IPAddress}]数量从{matInfo.MatQty}修改为{request.Qty}!"
                };
                DbHelp.db.Insertable(MatInfoLog).ExecuteCommand();


                //修改数量
                matInfo.MatQty = request.Qty;
                DbHelp.db.Updateable(matInfo).ExecuteCommand();

                DbHelp.db.CommitTran();
                return new ResponseBase()
                {
                    Code = 200,
                    Message = "success"
                };


            }
            catch (Exception ex)
            {
                DbHelp.db.RollbackTran();
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "操作失败：" + ex.Message
                };
            }



        }
    }
}
