using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.MES.Base.Application.Services
{
    [ApiDescriptionSettings("产线工艺管理", Order = 0, Name = "ProductLineSteps", Tag = "产线服务", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class ProductLineStepsService : IProductLineStepsService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public ProductLineStepsService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(ProductLineStepsAddInput input)
        {
            //ProductLineStepsEntity entity = await _dbContext.Queryable<ProductLineStepsEntity>().FirstAsync(x => x.Name == input.Name);
            //if (entity != null)
            //{
            //    throw Oops.Bah("名称已存在!");
            //}
            //ProductLineStepsEntity entity = input.Adapt<ProductLineStepsEntity>();
            //return await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandIdentityIntoEntityAsync();

            var step = await _dbContext.Queryable<ProductLineStepsEntity>().Where(x => x.LineId==input.LineId).MaxAsync(x => x.Step);
            ProductLineStepsEntity entity = input.Adapt<ProductLineStepsEntity>();
            entity.Step = step+1;
            return await _dbContext.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandIdentityIntoEntityAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Del(ProductLineStepsDelInput input)
        {
            var res = await _dbContext.Deleteable<ProductLineStepsEntity>().Where(x => x.Id==input.Id).ExecuteCommandHasChangeAsync();
            return res;
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<ProductLineStepsOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<ProductLineStepsEntity>().FirstAsync();
            ProductLineStepsOutput output = entity.Adapt<ProductLineStepsOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost, AllowAnonymous]
        public async Task<List<ProductLineStepsOutput>> List(ProductLineStepsQueryInput input)
        {
            var output = await _dbContext.Queryable<ProductLineStepsEntity>()
                .LeftJoin<ProcessEntity>((a, b) => a.ProcessId==b.Id)
                .WhereIF(!string.IsNullOrEmpty(input.LineId), (a, b) => a.LineId==input.LineId)
                .OrderBy((a, b) => a.Step)
                .Select((a, b) => new ProductLineStepsOutput
                {
                    Id = a.Id,
                    ProcessId = b.Id,
                    Step = a.Step,
                    LineId = a.LineId,
                    ProcessName = b.Name,
                    ProcessType = b.ProcessType
                })
                .ToListAsync();
            foreach (var item in output)
            {
                var devicelist = await _dbContext.Queryable<Pztj_LineDevicesEntity>()
                    .LeftJoin<Pztj_DevicesInfoEntity>((a, b) => a.DeviceId==b.Id)
                    .Where((a, b) => a.StepId==item.Id)
                    .Select((a, b) => new DevicesInfoOutput()
                    {
                        Code=b.Code,
                        Id = a.Id,
                        DeviceType = b.DeviceType,
                        Name = b.Name,
                        DeviceId= b.Id,
                        Status = SqlFunc.Subqueryable<FinalCraftStepsEntity>().Where(x=>x.DeviceId==b.Code && x.FinalId == input.FinalId && x.LineId==input.LineId && x.ProductLineStepsId==item.Id).Any()? "1" : "0",
                    })
                    .ToListAsync();
               
                item.DeviceId = devicelist.Select(e => e.DeviceId).ToArray();
                item.DevicesInfo = devicelist;
            }
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<ProductLineStepsOutput>> PagedList(ProductLineStepsQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<ProductLineStepsEntity>()
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<ProductLineStepsOutput> output = entity.Adapt<SqlSugarPagedList<ProductLineStepsOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<ProductLineStepsEntity>().Select(x => new
            {
                key = x.Id,
                value = x.ProcessName
            }).ToListAsync();
            return entity;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(ProductLineStepsUpdateInput input)
        {
            //var entity = await _dbContext.Queryable<ProductLineStepsEntity>().FirstAsync(x => x.Id != input.Id && x.Name == input.Name);
            //if (entity != null)
            //{
            //    throw Oops.Bah("名称已存在!");
            //}
            var entity = input.Adapt<ProductLineStepsEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(it=>it.Step).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="inputs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> BatchUpdate(List<ProductLineStepsUpdateInput> inputs)
        {
            List<ProductLineStepsEntity> entity = inputs.Adapt<List<ProductLineStepsEntity>>();
            var res = await _dbContext.Updateable(entity).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
