using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common.API;
using Edu.Common.Enum.User;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Flow;
using Edu.Module.System;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace Edu.WebApi.Controllers.Public
{
    /// <summary>
    /// 流程设置相关接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class FlowController : BaseController
    {
        /// <summary>
        /// 流程设置处理类对象
        /// </summary>
        private readonly BasicFlowModule basicFlowModule = AOP.AOPHelper.CreateAOPObject<BasicFlowModule>();


        /// <summary>
        /// 获取退课流程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetBackClassFlow()
        {
            var FlowId = base.ParmJObj.GetInt("FlowId");
            var flowModel = basicFlowModule.GetFlowModule(FlowId);
            return ApiResult.Success(data: flowModel);
        }

        /// <summary>
        /// 新增修改退课流程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetBackClassFlow()
        {
            var extModel = new RB_Flow_ViewModel()
            {
                FlowId = base.ParmJObj.GetInt("FlowId"),
                FlowName = base.ParmJObj.GetStringValue("FlowName"),
                Group_Id = base.UserInfo.Group_Id,
                CreateBy = base.UserInfo.Id,
                CreateTime = DateTime.Now,
                Status = Common.Enum.DateStateEnum.Normal,
                UpdateBy = base.UserInfo.Id,
                UpdateTime = DateTime.Now,
            };
            extModel.FlowNodeList = new List<RB_Flow_Node_ViewModel>();
            JArray jArray = JArray.Parse(base.ParmJObj.GetStringValue("FlowNodeList"));
            foreach (var jItem in jArray)
            {
                JObject jObj = JObject.Parse(jItem.ToString());
                RB_Flow_Node_ViewModel model = new RB_Flow_Node_ViewModel()
                {
                    FlowId=jObj.GetInt("FlowId"),
                    NodeId=jObj.GetInt("NodeId"),
                    NodeName=jObj.GetStringValue("NodeName"),
                    SortNum=jObj.GetInt("SortNum"),
                };
                model.FlowAduitList = new List<RB_Flow_AduitInfo_ViewModel>();
                //审核人数组
                JArray subArray= JArray.Parse(jObj.GetStringValue("FlowAduitList"));
                foreach (var sItem in subArray)
                {
                    JObject sObj = JObject.Parse(sItem.ToString());
                    model.FlowAduitList.Add(new RB_Flow_AduitInfo_ViewModel()
                    {
                        CheckId = sObj.GetInt("CheckId"),
                        NodeId=sObj.GetInt("NodeId"),
                        FlowId=sObj.GetInt("FlowId"),
                        AduitName=sObj.GetStringValue("AduitName"),
                        AuditType=(WFTAuditTypeEnum)sObj.GetInt("AuditType"),
                        UserId=sObj.GetInt("UserId"),
                        IsSpecNode=sObj.GetInt("IsSpecNode"),
                        PostType=sObj.GetInt("PostType"),
                        AuditWay=(WFTAuditWayEnum)sObj.GetInt("AuditWay"),
                    });
                }
            }
            if (!string.IsNullOrEmpty(base.ParmJObj.GetStringValue("FlowNodeList")))
            {
                try
                {
                    extModel.FlowNodeList = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Flow_Node_ViewModel>>(base.ParmJObj.GetStringValue("FlowNodeList"));
                }
                catch (Exception ex)
                {
                    Common.Plugin.LogHelper.Write(ex, "SetBackClassFlow_FlowNodeList");
                }
            }
            var flag = basicFlowModule.SetFlowModule(extModel);
            return flag ? ApiResult.Success(data: extModel) : ApiResult.Failed();
        }

        /// <summary>
        /// 获取调课流程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetChangeClassFlow()
        {
            var FlowId = base.ParmJObj.GetInt("FlowId");
            var flowModel = basicFlowModule.GetFlowModule(FlowId);
            return ApiResult.Success(data: flowModel);
        }

        /// <summary>
        /// 新增修改调课流程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetChangeClassFlow()
        {
            var extModel = new RB_Flow_ViewModel()
            {
                FlowId = base.ParmJObj.GetInt("FlowId"),
                FlowName = base.ParmJObj.GetStringValue("FlowName"),
                Group_Id = base.UserInfo.Group_Id,
                CreateBy = base.UserInfo.Id,
                CreateTime = DateTime.Now,
                Status = Common.Enum.DateStateEnum.Normal,
                UpdateBy = base.UserInfo.Id,
                UpdateTime = DateTime.Now,
            };
            if (!string.IsNullOrEmpty(base.ParmJObj.GetStringValue("FlowNodeList")))
            {
                try
                {
                    extModel.FlowNodeList = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Flow_Node_ViewModel>>(base.ParmJObj.GetStringValue("FlowNodeList"));
                }
                catch (Exception ex)
                {
                    Common.Plugin.LogHelper.Write(ex, "SetChangeClassFlow_FlowNodeList");
                }
            }
            var flag = basicFlowModule.SetFlowModule(extModel);
            return flag ? ApiResult.Success(data: extModel) : ApiResult.Failed();
        }
    }
}
