using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.BD;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.BaseData.Domain.IServices.FM;
using APT.Infrastructure.Api.Redis;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.PF.WebApiControllers.Api.PF
{
    [Route("api/PF/CodeRule")]
    public class CodeRuleController : AuthorizeApiController<T_PF_CODE_RULE>
    {

        IPFCodeRuleService CodeRuleService { get; set; }
        public CodeRuleController(IPFCodeRuleService coderuleService)
        {
            CodeRuleService = coderuleService;
        }


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

        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_PF_CODE_RULE>> OrderEntities([FromBody] KeywordFilter filter)
        {
            return WitOrderEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_PF_CODE_RULE> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_PF_CODE_RULE> 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 WitDelete(id);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody] T_PF_CODE_RULE entity)
        {
            return WitUpdate(entity);
        }

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


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

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        [HttpPost, Route("GenSystemCode")]
        public JsonActionResult<string> GenSystemCode([FromBody] SystemCodeFilter fiter)
        {
            return SafeExecute(() =>
            {
                return CodeRuleService.NewGenSerial(fiter);
            });
        }
        /// <summary>
        /// 跑流水码
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        [HttpPost, Route("Run")]
        public JsonActionResult<bool> Run([FromBody] SystemCodeFilter fiter)
        {
            return SafeExecute(() =>
            {
                var codeRuleService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFCodeRuleSerialService>();
                return codeRuleService.RunBatch();
            });
        }

        /// <summary>
        /// 跑流水码
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        [HttpPost, Route("GenSerial")]
        public JsonActionResult<bool> GenSerial([FromBody] KeywordFilter fiter)
        {
            return SafeExecute(() =>
            {
                var codeRuleService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFCodeRuleSerialService>();
                return codeRuleService.GenSerial(fiter);
            });
        }

        /// <summary>
        /// 重置流水码
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Check")]
        public JsonActionResult<bool> Check([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                var codeRuleService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFCodeRuleSerialService>();
                codeRuleService.SerialCheck();
                return true;
            });
        }
        /// <summary>
        /// 释放预占用
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("ReleasePreUseSerial")]
        public JsonActionResult<bool> ReleasePreUseSerial([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                if (string.IsNullOrEmpty(filter.Keyword))
                    return false;
                Expression<Func<T_PF_CODE_RULE_SERIAL, bool>> expression = t => false;

                string[] str = filter.Keyword.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (str != null)
                {
                    foreach (var item in str)
                    {
                        var src = item.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                        if (src != null && src.Length == 2)
                        {
                            int codeRuleType = LibUtils.ToInt(src[0]);
                            var codes = src[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                            expression = expression.Or(t => t.CODE_TYPE == codeRuleType && codes.Contains(t.SERIAL));
                        }
                    }
                }
                var tempFilter = new BaseFilter(filter.OrgId);
                tempFilter.OrgType = FilterOrgTypeEnum.仅本组织;
                var codeRuleSerials = this.GetEntities<T_PF_CODE_RULE_SERIAL>(expression, tempFilter).ToList();
                if (codeRuleSerials.Any())
                {
                    codeRuleSerials.ForEach(t =>
                    {
                        if (t.STATUS == (int)PFCodeRuleStatus.预取)
                            t.STATUS = (int)PFCodeRuleStatus.未用;
                    });
                    this.BantchUpdateEntity(codeRuleSerials);
                }
                return true;
            });
        }

        [HttpPost, Route("RedisTest")]
        public JsonActionResult<string> RedisTest([FromBody] KeywordFilter filter)
        {
            return SafeExecute<string>(() =>
            {
                CsRedisManager.StringSet("aaa", filter.Keyword);
                return CsRedisManager.StringGet("aaa");
            });
        }


    }
}
