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;
using WCS.WorkEngineering.Worlds.Logs;

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>
        /// <returns></returns>
        public static zhongTianIntoStockResponse 满轮出库(string matCode, string wbCode, string taskNo, string rfid, string matNo, bool isSurplus, bool isRework, bool matFast, string gradeCode)
        {
            return zhongTianIntoStock(new zhongTianIntoStockRequest()
            {
                matCode = matCode,
                wbCode = wbCode,
                inSpoolFull = "1",
                getOutEmpty = false,
                wetInto = true,
                wetIntoSpec = false,
                wetIntoReturn = false,
                wetSpecWbCode = "0",
                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",
            });
            //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)
        {
            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("-", ""),
                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;
        }
    }
}
