using FactorySystemBll;
using FactorySystemCommon;
using FactorySystemModel.BusinessModel;
using FactorySystemModel.ResponseModel;
using FactorySystemModel.SqlSugarModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace FactorySystemApi.Controllers
{
    /// <summary>
    /// 公共接口
    /// </summary>
    [UserLoginFilter]
    public class CommonController : BaseController<TFunction>
    {
        private readonly CommonBll CommonBll = new CommonBll();

        /// <summary>
        /// 获取配置集合
        /// </summary>
        [HttpPost]
        public ApiResult GetBasicList(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                if (inParam.TryGetValue("FType", out object objType) && int.TryParse(objType.ToString(), out int intType))
                {
                    apiResult.Data = CommonBll.GetBasicList(intType);
                }
            }, apiResult, Request, inParam);
        }

        /// <summary>
        /// 获取模板信息
        /// </summary>
        [HttpPost]
        public ApiResult GetTempFile(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                inParam.TryGetValue("FType", out object objType);
                objType = objType ?? "";
                string filePath = "/File/{0}/{1}.xlsx";
                switch (objType.ToString())
                {
                    case "2":
                        filePath = string.Format(filePath, "Material", "SAP物料导入模板");
                        break;
                    case "3":
                        filePath = string.Format(filePath, "Material", "替代料导入模板");
                        break;
                    case "4":
                        filePath = string.Format(filePath, "Material", "副产物导入模板");
                        break;
                    case "5":
                        filePath = string.Format(filePath, "Material", "物料信息补全导入模板");
                        break;
                    case "6":
                        filePath = string.Format(filePath, "Teamwork", "协同批量模板");
                        break;
                    case "1"://配方导入模板
                    default:
                        filePath = string.Format(filePath, "Formula", "配方导入模板");
                        break;
                }
                apiResult.Data = Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.AbsolutePath, "").Trim('/') + filePath;
            }, apiResult, Request, inParam);
        }

        /// <summary>
        /// 验证用户权限
        /// </summary>
        [HttpPost]
        public ApiResult CheckIsHasPower(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                Dictionary<string, object> outParam = new Dictionary<string, object>();
                List<int> funcList = new List<int>();
                inParam.TryGetValue("FType", out object oType);
                if (null != inParam)
                {
                    inParam.Remove("FType");
                    int.TryParse(oType.ToString(), out int iType);
                    ApiAuthInfo user = Request.Properties["token"] as ApiAuthInfo;
                    funcList = CommonBll.CheckIsHasPower(iType, user.FID);
                }
                foreach (var item in inParam)
                {
                    bool has = funcList.Contains(int.Parse(item.Key.Replace("P", "")));
                    outParam.Add(item.Key, has);
                }
                apiResult.Data = outParam;
            }, apiResult, Request, inParam);
        }


        /// <summary>
        /// 获取数据选择值
        /// </summary>
        [HttpPost]
        public ApiResult GetDataCodeList(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                if (inParam.TryGetValue("FType", out object oType))
                {
                    string[] typeIds = oType.ToString().Split(',');
                    List<TDataCode> codeList = CommonBll.GetDataCodeList(typeIds);
                    Dictionary<string, object> outParam = new Dictionary<string, object>();
                    foreach (var item in codeList.GroupBy(s => s.FType))
                    {
                        outParam.Add("FType" + item.Key, item.Select(s => new { s.FID, s.FName, s.FValue }));
                    }
                    apiResult.Data = outParam;
                }
                else
                {
                    apiResult.Data = CommonBll.GetDataCodeList(null);
                }
            }, apiResult, Request, inParam);
        }


        /// <summary>
        /// 修改数据值状态
        /// </summary>
        [HttpPost]
        public ApiResult StateDataCode(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                inParam.TryGetValue("FID", out object dataId);
                ApiAuthInfo user = Request.Properties["token"] as ApiAuthInfo;
                apiResult.Data = CommonBll.StateDataCode(int.Parse(dataId.ToString()), user.FID);
            }, apiResult, Request, inParam);
        }

        /// <summary>
        /// 删除系统配置值
        /// </summary>
        [HttpPost]
        public ApiResult DeleteDataCode(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                apiResult.Data = BaseBll.DeleteDataById(inParam, "TDataCode", true);
            }, apiResult, Request, inParam);
        }

        /// <summary>
        /// 设置系统配置值
        /// </summary>
        [HttpPost]
        public ApiResult ChangeDataCode(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                ApiAuthInfo user = Request.Properties["token"] as ApiAuthInfo;
                inParam.Add("FDateOpt", DateTime.Now);
                inParam.Add("FUserOpt", user.FID);
                if (inParam.ContainsKey("FID"))
                {
                    apiResult.Data = BaseBll.UpdateDataModel(inParam, "TDataCode");
                }
                else
                {
                    apiResult.Data = BaseBll.InsertDataModel(inParam, "TDataCode");
                }
            }, apiResult, Request, inParam);
        }

        /// <summary>
        /// 获取配置集合
        /// </summary>
        [HttpPost]
        public ApiResult GetConfigList(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                apiResult.Data = CommonBll.GetConfigList();
            }, apiResult, Request, inParam);
        }

        /// <summary>
        /// 获取系统配置值
        /// </summary>
        [HttpPost]
        public ApiResult GetConfigValue(Dictionary<string, object> inParam)
        {
            ApiResult apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                inParam.TryGetValue("FID", out object dataId);
                apiResult.Data = CommonBll.GetConfigValue(int.Parse(dataId.ToString()));
            }, apiResult, Request, inParam);
        }

    }
}
