using FactorySystemCommon;
using FactorySystemModel.ResponseModel;
using System.Collections.Generic;
using System.Web.Http;
using FactorySystemApi.Plm_WebSite;
using FactorySystemBll;
using FactorySystemModel.SqlSugarModel;
using Newtonsoft.Json;

namespace FactorySystemApi.Controllers
{
    /// <summary>
    /// PLM相关接口
    /// </summary>
    [UserLoginFilter]
    public class PlmApiController : ApiController
    {
        /// <summary>
        /// 获取工艺指导书链接
        /// </summary>
        /// <param name="inParam"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPlmWebSite(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();

            if (inParam["teamId"] != null)
            {
                List<Specifications> specifList = new List<Specifications>() { new Specifications() };

                return ExceptionHelper.TryReturnException(() => {
                    int teamId = int.Parse(inParam["teamId"].ToString()); ;
                    TFS_FTeamwork teamwork = BaseBll.GetTempModel<TFS_FTeamwork>(teamId);
                    TFS_Formula formula = BaseBll.GetTempModel<TFS_Formula>(teamwork.FFormulaID);

                    
                    specifList[0].SP = formula.FPlmCode;
                    specifList[0].SP_Version = formula.FVersionCode;

                    OAServiceSoapClient oAService = new OAServiceSoapClient();
                    RestResult restResult = oAService.GetCraftURL(specifList.ToArray());

                    if (restResult != null && restResult.data != null)
                    {
                        List<Dictionary<string, string>> data = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(restResult.data.ToString());
                        if (data != null && data.Count > 0)
                        {
                            apiResult.Data = data[0];
                        }
                    }
                }, apiResult, Request, specifList);                
            }
            else
            {
                return ExceptionHelper.TryReturnException(() => {
                    apiResult.Data = null;
                }, apiResult, Request, inParam);
            }            
        }

        /// <summary>
        /// 获取工艺指导书链接（试验号路线）
        /// </summary>
        /// <param name="inParam"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPlmWebSiteInHalf(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();

            if (inParam["teamId"] != null)
            {
                List<Specifications> specifList = new List<Specifications>() { new Specifications() };

                return ExceptionHelper.TryReturnException(() => {
                    int teamId = int.Parse(inParam["teamId"].ToString()); ;
                    TFS_HalfMaterialFTeamwork teamwork = BaseBll.GetTempModel<TFS_HalfMaterialFTeamwork>(teamId);
                    TFS_Formula formula = BaseBll.GetTempModel<TFS_Formula>(teamwork.FFormulaID);


                    specifList[0].SP = formula.FPlmCode;
                    specifList[0].SP_Version = formula.FVersionCode;

                    OAServiceSoapClient oAService = new OAServiceSoapClient();
                    RestResult restResult = oAService.GetCraftURL(specifList.ToArray());

                    if (restResult != null && restResult.data != null)
                    {
                        List<Dictionary<string, string>> data = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(restResult.data.ToString());
                        if (data != null && data.Count > 0)
                        {
                            apiResult.Data = data[0];
                        }
                    }
                }, apiResult, Request, specifList);
            }
            else
            {
                return ExceptionHelper.TryReturnException(() => {
                    apiResult.Data = null;
                }, apiResult, Request, inParam);
            }
        }
    }
}