using ServiceCenter.Logs;
using ServiceCenter.Redis;
using ServiceCenter.WebApi;
using WCS.WorkEngineering.WebApi.Models.AGV;
using WCS.WorkEngineering.WebApi.Models.AGV.Request;
using WCS.WorkEngineering.WebApi.Models.AGV.Response;

namespace WCS.WorkEngineering.WebApi.Controllers
{
    public class IwmsApi
    {
        private static string _IwmsUrl = null!;

        /// <summary>
        /// AGV地址
        /// </summary>
        public static string IwmsUrl
        {
            get
            {
                _IwmsUrl ??= RedisHub.Default.Check("IwmsUrl");
                if (string.IsNullOrEmpty(_IwmsUrl))
                {
                    throw new KnownException($"请在Redis配置IwmsUrl", LogLevelEnum.High);
                }
                return _IwmsUrl;
            }
        }

        /// <summary>
        ///  满轮出库
        /// </summary>
        /// <param name="matCode">物料编号</param>
        /// <param name="wbCode">取货点位置</param>
        /// <param name="taskNo">任务号</param>
        /// <param name="rfid">RFID</param>
        /// <param name="matNo">材料号</param>
        /// <param name="isSurplus">改手盘标记</param>
        /// <param name="isRework">返工标记</param>
        /// <param name="matFast">快投标记</param>
        /// <param name="gradeCode">质量等级</param>
        /// <param name="wetIntoBinCode">仓位号</param>
        /// <param name="reqCode">请求编号</param>
        /// <returns></returns>
        public static zhongTianIntoStockResponse 满轮出库(string matCode, string wbCode, string taskNo, string rfid, string matNo, bool isSurplus, bool isRework, bool matFast, string gradeCode, string wetIntoBinCode, string reqCode, bool wetIntoNoRequest = false)
        {
            var zhongTian = new zhongTianIntoStockRequest()
            {
                matCode = matCode,
                wbCode = wbCode,
                inSpoolFull = "1",
                getOutEmpty = false,
                wetIntoReturn = false,
                isSurplus = isSurplus == false ? "0" : "1",
                isRework = isRework == false ? "0" : "1",
                spoolNo = rfid,
                gradeCode = gradeCode,
                matFast = matFast == false ? "0" : "1",
                matNo = matNo,
                orderProcessLenOut = "0",
                taskNo = $"RK{taskNo}",
                returnDesc = "",
                lockFlag = "0",
                reqCode = reqCode,
            };
            if (wetIntoNoRequest == true)
            {
                //zhongTian.wetInto = true;
                zhongTian.wetIntoBinCode = "";

                zhongTian.wetIntoNoRequest = true;
                zhongTian.wetIntoSpec = false;
                zhongTian.wetSpecWbCode = "";
                zhongTian.matNo = matNo;
            }
            else
            {
                if (wetIntoBinCode.Length == 14)
                {
                    zhongTian.wetInto = true;
                    zhongTian.wetIntoBinCode = wetIntoBinCode;
                    zhongTian.wetIntoSpec = false;
                    zhongTian.wetSpecWbCode = "";
                }
                else
                {
                    zhongTian.wetInto = false;
                    zhongTian.wetIntoBinCode = "";
                    zhongTian.wetIntoSpec = true;
                    zhongTian.wetSpecWbCode = wetIntoBinCode;
                }
            }
         
            return zhongTianIntoStock(zhongTian);
            //return zhongTianOutStock(new zhongTianOutStockRequest()
            //{
            //    matCode = matCode,
            //    workAreaCode = wbCode,
            //    outSpoolFull = "1"
            //});
        }

        public static zhongTianOutStockResponse 空轮回库(string matCode, string wbCode, bool isSurplus, bool isRework, int taskNo, string RFID, string reqCode)
        {
            return zhongTianOutStock(new zhongTianOutStockRequest()
            {
                matCode = matCode,
                workAreaCode = wbCode,
                outSpoolFull = "0",
                intoEmpty = false,
                wetOut = "",
                wetOutReturn = true,
                spoolNo = RFID,
                isSurplus = isSurplus == false ? "0" : "1",
                isRework = isRework == false ? "0" : "1",
                //reqCode = Guid.NewGuid().ToString().Replace("-", ""),
                reqCode = reqCode,
                taskNo = $"CK{wbCode}{taskNo}"
            });
        }

        public static zhongTianIntoStockResponse zhongTianIntoStock(zhongTianIntoStockRequest req)
        {
            var res = APICaller.CallApi2<zhongTianIntoStockResponse>(IwmsUrl + "/databus/publish/zhongTianIntoStock", req);
            if (res.code != AgvResponseCode.Success)
            {
                throw new KnownException(res.message, LogLevelEnum.High);
            }
            return res;
        }

        public static zhongTianOutStockResponse zhongTianOutStock(zhongTianOutStockRequest req)
        {
            var res = APICaller.CallApi2<zhongTianOutStockResponse>(IwmsUrl + "/databus/publish/zhongTianOutStock", req);
            if (res.code != AgvResponseCode.Success)
            {
                throw new KnownException(res.message, LogLevelEnum.High);
            }
            return res;
        }
    }
}
