using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using APT.Infrastructure.Api;
namespace APT.PF.WebApiControllers.Api.PF
{
    [Route("api/PF/FlowSchemes")]
    [Microsoft.AspNetCore.Cors.EnableCors("AllowAllPolicy")]
    public class FlowSchemesController : APTApiController<T_PF_FLOW_SCHEME>
    {

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<IEnumerable<T_PF_FLOW_SCHEME>> Entities([FromBody] KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        /// 

        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_PF_FLOW_SCHEME>> OrderEntities([FromBody] KeywordFilter filter)
        {
            return WitOrderEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_PF_FLOW_SCHEME> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_PF_FLOW_SCHEME> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitOrderPaged(null, pageFilter);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("Delete")]
        public JsonActionResult<bool> Delete(string id)
        {
            return WitRealDelete(id);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update(string model)
        {
            return SafeExecute<bool>(() =>
            {
                //if (model.ID == null)
                //    model.ID = Guid.NewGuid();
                //var entity = Mapper.Map<FlowMode, T_PF_FLOW_SCHEME>(model);
                //return this.UpdateEntity(entity);
                if (!string.IsNullOrEmpty(model))
                {
                    var entity = Newtonsoft.Json.JsonConvert.DeserializeObject<T_PF_FLOW_SCHEME>(model);
                    if(entity!=null)
                        return this.UpdateEntity(entity);
                }
                return true;
            });
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet, Route("BatchDelete")]
        public JsonActionResult<bool> BatchDelete(string ids)
        {
            return WitRealBatchDelete(ids);
        }


        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Get")]
        public JsonActionResult<T_PF_FLOW_SCHEME> Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }


        [HttpGet, Route("GetFlowScheme")]
        public JsonActionResult<FlowSchemeResponse> GetFlowScheme(string id)
        {
            return SafeExecute<FlowSchemeResponse>(() =>
           {
               var flowService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFlowService>();
               KeywordFilter filter = new KeywordFilter();
               filter.Keyword = id;
               return flowService.GetFlowScheme(filter);
           });
        }
    }
}
