namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：工艺路线服务 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/2 16:11:04 
    /// 描    述 ：CraftsInfo 工艺管理   CraftsSteps工艺步骤管理
    /// </summary>
    [ApiDescriptionSettings("配方工艺管理", Order = 0, Tag = "工艺服务", Name = "CraftsSteps", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class CraftsStepsService : ICraftsStepsService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public CraftsStepsService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(CraftsStepsAddInput input)
        {
         
            var step = await _dbContext.Queryable<Pztj_CraftStepsEntity>().Where(x=>x.CraftId==input.CraftId).MaxAsync(x => x.Step);
            Pztj_CraftStepsEntity entity = input.Adapt<Pztj_CraftStepsEntity>();
            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(CraftsStepsDelInput input)
        {
            var stepItem =  await _dbContext.Queryable<Pztj_CraftStepsEntity>().Where(x => x.Id == input.Id).FirstAsync();
            string craftsId = stepItem.CraftId;
            int step = stepItem.Step;
            var res = await _dbContext.Deleteable<Pztj_CraftStepsEntity>().Where(x => x.Id == input.Id).ExecuteCommandHasChangeAsync();
            if (res)
            {
                var stepList = await _dbContext.Queryable<Pztj_CraftStepsEntity>().Where(x => x.CraftId == craftsId).OrderBy(x=>x.Step).ToListAsync();
                for (int i = 0; i < stepList.Count; i++)
                {
                    stepList[i].Step = i + 1;
                }
                await _dbContext.Updateable(stepList).ExecuteCommandAsync();
            }
            return res;
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<CraftsStepsOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<Pztj_CraftStepsEntity>().FirstAsync(x => x.Id == Id);
            CraftsStepsOutput output = entity.Adapt<CraftsStepsOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<CraftsStepsOutput>> List()
        {
            var entity = await _dbContext.Queryable<Pztj_CraftStepsEntity>().ToListAsync();
            List<CraftsStepsOutput> output = entity.Adapt<List<CraftsStepsOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<CraftsStepsOutput>> PagedList(CraftsStepsQueryPageInput input)
        {
            var output = await _dbContext.Queryable<Pztj_CraftStepsEntity>()
                .LeftJoin<DeviceProductFunctionEntity>((a,b)=>a.DeviceProductFunctionId==b.Id)
                .WhereIF(!string.IsNullOrEmpty(input.CraftId), (a, b) => a.CraftId == input.CraftId)
                .OrderBy((a, b) => a.Step)
                .Select((a,b)=>new CraftsStepsOutput
                {
                     Id = a.Id.SelectAll(),
                     DeviceProductFunctionName = b.Name,
                     DeviceProductFunctionId = a.DeviceProductFunctionId,
                     DeviceProductId = b.DeviceProductId
                })
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<Pztj_CraftStepsEntity>().Select(x => new
            {
                key = x.Id,
                value = x.Description
            }).ToListAsync();
            return entity;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(CraftsStepsUpdateInput input)
        {
            Pztj_CraftStepsEntity entity = input.Adapt<Pztj_CraftStepsEntity>();
            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<CraftsStepsUpdateInput> inputs)
        {
            List<Pztj_CraftStepsEntity> entity = inputs.Adapt<List<Pztj_CraftStepsEntity>>();
            var res = await _dbContext.Updateable(entity).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}

