using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common.API;
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;

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,
            };
            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();
        }
    }
}
