using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.PDAShelfLocationBindUnbind;

namespace WCS.BLL.Services.Service
{
    public class PDAShelfLocationBindUnbindService : IPDAShelfLocationBindUnbindService
    {
        public async Task<ResponseBase> getLocationInfoByShelfCode(ShelfLocationBindUnbindRequest request)
        {
            try
            {
                request.ShelfCode.Replace("\r", string.Empty)
                                 .Replace("\n", string.Empty)
                                 .Trim();
                //校验参数 传入数据为PDA扫描的货架编码
                if (request == null || string.IsNullOrEmpty(request.ShelfCode))
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"获取失败:参数异常，货架码为空！",
                    };
                }
                //获取货架数据
                var shelfInfo = await DbHelp.db.Queryable<ShelfInfo>()
                                         .Where(t => t.ShelfCode == request.ShelfCode)
                                         .Where(t => t.IsEnable)
                                         .FirstAsync();
                if (shelfInfo == null)
                {
                    //通过货架编码匹配不到就通过当前位置码去匹配 获取当前位置上的货架信息
                    shelfInfo = await DbHelp.db.Queryable<ShelfInfo>()
                                         .Where(t => t.CurrentLocaiotnCode == request.ShelfCode || t.DestinationLocaiotnCode == request.ShelfCode)
                                         .Where(t => t.IsEnable)
                                         .FirstAsync();
                    if (shelfInfo == null)
                    {
                        return new ResponseCommon()
                        {
                            Code = 201,
                            Message = $"获取失败:货架{request.ShelfCode}不存在或已被禁用！",
                        };
                    }
                }
                //货架数据不为空
                return new ResponseCommon<ShelfLocationBindUnbindResponseData>()
                {
                    Code = 200,
                    Message = "success",
                    Data = new ShelfLocationBindUnbindResponseData()
                    {
                        ShelfId = shelfInfo.Id,
                        ShelfCode = shelfInfo.ShelfCode,
                        CurrentLocationId = shelfInfo.CurrentLocationId,
                        CurrentLocationCode = shelfInfo.CurrentLocaiotnCode,
                        DestinationLocationId = shelfInfo.DestinationLocationId,
                        DestinationLocaiotnCode = shelfInfo.DestinationLocaiotnCode,
                        TransStatusStr = shelfInfo.TransStatus.ToString(),
                    },
                };
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = $"获取失败:发生异常{ex.Message}",
                };
            }
        }

        /// <summary>
        /// 货架和位置绑定  绑定都绑定为 静止状态 当前位置为所传入的位置
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ResponseBase> shelfLocationBind(ShelfLocationBindUnbindRequest request)
        {
            try
            {
                //校验参数 传入数据为货架编码、货架ID 为上一个接口的返回数据
                if (request == null || request.ShelfId == 0)
                {
                    return new ResponseCommon()
                    {
                        Code = 205,
                        Message = $"绑定失败:货架参数异常，请重新扫描货架码！",
                    };
                }
                //校验参数 传入数据为扫描的位置码
                if (string.IsNullOrEmpty(request.LocationCode))
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"绑定失败:货架码参数异常，请重新扫描位置码！",
                    };
                }

                //获取货架数据
                var shelfInfo = await DbHelp.db.Queryable<ShelfInfo>()
                                         .Where(t => t.Id == request.ShelfId)
                                         .Where(t => t.IsEnable)
                                         .FirstAsync();
                if (shelfInfo == null)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"绑定失败:货架{request.ShelfCode}不存在或已被禁用！",
                    };
                }

                //获取位置码代表的位置数据
                var locationInfo = await DbHelp.db.Queryable<LocationInfo>()
                                                  .Where(t => t.LocationCode == request.LocationCode)
                                                  .Where(t => t.IsEnable)
                                                  .FirstAsync();
                if (locationInfo == null)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"绑定失败:位置{request.LocationCode}不存在或已被禁用！",
                    };
                }
                //查询该位置是否已绑定货架
                var shelfAlreadyBinded = await DbHelp.db.Queryable<ShelfInfo>()
                                         .Where(t => t.DestinationLocationId == locationInfo.Id || t.CurrentLocationId == locationInfo.Id)
                                         .Where(t => t.IsEnable)
                                         .FirstAsync();
                if (shelfAlreadyBinded != null)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"绑定失败：位置{request.LocationCode}已绑定货架{request.ShelfCode}\r\n请扫描位置码解绑后再进行绑定！",
                    };
                }

                //调用RCS绑定
                var rcsResponse = AGVManager.BindPodAndBerth(shelfInfo.ShelfCode, locationInfo.LocationCode, BindPodAndBerthMethod.绑定);
                if (rcsResponse.code == "0")
                {
                    //开始绑定
                    shelfInfo.CurrentLocaiotnCode = locationInfo.LocationCode;
                    shelfInfo.CurrentLocationId = locationInfo.Id;
                    shelfInfo.DestinationLocationId = 0;
                    shelfInfo.DestinationLocaiotnCode = string.Empty;
                    shelfInfo.CurrentTaskCode = string.Empty;
                    shelfInfo.TransStatus = TransStatusEnum.静止;
                    DbHelp.db.Updateable(shelfInfo).ExecuteCommand();

                    //返回成功
                    return new ResponseCommon()
                    {
                        Code = 200,
                        Message = "success",
                        Data = null,
                    };
                }
                else
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"RCS返回：{rcsResponse.message}",
                        Data = null,
                    };
                }
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = $"绑定失败:发生异常{ex.Message}",
                };
            }
        }

        public async Task<ResponseBase> shelfLocationUnBind(ShelfLocationBindUnbindRequest request)
        {
            try
            {
                //校验参数 传入数据为货架编码、货架ID 为上一个接口的返回数据
                if (request == null || request.ShelfId == 0)
                {
                    return new ResponseCommon()
                    {
                        Code = 205,
                        Message = $"解绑失败:货架参数异常，请重新扫描货架码！",
                    };
                }

                //获取货架数据
                var shelfInfo = await DbHelp.db.Queryable<ShelfInfo>()
                                         .Where(t => t.Id == request.ShelfId)
                                         .Where(t => t.IsEnable)
                                         .FirstAsync();
                if (shelfInfo == null)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"解绑失败:货架{request.ShelfCode}不存在或已被禁用！",
                    };
                }

                //调用RCS解绑
                var rcsResponse = AGVManager.BindPodAndBerth(shelfInfo.ShelfCode, shelfInfo.CurrentLocaiotnCode, BindPodAndBerthMethod.解绑);
                if (rcsResponse.code == "0")
                {
                    //开始解绑
                    shelfInfo.CurrentLocationId = 0;
                    shelfInfo.CurrentLocaiotnCode = string.Empty;
                    shelfInfo.DestinationLocationId = 0;
                    shelfInfo.DestinationLocaiotnCode = string.Empty;
                    shelfInfo.CurrentTaskCode = string.Empty;
                    shelfInfo.TransStatus = TransStatusEnum.静止;
                    DbHelp.db.Updateable(shelfInfo).ExecuteCommand();

                    //返回成功
                    return new ResponseCommon()
                    {
                        Code = 200,
                        Message = "success",
                        Data = null,
                    };
                }
                else
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"RCS返回：{rcsResponse.message}",
                        Data = null,
                    };
                }
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = $"解绑失败:发生异常{ex.Message}",
                };
            }
        }
    }
}
