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

namespace FactorySystemApi.Controllers
{
    /// <summary>
    /// 视图接口
    /// </summary>
    [UserLoginFilter]
    public class View2Controller : BaseController<TFS_ViewMaterial>
    {
        private readonly ViewBll _viewBll = new ViewBll();

        /// <summary>
        /// 根据协同ID获取物料视图
        /// </summary>
        [HttpPost]
        public ApiResult GetListByTeamId(Dictionary<string, object> inParam)
        {
            var apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                if (Request.Properties["token"] is ApiAuthInfo user)
                {
                    int teamId = int.Parse(inParam["teamId"].ToString());
                    int viewType = int.Parse(inParam["viewType"].ToString());
                    apiResult.Data = new
                    {
                        columns = _viewBll.GetColumns(),
                        rows = _viewBll.GetListByTeamId(teamId, viewType, user.FID, out List<int> materialId, out string FGuaranteePeriod, out string FStorageConditions, !inParam.ContainsKey("FAllView")),
                        infos = _viewBll.GetMaterialInfoList(materialId, FGuaranteePeriod, FStorageConditions, user.FID),
                        types = _viewBll.GetMaterialTypeList()
                    };
                }
            }, apiResult, Request);
        }

        /// <summary>
        /// 保存视图编辑的内容
        /// </summary>
        [HttpPost]
        public ApiResult UpdateBatchById(List<Dictionary<string, object>> rows)
        {
            var apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                if (Request.Properties["token"] is ApiAuthInfo user)
                {
                    apiResult.Data = _viewBll.UpdateBatchById(rows, user.FID);
                }
            }, apiResult, Request);
        }

        /// <summary>
        /// 保存视图编辑的内容
        /// </summary>
        [HttpPost]
        public ApiResult UpdateBatchById2(Dictionary<string, object> inParam)
        {
            var apiResult = new ApiResult();
            return ExceptionHelper.TryReturnException(() =>
            {
                if (Request.Properties["token"] is ApiAuthInfo user)
                {
                    inParam.TryGetValue("TFS_ViewMaterial", out object viewObj);
                    inParam.TryGetValue("TFS_Material", out object materialObj);
                    inParam.TryGetValue("TFS_MaterialInfo", out object infoObj);
                    List<Dictionary<string, object>> viewList = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(JsonConvert.SerializeObject(viewObj));
                    List<Dictionary<string, object>> materialList = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(JsonConvert.SerializeObject(materialObj));
                    List<Dictionary<string, object>> infoList = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(JsonConvert.SerializeObject(infoObj));
                    int teamId = int.Parse(inParam["FTeamID"].ToString());
                    int viewType = int.Parse(inParam["FViewType"].ToString());
                    _viewBll.UpdateBatchById2(viewList, materialList, infoList, teamId, viewType, user.FID);
                }
            }, apiResult, Request);
        }
    }
}
