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

namespace BPA.MES.Base.Application.Services
{
    /// <summary>
    /// 名    称 ：分组信息 
    /// 创 建 人 ：yangxiao  
    /// 创建时间 ：2023/3/10 14:03:06 
    /// 描    述 ：  
    /// </summary>
    [ApiDescriptionSettings("工单管理", Order = 0, Name = "WorkPlan", Tag = "生产计划", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class WorkPlanService : IWorkPlanService, ITransient, IDynamicApiController
    {
        private readonly ISqlSugarClient _dbContext;
        public WorkPlanService(ISqlSugarClient db)
        {
            _dbContext = db;
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Add(WorkPlanAddInput input)
        {
            WorkPlanEntity entity = await _dbContext.Queryable<WorkPlanEntity>().FirstAsync(x => x.Name == input.Name);
            if (entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            entity = input.Adapt<WorkPlanEntity>();
            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(WorkPlanDelInput input)
        {
            var res = await _dbContext.Deleteable<WorkPlanEntity>().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<WorkPlanOutput> Detail(string Id)
        {
            var entity = await _dbContext.Queryable<WorkPlanEntity>()
                .LeftJoin<Pztj_FinalsInfoEntity>((a,b)=>a.FinalId==b.Id)
                .Where((a,b)=>a.Id==Id)
                .Select((a,b)=>new WorkPlanOutput
                {
                    Id = a.Id.SelectAll(),
                    FinalName = b.Name
                })
                .FirstAsync();
            WorkPlanOutput output = entity.Adapt<WorkPlanOutput>();
            return output;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost, AllowAnonymous]
        public async Task<List<WorkPlanOutput>> List()
        {
            var entity = await _dbContext.Queryable<WorkPlanEntity>().ToListAsync();
            List<WorkPlanOutput> output = entity.Adapt<List<WorkPlanOutput>>();
            return output;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SqlSugarPagedList<WorkPlanOutput>> PagedList(WorkPlanQueryPageInput input)
        {
            var entity = await _dbContext.Queryable<WorkPlanEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.Name),x=>x.Name.Contains(input.Name))
                .WhereIF(!string.IsNullOrEmpty(input.FinalId),x=>x.FinalId ==input.FinalId)
                .ToPagedListAsync(input.PageIndex, input.PageSize);
            SqlSugarPagedList<WorkPlanOutput> output = entity.Adapt<SqlSugarPagedList<WorkPlanOutput>>();
            return output;
        }
        /// <summary>
        /// 下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> SelectList()
        {
            var entity = await _dbContext.Queryable<WorkPlanEntity>().Select(x => new
            {
                key = x.Id,
                value = x.Name
            }).ToListAsync();
            return entity;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<bool> Update(WorkPlanUpdateInput input)
        {
            var entity = await _dbContext.Queryable<WorkPlanEntity>().FirstAsync(x => x.Id != input.Id && x.Name == input.Name);
            if (entity != null)
            {
                throw Oops.Bah("名称已存在!");
            }
            entity = input.Adapt<WorkPlanEntity>();
            var res = await _dbContext.Updateable(entity).IgnoreColumns(true).ExecuteCommandHasChangeAsync();
            return res;
        }
    }
}
